From 1dab1722644981d0436a12b9246f8cec3e0a5301 Mon Sep 17 00:00:00 2001 From: Johannes Rave Date: Mon, 4 Mar 2019 18:43:32 +0200 Subject: [PATCH] Add wrapper for bind parameter count --- include/SQLiteCpp/Statement.h | 3 +++ src/Statement.cpp | 5 +++++ tests/Statement_test.cpp | 15 +++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/include/SQLiteCpp/Statement.h b/include/SQLiteCpp/Statement.h index ecf417ba..60de5a75 100644 --- a/include/SQLiteCpp/Statement.h +++ b/include/SQLiteCpp/Statement.h @@ -620,6 +620,9 @@ class Statement return mbDone; } + /// Return the number of bind parameters in the statement + int getBindParameterCount() const noexcept; + /// Return the numeric result code for the most recent failed API call (if any). int getErrorCode() const noexcept; // nothrow /// Return the extended numeric result code for the most recent failed API call (if any). diff --git a/src/Statement.cpp b/src/Statement.cpp index bcf5d956..3f9b1dc9 100644 --- a/src/Statement.cpp +++ b/src/Statement.cpp @@ -378,6 +378,11 @@ int Statement::getColumnIndex(const char* apName) const return (*iIndex).second; } +int Statement::getBindParameterCount() const noexcept +{ + return sqlite3_bind_parameter_count(mStmtPtr); +} + // Return the numeric result code for the most recent failed API call (if any). int Statement::getErrorCode() const noexcept // nothrow { diff --git a/tests/Statement_test.cpp b/tests/Statement_test.cpp index 3e4d9197..55d9ce32 100644 --- a/tests/Statement_test.cpp +++ b/tests/Statement_test.cpp @@ -797,3 +797,18 @@ TEST(Statement, bind64bitsLong) { EXPECT_EQ(4294967297L, query.getColumn(0).getInt64()); } #endif + +TEST(Statement, getBindParameterCount) { + // Create a new database + SQLite::Database db(":memory:", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE); + EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT)")); + + SQLite::Statement query(db, "SELECT id, msg FROM test where id = ?"); + EXPECT_EQ(1, query.getBindParameterCount()); + + SQLite::Statement query2(db, "SELECT id, msg FROM test where id = ? and msg = ?"); + EXPECT_EQ(2, query2.getBindParameterCount()); + + SQLite::Statement query3(db, "SELECT id, msg FROM test"); + EXPECT_EQ(0, query3.getBindParameterCount()); +}