#include <string>
#include <vector>
+#include <database.hpp>
#include "db_handle_provider.hh"
#include "utils/logging.hh"
constexpr useconds_t BUSY_WAITING_USEC = (1000000 / 10 / 2); /* 0.05 sec */
constexpr int BUSY_WAITING_MAX = 100; /* wait for max 5 sec */
-int __readdb_busy_handler(void *data, int count) {
+bool ReadDbBusyHandler(int count) {
if (count < BUSY_WAITING_MAX) {
usleep(BUSY_WAITING_USEC);
- return 1;
+ return true;
} else {
- /* sqlite3_prepare_v2 will return SQLITE_BUSY */
- return 0;
+ return false;
}
}
-int __open_read_db(const char *path, sqlite3 **db) {
- int ret;
-
- ret = sqlite3_open_v2(path, db,
- SQLITE_OPEN_READONLY | SQLITE_OPEN_URI, NULL);
- if (ret != SQLITE_OK) {
- sqlite3_close_v2(*db);
- return ret;
- }
-
- ret = sqlite3_busy_handler(*db, __readdb_busy_handler, NULL);
- if (ret != SQLITE_OK) {
- LOG(ERROR) << "failed to register busy handler:" << sqlite3_errmsg(*db);
- sqlite3_close_v2(*db);
- return ret;
- }
+tizen_base::Database OpenReadDb(const std::string& path) {
+ tizen_base::Database db(path.c_str(), SQLITE_OPEN_READONLY | SQLITE_OPEN_URI,
+ ReadDbBusyHandler);
- return ret;
+ return db;
}
constexpr const char RESOURCED_BUS_NAME[] = "org.tizen.resourced";
constexpr const char RESOURCED_PROC_INTERFACE[] = "org.tizen.resourced.process";
constexpr const char RESOURCED_PROC_METHOD[] = "ProcExclude";
-int __writedb_busy_handler(void *data, int count) {
+bool WriteDbBusyHandler(int count) {
if (count < (BUSY_WAITING_MAX / 2)) {
usleep(BUSY_WAITING_USEC);
- return 1;
+ return true;
} else if (count < BUSY_WAITING_MAX) {
usleep(BUSY_WAITING_USEC);
- return 1;
+ return true;
} else {
- /* sqlite3_prepare_v2 will return SQLITE_BUSY */
- return 0;
+ return false;
}
}
-int __open_write_db(uid_t uid, const char* path,
- sqlite3** db) {
- int ret;
-
- ret = sqlite3_open_v2(path, db, SQLITE_OPEN_READWRITE, NULL);
- if (ret != SQLITE_OK) {
- sqlite3_close_v2(*db);
- return ret;
- }
-
- ret = sqlite3_busy_handler(*db, __writedb_busy_handler,
- reinterpret_cast<void*>(const_cast<char*>(path)));
- if (ret != SQLITE_OK) {
- LOG(ERROR) << "failed to register busy handler:" << sqlite3_errmsg(*db);
- sqlite3_close_v2(*db);
- return ret;
- }
-
- ret = sqlite3_exec(*db, "PRAGMA foreign_keys=ON", NULL, NULL, NULL);
- if (ret != SQLITE_OK) {
- LOG(ERROR) << "failed to enable foreign key support:"
- << sqlite3_errmsg(*db);
- sqlite3_close_v2(*db);
- return ret;
- }
+tizen_base::Database OpenWriteDb(uid_t uid, const std::string& path) {
+ tizen_base::Database db(path.c_str(), SQLITE_OPEN_READWRITE,
+ WriteDbBusyHandler);
+ db.OneStepExec({ "PRAGMA foreign_keys=ON" });
- return ret;
+ return db;
}
-int __open_create_db(uid_t uid, const char* path,
- sqlite3** db) {
- int ret;
+tizen_base::Database OpenCreateDb(uid_t uid, const std::string& path) {
+ tizen_base::Database db(path.c_str(), SQLITE_OPEN_READWRITE |
+ SQLITE_OPEN_CREATE, WriteDbBusyHandler);
- ret = sqlite3_open_v2(path, db,
- SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
- if (ret != SQLITE_OK) {
- sqlite3_close_v2(*db);
- return ret;
- }
-
- ret = sqlite3_busy_handler(*db, __writedb_busy_handler,
- reinterpret_cast<void*>(const_cast<char*>(path)));
- if (ret != SQLITE_OK) {
- LOG(ERROR) << "failed to register busy handler:" << sqlite3_errmsg(*db);
- sqlite3_close_v2(*db);
- return ret;
- }
-
- return ret;
+ return db;
}
-static uid_t globaluser_uid = -1;
-
uid_t ConvertUID(uid_t uid) {
+ static uid_t globaluser_uid = -1;
+
if (uid < REGULAR_USER) {
if (globaluser_uid == (uid_t)-1)
globaluser_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
std::shared_mutex AbstractDBHandler::lock_;
-AbstractDBHandler::~AbstractDBHandler() {
- for (auto& db_handle : db_handle_list_)
- sqlite3_close_v2(db_handle.first);
-}
+AbstractDBHandler::~AbstractDBHandler() = default;
std::vector<std::pair<std::string, uid_t>> AbstractDBHandler::GetDBPath() {
std::vector<std::pair<std::string, uid_t>> db_path;
}
auto dbpath_list = GetDBPath();
- sqlite3* db;
- for (auto& dbpath : dbpath_list) {
- int ret = 0;
- if (op_type_ == pkgmgr_common::DBOperationType::OPERATION_TYPE_READ) {
- ret = __open_read_db(dbpath.first.c_str(), &db);
- } else if (
- op_type_ == pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE) {
- if (ConvertUID(dbpath.second) != ConvertUID(uid_))
- continue;
- ret = __open_write_db(uid_, dbpath.first.c_str(), &db);
- } else {
- if (ConvertUID(dbpath.second) != ConvertUID(uid_))
- continue;
-
- if (access(dbpath.first.c_str(), F_OK) != -1) {
- LOG(ERROR) << "Database for user " << uid_ << " is already exists";
- return false;
+
+ try {
+ for (const auto& [path, uid] : dbpath_list) {
+ if (op_type_ == pkgmgr_common::DBOperationType::OPERATION_TYPE_READ) {
+ db_handle_list_.emplace_back(OpenReadDb(path), uid);
+ } else if (
+ op_type_ == pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE) {
+ if (ConvertUID(uid) != ConvertUID(uid_))
+ continue;
+ db_handle_list_.emplace_back(OpenWriteDb(uid_, path), uid);
+ } else {
+ if (ConvertUID(uid) != ConvertUID(uid_))
+ continue;
+
+ if (access(path.c_str(), F_OK) != -1) {
+ LOG(ERROR) << "Database for user " << uid_ << " is already exists";
+ return false;
+ }
+
+ db_handle_list_.emplace_back(OpenCreateDb(uid_, path), uid);
}
- ret = __open_create_db(uid_, dbpath.first.c_str(), &db);
}
-
- if (ret != SQLITE_OK)
- return false;
-
- db_handle_list_.emplace_back(std::make_pair(db, dbpath.second));
+ } catch (const tizen_base::DbException& e) {
+ LOG(ERROR) << e.msg();
+ return false;
}
return true;
}
void AbstractDBHandler::ClearDBHandle() {
- for (const auto& db_handle : db_handle_list_)
- sqlite3_close_v2(db_handle.first);
-
db_handle_list_.clear();
}
-std::vector<std::pair<sqlite3*, uid_t>> AbstractDBHandler::GetConnection() {
+const std::vector<std::pair<tizen_base::Database, uid_t>>&
+ AbstractDBHandler::GetConnection() {
return db_handle_list_;
}