2 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "db_manager.h"
25 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
27 static int checkIntegrityDBCb(void *user_data, int argc, char **argv, char **not_used)
29 bool* is_db_corrupted = static_cast<bool*>(user_data);
30 std::string result_DB_check = argv[0];
31 std::transform(result_DB_check.begin(), result_DB_check.end(), result_DB_check.begin(), ::tolower);
32 if (result_DB_check != "ok") {
33 *is_db_corrupted = true;
34 _ERR("DB integrity check failed");
37 _INFO("DB integrity result : %s", argv[0]);
41 sqlite3* createDB(std::string path, std::string query)
43 sqlite3 *sqlite = NULL;
44 bool is_db_corrupted = false;
45 int ret = sqlite3_open(path.c_str(), &sqlite);
46 if (ret != SQLITE_OK) {
47 _INFO("DB file is corrupted, start to recover corrupted db");
50 ret = sqlite3_exec(sqlite, "PRAGMA journal_mode = PERSIST", NULL, NULL, NULL);
51 if (ret != SQLITE_OK) {
52 _ERR("Sqlite error : [%d, %s]", ret, path.c_str());
53 sqlite3_close(sqlite);
57 ret = sqlite3_exec(sqlite, query.c_str(), NULL, NULL, NULL);
58 if (ret != SQLITE_OK) {
59 _ERR("Sqlite error : [%d, %s]", ret, path.c_str());
60 sqlite3_close(sqlite);
64 ret = sqlite3_exec(sqlite, "PRAGMA integrity_check", checkIntegrityDBCb, &is_db_corrupted, NULL);
65 if (ret == SQLITE_CORRUPT || is_db_corrupted) {
66 _INFO("DB file is corrupted, start to recover corrupted db");
67 sqlite3_close(sqlite);
74 sqlite3* openDB(std::string path)
76 sqlite3 *sqlite = NULL;
77 bool is_db_corrupted = false;
78 int ret = sqlite3_open(path.c_str(), &sqlite);
79 if (ret != SQLITE_OK) {
80 _INFO("DB file is corrupted, start to recover corrupted db");
83 ret = sqlite3_exec(sqlite, "PRAGMA integrity_check", checkIntegrityDBCb, &is_db_corrupted, NULL);
84 if (ret == SQLITE_CORRUPT || is_db_corrupted) {
85 _INFO("DB file is corrupted, start to recover corrupted db");
86 sqlite3_close(sqlite);
93 void finalizeDB(sqlite3_stmt *stmt)
96 sqlite3_finalize(stmt);
101 void closeDB(sqlite3 *sqlite)
104 sqlite3_exec(sqlite, "COMMIT;", NULL, NULL, NULL);
105 sqlite3_close(sqlite);
110 void rollbackDB(sqlite3 *sqlite)
113 sqlite3_exec(sqlite, "ROLLBACK;", NULL, NULL, NULL);
114 sqlite3_close(sqlite);
119 static bool execDB(sqlite3 *sqlite, std::string query)
121 int ret = sqlite3_exec(sqlite, query.c_str(), NULL, NULL, NULL);
122 if (ret != SQLITE_OK) {
123 _ERR("Sqlite error : [%d, %s]", ret, sqlite3_errmsg(sqlite));
129 bool updateDB(sqlite3 *sqlite, std::string query)
131 return execDB(sqlite, query);
134 bool insertDB(sqlite3 *sqlite, std::string query)
136 return execDB(sqlite, query);
139 bool deleteDB(sqlite3 *sqlite, std::string query)
141 return execDB(sqlite, query);
144 std::vector<std::string> selectDB(sqlite3 *sqlite, std::string query)
146 std::vector<std::string> selectData;
147 sqlite3_stmt* stmt = NULL;
148 const char* str = NULL;
149 int ret = sqlite3_prepare_v2(sqlite, query.c_str(), query.size(), &stmt, NULL);
150 if (ret != SQLITE_OK) {
151 _ERR("Sqlite error : [%d, %s]", ret, sqlite3_errmsg(sqlite));
154 while (sqlite3_step(stmt) == SQLITE_ROW) {
155 str = (const char *) sqlite3_column_text(stmt, 2);
156 selectData.push_back((!str || !strlen(str)) ? NULL : strdup(str));