2 * Copyright (c) 2016 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"
20 sqlite3* dbCreate(std::string path)
22 sqlite3 *sqlite = NULL;
24 int ret = sqlite3_open(path.c_str(), &sqlite);
25 if (ret != SQLITE_OK) {
26 _ERR("Sqlite error : [%d] : path [%s]", ret, path.c_str());
29 ret = sqlite3_exec(sqlite, "PRAGMA journal_mode = PERSIST", NULL, NULL, &error);
30 if (ret != SQLITE_OK) {
31 _ERR("Sqlite error(%d) : %s", ret, error);
35 ret = sqlite3_exec(sqlite, CREATE_TAC_DB_TABLE, NULL, NULL, &error);
36 if (ret != SQLITE_OK) {
37 _ERR("Sqlite error(%d) : %s", ret, error);
44 bool dbOpen(sqlite3 *tac_db, std::string path)
47 int ret = sqlite3_open(path.c_str(), &tac_db);
48 if (ret != SQLITE_OK) {
49 _ERR("Sqlite error : [%d] : path [%s]", ret, path.c_str());
56 bool dbClose(sqlite3 *tac_db)
59 sqlite3_exec(tac_db, "COMMIT;", NULL, NULL, NULL);
60 sqlite3_close(tac_db);
66 bool dbRollback(sqlite3 *tac_db)
69 sqlite3_exec(tac_db, "ROLLBACK;", NULL, NULL, NULL);
70 sqlite3_close(tac_db);
76 bool dbUpdate(sqlite3 *tac_db, std::string path, std::string query)
79 if (!dbOpen(tac_db, path)) {
82 int ret = sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
83 ret = sqlite3_prepare(tac_db, query.c_str(), QUERY_MAX_LEN , &stmt, NULL);
84 if (ret != SQLITE_OK) {
85 _ERR("Sqlite error : [%s,%s]", query.c_str(), sqlite3_errmsg(tac_db));
88 ret = sqlite3_step(stmt);
89 if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
90 _ERR("Sqlite error [%d]", ret);
93 ret = sqlite3_finalize(stmt);
94 if (ret != SQLITE_OK) {
95 _ERR("Sqlite error [%d]", ret);
101 ret = sqlite3_finalize(stmt);
102 if (ret != SQLITE_OK) {
103 _ERR("Sqlite error [%d]", ret);
109 bool dbInsert(sqlite3 *tac_db, std::string path, std::string query)
112 if (!dbOpen(tac_db, path)) {
115 int ret = sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
116 ret = sqlite3_prepare(tac_db, query.c_str(), QUERY_MAX_LEN , &stmt, NULL);
117 if (ret != SQLITE_OK) {
118 _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);
126 ret = sqlite3_finalize(stmt);
127 if (ret != SQLITE_OK) {
128 _ERR("Sqlite error [%d]", ret);
134 ret = sqlite3_finalize(stmt);
135 if (ret != SQLITE_OK) {
136 _ERR("Sqlite error [%d]", ret);
142 std::vector<std::string> dbSelect(sqlite3 *tac_db, std::string path, std::string query)
144 std::vector<std::string> updateDB;
146 const char* str = NULL;
147 if (!dbOpen(tac_db, path)) {
150 int ret = sqlite3_prepare_v2(tac_db, query.c_str(), strlen(query.c_str()), &stmt, NULL);
151 if (ret != SQLITE_OK) {
152 _ERR("Sqlite error : [%s,%s]", query.c_str(), sqlite3_errmsg(tac_db));
155 while (sqlite3_step(stmt) == SQLITE_ROW) {
156 //str = (const char *) sqlite3_column_text(stmt, 1);
157 //_DBG("pkgid : %s", (!str || !strlen(str)) ? NULL : strdup(str));
158 //str = (const char *) sqlite3_column_text(stmt, 3);
159 //_DBG("name : %s", (!str || !strlen(str)) ? NULL : strdup(str));
160 //str = (const char *) sqlite3_column_text(stmt, 4);
161 //_DBG("version : %s", (!str || !strlen(str)) ? NULL : strdup(str));
162 str = (const char *) sqlite3_column_text(stmt, 2);
163 _DBG("Nuget : %s", (!str || !strlen(str)) ? NULL : strdup(str));
164 updateDB.push_back((!str || !strlen(str)) ? NULL : strdup(str));
166 ret = sqlite3_finalize(stmt);
167 if (ret != SQLITE_OK) {
168 _ERR("Sqlite error [%d]", ret);
174 ret = sqlite3_finalize(stmt);
175 if (ret != SQLITE_OK) {
176 _ERR("Sqlite error [%d]", ret);
182 bool dbDelete(sqlite3 *tac_db, std::string path, std::string query)
185 if (!dbOpen(tac_db, path)) {
188 int ret = sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
189 ret = sqlite3_prepare(tac_db, query.c_str(), QUERY_MAX_LEN , &stmt, NULL);
190 if (ret != SQLITE_OK) {
191 _ERR("Sqlite error : [%s,%s]", query.c_str(), sqlite3_errmsg(tac_db));
194 ret = sqlite3_step(stmt);
195 if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
196 _ERR("Sqlite error [%d]", ret);
199 ret = sqlite3_finalize(stmt);
200 if (ret != SQLITE_OK) {
201 _ERR("Sqlite error [%d]", ret);
207 ret = sqlite3_finalize(stmt);
208 if (ret != SQLITE_OK) {
209 _ERR("Sqlite error [%d]", ret);