Use tizen-database package 69/285069/2
authorjh9216.park <jh9216.park@samsung.com>
Tue, 6 Dec 2022 01:16:30 +0000 (20:16 -0500)
committerjh9216.park <jh9216.park@samsung.com>
Tue, 6 Dec 2022 01:19:44 +0000 (20:19 -0500)
- Requires: https://review.tizen.org/gerrit/#/c/platform/core/base/bundle/+/285042/

Change-Id: I21ece5c845da118ecc680b52d3af9b00e239264a
Signed-off-by: jh9216.park <jh9216.park@samsung.com>
CMakeLists.txt
packaging/esd.spec
src/modules/cion/CMakeLists.txt
src/modules/cion/cion_module.cc
src/modules/cion/cion_module.hh
src/modules/cion/cion_ondemand_server.cc
src/modules/cion/database.hpp [deleted file]

index 6123889..f566092 100644 (file)
@@ -51,6 +51,7 @@ PKG_CHECK_MODULES(UUID_DEPS REQUIRED uuid)
 PKG_CHECK_MODULES(VCONF_DEPS REQUIRED vconf)
 PKG_CHECK_MODULES(PKGMGR_DEPS REQUIRED pkgmgr)
 PKG_CHECK_MODULES(EVENTSYSTEM_DEPS REQUIRED eventsystem)
+PKG_CHECK_MODULES(DATABASE_DEPS REQUIRED tizen-database)
 
 ADD_SUBDIRECTORY(src)
 
index e633ce3..a4c2315 100644 (file)
@@ -30,6 +30,7 @@ BuildRequires:  pkgconfig(uuid)
 BuildRequires:  pkgconfig(sqlite3)
 BuildRequires:  pkgconfig(cion)
 BuildRequires:  pkgconfig(capi-system-info)
+BuildRequires:  pkgconfig(tizen-database)
 BuildRequires:  pkgconfig(gmock)
 
 Requires(post): /sbin/ldconfig
index 3a591da..84c326a 100644 (file)
@@ -21,6 +21,7 @@ APPLY_PKG_CONFIG(${TARGET_ESD_MOD_CION} PUBLIC
   LIBTZPLATFORM_CONFIG_DEPS
   UUID_DEPS
   SQLITE3_DEPS
+  DATABASE_DEPS
 )
 
 INSTALL(TARGETS ${TARGET_ESD_MOD_CION} DESTINATION ${ESD_MODULES_DIR}/mod
index de10eec..ec945f0 100644 (file)
@@ -71,7 +71,7 @@ bool IsTableExist(const tizen_base::Database& db) {
     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;
   }
@@ -88,7 +88,8 @@ bool CionModule::Startup(api::ToolBox* tools) {
             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);
@@ -122,7 +123,8 @@ void CionModule::Shutdown() {
 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;
@@ -269,7 +271,8 @@ void CionModule::GetUuidWithGenerate(const std::string& appid,
 }
 
 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;
@@ -327,8 +330,8 @@ void CionModule::GetDisplayName(const std::string& appid,
   }
 }
 
-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);
 
@@ -355,7 +358,7 @@ void CionModule::SetEnabled(const std::string& appid, const std::string& service
       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
@@ -368,8 +371,8 @@ void CionModule::SetEnabled(const std::string& appid, const std::string& service
   }
 }
 
-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);
 
index beeaba1..8c2f23a 100644 (file)
 #include <optional>
 #include <string>
 #include <imodule.hh>
+#include <database.hpp>
 
 #include "cion_ondemand_server.h"
 #include "cion_service_info.hh"
-#include "database.hpp"
 
 namespace esd::module {
 
@@ -49,9 +49,11 @@ class CionModule : public api::IModule {
   bool Init();
   bool DbInit();
   void ChangeListenStatusCionServer();
-  void SetDisplayName(const std::string& appid, const std::string& service_name,
+  void SetDisplayName(const std::string& appid,
+      const std::string& service_name,
       const std::string& display_name, int& ret);
-  void GetDisplayName(const std::string& appid, const std::string& service_name,
+  void GetDisplayName(const std::string& appid,
+      const std::string& service_name,
       std::string& display_name, int& ret);
   void SetEnabled(const std::string& appid, const std::string& service_name,
       bool enabled, int& ret);
index fa396dd..d5787b0 100644 (file)
@@ -95,18 +95,21 @@ std::string GetVconfString(const char* key) {
   return val_str;
 }
 
-std::string device_id = GetSystemInfoPlatformString("http://tizen.org/system/tizenid");
+std::string device_id =
+    GetSystemInfoPlatformString("http://tizen.org/system/tizenid");
 std::string device_name = GetVconfString(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
 std::string device_platform = "Tizen";
 std::string device_platform_version =
     GetSystemInfoPlatformString("http://tizen.org/feature/platform.version");
-std::string device_type = GetSystemInfoPlatformString("http://tizen.org/system/device_type");
+std::string device_type =
+    GetSystemInfoPlatformString("http://tizen.org/system/device_type");
 
 }  // namespace
 
 CionOndemandServer::CionOndemandServer(std::string service_name,
     std::string display_name, esd::api::IModule* module)
-    : cion::channel::ServerChannel(service_name, display_name), module_(module) {
+    : cion::channel::ServerChannel(service_name, display_name),
+    module_(module) {
   LoadOndemandServiceList();
 }
 
@@ -117,7 +120,8 @@ CionOndemandServer::CionOndemandServer(std::string service_name,
   LoadOndemandServiceList();
 }
 
-void CionOndemandServer::OnConnectionResult(std::shared_ptr<cion::PeerInfo> info,
+void CionOndemandServer::OnConnectionResult(
+    std::shared_ptr<cion::PeerInfo> info,
     const cion::ConnectionResult& result) {
 }
 
@@ -132,7 +136,8 @@ std::vector<char> CionOndemandServer::OnDataReceived(
   return v;
 }
 
-void CionOndemandServer::OnPayloadReceived(std::shared_ptr<cion::IPayload> data,
+void CionOndemandServer::OnPayloadReceived(
+    std::shared_ptr<cion::IPayload> data,
     std::shared_ptr<cion::PeerInfo> peer,
     IPayloadReceiver::PayloadTransferStatus status) {
 }
@@ -225,7 +230,8 @@ int CionOndemandServer::OndemandLaunchApp(std::string appid) {
     aul_svc_set_operation(b.GetHandle(), AUL_SVC_OPERATION_DEFAULT);
     aul_svc_set_appid(b.GetHandle(), appid.c_str());
 
-    ret = aul_svc_run_service_async_for_uid(b.GetHandle(), 0, NULL, NULL, uid);
+    ret = aul_svc_run_service_async_for_uid(b.GetHandle(), 0,
+        nullptr, nullptr, uid);
     if (ret < 0)
       _E("Failed to launch app : %s", appid.c_str());
 
diff --git a/src/modules/cion/database.hpp b/src/modules/cion/database.hpp
deleted file mode 100644 (file)
index a2d8fca..0000000
+++ /dev/null
@@ -1,470 +0,0 @@
-/*
- * 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_