diff --git a/Data/SQLite/include/Poco/Data/SQLite/SQLiteException.h b/Data/SQLite/include/Poco/Data/SQLite/SQLiteException.h index 367042a80..4c74dcff7 100644 --- a/Data/SQLite/include/Poco/Data/SQLite/SQLiteException.h +++ b/Data/SQLite/include/Poco/Data/SQLite/SQLiteException.h @@ -69,6 +69,7 @@ POCO_DECLARE_EXCEPTION(SQLite_API, SchemaDiffersException, SQLiteException) POCO_DECLARE_EXCEPTION(SQLite_API, RowTooBigException, SQLiteException) POCO_DECLARE_EXCEPTION(SQLite_API, ConstraintViolationException, SQLiteException) POCO_DECLARE_EXCEPTION(SQLite_API, DataTypeMismatchException, SQLiteException) +POCO_DECLARE_EXCEPTION(SQLite_API, ParameterCountMismatchException, SQLiteException) POCO_DECLARE_EXCEPTION(SQLite_API, InvalidLibraryUseException, SQLiteException) POCO_DECLARE_EXCEPTION(SQLite_API, OSFeaturesMissingException, SQLiteException) POCO_DECLARE_EXCEPTION(SQLite_API, AuthorizationDeniedException, SQLiteException) diff --git a/Data/SQLite/src/SQLiteException.cpp b/Data/SQLite/src/SQLiteException.cpp index 11cd869e7..8b3463e4b 100644 --- a/Data/SQLite/src/SQLiteException.cpp +++ b/Data/SQLite/src/SQLiteException.cpp @@ -63,6 +63,7 @@ POCO_IMPLEMENT_EXCEPTION(SchemaDiffersException, SQLiteException, "The database POCO_IMPLEMENT_EXCEPTION(RowTooBigException, SQLiteException, "Too much data for one row of a table") POCO_IMPLEMENT_EXCEPTION(ConstraintViolationException, SQLiteException, "Abort due to constraint violation") POCO_IMPLEMENT_EXCEPTION(DataTypeMismatchException, SQLiteException, "Data type mismatch") +POCO_IMPLEMENT_EXCEPTION(ParameterCountMismatchException, SQLiteException, "Parameter count mismatch") POCO_IMPLEMENT_EXCEPTION(InvalidLibraryUseException, SQLiteException, "Library used incorrectly") POCO_IMPLEMENT_EXCEPTION(OSFeaturesMissingException, SQLiteException, "Uses OS features not supported on host") POCO_IMPLEMENT_EXCEPTION(AuthorizationDeniedException, SQLiteException, "Authorization denied") diff --git a/Data/SQLite/src/SQLiteStatementImpl.cpp b/Data/SQLite/src/SQLiteStatementImpl.cpp index 38896881b..d8cdc64bf 100644 --- a/Data/SQLite/src/SQLiteStatementImpl.cpp +++ b/Data/SQLite/src/SQLiteStatementImpl.cpp @@ -79,7 +79,7 @@ void SQLiteStatementImpl::compileImpl() while (rc == SQLITE_OK && !pStmt && !queryFound) { - rc = sqlite3_prepare(_pDB, pSql, -1, &pStmt, &pLeftover); + rc = sqlite3_prepare_v2(_pDB, pSql, -1, &pStmt, &pLeftover); if (rc != SQLITE_OK) { if (pStmt) @@ -143,7 +143,12 @@ void SQLiteStatementImpl::bindImpl() // bind Bindings& binds = bindings(); - if (binds.empty()) return; + int pc = sqlite3_bind_parameter_count(_pStmt); + if (binds.empty() && 0 == pc) return; + else if (binds.empty() && pc > 0) + throw ParameterCountMismatchException(); + else if (!binds.empty() && binds.size() * (*binds.begin())->numOfColumnsHandled() != pc) + throw ParameterCountMismatchException(); std::size_t pos = 1; // sqlite starts with 1 not 0! diff --git a/Data/SQLite/testsuite/src/SQLiteTest.cpp b/Data/SQLite/testsuite/src/SQLiteTest.cpp index 861ace641..da4b56de4 100644 --- a/Data/SQLite/testsuite/src/SQLiteTest.cpp +++ b/Data/SQLite/testsuite/src/SQLiteTest.cpp @@ -44,6 +44,7 @@ #include #include "Poco/File.h" #include "Poco/Stopwatch.h" +#include "Poco/Data/SQLite/SQLiteException.h" using namespace Poco::Data; @@ -53,6 +54,7 @@ using Poco::AnyCast; using Poco::InvalidAccessException; using Poco::RangeException; using Poco::BadCastException; +using Poco::Data::SQLite::ParameterCountMismatchException; struct Person @@ -1577,6 +1579,29 @@ void SQLiteTest::testInternalExtraction() } +void SQLiteTest::testBindingCount() +{ + Session tmp (SQLite::Connector::KEY, "dummy.db"); + + tmp << "DROP TABLE IF EXISTS Ints", now; + tmp << "CREATE TABLE Ints (int0 INTEGER)", now; + + int i = 42; + try { tmp << "INSERT INTO Ints VALUES (?)", now; } + catch (ParameterCountMismatchException&) { } + + try { tmp << "INSERT INTO Ints VALUES (?)", use(i), use(i), now; } + catch (ParameterCountMismatchException&) { } + tmp << "INSERT INTO Ints VALUES (?)", use(i), now; + + int j = 0; + try { tmp << "SELECT int0 from Ints where int0 = ?", into(i), now; } + catch (ParameterCountMismatchException&) { } + tmp << "SELECT int0 from Ints where int0 = ?", use(i), into(j), now; + assert (42 == j); +} + + void SQLiteTest::setUp() { } @@ -1648,6 +1673,7 @@ CppUnit::Test* SQLiteTest::suite() CppUnit_addTest(pSuite, SQLiteTest, testTuple1); CppUnit_addTest(pSuite, SQLiteTest, testTupleVector1); CppUnit_addTest(pSuite, SQLiteTest, testInternalExtraction); + CppUnit_addTest(pSuite, SQLiteTest, testBindingCount); return pSuite; } diff --git a/Data/SQLite/testsuite/src/SQLiteTest.h b/Data/SQLite/testsuite/src/SQLiteTest.h index 5d42f5167..ea68155b5 100644 --- a/Data/SQLite/testsuite/src/SQLiteTest.h +++ b/Data/SQLite/testsuite/src/SQLiteTest.h @@ -109,6 +109,7 @@ public: void testTupleVector10(); void testInternalExtraction(); + void testBindingCount(); void setUp(); void tearDown();