Add Prepare() method 36/286336/1
authorjh9216.park <jh9216.park@samsung.com>
Wed, 4 Jan 2023 06:31:18 +0000 (01:31 -0500)
committerjh9216.park <jh9216.park@samsung.com>
Wed, 4 Jan 2023 06:31:18 +0000 (01:31 -0500)
- To reuse prepared the statement, this method was added

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

index 866be6f..d4239a4 100644 (file)
@@ -254,6 +254,29 @@ TEST_F(DatabaseTest, test_reuse_result) {
   EXPECT_TRUE(ret);
 }
 
+TEST_F(DatabaseTest, test_reuse_result2) {
+  tizen_base::Database db(TEST_DB, SQLITE_OPEN_READWRITE);
+  auto q = tizen_base::Database::Sql(Q_INSERT);
+  auto r = db.Prepare(q);
+  EXPECT_TRUE(r);
+
+  q.Reset()
+      .Bind("gugu")
+      .Bind(5678)
+      .Bind(9.216)
+      .Bind(std::nullopt);
+  bool ret = db.Exec(q, r);
+  EXPECT_TRUE(ret);
+
+  q.Reset()
+      .Bind("gg")
+      .Bind(7777)
+      .Bind(9.216)
+      .Bind(std::nullopt);
+  ret = db.Exec(q, r);
+  EXPECT_TRUE(ret);
+}
+
 TEST_F(DatabaseTest, test_set_empty_string_as_null) {
   tizen_base::Database db(TEST_DB, SQLITE_OPEN_READWRITE);
   auto q = tizen_base::Database::Sql(Q_INSERT)
index 7b36809..88cb2a6 100644 (file)
@@ -665,6 +665,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");