print HW clock log (#385)
[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 "log.h"
18 #include "db_manager.h"
19
20 #include <algorithm>
21
22 #ifdef  LOG_TAG
23 #undef  LOG_TAG
24 #endif
25 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
26
27 static int checkIntegrityDBCb(void *user_data, int argc, char **argv, char **not_used)
28 {
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");
35                 return -1;
36         }
37         _INFO("DB integrity result : %s", argv[0]);
38         return 0;
39 }
40
41 sqlite3* createDB(std::string path, std::string query)
42 {
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");
48                 return sqlite;
49         }
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);
54                 sqlite = NULL;
55                 return sqlite;
56         }
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);
61                 sqlite = NULL;
62                 return sqlite;
63         }
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);
68                 sqlite = NULL;
69                 return sqlite;
70         }
71         return sqlite;
72 }
73
74 sqlite3* openDB(std::string path)
75 {
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");
81                 return sqlite;
82         }
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);
87                 sqlite = NULL;
88                 return sqlite;
89         }
90         return sqlite;
91 }
92
93 void finalizeDB(sqlite3_stmt *stmt)
94 {
95         if (stmt) {
96                 sqlite3_finalize(stmt);
97                 stmt = NULL;
98         }
99 }
100
101 void closeDB(sqlite3 *sqlite)
102 {
103         if (sqlite) {
104                 sqlite3_exec(sqlite, "COMMIT;", NULL, NULL, NULL);
105                 sqlite3_close(sqlite);
106                 sqlite = NULL;
107         }
108 }
109
110 void rollbackDB(sqlite3 *sqlite)
111 {
112         if (sqlite) {
113                 sqlite3_exec(sqlite, "ROLLBACK;", NULL, NULL, NULL);
114                 sqlite3_close(sqlite);
115                 sqlite = NULL;
116         }
117 }
118
119 static bool execDB(sqlite3 *sqlite, std::string query)
120 {
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));
124                 return false;
125         }
126         return true;
127 }
128
129 bool updateDB(sqlite3 *sqlite, std::string query)
130 {
131         return execDB(sqlite, query);
132 }
133
134 bool insertDB(sqlite3 *sqlite, std::string query)
135 {
136         return execDB(sqlite, query);
137 }
138
139 bool deleteDB(sqlite3 *sqlite, std::string query)
140 {
141         return execDB(sqlite, query);
142 }
143
144 std::vector<std::string> selectDB(sqlite3 *sqlite, std::string query)
145 {
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));
152                 return selectData;
153         }
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));
157         }
158         finalizeDB(stmt);
159         return selectData;
160 }