2 * Copyright (c) 2021 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 "server/common_private.hh"
22 #include "server/database.hh"
23 #include "server/exception.hh"
24 #include "server/log_private.hh"
29 const int BUSY_WAITING_USEC = 50000;
30 const int BUSY_WAITING_MAX = 40;
34 Database::Database(std::string path) : path_(std::move(path)) {
37 Database::~Database() {
41 void Database::BeginTransaction() {
42 int ret = sqlite3_exec(db_, "BEGIN TRANSACTION", nullptr, nullptr, nullptr);
44 _E("sqlite3_exec() is failed. error(%s:%d)", sqlite3_errmsg(db_), ret);
47 void Database::EndTransaction() {
48 int ret = sqlite3_exec(db_, "END TRANSACTION", nullptr, nullptr, nullptr);
50 _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);
56 _E("sqlite3_exec() is failed. error(%s:%d", sqlite3_errmsg(db_), ret);
59 void Database::Open(int flags) {
61 int ret = sqlite3_open_v2(path_.c_str(), &db, flags, nullptr);
62 if (ret != SQLITE_OK) {
63 _E("sqlite3_open_v2() is failed. path(%s), error(%d)", path_.c_str(), ret);
67 ret = sqlite3_busy_handler(db, BusyHandler,
68 static_cast<void*>(const_cast<char*>(path_.c_str())));
69 if (ret != SQLITE_OK) {
70 _E("sqlite3_busy_handler() is failed. error(%s:%d)",
71 sqlite3_errmsg(db), ret);
76 ret = sqlite3_exec(db, "PRAGMA foreign_keys=ON", nullptr, nullptr, nullptr);
77 if (ret != SQLITE_OK) {
78 _E("sqlite3_exec() is failed. error(%s:%d)", sqlite3_errmsg(db), ret);
86 void Database::Close() {
93 sqlite3* Database::GetHandle() {
97 std::string Database::ColumnText(sqlite3_stmt* stmt, int index) {
98 auto* text = reinterpret_cast<const char*>(sqlite3_column_text(stmt, index));
100 return std::string(text);
105 bool Database::IntegrityCheck() {
106 static const char query[] = "PRAGMA integrity_check";
108 int ret = sqlite3_prepare_v2(db_, query, strlen(query), &stmt, nullptr);
109 if (ret != SQLITE_OK) {
110 _E("sqlite3_prepare_v2() is failed. error(%s:%d)",
111 sqlite3_errmsg(db_), ret);
115 std::unique_ptr<sqlite3_stmt, decltype(sqlite3_finalize)*> stmt_ptr(
116 stmt, sqlite3_finalize);
118 ret = sqlite3_step(stmt);
119 if (ret != SQLITE_ROW) {
120 _E("sqlite3_step() is failed. error(%s:%d)", sqlite3_errmsg(db_), ret);
124 auto* res = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
125 if (res == nullptr) {
126 _E("sqlite3_column_text() is failed. error(%s)", sqlite3_errmsg(db_));
130 if (strcmp(res, "ok") != 0)
136 std::string Database::GetPath(const std::string& name, uid_t uid) {
137 std::string path = std::string(tzplatform_getenv(TZ_SYS_DB)) + "/";
138 if (uid >= REGULAR_UID_MIN)
139 path += "user/" + std::to_string(uid) + "/";
145 int Database::BusyHandler(void* data, int count) {
146 if (count < BUSY_WAITING_MAX) {
147 usleep(BUSY_WAITING_USEC);
151 auto* path = static_cast<char*>(data);
152 _E("Database(%s) is busy", path);