Refactoring to extract duplicate logic as a separate method
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / tac_db.cc
1 /*
2  * Copyright (c) 2024 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 "log.h"
18 #include "utils.h"
19 #include "db_manager.h"
20 #include "tac_common.h"
21 #include "tac_db.h"
22
23 #ifdef  LOG_TAG
24 #undef  LOG_TAG
25 #endif
26 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
27
28 #define __XSTR(x) #x
29 #define __STR(x) __XSTR(x)
30 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
31 #undef __STR
32 #undef __XSTR
33
34 static sqlite3 *tac_db = NULL;
35 static sqlite3 *tlc_db = NULL;
36
37 // callback function of "sqlite3_exec"
38 int sqliteCb(void *count, int argc, char **argv, char **colName)
39 {
40         int *c = (int*)count;
41         *c = atoi(argv[0]);
42         return 0;
43 }
44
45 int tac_createDB()
46 {
47         tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
48         if (!tac_db) {
49                 _ERR("Sqlite create error. So restore the database.");
50                 if (tac_restoreDB() != TAC_ERROR_NONE) {
51                         _ERR("Sqlite create error");
52                         return -1;
53                 }
54                 tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
55                 if (!tac_db) {
56                         _ERR("Sqlite create error");
57                         return -1;
58                 }
59         }
60         sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
61         return 0;
62 }
63
64 int tac_openDB()
65 {
66         tac_db = openDB(TAC_APP_LIST_DB);
67         if (!tac_db) {
68                 _ERR("Sqlite open error. So restore the database.");
69                 if (tac_restoreDB() != TAC_ERROR_NONE) {
70                         _ERR("Sqlite open error");
71                         return -1;
72                 }
73                 tac_db = openDB(TAC_APP_LIST_DB);
74                 if (!tac_db) {
75                         _ERR("Sqlite open error");
76                         return -1;
77                 }
78         }
79         sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
80         return 0;
81 }
82
83 int tac_insertDB(const std::string& pkgId, const std::string& np, const std::string& tac_name, const std::string& tac_version)
84 {
85         char *sql = sqlite3_mprintf(
86                 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
87                 "VALUES (%Q, %Q, %Q, %Q);", pkgId.c_str(), np.c_str(), tac_name.c_str(), tac_version.c_str());
88         if (!insertDB(tac_db, sql)) {
89                 _ERR("Sqlite insert error");
90                 sqlite3_free(sql);
91                 return -1;
92         }
93         sqlite3_free(sql);
94         return 0;
95 }
96
97 int tac_countDB(const std::string& pkgId, const std::string& tac_name, const std::string& unp, int& count)
98 {
99         char *sql = NULL;
100         if (unp == "") {
101                 sql = sqlite3_mprintf("SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId.c_str(), tac_name.c_str());
102         } else {
103                 sql = sqlite3_mprintf("SELECT COUNT(NUGET) FROM TAC WHERE NUGET = %Q;", unp.c_str());
104         }
105         if (sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL) != SQLITE_OK) {
106                 _ERR("Sqlite select error");
107                 sqlite3_free(sql);
108                 return -1;
109         }
110         sqlite3_free(sql);
111         return 0;
112 }
113
114 std::vector<std::string> tac_selectDB(const std::string& pkgId)
115 {
116         char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId.c_str());
117         std::vector<std::string> update_tac = selectDB(tac_db, sql);
118         sqlite3_free(sql);
119         return update_tac;
120 }
121
122 int tac_updateDB(const std::string& pkgId, const std::string& np, const std::string& tac_name, const std::string& tac_version)
123 {
124         char *sql = sqlite3_mprintf(
125                 "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;",
126                 tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId.c_str(), tac_name.c_str());
127         if (!updateDB(tac_db, sql)) {
128                 _ERR("Sqlite update error");
129                 sqlite3_free(sql);
130                 return -1;
131         }
132         sqlite3_free(sql);
133         return 0;
134 }
135
136 int tac_deleteDB(const std::string& pkgId, const std::string& unp)
137 {
138         char *sql = NULL;
139         if (unp == "") {
140                 sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId.c_str());
141         } else {
142                 sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q AND NUGET = %Q;", pkgId.c_str(), unp.c_str());
143         }
144         if (!deleteDB(tac_db, sql)) {
145                 _ERR("Sqlite delete error");
146                 sqlite3_free(sql);
147                 return -1;
148         }
149         sqlite3_free(sql);
150         return 0;
151 }
152
153 void tac_rollbackDB()
154 {
155         if (tac_db) {
156                 rollbackDB(tac_db);
157                 tac_db = NULL;
158         }
159 }
160
161 bool tac_closeDB()
162 {
163         if (tac_db) {
164                 closeDB(tac_db);
165                 tac_db = NULL;
166                 return true;
167         }
168         return false;
169 }
170
171 int tlc_createDB()
172 {
173         if (!createDir(TLC_LIBRARIES_DIR)) {
174                 _ERR("Cannot create directory: %s", TLC_LIBRARIES_DIR);
175                 return -1;
176         }
177         copySmackAndOwnership(__DOTNET_DIR, TLC_LIBRARIES_DIR);
178
179         tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE);
180         if (!tlc_db) {
181                 _ERR("Sqlite create error. So restore the database.");
182                 if (tlc_restoreDB() != TAC_ERROR_NONE) {
183                         _ERR("Sqlite create error");
184                         return -1;
185                 }
186                 tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE);
187                 if (!tlc_db) {
188                         _ERR("Sqlite create error");
189                         return -1;
190                 }
191         }
192         sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
193         return 0;
194 }
195
196 int tlc_openDB()
197 {
198         tlc_db = openDB(TLC_APP_LIST_DB);
199         if (!tlc_db) {
200                 _ERR("Sqlite open error. So restore the database.");
201                 if (tlc_restoreDB() != TAC_ERROR_NONE) {
202                         _ERR("Sqlite open error");
203                         return 0;
204                 }
205                 tlc_db = openDB(TLC_APP_LIST_DB);
206                 if (!tlc_db) {
207                         _ERR("Sqlite open error");
208                         return 0;
209                 }
210         }
211         sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
212         return 0;
213 }
214
215 int tlc_insertDB(const std::string& pkgId, const std::string& fileSha)
216 {
217         char *sql = sqlite3_mprintf("INSERT INTO TLC (PKGID, LIBRARY) VALUES (%Q, %Q);", pkgId.c_str(), fileSha.c_str());
218         if (!insertDB(tlc_db, sql)) {
219                 _ERR("Sqlite insert error");
220                 sqlite3_free(sql);
221                 return -1;
222         }
223         sqlite3_free(sql);
224         return 0;
225 }
226
227 int tlc_countDB(const std::string& pkgId, const std::string& ulp, int& count)
228 {
229         char *sql = sqlite3_mprintf("SELECT COUNT(LIBRARY) FROM TLC WHERE LIBRARY = %Q;", ulp.c_str());
230         if (sqlite3_exec(tlc_db, sql, sqliteCb, &count, NULL) != SQLITE_OK) {
231                 _ERR("Sqlite select error");
232                 sqlite3_free(sql);
233                 return -1;
234         }
235         sqlite3_free(sql);
236         return 0;
237 }
238
239 std::vector<std::string> tlc_selectDB(const std::string& pkgId)
240 {
241         char *sql = sqlite3_mprintf("SELECT * FROM TLC WHERE PKGID = %Q;", pkgId.c_str());
242         std::vector<std::string> update_tlc = selectDB(tlc_db, sql);
243         sqlite3_free(sql);
244         return update_tlc;
245 }
246
247 int tlc_deleteDB(const std::string& pkgId)
248 {
249         char *sql = sqlite3_mprintf("DELETE FROM TLC WHERE PKGID = %Q;", pkgId.c_str());
250         if (!deleteDB(tlc_db, sql)) {
251                 _ERR("Sqlite delete error");
252                 sqlite3_free(sql);
253                 return -1;
254         }
255         sqlite3_free(sql);
256         return 0;
257 }
258
259 void tlc_rollbackDB()
260 {
261         if (tlc_db) {
262                 rollbackDB(tlc_db);
263                 tlc_db = NULL;
264         }
265 }
266
267 bool tlc_closeDB()
268 {
269         if (tlc_db) {
270                 closeDB(tlc_db);
271                 tlc_db = NULL;
272                 return true;
273         }
274         return false;
275 }