Fix Metadata Plugin Parser
[platform/core/appfw/aul-1.git] / parser / metadata / alias-appid / appsvc_db.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <tzplatform_config.h>
18
19 #include <memory>
20
21 #include "alias-appid/appsvc_db.hh"
22 #include "common/log_private.hh"
23
24 namespace plugin {
25
26 static const uid_t ROOT_UID = 0;
27 static const uid_t GLOBAL_USER = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
28
29 AppSvcDB::AppSvcDB(uid_t uid) : Database(GetDBPath(uid)) {
30 }
31
32 AppSvcDB::~AppSvcDB() = default;
33
34 std::vector<std::shared_ptr<AliasInfo>> AppSvcDB::Select(
35     const std::string& appid) {
36   static const char query[] = "SELECT alias_appid FROM alias_info "
37     "WHERE appid = ?;";
38   sqlite3_stmt* stmt;
39   __PREPARE_V2(GetHandle(), query, strlen(query), stmt);
40   auto stmt_ptr = std::unique_ptr<sqlite3_stmt, decltype(sqlite3_finalize)*>(
41       stmt, sqlite3_finalize);
42   __BIND_TEXT(GetHandle(), stmt, 1, appid.c_str());
43
44   std::vector<std::shared_ptr<AliasInfo>> infos;
45   while (sqlite3_step(stmt) == SQLITE_ROW) {
46     std::string alias_appid = ColumnText(stmt, 0);
47     if (!alias_appid.empty())
48       infos.emplace_back(new (std::nothrow) AliasInfo(alias_appid, appid));
49   }
50
51   return infos;
52 }
53
54 void AppSvcDB::Insert(const std::string& alias_appid,
55     const std::string& appid) {
56   static const char query[] = "INSERT OR REPLACE INTO "
57     "alias_info(alias_appid, appid) VALUES(?, ?);";
58   sqlite3_stmt* stmt;
59   __PREPARE_V2(GetHandle(), query, strlen(query), stmt);
60   std::unique_ptr<sqlite3_stmt, decltype(sqlite3_finalize)*> stmt_ptr(
61       stmt, sqlite3_finalize);
62   __BIND_TEXT(GetHandle(), stmt, 1, alias_appid.c_str());
63   __BIND_TEXT(GetHandle(), stmt, 2, appid.c_str());
64   __STEP(GetHandle(), stmt);
65 }
66
67 void AppSvcDB::Delete(const std::string& alias_appid,
68     const std::string& appid) {
69   static const char query[] = "DELETE FROM alias_info WHERE "
70     "alias_appid = ? AND appid = ?;";
71   sqlite3_stmt* stmt;
72   __PREPARE_V2(GetHandle(), query, strlen(query), stmt);
73   std::unique_ptr<sqlite3_stmt, decltype(sqlite3_finalize)*> stmt_ptr(
74       stmt, sqlite3_finalize);
75   __BIND_TEXT(GetHandle(), stmt, 1, alias_appid.c_str());
76   __BIND_TEXT(GetHandle(), stmt, 2, appid.c_str());
77   __STEP(GetHandle(), stmt);
78 }
79
80 void AppSvcDB::Delete(const std::string& appid) {
81   static const char query[] = "DELETE FROM alias_info WHERE appid = ?;";
82   sqlite3_stmt* stmt;
83   __PREPARE_V2(GetHandle(), query, strlen(query), stmt);
84   std::unique_ptr<sqlite3_stmt, decltype(sqlite3_finalize)*> stmt_ptr(
85       stmt, sqlite3_finalize);
86   __BIND_TEXT(GetHandle(), stmt, 1, appid.c_str());
87   __STEP(GetHandle(), stmt);
88 }
89
90 std::string AppSvcDB::GetDBPath(uid_t uid) {
91   std::string db_path;
92   if (uid == ROOT_UID || uid == GLOBAL_USER) {
93     db_path = std::string(tzplatform_getenv(TZ_SYS_DB)) + "/.appsvc.db";
94   } else {
95     db_path = std::string(tzplatform_getenv(TZ_SYS_DB)) + "/user/" +
96       std::to_string(uid) +  "/.appsvc.db";
97   }
98   return db_path;
99 }
100
101 }  // namespace plugin