2 * Copyright (c) 2020 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.
21 #include "common/database.hh"
22 #include "common/exception.hh"
23 #include "common/log_private.hh"
27 static const int BUSY_WAITING_USEC = 50000;
28 static const int BUSY_WAITING_MAX = 40;
30 Database::Database(std::string path) : path_(std::move(path)) {
33 Database::~Database() {
37 void Database::BeginTransaction() {
38 int ret = sqlite3_exec(db_, "BEGIN TRANSACTION", nullptr, nullptr, nullptr);
39 if (ret != SQLITE_OK) {
40 _E("sqlite3_exec() is failed. error(%s:%d)", sqlite3_errmsg(db_), ret);
45 void Database::EndTransaction() {
46 int ret = sqlite3_exec(db_, "END TRANSACTION", nullptr, nullptr, nullptr);
47 if (ret != SQLITE_OK) {
48 _E("sqlite3_exec() is failed. error(%s:%d)", sqlite3_errmsg(db_), ret);
53 void Database::Rollback() {
54 int ret = sqlite3_exec(db_, "ROLLBACK", nullptr, nullptr, nullptr);
55 if (ret != SQLITE_OK) {
56 _E("sqlite3_exec() is failed. error(%s:%d", sqlite3_errmsg(db_), ret);
61 void Database::Open() {
63 int ret = sqlite3_open_v2(path_.c_str(), &db, SQLITE_OPEN_READWRITE, nullptr);
64 if (ret != SQLITE_OK) {
65 _E("sqlite3_open_v2() is failed. error(%d)", ret);
69 ret = sqlite3_busy_handler(db, BusyHandler,
70 static_cast<void*>(const_cast<char*>(path_.c_str())));
71 if (ret != SQLITE_OK) {
72 _E("sqlite3_busy_handler() is failed. error(%s:%d)",
73 sqlite3_errmsg(db), ret);
78 ret = sqlite3_exec(db, "PRAGMA foreign_keys=ON", nullptr, nullptr, nullptr);
79 if (ret != SQLITE_OK) {
80 _E("sqlite3_exec() is failed. error(%s:%d)", sqlite3_errmsg(db), ret);
88 void Database::Close() {
95 sqlite3* Database::GetHandle() {
99 std::string Database::ColumnText(sqlite3_stmt* stmt, int index) {
100 auto* text = reinterpret_cast<const char*>(sqlite3_column_text(stmt, index));
102 return std::string(text);
107 bool Database::IntegrityCheck() {
108 static const char query[] = "PRAGMA integrity_check";
110 int ret = sqlite3_prepare_v2(db_, query, strlen(query), &stmt, nullptr);
111 if (ret != SQLITE_OK) {
112 _E("sqlite3_prepare_v2() is failed. error(%s:%d)",
113 sqlite3_errmsg(db_), ret);
117 std::unique_ptr<sqlite3_stmt, decltype(sqlite3_finalize)*> stmt_ptr(
118 stmt, sqlite3_finalize);
120 ret = sqlite3_step(stmt);
121 if (ret != SQLITE_ROW) {
122 _E("sqlite3_step() is failed. error(%s:%d)", sqlite3_errmsg(db_), ret);
126 auto* res = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
127 if (res == nullptr) {
128 _E("sqlite3_column_text() is failed. error(%s)", sqlite3_errmsg(db_));
132 if (strcmp(res, "ok") != 0)
138 int Database::BusyHandler(void* data, int count) {
139 if (count < BUSY_WAITING_MAX) {
140 usleep(BUSY_WAITING_USEC);
143 auto* path = static_cast<char*>(data);
144 _E("Database(%s) is busy", path);
148 } // namespace plugin