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.
17 #include "db_manager.h"
23 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
25 sqlite3* dbCreate(std::string path)
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());
33 ret = sqlite3_exec(sqlite, "PRAGMA journal_mode = PERSIST", NULL, NULL, NULL);
34 if (ret != SQLITE_OK) {
35 _ERR("Sqlite error : [%d]", ret);
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());
46 bool dbOpen(sqlite3 *tac_db, std::string path)
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());
58 void dbFinalize(sqlite3_stmt *stmt)
61 sqlite3_finalize(stmt);
66 void dbClose(sqlite3 *tac_db)
69 sqlite3_exec(tac_db, "COMMIT;", NULL, NULL, NULL);
70 sqlite3_close(tac_db);
75 void dbRollback(sqlite3 *tac_db)
78 sqlite3_exec(tac_db, "ROLLBACK;", NULL, NULL, NULL);
79 sqlite3_close(tac_db);
84 bool dbUpdate(sqlite3 *tac_db, std::string path, std::string query)
86 sqlite3_stmt *stmt = NULL;
87 if (!dbOpen(tac_db, path)) {
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));
97 ret = sqlite3_step(stmt);
98 if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
99 _ERR("Sqlite error [%d]", ret);
108 bool dbInsert(sqlite3 *tac_db, std::string path, std::string query)
110 sqlite3_stmt *stmt = NULL;
111 if (!dbOpen(tac_db, path)) {
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));
121 ret = sqlite3_step(stmt);
122 if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
123 _ERR("Sqlite error [%d]", ret);
132 std::vector<std::string> dbSelect(sqlite3 *tac_db, std::string path, std::string query)
134 std::vector<std::string> updateDB;
135 sqlite3_stmt* stmt = NULL;
136 const char* str = NULL;
137 if (!dbOpen(tac_db, path)) {
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));
146 while (sqlite3_step(stmt) == SQLITE_ROW) {
147 str = (const char *) sqlite3_column_text(stmt, 2);
148 _DBG("Nuget : %s", (!str || !strlen(str)) ? NULL : strdup(str));
149 updateDB.push_back((!str || !strlen(str)) ? NULL : strdup(str));
155 bool dbDelete(sqlite3 *tac_db, std::string path, std::string query)
157 sqlite3_stmt *stmt = NULL;
158 if (!dbOpen(tac_db, path)) {
161 int ret = sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
162 ret = sqlite3_prepare(tac_db, query.c_str(), QUERY_MAX_LEN , &stmt, NULL);
163 if (ret != SQLITE_OK) {
164 _ERR("Sqlite error : [%s,%s]", query.c_str(), sqlite3_errmsg(tac_db));
168 ret = sqlite3_step(stmt);
169 if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
170 _ERR("Sqlite error [%d]", ret);