/*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
#include "db_manager.h"
#include "log.h"
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
+
sqlite3* dbCreate(std::string path)
{
sqlite3 *sqlite = NULL;
- char *error;
int ret = sqlite3_open(path.c_str(), &sqlite);
if (ret != SQLITE_OK) {
_ERR("Sqlite error : [%d] : path [%s]", ret, path.c_str());
- return 0;
+ return NULL;
}
- ret = sqlite3_exec(sqlite, "PRAGMA journal_mode = PERSIST", NULL, NULL, &error);
+ ret = sqlite3_exec(sqlite, "PRAGMA journal_mode = PERSIST", NULL, NULL, NULL);
if (ret != SQLITE_OK) {
- _ERR("Sqlite error(%d) : %s", ret, error);
- sqlite3_free(error);
- return 0;
+ _ERR("Sqlite error : [%d]", ret);
+ return NULL;
}
- ret = sqlite3_exec(sqlite, CREATE_TAC_DB_TABLE, NULL, NULL, &error);
+ ret = sqlite3_exec(sqlite, CREATE_TAC_DB_TABLE, NULL, NULL, NULL);
if (ret != SQLITE_OK) {
- _ERR("Sqlite error(%d) : %s", ret, error);
- sqlite3_free(error);
- return 0;
+ _ERR("Sqlite error : [%d] : path [%s]", ret, path.c_str());
+ return NULL;
}
return sqlite;
}
return true;
}
-bool dbClose(sqlite3 *tac_db)
+void dbFinalize(sqlite3_stmt *stmt)
+{
+ if (stmt) {
+ sqlite3_finalize(stmt);
+ stmt = NULL;
+ }
+}
+
+void dbClose(sqlite3 *tac_db)
{
if (tac_db) {
sqlite3_exec(tac_db, "COMMIT;", NULL, NULL, NULL);
sqlite3_close(tac_db);
tac_db = NULL;
}
- return true;
}
-bool dbRollback(sqlite3 *tac_db)
+void dbRollback(sqlite3 *tac_db)
{
if (tac_db) {
sqlite3_exec(tac_db, "ROLLBACK;", NULL, NULL, NULL);
sqlite3_close(tac_db);
tac_db = NULL;
}
- return true;
}
bool dbUpdate(sqlite3 *tac_db, std::string path, std::string query)
{
- sqlite3_stmt *stmt;
+ sqlite3_stmt *stmt = NULL;
if (!dbOpen(tac_db, path)) {
return false;
}
int ret = sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
ret = sqlite3_prepare(tac_db, query.c_str(), QUERY_MAX_LEN , &stmt, NULL);
if (ret != SQLITE_OK) {
- _ERR("Sqlite error : [%s,%s]", query.c_str(), sqlite3_errmsg(tac_db));
- goto ERROR;
+ _ERR("Sqlite error : [%s, %s]", query.c_str(), sqlite3_errmsg(tac_db));
+ dbClose(tac_db);
+ return false;
}
ret = sqlite3_step(stmt);
if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
- _ERR("Sqlite error [%d]", ret);
- goto ERROR;
- }
- ret = sqlite3_finalize(stmt);
- if (ret != SQLITE_OK) {
- _ERR("Sqlite error [%d]", ret);
+ _ERR("Sqlite error : [%d]", ret);
+ dbFinalize(stmt);
dbClose(tac_db);
return false;
}
+ dbFinalize(stmt);
return true;
-ERROR:
- ret = sqlite3_finalize(stmt);
- if (ret != SQLITE_OK) {
- _ERR("Sqlite error [%d]", ret);
- }
- dbClose(tac_db);
- return false;
}
bool dbInsert(sqlite3 *tac_db, std::string path, std::string query)
{
- sqlite3_stmt *stmt;
+ sqlite3_stmt *stmt = NULL;
if (!dbOpen(tac_db, path)) {
return false;
}
int ret = sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
ret = sqlite3_prepare(tac_db, query.c_str(), QUERY_MAX_LEN , &stmt, NULL);
if (ret != SQLITE_OK) {
- _ERR("Sqlite error : [%s,%s]", query.c_str(), sqlite3_errmsg(tac_db));
- goto ERROR;
+ _ERR("Sqlite error : [%s, %s]", query.c_str(), sqlite3_errmsg(tac_db));
+ dbClose(tac_db);
+ return false;
}
ret = sqlite3_step(stmt);
if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
- _ERR("Sqlite error [%d]", ret);
- goto ERROR;
- }
- ret = sqlite3_finalize(stmt);
- if (ret != SQLITE_OK) {
- _ERR("Sqlite error [%d]", ret);
+ _ERR("Sqlite error : [%d]", ret);
+ dbFinalize(stmt);
dbClose(tac_db);
return false;
}
+ dbFinalize(stmt);
return true;
-ERROR:
- ret = sqlite3_finalize(stmt);
- if (ret != SQLITE_OK) {
- _ERR("Sqlite error [%d]", ret);
- }
- dbClose(tac_db);
- return false;
}
std::vector<std::string> dbSelect(sqlite3 *tac_db, std::string path, std::string query)
{
std::vector<std::string> updateDB;
- sqlite3_stmt* stmt;
+ sqlite3_stmt* stmt = NULL;
const char* str = NULL;
if (!dbOpen(tac_db, path)) {
return updateDB;
}
int ret = sqlite3_prepare_v2(tac_db, query.c_str(), strlen(query.c_str()), &stmt, NULL);
if (ret != SQLITE_OK) {
- _ERR("Sqlite error : [%s,%s]", query.c_str(), sqlite3_errmsg(tac_db));
- goto ERROR;
+ _ERR("Sqlite error : [%s, %s]", query.c_str(), sqlite3_errmsg(tac_db));
+ dbClose(tac_db);
+ return updateDB;
}
while (sqlite3_step(stmt) == SQLITE_ROW) {
- //str = (const char *) sqlite3_column_text(stmt, 1);
- //_DBG("pkgid : %s", (!str || !strlen(str)) ? NULL : strdup(str));
- //str = (const char *) sqlite3_column_text(stmt, 3);
- //_DBG("name : %s", (!str || !strlen(str)) ? NULL : strdup(str));
- //str = (const char *) sqlite3_column_text(stmt, 4);
- //_DBG("version : %s", (!str || !strlen(str)) ? NULL : strdup(str));
str = (const char *) sqlite3_column_text(stmt, 2);
- _DBG("Nuget : %s", (!str || !strlen(str)) ? NULL : strdup(str));
updateDB.push_back((!str || !strlen(str)) ? NULL : strdup(str));
}
- ret = sqlite3_finalize(stmt);
- if (ret != SQLITE_OK) {
- _ERR("Sqlite error [%d]", ret);
- dbClose(tac_db);
- return updateDB;
- }
- return updateDB;
-ERROR:
- ret = sqlite3_finalize(stmt);
- if (ret != SQLITE_OK) {
- _ERR("Sqlite error [%d]", ret);
- }
- dbClose(tac_db);
+ dbFinalize(stmt);
return updateDB;
}
bool dbDelete(sqlite3 *tac_db, std::string path, std::string query)
{
- sqlite3_stmt *stmt;
+ sqlite3_stmt *stmt = NULL;
if (!dbOpen(tac_db, path)) {
return false;
}
int ret = sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
ret = sqlite3_prepare(tac_db, query.c_str(), QUERY_MAX_LEN , &stmt, NULL);
if (ret != SQLITE_OK) {
- _ERR("Sqlite error : [%s,%s]", query.c_str(), sqlite3_errmsg(tac_db));
- goto ERROR;
+ _ERR("Sqlite error : [%s, %s]", query.c_str(), sqlite3_errmsg(tac_db));
+ dbClose(tac_db);
+ return false;
}
ret = sqlite3_step(stmt);
if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
- _ERR("Sqlite error [%d]", ret);
- goto ERROR;
- }
- ret = sqlite3_finalize(stmt);
- if (ret != SQLITE_OK) {
- _ERR("Sqlite error [%d]", ret);
+ _ERR("Sqlite error : [%d]", ret);
+ dbFinalize(stmt);
dbClose(tac_db);
return false;
}
+ dbFinalize(stmt);
return true;
-ERROR:
- ret = sqlite3_finalize(stmt);
- if (ret != SQLITE_OK) {
- _ERR("Sqlite error [%d]", ret);
- }
- dbClose(tac_db);
- return false;
}