Code cleanup (#251)
[platform/core/dotnet/launcher.git] / NativeLauncher / util / db_manager.cc
1 /*
2  * Copyright (c) 2019 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 <algorithm>
18
19 #include "db_manager.h"
20 #include "log.h"
21 #include "tac_common.h"
22
23 #ifdef  LOG_TAG
24 #undef  LOG_TAG
25 #endif
26 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
27
28 static int dbIntegrityCheckCb(void *user_data, int argc, char **argv, char **not_used)
29 {
30         bool* is_db_corrupted = static_cast<bool*>(user_data);
31         std::string result_DB_check = argv[0];
32         std::transform(result_DB_check.begin(), result_DB_check.end(), result_DB_check.begin(), ::tolower);
33         if (result_DB_check != "ok") {
34                 *is_db_corrupted = true;
35                 _ERR("DB integrity check failed");
36                 return -1;
37         }
38         _INFO("DB integrity result : %s", argv[0]);
39         return 0;
40 }
41
42 sqlite3* dbCreate(std::string path, std::string query)
43 {
44         sqlite3 *sqlite = NULL;
45         bool is_db_corrupted = false;
46         int ret = sqlite3_open(path.c_str(), &sqlite);
47         if (ret != SQLITE_OK) {
48                 _INFO("DB file is corrupted, start to recover corrupted db");
49                 if (restoreTACDB() != TAC_ERROR_NONE) {
50                         _ERR("Sqlite error : [%d, %s]", ret, path.c_str());
51                         return NULL;
52                 }
53                 return dbCreate(path, query);
54         }
55         ret = sqlite3_exec(sqlite, "PRAGMA journal_mode = PERSIST", NULL, NULL, NULL);
56         if (ret != SQLITE_OK) {
57                 _ERR("Sqlite error : [%d, %s]", ret, path.c_str());
58                 sqlite3_close(sqlite);
59                 sqlite = NULL;
60                 return sqlite;
61         }
62         ret = sqlite3_exec(sqlite, query.c_str(), NULL, NULL, NULL);
63         if (ret != SQLITE_OK) {
64                 _ERR("Sqlite error : [%d, %s]", ret, path.c_str());
65                 sqlite3_close(sqlite);
66                 sqlite = NULL;
67                 return sqlite;
68         }
69         ret = sqlite3_exec(sqlite, "PRAGMA integrity_check", dbIntegrityCheckCb, &is_db_corrupted, NULL);
70         if (ret == SQLITE_CORRUPT || is_db_corrupted) {
71                 _INFO("DB file is corrupted, start to recover corrupted db");
72                 if (restoreTACDB() != TAC_ERROR_NONE) {
73                         _ERR("Sqlite error : [%d, %s]", ret, path.c_str());
74                         sqlite3_close(sqlite);
75                         sqlite = NULL;
76                         return sqlite;
77                 }
78                 return dbCreate(path, query);
79         }
80         return sqlite;
81 }
82
83 sqlite3* dbOpen(std::string path)
84 {
85         sqlite3 *sqlite = NULL;
86         bool is_db_corrupted = false;
87         int ret = sqlite3_open(path.c_str(), &sqlite);
88         if (ret != SQLITE_OK) {
89                 _INFO("DB file is corrupted, start to recover corrupted db");
90                 if (restoreTACDB() != TAC_ERROR_NONE) {
91                         _ERR("Sqlite error : [%d, %s]", ret, path.c_str());
92                         return NULL;
93                 }
94                 return dbOpen(path);
95         }
96         ret = sqlite3_exec(sqlite, "PRAGMA integrity_check", dbIntegrityCheckCb, &is_db_corrupted, NULL);
97         if (ret == SQLITE_CORRUPT || is_db_corrupted) {
98                 _INFO("DB file is corrupted, start to recover corrupted db");
99                 if (restoreTACDB() != TAC_ERROR_NONE) {
100                         _ERR("Sqlite error : [%d, %s]", ret, path.c_str());
101                         sqlite3_close(sqlite);
102                         sqlite = NULL;
103                         return sqlite;
104                 }
105                 return dbOpen(path);
106         }
107         return sqlite;
108 }
109
110 void dbFinalize(sqlite3_stmt *stmt)
111 {
112         if (stmt) {
113                 sqlite3_finalize(stmt);
114                 stmt = NULL;
115         }
116 }
117
118 void dbClose(sqlite3 *sqlite)
119 {
120         if (sqlite) {
121                 sqlite3_exec(sqlite, "COMMIT;", NULL, NULL, NULL);
122                 sqlite3_close(sqlite);
123                 sqlite = NULL;
124         }
125 }
126
127 void dbRollback(sqlite3 *sqlite)
128 {
129         if (sqlite) {
130                 sqlite3_exec(sqlite, "ROLLBACK;", NULL, NULL, NULL);
131                 sqlite3_close(sqlite);
132                 sqlite = NULL;
133         }
134 }
135
136 bool dbUpdate(sqlite3 *sqlite, std::string path, std::string query)
137 {
138         sqlite3_stmt *stmt = NULL;
139         int ret = sqlite3_exec(sqlite, "BEGIN;", NULL, NULL, NULL);
140         ret = sqlite3_prepare(sqlite, query.c_str(), query.size(), &stmt, NULL);
141         if (ret != SQLITE_OK) {
142                 _ERR("Sqlite error : [%d, %s]", ret, sqlite3_errmsg(sqlite));
143                 dbClose(sqlite);
144                 return false;
145         }
146         ret = sqlite3_step(stmt);
147         if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
148                 _ERR("Sqlite error : [%d, %s]", ret, sqlite3_errmsg(sqlite));
149                 dbFinalize(stmt);
150                 dbClose(sqlite);
151                 return false;
152         }
153         dbFinalize(stmt);
154         return true;
155 }
156
157 bool dbInsert(sqlite3 *sqlite, std::string path, std::string query)
158 {
159         sqlite3_stmt *stmt = NULL;
160         int ret = sqlite3_exec(sqlite, "BEGIN;", NULL, NULL, NULL);
161         ret = sqlite3_prepare(sqlite, query.c_str(), query.size(), &stmt, NULL);
162         if (ret != SQLITE_OK) {
163                 _ERR("Sqlite error : [%d, %s]", ret, sqlite3_errmsg(sqlite));
164                 dbClose(sqlite);
165                 return false;
166         }
167         ret = sqlite3_step(stmt);
168         if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
169                 _ERR("Sqlite error : [%d, %s]", ret, sqlite3_errmsg(sqlite));
170                 dbFinalize(stmt);
171                 dbClose(sqlite);
172                 return false;
173         }
174         dbFinalize(stmt);
175         return true;
176 }
177
178 std::vector<std::string> dbSelect(sqlite3 *sqlite, std::string path, std::string query)
179 {
180         std::vector<std::string> updateDB;
181         sqlite3_stmt* stmt = NULL;
182         const char* str = NULL;
183         int ret = sqlite3_prepare_v2(sqlite, query.c_str(), query.size(), &stmt, NULL);
184         if (ret != SQLITE_OK) {
185                 _ERR("Sqlite error : [%d, %s]", ret, sqlite3_errmsg(sqlite));
186                 dbClose(sqlite);
187                 return updateDB;
188         }
189         while (sqlite3_step(stmt) == SQLITE_ROW) {
190                 str = (const char *) sqlite3_column_text(stmt, 2);
191                 updateDB.push_back((!str || !strlen(str)) ? NULL : strdup(str));
192         }
193         dbFinalize(stmt);
194         return updateDB;
195 }
196
197 bool dbDelete(sqlite3 *sqlite, std::string path, std::string query)
198 {
199         sqlite3_stmt *stmt = NULL;
200         int ret = sqlite3_exec(sqlite, "BEGIN;", NULL, NULL, NULL);
201         ret = sqlite3_prepare(sqlite, query.c_str(), query.size(), &stmt, NULL);
202         if (ret != SQLITE_OK) {
203                 _ERR("Sqlite error : [%d, %s]", ret, sqlite3_errmsg(sqlite));
204                 dbClose(sqlite);
205                 return false;
206         }
207         ret = sqlite3_step(stmt);
208         if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
209                 _ERR("Sqlite error : [%d, %s]", ret, sqlite3_errmsg(sqlite));
210                 dbFinalize(stmt);
211                 dbClose(sqlite);
212                 return false;
213         }
214         dbFinalize(stmt);
215         return true;
216 }