Fix static analysis issues
[platform/core/base/bundle.git] / tizen-database / database.hpp
index 9b752f7..2ed94fa 100644 (file)
@@ -79,17 +79,28 @@ class DbException : public std::runtime_error {
 class AutoDbType {
  public:
   AutoDbType() = default;
-  explicit AutoDbType(DbType db_type) : db_type_(db_type) {}
+  explicit AutoDbType(DbType db_type, int real_type)
+      : db_type_(db_type), real_type_(real_type) {}
+
+  int GetType() const {
+    return real_type_;
+  }
 
   explicit operator int () {
     if (!db_type_)
       throw DbException("invalid type conversion from nullopt to int");
+    if (real_type_ != SQLITE_INTEGER)
+      throw DbException("invalid type conversion to int");
+
     return std::get<int64_t>(*db_type_);
   }
 
   explicit operator int64_t () {
     if (!db_type_)
-      throw DbException("invalid type conversion from nullopt to int");
+      throw DbException("invalid type conversion from nullopt to int64_t");
+    if (real_type_ != SQLITE_INTEGER)
+      throw DbException("invalid type conversion to int64_t");
+
     return std::get<int64_t>(*db_type_);
   }
 
@@ -99,6 +110,9 @@ class AutoDbType {
           "invalid type conversion from nullopt to string");
     }
 
+    if (real_type_ != SQLITE_TEXT)
+      throw DbException("invalid type conversion to string");
+
     return std::get<std::string>(*db_type_);
   }
 
@@ -108,6 +122,9 @@ class AutoDbType {
           "invalid type conversion from nullopt to double");
     }
 
+    if (real_type_ != SQLITE_FLOAT)
+      throw DbException("invalid type conversion to double");
+
     return std::get<double>(*db_type_);
   }
 
@@ -117,41 +134,60 @@ class AutoDbType {
           "invalid type conversion from nullopt to std::vector<unsigned char>");
     }
 
+    if (real_type_ != SQLITE_BLOB)
+      throw DbException("invalid type conversion to std::vector<unsigned char>");
+
     return std::get<std::vector<unsigned char>>(*db_type_);
   }
 
   operator std::optional<int> () {
     if (!db_type_)
       return std::nullopt;
+    if (real_type_ != SQLITE_INTEGER)
+      throw DbException("invalid type conversion to int");
+
     return std::get<int64_t>(*db_type_);
   }
 
   operator std::optional<int64_t> () {
     if (!db_type_)
       return std::nullopt;
+    if (real_type_ != SQLITE_INTEGER)
+      throw DbException("invalid type conversion to int64_t");
+
     return std::get<int64_t>(*db_type_);
   }
 
   operator std::optional<std::string> () {
     if (!db_type_)
       return std::nullopt;
+    if (real_type_ != SQLITE_TEXT)
+      throw DbException("invalid type conversion to string");
+
     return std::get<std::string>(*db_type_);
   }
 
   operator std::optional<double> () {
     if (!db_type_)
       return std::nullopt;
+    if (real_type_ != SQLITE_FLOAT)
+      throw DbException("invalid type conversion to double");
+
     return std::get<double>(*db_type_);
   }
 
   operator std::optional<std::vector<unsigned char>> () {
     if (!db_type_)
       return std::nullopt;
+    if (real_type_ != SQLITE_BLOB)
+      throw DbException("invalid type conversion to std::vector<unsigned char>");
+
     return std::get<std::vector<unsigned char>>(*db_type_);
   }
 
  private:
   DbType db_type_;
+  int real_type_ = SQLITE_NULL;
 };
 
 using _ = AutoDbType;
@@ -209,6 +245,19 @@ class Database {
    public:
     Sql(std::string query) : query_(std::move(query)) {}
 
+    Sql& Bind(const char* val) {
+      if (val == nullptr) {
+        bindings_.push_back(DbType(std::nullopt));
+      } else {
+        std::string str = val;
+        if (empty_string_as_null_ && str.empty())
+          bindings_.push_back(DbType(std::nullopt));
+        else
+          bindings_.push_back(DbType(std::move(str)));
+      }
+      return *this;
+    }
+
     Sql& Bind(std::string val) {
       if (empty_string_as_null_ && val.empty())
         bindings_.push_back(DbType(std::nullopt));
@@ -245,6 +294,19 @@ class Database {
       return *this;
     }
 
+    Sql& Bind(int pos, const char* val) {
+      if (val == nullptr) {
+        binding_map_[pos] = DbType(std::nullopt);
+      } else {
+        std::string str = val;
+        if (empty_string_as_null_ && str.empty())
+          binding_map_[pos] = DbType(std::nullopt);
+        else
+          binding_map_[pos] = DbType(std::move(str));
+      }
+      return *this;
+    }
+
     Sql& Bind(int pos, std::string val) {
       if (empty_string_as_null_ && val.empty())
         binding_map_[pos] = DbType(std::nullopt);
@@ -281,6 +343,19 @@ class Database {
       return *this;
     }
 
+    Sql& Bind(std::string name, const char* val) {
+      if (val == nullptr) {
+        binding_name_map_[std::move(name)] = DbType(std::nullopt);
+      } else {
+        std::string str = val;
+        if (empty_string_as_null_ && str.empty())
+          binding_name_map_[std::move(name)] = DbType(std::nullopt);
+        else
+          binding_name_map_[std::move(name)] = DbType(std::move(str));
+      }
+      return *this;
+    }
+
     Sql& Bind(std::string name, std::string val) {
       if (empty_string_as_null_ && val.empty())
         binding_name_map_[std::move(name)] = DbType(std::nullopt);
@@ -396,6 +471,16 @@ class Database {
      public:
       explicit Record(const sqlite3_stmt* stmt) : stmt_(stmt) {}
 
+      std::optional<std::string> GetString(int pos) const {
+        sqlite3_stmt* stmt = const_cast<sqlite3_stmt*>(stmt_);
+        const char* text = reinterpret_cast<const char*>(
+            sqlite3_column_text(stmt, pos));
+        if (!text)
+          return std::nullopt;
+
+        return text;
+      }
+
       AutoDbType Get(int pos) const {
         sqlite3_stmt* stmt = const_cast<sqlite3_stmt*>(stmt_);
         int type = sqlite3_column_type(stmt, pos);
@@ -424,7 +509,7 @@ class Database {
           throw DbException("invalid column type", type);
         }
 
-        return AutoDbType(dbt);
+        return AutoDbType(dbt, type);
       }
 
       template <typename ...Types>
@@ -555,6 +640,10 @@ class Database {
       is_done_ = is_done;
     }
 
+    size_t GetColumnCount() const {
+      return sqlite3_column_count(stmt_);
+    }
+
    private:
     friend class Database;
     Result(sqlite3_stmt* stmt, sqlite3* db, std::string query, bool is_done)
@@ -563,7 +652,7 @@ class Database {
     sqlite3_stmt* stmt_ = nullptr;
     sqlite3* db_ = nullptr;
     std::string query_;
-    bool is_done_;
+    bool is_done_ = false;
   };
 
   Database(std::string db, int flags) {
@@ -626,6 +715,18 @@ class Database {
     return TransactionGuard(db_);
   }
 
+  Result Prepare(const Sql& sql) const {
+    if (!db_)
+      throw DbException("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, "", true };
+    return { stmt, db_, sql.GetQuery(), false };
+  }
+
   Result Exec(const Sql& sql) const {
     if (!db_)
       throw DbException("Not opened");
@@ -705,8 +806,9 @@ class Database {
     int ret = sqlite3_exec(db_, sql.GetQuery().c_str(), nullptr, nullptr,
         &errmsg);
     if (ret != SQLITE_OK) {
-       std::unique_ptr<char, decltype(free)*> errmsg_auto(errmsg, free);
-       throw DbException(errmsg);
+      std::unique_ptr<char, decltype(sqlite3_free)*> errmsg_auto(
+          errmsg, sqlite3_free);
+      throw DbException(errmsg);
     }
   }