Add overloading Bind() methods 24/286324/1
authorjh9216.park <jh9216.park@samsung.com>
Wed, 4 Jan 2023 03:10:20 +0000 (22:10 -0500)
committerjh9216.park <jh9216.park@samsung.com>
Wed, 4 Jan 2023 03:10:20 +0000 (22:10 -0500)
- To support 'Bind(nullptr)' expression, some overloaded methods are
added

Change-Id: I24b278390a13120e0b4f73dee285710aa2d79373
Signed-off-by: jh9216.park <jh9216.park@samsung.com>
tests/tizen-database_unittests/src/test_database.cc
tizen-database/database.hpp

index d949056..866be6f 100644 (file)
@@ -511,3 +511,14 @@ TEST_F(DatabaseTest, test_get_raw) {
 
   EXPECT_NE(db_raw, nullptr);
 }
+
+TEST_F(DatabaseTest, test_nullptr_bind) {
+  tizen_base::Database db(TEST_DB, SQLITE_OPEN_READWRITE);
+  auto q = tizen_base::Database::Sql(Q_INSERT)
+      .Bind(nullptr)
+      .Bind(1234)
+      .Bind(9.216)
+      .Bind(std::vector<unsigned char> {'9', '2', '1', '6' });
+  auto r = db.Exec(q);
+  ASSERT_TRUE(r);
+}
index 9b752f7..7b36809 100644 (file)
@@ -209,6 +209,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 +258,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 +307,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);