From 9a717577d06f00bb8a0c7338f7b687540b5e017d Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 4 Sep 2010 16:54:57 +0200 Subject: some fixes for unsigned int parameters for prepared statements --- include/sqlite3xx/connection.hpp | 1 + include/sqlite3xx/prepared_statement.hpp | 2 ++ src/connection.cpp | 7 +++++++ src/prepared_statement.cpp | 20 ++++++++++++++++++++ tests/test9.cpp | 2 +- 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/include/sqlite3xx/connection.hpp b/include/sqlite3xx/connection.hpp index 0d95a7e..c7c1069 100644 --- a/include/sqlite3xx/connection.hpp +++ b/include/sqlite3xx/connection.hpp @@ -71,6 +71,7 @@ class SQLITEXX_LIBEXPORT connection { void prepare_param_declare( const string& stmt, const string& sqltype, prepare::param_treatment& treatment ); void prepared_reset( const string& name ); void prepare_setparam( const string& name, const int pos, const int value ); + void prepare_setparam( const string& name, const int pos, const unsigned int value ); void prepare_setparam( const string& name, const int pos, const char* value ); void prepare_setparam( const string& name, const int pos, const double value ); void prepare_setparam( const string& name, const int pos, const long value ); diff --git a/include/sqlite3xx/prepared_statement.hpp b/include/sqlite3xx/prepared_statement.hpp index 4542941..336adaf 100644 --- a/include/sqlite3xx/prepared_statement.hpp +++ b/include/sqlite3xx/prepared_statement.hpp @@ -71,6 +71,7 @@ namespace prepare { invocation( connection& c, transaction& t, const string& stmt ); invocation &setparam( const int& value, bool nonnull ); + invocation &setparam( const unsigned int& value, bool nonnull ); invocation &setparam( const char* value, bool nonnull ); invocation &setparam( const string& value, bool nonnull ); invocation &setparam( const double& value, bool nonnull ); @@ -104,6 +105,7 @@ class SQLITEXX_LIBEXPORT prepared_stmt { void reset( ); void setparam( const int pos, const int value ); + void setparam( const int pos, const unsigned int value ); void setparam( const int pos, const char* value ); void setparam( const int pos, const double value ); void setparam( const int pos, const long value ); diff --git a/src/connection.cpp b/src/connection.cpp index 30ab00a..5db2e4d 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -154,6 +154,13 @@ void connection::prepare_setparam( const string& name, const int pos, const int stmt->setparam( pos, value ); } +void connection::prepare_setparam( const string& name, const int pos, const unsigned int value ) { + prepared_stmt* stmt = find_prepared( name ); + if( _trace ) cout << "TRACE: prepared '" << name << "' param '" << + pos << "'" << " value: " << value << endl; + stmt->setparam( pos, value ); +} + void connection::prepare_setparam( const string& name, const int pos, const char* value ) { prepared_stmt* stmt = find_prepared( name ); if( _trace ) cout << "TRACE: prepared '" << name << "' param '" << diff --git a/src/prepared_statement.cpp b/src/prepared_statement.cpp index fef5090..b8074b3 100644 --- a/src/prepared_statement.cpp +++ b/src/prepared_statement.cpp @@ -68,6 +68,13 @@ prepare::invocation& prepare::invocation::setparam( const int& value, bool nonnu return *this; } +prepare::invocation& prepare::invocation::setparam( const unsigned int& value, bool nonnull ) { + SQLITEXX_UNUSED( nonnull ); + _pos++; + _c.prepare_setparam( _stmt, _pos, value ); + return *this; +} + prepare::invocation& prepare::invocation::setparam( const char* value, bool nonnull ) { SQLITEXX_UNUSED( nonnull ); _pos++; @@ -163,6 +170,19 @@ void prepared_stmt::setparam( const int pos, const int v ) { } } +void prepared_stmt::setparam( const int pos, const unsigned int v ) { + int rc; + assert( _stmt != NULL ); + rc = sqlite3_bind_int( _stmt, pos, v ); + if( rc != SQLITE_OK ) { + ostringstream s; + s << "sqlite3::prepared_stmt::setparam bind error: " + << sqlite3_errmsg( _db ) << " (rc: " << rc << ")"; + string msg = s.str( ); + throw sql_error( msg, _sql ); + } +} + void prepared_stmt::setparam( const int pos, const char* v ) { int rc; assert( _stmt != NULL ); diff --git a/tests/test9.cpp b/tests/test9.cpp index 62e17dc..bbcddb6 100644 --- a/tests/test9.cpp +++ b/tests/test9.cpp @@ -52,7 +52,7 @@ static THREAD_FUNC_DECL produce( void *thread_data ) try { connection c( "test9.db" ); - c.prepare( "ins", "insert into x values( ? )" )( "text", sqlite3xx::prepare::treat_direct ); + c.prepare( "ins", "insert into x values( ? )" )( "integer", prepare::treat_direct ); for( int i = 0; i < NOF_PRODUCER_TRANSACTIONS; i++ ) { work t( c, "ins" ); -- cgit v1.2.3-54-g00ecf