Fix coding rule for db handlers
[platform/core/appfw/pkgmgr-info.git] / src / common / database / abstract_db_handler.cc
index 872c53e..5950513 100644 (file)
@@ -24,7 +24,9 @@
 #include <tzplatform_config.h>
 
 #include <string>
+#include <vector>
 
+#include "db_handle_provider.hh"
 #include "pkgmgr-info.h"
 #include "pkgmgrinfo_debug.h"
 #include "pkgmgrinfo_private.h"
@@ -34,13 +36,6 @@ namespace {
 const char kMemoryDBPrefix[] = "file:memdb_";
 const char kMemoryDBPostFix[] = "?mode=memory&cache=shared";
 
-uid_t ConvertUID(uid_t uid) {
-  if (uid < REGULAR_USER)
-    return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
-  else
-    return uid;
-}
-
 #define BUSY_WAITING_USEC (1000000 / 10 / 2) /* 0.05 sec */
 #define BUSY_WAITING_MAX 100 /* wait for max 5 sec */
 
@@ -188,7 +183,8 @@ static int __writedb_busy_handler(void *data, int count) {
   }
 }
 
-static int __open_write_db(uid_t uid, const char *path, sqlite3 **db, int flags) {
+static int __open_write_db(uid_t uid, const char* path,
+    sqlite3** db, int flags) {
   int ret;
 
   ret = sqlite3_open_v2(path, db, flags, NULL);
@@ -197,7 +193,8 @@ static int __open_write_db(uid_t uid, const char *path, sqlite3 **db, int flags)
     return ret;
   }
 
-  ret = sqlite3_busy_handler(*db, __writedb_busy_handler, (void *)path);
+  ret = sqlite3_busy_handler(*db, __writedb_busy_handler,
+      reinterpret_cast<void*>(const_cast<char*>(path)));
   if (ret != SQLITE_OK) {
     _LOGE("failed to register busy handler: %s",
         sqlite3_errmsg(*db));
@@ -216,37 +213,36 @@ static int __open_write_db(uid_t uid, const char *path, sqlite3 **db, int flags)
   return ret;
 }
 
+uid_t ConvertUID(uid_t uid) {
+  if (uid < REGULAR_USER)
+    return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
+  else
+    return uid;
+}
+
 }  // namespace
 
 namespace pkgmgr_common {
 namespace database {
 
-AbstractDBHandler::AbstractDBHandler(uid_t uid) { uid_ = uid; }
+AbstractDBHandler::AbstractDBHandler(uid_t uid, int pid) {
+  uid_ = uid;
+  pid_ = pid;
+}
 
 AbstractDBHandler::~AbstractDBHandler() {
-  // Is this necessary?
-  sqlite3_close(db_);
+  for (auto db_handle : db_handle_list_)
+    sqlite3_close(db_handle.first);
 }
 
-std::string AbstractDBHandler::GetDBPath() {
-  std::string db_path;
-  if (db_type_ == DB_TYPE_MEMORY_PKGDB) {
-    db_path += kMemoryDBPrefix;
-    db_path += std::to_string(ConvertUID(uid_));
-    db_path += kMemoryDBPostFix;
-  } else if (db_type_ == DB_TYPE_FILE_PKGDB) {
-    char* tmp_dbpath = getUserPkgParserDBPathUID(uid_);
-    if (!tmp_dbpath)
-      return "";
-    db_path = tmp_dbpath;
-    free(tmp_dbpath);
-  } else if (db_type_ == DB_TYPE_FILE_CERTDB) {
-    char *tmp_dbpath = getUserPkgCertDBPath();
-    if (!tmp_dbpath)
-      return "";
-    db_path = tmp_dbpath;
-    free(tmp_dbpath);
-  }
+std::vector<std::pair<std::string, uid_t>> AbstractDBHandler::GetDBPath() {
+  std::vector<std::pair<std::string, uid_t>> db_path;
+  if (db_type_ == DB_TYPE_FILE_PKGDB)
+    db_path = DBHandleProvider::GetInst(uid_).GetParserDBPath(pid_);
+  else if (db_type_ == DB_TYPE_FILE_CERTDB)
+    db_path.emplace_back(
+        std::make_pair(
+            DBHandleProvider::GetInst(uid_).GetCertDBPath(pid_), uid_));
 
   return db_path;
 }
@@ -256,23 +252,32 @@ bool AbstractDBHandler::Connect() {
     // error log
     return false;
   }
-  std::string db_path = GetDBPath();
-  int flag = 0;
+  auto dbpath_list = GetDBPath();
   int ret = 0;
-  if (op_type_ == OPERATION_TYPE_READ)
-    ret = __open_read_db(db_path.c_str(), &db_, SQLITE_OPEN_READONLY);
-  else
-    ret = __open_write_db(uid_, db_path.c_str(), &db_,
-        SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
+  sqlite3* db;
+  for (auto dbpath : dbpath_list) {
+    if (op_type_ == OPERATION_TYPE_READ) {
+      ret = __open_read_db(dbpath.first.c_str(), &db, SQLITE_OPEN_READONLY |
+          SQLITE_OPEN_URI);
+    } else {
+      if (ConvertUID(dbpath.second) != ConvertUID(uid_))
+        continue;
+      ret = __open_write_db(uid_, dbpath.first.c_str(), &db,
+          SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
+    }
 
-  if (ret != SQLITE_OK) {
-    // error log
-    return false;
+    if (ret != SQLITE_OK)
+      return false;
+
+    db_handle_list_.emplace_back(std::make_pair(db, dbpath.second));
   }
+
   return true;
 }
 
-sqlite3* AbstractDBHandler::GetConnection() { return db_; }
+std::vector<std::pair<sqlite3*, uid_t>> AbstractDBHandler::GetConnection() {
+  return db_handle_list_;
+}
 
 void AbstractDBHandler::SetOpType(OperationType type) {
   op_type_ = type;
@@ -280,13 +285,17 @@ void AbstractDBHandler::SetOpType(OperationType type) {
 
 std::string AbstractDBHandler::GetLocale() { return locale_; }
 
+int AbstractDBHandler::GetPID() { return pid_; }
+
 void AbstractDBHandler::SetLocale(const std::string& locale) {
   locale_ = locale;
 }
 
 void AbstractDBHandler::SetDBType(DBType type) { db_type_ = type; }
 
-AbstractDBHandler::OperationType AbstractDBHandler::GetOpType() { return op_type_; }
+AbstractDBHandler::OperationType AbstractDBHandler::GetOpType() {
+  return op_type_;
+}
 
 }  // namespace database
 }  // namespace pkgmgr_common