return false;
for (const auto& i : r) {
- std::string table_name = i.Get(0);
+ auto table_name = static_cast<std::string>(i.Get(0));
if (table_name == "cion")
return true;
}
GetUuidWithGenerate(appid, uuid);
}));
tools->GetMethodBroker().Register("Cion.SetDisplayName",
- api::Params<const std::string&, const std::string&, const std::string&, int&>(
+ api::Params<const std::string&, const std::string&,
+ const std::string&, int&>(
[this](const std::string& appid, const std::string& service_name,
const std::string& display_name, int& ret) {
SetDisplayName(appid, service_name, display_name, ret);
bool CionModule::Init() {
try {
esd_cion_server_ =
- std::make_shared<CionOndemandServer>("__CION_INTERNAL_DAEMON__", "", this);
+ std::make_shared<CionOndemandServer>("__CION_INTERNAL_DAEMON__",
+ "", this);
} catch (const cion::Exception& e) {
_D("cion_init failed : %s", e.what());
return false;
}
void CionModule::SetDisplayName(const std::string& appid,
- const std::string& service_name, const std::string& display_name, int& ret) {
+ const std::string& service_name, const std::string& display_name,
+ int& ret) {
try {
tizen_base::Database db(DBPATH, SQLITE_OPEN_READWRITE);
std::string uuid;
}
}
-void CionModule::SetEnabled(const std::string& appid, const std::string& service_name,
- bool enabled, int& ret) {
+void CionModule::SetEnabled(const std::string& appid,
+ const std::string& service_name, bool enabled, int& ret) {
try {
tizen_base::Database db(DBPATH, SQLITE_OPEN_READWRITE);
return;
for (const auto& i : r2) {
- std::string display_name = i.Get(0);
+ auto display_name = static_cast<std::string>(i.Get(0));
if (enabled)
AddEnabledApp(service_name, appid, display_name);
else
}
}
-void CionModule::GetEnabled(const std::string& appid, const std::string& service_name,
- bool& enabled, int& ret) {
+void CionModule::GetEnabled(const std::string& appid,
+ const std::string& service_name, bool& enabled, int& ret) {
try {
tizen_base::Database db(DBPATH, SQLITE_OPEN_READWRITE);
+++ /dev/null
-/*
- * Copyright (c) 2022 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef DATABASE_HPP_
-#define DATABASE_HPP_
-
-#include <sqlite3.h>
-
-#include <functional>
-#include <map>
-#include <memory>
-#include <string>
-#include <tuple>
-#include <variant>
-#include <vector>
-
-namespace tizen_base {
-
-template<std::size_t N>
-struct num {
- static const constexpr auto value = N;
-};
-
-template <class F, std::size_t... Is>
-void for_(F func, std::index_sequence<Is...>) {
- (func(num<Is>{}), ...);
-}
-
-template <std::size_t N, typename F>
-void for_(F func) {
- for_(func, std::make_index_sequence<N>());
-}
-
-using DbType = std::variant<int, double, std::string,
- std::vector<unsigned char>>;
-
-class AutoDbType {
- public:
- AutoDbType() = default;
- AutoDbType(DbType db_type) : db_type_(db_type) {}
-
- operator int () {
- return std::get<int>(db_type_);
- }
-
- operator std::string () {
- return std::get<std::string>(db_type_);
- }
-
- operator double () {
- return std::get<double>(db_type_);
- }
-
- operator std::vector<unsigned char> () {
- return std::get<std::vector<unsigned char>>(db_type_);
- }
-
- private:
- DbType db_type_;
-};
-
-using _ = AutoDbType;
-
-class Database {
- public:
- class TransactionGuard {
- public:
- TransactionGuard(const TransactionGuard&) = delete;
- TransactionGuard& operator = (const TransactionGuard&) = delete;
-
- TransactionGuard(TransactionGuard&& t) noexcept {
- db_ = t.db_;
- t.db_ = nullptr;
- }
-
- TransactionGuard& operator = (TransactionGuard&& t) noexcept {
- if (this != &t) {
- if (db_)
- sqlite3_exec(db_, "ROLLBACK", nullptr, nullptr, nullptr);
- db_ = t.db_;
- t.db_ = nullptr;
- }
-
- return *this;
- }
-
- TransactionGuard(sqlite3* db) : db_(db) {
- if (sqlite3_exec(db, "BEGIN DEFERRED", NULL, NULL, NULL) != SQLITE_OK) {
- throw std::runtime_error("begin transaction failed");
- }
- }
-
- ~TransactionGuard() {
- if (db_)
- sqlite3_exec(db_, "ROLLBACK", nullptr, nullptr, nullptr);
- }
-
- int Commit() {
- int ret = sqlite3_exec(db_, "COMMIT", nullptr, nullptr, nullptr);
- if (ret != SQLITE_OK) {
- sqlite3_exec(db_, "ROLLBACK", nullptr, nullptr, nullptr);
- }
-
- db_ = nullptr;
- return ret;
- }
-
- private:
- sqlite3* db_ = nullptr;
- };
-
- class Sql {
- public:
- Sql(std::string query) : query_(std::move(query)) {}
-
- Sql& Bind(std::string val) {
- bindings_.push_back(DbType(std::move(val)));
- return *this;
- }
-
- Sql& Bind(int val) {
- bindings_.push_back(DbType(val));
- return *this;
- }
-
- Sql& Bind(double val) {
- bindings_.push_back(DbType(val));
- return *this;
- }
-
- Sql& Bind(std::vector<unsigned char> val) {
- bindings_.push_back(DbType(std::move(val)));
- return *this;
- }
-
- Sql& Bind(int pos, std::string val) {
- binding_map_[pos] = DbType(std::move(val));
- return *this;
- }
-
- Sql& Bind(int pos, int val) {
- binding_map_[pos] = DbType(val);
- return *this;
- }
-
- Sql& Bind(int pos, double val) {
- binding_map_[pos] = DbType(val);
- return *this;
- }
-
- Sql& Bind(int pos, std::vector<unsigned char> val) {
- binding_map_[pos] = DbType(std::move(val));
- return *this;
- }
-
- Sql& Bind(std::string name, std::string val) {
- binding_name_map_[std::move(name)] = DbType(std::move(val));
- return *this;
- }
-
- Sql& Bind(std::string name, int val) {
- binding_name_map_[std::move(name)] = DbType(val);
- return *this;
- }
-
- Sql& Bind(std::string name, double val) {
- binding_name_map_[std::move(name)] = DbType(val);
- return *this;
- }
-
- Sql& Bind(std::string name, std::vector<unsigned char> val) {
- binding_name_map_[std::move(name)] = DbType(std::move(val));
- return *this;
- }
-
- const std::vector<DbType>& GetBindings() const {
- return bindings_;
- }
-
- const std::map<int, DbType>& GetBindingMap() const {
- return binding_map_;
- }
-
- const std::map<std::string, DbType>& GetBindingNameMap() const {
- return binding_name_map_;
- }
-
- const std::string& GetQuery() const {
- return query_;
- }
-
- private:
- std::string query_;
- std::vector<DbType> bindings_;
- std::map<int, DbType> binding_map_;
- std::map<std::string, DbType> binding_name_map_;
- };
-
- class Result {
- public:
- Result() = default;
- ~Result() {
- if (stmt_)
- sqlite3_finalize(stmt_);
- }
-
- Result(const Result&) = delete;
- Result& operator = (const Result&) = delete;
-
- Result(Result&& r) noexcept {
- stmt_ = r.stmt_;
- r.stmt_ = nullptr;
- }
-
- Result& operator = (Result&& r) noexcept {
- if (this != &r) {
- if (stmt_)
- sqlite3_finalize(stmt_);
- stmt_ = r.stmt_;
- r.stmt_ = nullptr;
- }
-
- return *this;
- }
-
- class Record {
- public:
- Record(const sqlite3_stmt* stmt) : stmt_(stmt) {}
-
- AutoDbType Get(int pos) const {
- sqlite3_stmt* stmt = const_cast<sqlite3_stmt*>(stmt_);
- int type = sqlite3_column_type(stmt, pos);
- if (type == SQLITE_NULL)
- throw std::runtime_error("invalid column");;
-
- DbType dbt;
- if (type == SQLITE_TEXT) {
- dbt = DbType(reinterpret_cast<const char*>(
- sqlite3_column_text(stmt, pos)));
- } else if (type == SQLITE_INTEGER) {
- dbt = DbType(sqlite3_column_int(stmt, pos));
- } else if (type == SQLITE_FLOAT) {
- dbt = DbType(sqlite3_column_double(stmt, pos));
- } else if (type == SQLITE_BLOB) {
- const unsigned char* val = reinterpret_cast<const unsigned char*>(
- sqlite3_column_blob(stmt, pos));
- int len = sqlite3_column_bytes(stmt, pos);
-
- if (!val || len < 0) {
- throw std::runtime_error("invalid blob");;
- } else {
- dbt = DbType(std::vector<unsigned char>(val, val + len));
- }
- } else {
- throw std::runtime_error("invalid column type");
- }
-
- return AutoDbType(dbt);
- }
-
- template <typename ...Types>
- auto Get() const {
- std::tuple<Types...> t;
- int pos = 0;
- for_<std::tuple_size_v<std::tuple<Types...>>>([&] (auto i) {
- std::get<i.value>(t) = Get(pos++);
- });
-
- return t;
- }
-
- private:
- const sqlite3_stmt* stmt_;
- };
-
- class Iterator {
- public:
- Iterator(sqlite3_stmt* stmt) : stmt_(stmt) {}
-
- Record operator*() { return Record(stmt_); }
-
- bool operator != (const Iterator& rhs) const {
- return stmt_ != rhs.stmt_;
- }
-
- void operator ++() {
- int r = sqlite3_step(stmt_);
- if (r != SQLITE_ROW)
- stmt_ = nullptr;
- }
-
- private:
- sqlite3_stmt* stmt_ = nullptr;
- };
-
- Iterator begin() const {
- return Iterator(stmt_);
- }
-
- Iterator end() const {
- return Iterator(nullptr);
- }
-
- explicit operator bool() {
- if (stmt_ == nullptr)
- return false;
- return true;
- }
-
- explicit operator int() {
- if (db_ == nullptr)
- return SQLITE_ERROR;
- return sqlite3_errcode(db_);
- }
-
- operator const char*() {
- if (db_ == nullptr)
- return "";
- return sqlite3_errmsg(db_);
- }
-
- private:
- friend class Database;
- Result(sqlite3_stmt* stmt, sqlite3* db) : stmt_(stmt), db_(db) {}
-
- sqlite3_stmt* stmt_ = nullptr;
- sqlite3* db_ = nullptr;
- };
-
- Database(std::string db, int flags) {
- int r = sqlite3_open_v2(db.c_str(), &db_, flags, nullptr);
- if (r != SQLITE_OK)
- throw std::runtime_error("open failed");
- }
-
- Database(std::string db, int flags, std::function<bool(int)> busy_handler) {
- int r = sqlite3_open_v2(db.c_str(), &db_, flags, nullptr);
- if (r != SQLITE_OK)
- throw std::runtime_error("sqlite3_open_v2() failed");
-
- busy_handler_ = std::move(busy_handler);
- r = sqlite3_busy_handler(db_, [](void* data, int count) {
- Database* pDb = static_cast<Database*>(data);
- if (pDb->busy_handler_(count))
- return 1;
- return 0;
- }, this);
-
- if (r != SQLITE_OK) {
- sqlite3_close_v2(db_);
- throw std::runtime_error("sqlite3_busy_handler() failed");
- }
- }
-
- ~Database() {
- if (db_)
- sqlite3_close_v2(db_);
- }
-
- Database() = default;
- Database(const Database&) = delete;
- Database& operator = (const Database&) = delete;
-
- Database(Database&& db) noexcept {
- db_ = db.db_;
- db.db_ = nullptr;
- }
-
- explicit operator bool() {
- if (db_ == nullptr)
- return false;
- return true;
- }
-
- Database& operator = (Database&& db) noexcept {
- if (this != &db) {
- if (db_)
- sqlite3_close_v2(db_);
- db_ = db.db_;
- db.db_ = nullptr;
- }
-
- return *this;
- }
-
- TransactionGuard CreateTransactionGuard() {
- return TransactionGuard(db_);
- }
-
- Result Exec(const Sql& sql) const {
- if (!db_)
- throw std::runtime_error("Not opened");
-
- sqlite3_stmt* stmt = nullptr;
- int r = sqlite3_prepare_v2(db_, sql.GetQuery().c_str(),
- -1, &stmt, nullptr);
- if (r != SQLITE_OK) {
- return { nullptr, nullptr };
- }
-
- std::unique_ptr<sqlite3_stmt, decltype(sqlite3_finalize)*> stmt_auto(stmt,
- sqlite3_finalize);
- int pos = 1;
- for (const auto& i : sql.GetBindings()) {
- Bind(pos++, i, stmt);
- }
-
- for (const auto& i : sql.GetBindingMap()) {
- Bind(i.first, i.second, stmt);
- }
-
- for (const auto& i : sql.GetBindingNameMap()) {
- int pos = sqlite3_bind_parameter_index(stmt, i.first.c_str());
- if (pos == 0)
- throw std::runtime_error("Invalid binding");
- Bind(pos, i.second, stmt);
- }
-
- r = sqlite3_step(stmt);
- if (r != SQLITE_ROW && r != SQLITE_DONE) {
- return { nullptr, db_ };
- }
-
- return { stmt_auto.release(), db_ };
- }
-
- private:
- void Bind(int pos, const DbType& type, sqlite3_stmt* stmt) const {
- int r;
- if (const std::string* pstr = std::get_if<std::string>(&type)) {
- r = sqlite3_bind_text(stmt, pos, (*pstr).c_str(), -1,
- SQLITE_TRANSIENT);
- } else if (const int* pint = std::get_if<int>(&type)) {
- r = sqlite3_bind_int(stmt, pos, (*pint));
- } else if (const double* pdouble = std::get_if<double>(&type)) {
- r = sqlite3_bind_double(stmt, pos, (*pdouble));
- } else if (const std::vector<unsigned char>* pvector =
- std::get_if<std::vector<unsigned char>>(&type)) {
- r = sqlite3_bind_blob(stmt, pos, (*pvector).data(),
- (*pvector).size(), nullptr);
- } else {
- r = -1;
- }
-
- if (r != SQLITE_OK) {
- throw std::runtime_error("Invalid binding");
- }
- }
-
- private:
- sqlite3* db_ = nullptr;
- std::function<bool(int)> busy_handler_;
-};
-
-} // namespace tizen_base
-
-#endif // DATABASE_HPP_