From 2534d35232467a578c1af21241b8902501830fbd Mon Sep 17 00:00:00 2001 From: Wongoo Lee Date: Sun, 5 Nov 2023 13:37:55 -0800 Subject: [PATCH] add clear_bindgins() in statement. --- headeronly_src/sqlite3pp.h | 1 + headeronly_src/sqlite3pp.ipp | 5 ++++ test/testdb.cpp | 55 +++++++++++++++++++++++++----------- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/headeronly_src/sqlite3pp.h b/headeronly_src/sqlite3pp.h index f339cf2..65891b0 100644 --- a/headeronly_src/sqlite3pp.h +++ b/headeronly_src/sqlite3pp.h @@ -178,6 +178,7 @@ namespace sqlite3pp int step(); int reset(); + int clear_bindings(); protected: explicit statement(database& db, char const* stmt = nullptr); diff --git a/headeronly_src/sqlite3pp.ipp b/headeronly_src/sqlite3pp.ipp index 8e37a0a..0ee04a0 100644 --- a/headeronly_src/sqlite3pp.ipp +++ b/headeronly_src/sqlite3pp.ipp @@ -313,6 +313,11 @@ namespace sqlite3pp return sqlite3_reset(stmt_); } + inline int statement::clear_bindings() + { + return sqlite3_clear_bindings(stmt_); + } + inline int statement::bind(int idx, int value) { return sqlite3_bind_int(stmt_, idx, value); diff --git a/test/testdb.cpp b/test/testdb.cpp index 402762e..8d2a085 100644 --- a/test/testdb.cpp +++ b/test/testdb.cpp @@ -20,14 +20,14 @@ using namespace std; if(!((arg1) == (arg2))) { \ std::cout << "Unexpected false at " \ << __FILE__ << ", " << __LINE__ << ", " << __func__ << ": " \ - << #arg1 << ", " << #arg2 << "(" << arg2 << ")" << std::endl; } \ + << #arg1 << ", " << #arg2 << std::endl; } \ } while(false); sqlite3pp::database contacts_db(); void test_insert_execute() { auto db = contacts_db(); - db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')"); + expect_eq(0, db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')")); sqlite3pp::query qry(db, "SELECT name, phone FROM contacts"); auto iter = qry.begin(); @@ -45,7 +45,7 @@ void test_insert_execute_all() { "INSERT INTO contacts (name, phone) VALUES (:user, '555-1111');" "INSERT INTO contacts (name, phone) VALUES (:user, '555-2222')"); cmd.bind(":user", "Mike", sqlite3pp::nocopy); - cmd.execute_all(); + expect_eq(0, cmd.execute_all()); sqlite3pp::query qry(db, "SELECT COUNT(*) FROM contacts"); auto iter = qry.begin(); @@ -57,7 +57,7 @@ void test_insert_binder() { auto db = contacts_db(); sqlite3pp::command cmd(db, "INSERT INTO contacts (name, phone) VALUES (?, ?)"); cmd.binder() << "Mike" << "555-1234"; - cmd.execute(); + expect_eq(0, cmd.execute()); sqlite3pp::query qry(db, "SELECT name, phone FROM contacts"); auto iter = qry.begin(); @@ -72,7 +72,7 @@ void test_insert_bind1() { sqlite3pp::command cmd(db, "INSERT INTO contacts (name, phone) VALUES (?, ?)"); cmd.bind(1, "Mike", sqlite3pp::nocopy); cmd.bind(2, "555-1234", sqlite3pp::nocopy); - cmd.execute(); + expect_eq(0, cmd.execute()); sqlite3pp::query qry(db, "SELECT name, phone FROM contacts"); auto iter = qry.begin(); @@ -87,7 +87,7 @@ void test_insert_bind2() { sqlite3pp::command cmd(db, "INSERT INTO contacts (name, phone) VALUES (?100, ?101)"); cmd.bind(100, "Mike", sqlite3pp::nocopy); cmd.bind(101, "555-1234", sqlite3pp::nocopy); - cmd.execute(); + expect_eq(0, cmd.execute()); sqlite3pp::query qry(db, "SELECT name, phone FROM contacts"); auto iter = qry.begin(); @@ -102,7 +102,7 @@ void test_insert_bind3() { sqlite3pp::command cmd(db, "INSERT INTO contacts (name, phone) VALUES (:user, :phone)"); cmd.bind(":user", "Mike", sqlite3pp::nocopy); cmd.bind(":phone", "555-1234", sqlite3pp::nocopy); - cmd.execute(); + expect_eq(0, cmd.execute()); sqlite3pp::query qry(db, "SELECT name, phone FROM contacts"); auto iter = qry.begin(); @@ -114,7 +114,7 @@ void test_insert_bind3() { void test_query_columns() { auto db = contacts_db(); - db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')"); + expect_eq(0, db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')")); sqlite3pp::query qry(db, "SELECT id, name, phone FROM contacts"); expect_eq(3, qry.column_count()); @@ -125,7 +125,7 @@ void test_query_columns() { void test_query_get() { auto db = contacts_db(); - db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')"); + expect_eq(0, db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')")); sqlite3pp::query qry(db, "SELECT id, name, phone FROM contacts"); auto iter = qry.begin(); @@ -135,7 +135,7 @@ void test_query_get() { void test_query_tie() { auto db = contacts_db(); - db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')"); + expect_eq(0, db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')")); sqlite3pp::query qry(db, "SELECT id, name, phone FROM contacts"); auto iter = qry.begin(); @@ -147,7 +147,7 @@ void test_query_tie() { void test_query_getter() { auto db = contacts_db(); - db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')"); + expect_eq(0, db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')")); sqlite3pp::query qry(db, "SELECT id, name, phone FROM contacts"); auto iter = qry.begin(); @@ -159,7 +159,7 @@ void test_query_getter() { void test_query_iterator() { auto db = contacts_db(); - db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')"); + expect_eq(0, db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')")); sqlite3pp::query qry(db, "SELECT id, name, phone FROM contacts"); for (auto row : qry) { @@ -173,7 +173,7 @@ void test_query_iterator() { void test_function() { auto db = contacts_db(); sqlite3pp::ext::function func(db); - func.create("test_fn", []{return 100;}); + expect_eq(0, func.create("test_fn", []{return 100;})); sqlite3pp::query qry(db, "SELECT test_fn()"); auto iter = qry.begin(); @@ -186,7 +186,7 @@ void test_function_args() { sqlite3pp::ext::function func(db); func.create("test_fn", [](const string& name){return "Hello " + name;}); - db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')"); + expect_eq(0, db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')")); sqlite3pp::query qry(db, "SELECT name, test_fn(name) FROM contacts"); auto iter = qry.begin(); @@ -212,8 +212,8 @@ void test_aggregate() { sqlite3pp::ext::aggregate aggr(db); aggr.create("strlen_aggr"); - db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')"); - db.execute("INSERT INTO contacts (name, phone) VALUES ('Janette', '555-4321')"); + expect_eq(0, db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')")); + expect_eq(0, db.execute("INSERT INTO contacts (name, phone) VALUES ('Janette', '555-4321')")); sqlite3pp::query qry(db, "SELECT strlen_aggr(name), strlen_aggr(phone) FROM contacts"); auto iter = qry.begin(); @@ -230,6 +230,28 @@ void test_invalid_path() { expect_true(false); } +void test_reset() { + auto db = contacts_db(); + sqlite3pp::command cmd(db, "INSERT INTO contacts (name, phone) VALUES (:user, :phone)"); + cmd.bind(":user", "Mike", sqlite3pp::nocopy); + cmd.bind(":phone", "555-1234", sqlite3pp::nocopy); + expect_eq(0, cmd.execute()); + + cmd.reset(); + cmd.bind(":user", "Janette", sqlite3pp::nocopy); + expect_eq(0, cmd.execute()); + + sqlite3pp::query qry(db, "SELECT COUNT(*) FROM contacts"); + auto iter = qry.begin(); + int count = (*iter).get(0); + expect_eq(2, count); + + cmd.reset(); + cmd.clear_bindings(); + cmd.bind(":user", "Dave", sqlite3pp::nocopy); + expect_eq(SQLITE_CONSTRAINT, cmd.execute()); +} + int main() { test_insert_execute(); @@ -247,6 +269,7 @@ int main() test_function_args(); test_aggregate(); test_invalid_path(); + test_reset(); } sqlite3pp::database contacts_db() {