Bug fixed. Break out of nested loop.
[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 "db_manager.h"
18 #include "log.h"
19
20 #ifdef  LOG_TAG
21 #undef  LOG_TAG
22 #endif
23 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
24
25 sqlite3* dbCreate(std::string path)
26 {
27         sqlite3 *sqlite = NULL;
28         int ret = sqlite3_open(path.c_str(), &sqlite);
29         if (ret != SQLITE_OK) {
30                 _ERR("Sqlite error : [%d] : path [%s]", ret, path.c_str());
31                 return NULL;
32         }
33         ret = sqlite3_exec(sqlite, "PRAGMA journal_mode = PERSIST", NULL, NULL, NULL);
34         if (ret != SQLITE_OK) {
35                 _ERR("Sqlite error : [%d]", ret);
36                 return NULL;
37         }
38         ret = sqlite3_exec(sqlite, CREATE_TAC_DB_TABLE, NULL, NULL, NULL);
39         if (ret != SQLITE_OK) {
40                 _ERR("Sqlite error : [%d] : path [%s]", ret, path.c_str());
41                 return NULL;
42         }
43         return sqlite;
44 }
45
46 bool dbOpen(sqlite3 *tac_db, std::string path)
47 {
48         if (!tac_db) {
49                 int ret = sqlite3_open(path.c_str(), &tac_db);
50                 if (ret != SQLITE_OK) {
51                         _ERR("Sqlite error : [%d] : path [%s]", ret, path.c_str());
52                         return false;
53                 }
54         }
55         return true;
56 }
57
58 void dbFinalize(sqlite3_stmt *stmt)
59 {
60         if (stmt) {
61                 sqlite3_finalize(stmt);
62                 stmt = NULL;
63         }
64 }
65
66 void dbClose(sqlite3 *tac_db)
67 {
68         if (tac_db) {
69                 sqlite3_exec(tac_db, "COMMIT;", NULL, NULL, NULL);
70                 sqlite3_close(tac_db);
71                 tac_db = NULL;
72         }
73 }
74
75 void dbRollback(sqlite3 *tac_db)
76 {
77         if (tac_db) {
78                 sqlite3_exec(tac_db, "ROLLBACK;", NULL, NULL, NULL);
79                 sqlite3_close(tac_db);
80                 tac_db = NULL;
81         }
82 }
83
84 bool dbUpdate(sqlite3 *tac_db, std::string path, std::string query)
85 {
86         sqlite3_stmt *stmt = NULL;
87         if (!dbOpen(tac_db, path)) {
88                 return false;
89         }
90         int ret = sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
91         ret = sqlite3_prepare(tac_db, query.c_str(), QUERY_MAX_LEN , &stmt, NULL);
92         if (ret != SQLITE_OK) {
93                 _ERR("Sqlite error : [%s, %s]", query.c_str(), sqlite3_errmsg(tac_db));
94                 dbClose(tac_db);
95                 return false;
96         }
97         ret = sqlite3_step(stmt);
98         if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
99                 _ERR("Sqlite error : [%d]", ret);
100                 dbFinalize(stmt);
101                 dbClose(tac_db);
102                 return false;
103         }
104         dbFinalize(stmt);
105         return true;
106 }
107
108 bool dbInsert(sqlite3 *tac_db, std::string path, std::string query)
109 {
110         sqlite3_stmt *stmt = NULL;
111         if (!dbOpen(tac_db, path)) {
112                 return false;
113         }
114         int ret = sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
115         ret = sqlite3_prepare(tac_db, query.c_str(), QUERY_MAX_LEN , &stmt, NULL);
116         if (ret != SQLITE_OK) {
117                 _ERR("Sqlite error : [%s, %s]", query.c_str(), sqlite3_errmsg(tac_db));
118                 dbClose(tac_db);
119                 return false;
120         }
121         ret = sqlite3_step(stmt);
122         if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
123                 _ERR("Sqlite error : [%d]", ret);
124                 dbFinalize(stmt);
125                 dbClose(tac_db);
126                 return false;
127         }
128         dbFinalize(stmt);
129         return true;
130 }
131
132 std::vector<std::string> dbSelect(sqlite3 *tac_db, std::string path, std::string query)
133 {
134         std::vector<std::string> updateDB;
135         sqlite3_stmt* stmt = NULL;
136         const char* str = NULL;
137         if (!dbOpen(tac_db, path)) {
138                 return updateDB;
139         }
140         int ret = sqlite3_prepare_v2(tac_db, query.c_str(), strlen(query.c_str()), &stmt, NULL);
141         if (ret != SQLITE_OK) {
142                 _ERR("Sqlite error : [%s, %s]", query.c_str(), sqlite3_errmsg(tac_db));
143                 dbClose(tac_db);
144                 return updateDB;
145         }
146         while (sqlite3_step(stmt) == SQLITE_ROW) {
147                 str = (const char *) sqlite3_column_text(stmt, 2);
148                 updateDB.push_back((!str || !strlen(str)) ? NULL : strdup(str));
149         }
150         dbFinalize(stmt);
151         return updateDB;
152 }
153
154 bool dbDelete(sqlite3 *tac_db, std::string path, std::string query)
155 {
156         sqlite3_stmt *stmt = NULL;
157         if (!dbOpen(tac_db, path)) {
158                 return false;
159         }
160         int ret = sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
161         ret = sqlite3_prepare(tac_db, query.c_str(), QUERY_MAX_LEN , &stmt, NULL);
162         if (ret != SQLITE_OK) {
163                 _ERR("Sqlite error : [%s, %s]", query.c_str(), sqlite3_errmsg(tac_db));
164                 dbClose(tac_db);
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]", ret);
170                 dbFinalize(stmt);
171                 dbClose(tac_db);
172                 return false;
173         }
174         dbFinalize(stmt);
175         return true;
176 }