#include <string>
#include "theme/utils/logging.h"
-#include "theme_provider/sqlite_connection.h"
-#include "theme_provider/sqlite_statement.h"
namespace {
namespace ttm {
namespace provider {
-DbManager::DbManager(const std::string& db_path) {
- conn_ = std::make_shared<SQLiteConnection>(db_path, false);
- if (!conn_->Execute(kCreateThemeTableQuery))
+DbManager::DbManager(const std::string& db_path)
+ : conn_(db_path, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) {
+ if (!static_cast<bool>(conn_.Exec({ kCreateThemeTableQuery })))
LOG(ERROR) << "Failed to create theme table";
- if (!conn_->Execute(kCreateIndexQuery))
+ if (!static_cast<bool>(conn_.Exec({ kCreateIndexQuery })))
LOG(ERROR) << "Failed to create unique index for theme table";
- if (!conn_->Execute(kCreateThemeSettingTableQuery))
+ if (!static_cast<bool>(conn_.Exec({ kCreateThemeSettingTableQuery })))
LOG(ERROR) << "Failed to create theme_setting table";
// if current process running as root, this is image creation
}
bool DbManager::Insert(const tizen_base::Bundle& info) {
- std::unique_ptr<SQLiteStatement> stmt = conn_->PrepareStatement(kInsertQuery);
- if (!stmt) {
- LOG(ERROR) << "Failed to prepare statement";
- return false;
- }
-
- int idx = 1;
- if (!stmt->BindString(idx++, info.GetString("id")))
- return false;
- if (!stmt->BindString(idx++, info.GetString("pkgid")))
- return false;
- if (!stmt->BindString(idx++, info.GetString("/version")))
- return false;
- if (!stmt->BindString(idx++, info.GetString("/tool_version")))
- return false;
- if (!stmt->BindString(idx++, info.GetString("/title")))
- return false;
- if (!stmt->BindString(idx++, info.GetString("/description")))
- return false;
- if (!stmt->BindString(idx++, info.GetString("/preview")))
- return false;
- if (!stmt->BindString(idx++, info.GetString("/resolution")))
- return false;
- if (!stmt->BindString(idx++, info.GetString("is_default")))
- return false;
auto raw = const_cast<tizen_base::Bundle&>(info).ToRaw();
std::vector<unsigned char> v(raw.first.get(), raw.first.get() + raw.second);
- if (!stmt->BindBlob(idx++, v))
- return false;
-
- if (stmt->Step() != SQLiteStatement::StepResult::DONE) {
+ auto q = tizen_base::Database::Sql(kInsertQuery)
+ .SetEmptyStringAsNull(true)
+ .Bind(info.GetString("id"))
+ .Bind(info.GetString("pkgid"))
+ .Bind(info.GetString("/version"))
+ .Bind(info.GetString("/tool_version"))
+ .Bind(info.GetString("/title"))
+ .Bind(info.GetString("/description"))
+ .Bind(info.GetString("/preview"))
+ .Bind(info.GetString("/resolution"))
+ .Bind(info.GetString("is_default"))
+ .Bind(std::move(v));
+
+ auto r = conn_.Exec(q);
+ if (!static_cast<bool>(r)) {
LOG(ERROR) << "Failed to insert ThemeInfo into DB";
return false;
}
}
bool DbManager::Update(const tizen_base::Bundle& info) {
- std::unique_ptr<SQLiteStatement> stmt = conn_->PrepareStatement(kUpdateQuery);
- if (!stmt) {
- LOG(ERROR) << "Failed to prepare statement";
- return false;
- }
-
- int idx = 1;
- if (!stmt->BindString(idx++, info.GetString("version")))
- return false;
- if (!stmt->BindString(idx++, info.GetString("tool_version")))
- return false;
- if (!stmt->BindString(idx++, info.GetString("title")))
- return false;
- if (!stmt->BindString(idx++, info.GetString("description")))
- return false;
- if (!stmt->BindString(idx++, info.GetString("preview")))
- return false;
- if (!stmt->BindString(idx++, info.GetString("resolution")))
- return false;
auto raw = const_cast<tizen_base::Bundle&>(info).ToRaw();
std::vector<unsigned char> v(raw.first.get(), raw.first.get() + raw.second);
- if (!stmt->BindBlob(idx++, v))
- return false;
- if (!stmt->BindString(idx++, info.GetString("id")))
- return false;
-
- if (stmt->Step() != SQLiteStatement::StepResult::DONE) {
+ auto q = tizen_base::Database::Sql(kUpdateQuery)
+ .SetEmptyStringAsNull(true)
+ .Bind(info.GetString("version"))
+ .Bind(info.GetString("tool_version"))
+ .Bind(info.GetString("title"))
+ .Bind(info.GetString("description"))
+ .Bind(info.GetString("preview"))
+ .Bind(info.GetString("resolution"))
+ .Bind(std::move(v))
+ .Bind(info.GetString("id"));
+
+ auto r = conn_.Exec(q);
+ if (!static_cast<bool>(r)) {
LOG(ERROR) << "Failed to update ThemeInfo into DB";
return false;
}
}
bool DbManager::Delete(const std::string& id) {
- std::unique_ptr<SQLiteStatement> stmt = conn_->PrepareStatement(kDeleteQuery);
- if (!stmt) {
- LOG(ERROR) << "Failed to prepare statement";
- return false;
- }
+ auto q = tizen_base::Database::Sql(kDeleteQuery)
+ .Bind(id);
- if (!stmt->BindString(1, id))
- return false;
-
- if (stmt->Step() != SQLiteStatement::StepResult::DONE) {
+ auto r = conn_.Exec(q);
+ if (!static_cast<bool>(r)) {
LOG(ERROR) << "Failed to delete ThemeInfo of " << id;
return false;
}
}
tizen_base::Bundle DbManager::Select(const std::string& id) {
- std::unique_ptr<SQLiteStatement> stmt = conn_->PrepareStatement(kSelectQuery);
- if (!stmt) {
- LOG(ERROR) << "Failed to prepare statement";
+ auto q = tizen_base::Database::Sql(kSelectQuery)
+ .Bind(id);
+
+ auto r = conn_.Exec(q);
+ if (!static_cast<bool>(r)) {
+ LOG(ERROR) << "Failed to select ThemeInfo of " << id;
return {};
}
- if (!stmt->BindString(1, id))
+ auto rec = r.GetFirstRecord();
+ if (!rec)
return {};
- if (stmt->Step() != SQLiteStatement::StepResult::ROW) {
- LOG(ERROR) << "Failed to select ThemeInfo of " << id;
+ std::optional<std::vector<unsigned char>> v = rec->Get(0);
+ if (!v)
return {};
- }
- int idx = 0;
- std::vector<unsigned char> v = stmt->GetColumnBlob(idx++);
- std::string raw(reinterpret_cast<char const*>(v.data()), v.size());
+ std::string raw(reinterpret_cast<char const*>(v->data()), v->size());
tizen_base::Bundle b(raw);
return b;
}
tizen_base::Bundle DbManager::SelectDefault() {
- std::unique_ptr<SQLiteStatement> stmt =
- conn_->PrepareStatement(kSelectDefaultQuery);
- if (!stmt) {
- LOG(ERROR) << "Failed to prepare statement";
+ auto r = conn_.Exec({ kSelectDefaultQuery });
+ if (!static_cast<bool>(r)) {
+ LOG(ERROR) << "Failed to select default ThemeInfo";
return {};
}
- if (stmt->Step() != SQLiteStatement::StepResult::ROW) {
- LOG(ERROR) << "Failed to select default ThemeInfo";
+ auto rec = r.GetFirstRecord();
+ if (!rec)
return {};
- }
- int idx = 0;
- std::vector<unsigned char> v = stmt->GetColumnBlob(idx++);
- std::string raw(reinterpret_cast<char const*>(v.data()), v.size());
+ std::optional<std::vector<unsigned char>> v = rec->Get(0);
+ if (!v)
+ return {};
+
+ std::string raw(reinterpret_cast<char const*>(v->data()), v->size());
tizen_base::Bundle b(raw);
return b;
}
tizen_base::Bundle DbManager::SelectCurrent() {
- std::unique_ptr<SQLiteStatement> stmt =
- conn_->PrepareStatement(kSelectCurrentQuery);
- if (!stmt) {
- LOG(ERROR) << "Failed to prepare statement";
+ auto r = conn_.Exec({ kSelectCurrentQuery });
+ if (!static_cast<bool>(r)) {
+ LOG(ERROR) << "Failed to select default ThemeInfo";
return {};
}
- if (stmt->Step() != SQLiteStatement::StepResult::ROW) {
- LOG(ERROR) << "Failed to select default ThemeInfo";
+ auto rec = r.GetFirstRecord();
+ if (!rec)
return {};
- }
- int idx = 0;
- std::vector<unsigned char> v = stmt->GetColumnBlob(idx++);
- std::string raw(reinterpret_cast<char const*>(v.data()), v.size());
+ std::optional<std::vector<unsigned char>> v = rec->Get(0);
+ if (!v)
+ return {};
+
+ std::string raw(reinterpret_cast<char const*>(v->data()), v->size());
tizen_base::Bundle b(raw);
return b;
}
std::vector<std::string> DbManager::SelectIds() {
- std::unique_ptr<SQLiteStatement> stmt =
- conn_->PrepareStatement(kSelectIdsQuery);
- if (!stmt) {
+ auto r = conn_.Exec({ kSelectIdsQuery });
+ if (!static_cast<bool>(r)) {
LOG(ERROR) << "Failed to prepare statement";
return {};
}
std::vector<std::string> ids;
- while (stmt->Step() == SQLiteStatement::StepResult::ROW)
- ids.emplace_back(stmt->GetColumnString(0));
+ for (const auto& i : r)
+ ids.emplace_back(static_cast<std::string>(i.Get(0)));
return ids;
}
bool DbManager::UpdateCurrentId(const std::string& id) {
- std::unique_ptr<SQLiteStatement> stmt =
- conn_->PrepareStatement(kSetThemeQuery);
- if (!stmt) {
- LOG(ERROR) << "Failed to prepare statement";
- return false;
- }
-
- if (!stmt->BindString(1, id))
- return false;
+ auto q = tizen_base::Database::Sql(kSetThemeQuery)
+ .Bind(id);
- if (stmt->Step() != SQLiteStatement::StepResult::DONE) {
+ auto r = conn_.Exec(q);
+ if (!static_cast<bool>(r)) {
LOG(ERROR) << "Failed to set current theme as " << id;
return false;
}
+++ /dev/null
-// Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
-// Use of this source code is governed by a apache 2.0 license that can be
-// found in the LICENSE file.
-
-#include "theme_provider/sqlite_connection.h"
-
-#include <sqlite3.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include <string>
-#include <utility>
-
-#include "theme/utils/logging.h"
-#include "theme_provider/sqlite_statement.h"
-
-namespace ttm {
-namespace provider {
-
-SQLiteConnection::SQLiteConnection(std::string path, bool readonly)
- : path_(std::move(path)), db_(nullptr) {
- if (!Connect(readonly))
- LOG(ERROR) << "Failed to connect db";
-}
-
-SQLiteConnection::~SQLiteConnection() {
- Disconnect();
-}
-
-bool SQLiteConnection::Connect(bool readonly) {
- int flags;
- if (readonly)
- flags = SQLITE_OPEN_READONLY;
- else
- flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
-
- sqlite3* db;
- int r = sqlite3_open_v2(path_.c_str(), &db, flags, nullptr);
- if (r != SQLITE_OK) {
- LOG(ERROR) << "Failed to open sqlite3 db("
- << path_ << "). Error code: " << r;
- SetErrorCode(r);
- sqlite3_close_v2(db);
- return false;
- }
- r = sqlite3_exec(db, "PRAGMA foreign_keys=ON", nullptr, nullptr, nullptr);
- if (r != SQLITE_OK)
- LOG(ERROR) << "Failed to enable foreign key support: " << r;
- db_ = db;
- return true;
-}
-
-bool SQLiteConnection::Disconnect() {
- if (db_) {
- sqlite3_close_v2(db_);
- db_ = nullptr;
- }
- return true;
-}
-
-void SQLiteConnection::SetErrorCode(int error_code) {
- error_code_ = error_code;
-}
-
-int SQLiteConnection::GetErrorCode() {
- return error_code_;
-}
-
-bool SQLiteConnection::Execute(const std::string& command) {
- if (!db_) {
- LOG(ERROR) << "Invalid SQLite connection";
- return false;
- }
-
- int r = sqlite3_exec(db_, command.c_str(), nullptr, nullptr, nullptr);
- if (r != SQLITE_OK) {
- LOG(ERROR) << "Execute command(" << command << ") failed: "
- << GetErrorMessage();
- SetErrorCode(r);
- return false;
- }
- return true;
-}
-
-std::string SQLiteConnection::GetErrorMessage() const {
- return sqlite3_errmsg(db_);
-}
-
-std::unique_ptr<SQLiteStatement> SQLiteConnection::PrepareStatement(
- const std::string& query) {
- if (!db_) {
- LOG(ERROR) << "Invalid SQLite connection";
- return {};
- }
-
- sqlite3_stmt* stmt;
- int r = sqlite3_prepare_v2(db_, query.c_str(), query.length(), &stmt,
- nullptr);
- if (r != SQLITE_OK) {
- LOG(ERROR) << "sqlite3_prepare_v2() failed: " << GetErrorMessage();
- return {};
- }
- return std::make_unique<SQLiteStatement>(shared_from_this(), stmt);
-}
-
-bool SQLiteConnection::IsValid() {
- if (db_)
- return true;
- return false;
-}
-
-} // namespace provider
-} // namespace ttm
+++ /dev/null
-// Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
-// Use of this source code is governed by a apache 2.0 license that can be
-// found in the LICENSE file.
-
-#include "theme_provider/sqlite_statement.h"
-
-#include <sqlite3.h>
-
-#include <string>
-#include <vector>
-
-#include "theme/utils/logging.h"
-#include "theme_provider/sqlite_connection.h"
-
-namespace ttm {
-namespace provider {
-
-SQLiteStatement::SQLiteStatement(std::shared_ptr<SQLiteConnection> sql_conn,
- sqlite3_stmt* stmt) : sql_conn_(sql_conn), stmt_(stmt) {}
-
-SQLiteStatement::~SQLiteStatement() {
- int r = sqlite3_finalize(stmt_);
- if (r != SQLITE_OK)
- LOG(ERROR) << "sqlite3_finalize() failed: " << GetErrorMessage();
-}
-
-SQLiteStatement::StepResult SQLiteStatement::Step() {
- int r = sqlite3_step(stmt_);
- if (r != SQLITE_ROW && r != SQLITE_DONE) {
- LOG(ERROR) << "sqlite3_step() failed: " << GetErrorMessage();
- sql_conn_->SetErrorCode(r);
- return SQLiteStatement::StepResult::ERROR;
- }
- SQLiteStatement::StepResult res;
- if (r == SQLITE_ROW) {
- res = SQLiteStatement::StepResult::ROW;
- } else if (r == SQLITE_DONE) {
- res = SQLiteStatement::StepResult::DONE;
- } else {
- LOG(ERROR) << "Unexpected sqlite3 result code: " << r;
- res = SQLiteStatement::StepResult::ERROR;
- }
- return res;
-}
-
-bool SQLiteStatement::BindNull(int pos) {
- int r = sqlite3_bind_null(stmt_, pos);
- if (r != SQLITE_OK) {
- LOG(ERROR) << "sqlite3_bind_null() failed: " << GetErrorMessage();
- sql_conn_->SetErrorCode(r);
- return false;
- }
- return true;
-}
-
-bool SQLiteStatement::BindString(int pos, const std::string& val) {
- if (val.empty())
- return BindNull(pos);
-
- int r = sqlite3_bind_text(stmt_, pos, val.c_str(), -1, SQLITE_TRANSIENT);
- if (r != SQLITE_OK) {
- LOG(ERROR) << "sqlite3_bind_text() failed: " << GetErrorMessage();
- sql_conn_->SetErrorCode(r);
- return false;
- }
- return true;
-}
-
-bool SQLiteStatement::BindBlob(int pos, const std::vector<unsigned char>& val) {
- int r = sqlite3_bind_blob(stmt_, pos, val.data(), val.size(),
- SQLITE_TRANSIENT);
- if (r != SQLITE_OK) {
- LOG(ERROR) << "sqlite3_bind_blob() failed: " << GetErrorMessage();
- sql_conn_->SetErrorCode(r);
- return false;
- }
- return true;
-}
-
-std::string SQLiteStatement::GetColumnString(int pos) const {
- const char* val = reinterpret_cast<const char*>(
- sqlite3_column_text(stmt_, pos));
- if (!val)
- return {};
- return std::string(val);
-}
-
-std::vector<unsigned char> SQLiteStatement::GetColumnBlob(int pos) const {
- const unsigned char* val = reinterpret_cast<const unsigned char*>(
- sqlite3_column_blob(stmt_, pos));
- if (!val)
- return {};
- int len = sqlite3_column_bytes(stmt_, pos);
- if (len < 0) {
- LOG(ERROR) << "sqlite3_column_bytes() failed: " << GetErrorMessage();
- return {};
- }
- return std::vector<unsigned char>(val, val + len);
-}
-
-std::string SQLiteStatement::GetErrorMessage() const {
- return sqlite3_errmsg(sqlite3_db_handle(stmt_));
-}
-
-} // namespace provider
-} // namespace ttm