From 1dcc640c490a6619eeba19a6bdb93c4ac8d5a6fc Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Mon, 6 Sep 2010 10:53:02 +0200 Subject: more cleanup in exceptions, added a database_locked exception, now we must fix the code to throw this exception all the time and not do funny sleeps in the middle of the sqlitexx layer! --- include/sqlite3xx/except.hpp | 13 +++++----- src/except.cpp | 11 ++++++-- src/prepared_statement.cpp | 19 ++++++++++---- src/result.cpp | 4 +-- tests/test9.cpp | 62 +++++--------------------------------------- 5 files changed, 37 insertions(+), 72 deletions(-) diff --git a/include/sqlite3xx/except.hpp b/include/sqlite3xx/except.hpp index 6a70216..742f17c 100644 --- a/include/sqlite3xx/except.hpp +++ b/include/sqlite3xx/except.hpp @@ -47,7 +47,8 @@ class SQLITEXX_LIBEXPORT failure : public sqlitexx_exception, public runtime_err explicit failure( const string &s ); }; -class SQLITEXX_LIBEXPORT sql_error : public failure { +class SQLITEXX_LIBEXPORT sql_error : public failure +{ string m_q; public: @@ -60,12 +61,12 @@ class SQLITEXX_LIBEXPORT sql_error : public failure { const string& query( ) const throw( ); }; -/* -class SQLITEXX_LIBEXPORT db_locked : sql_error { +class SQLITEXX_LIBEXPORT database_locked : public failure +{ public: - explicit db_locket( string &q ); + database_locked( ); }; -*/ -} + +} // namespace sqlite3xx #endif /* SQLITE3XX_EXCEPT_H */ diff --git a/src/except.cpp b/src/except.cpp index 5ae357d..9a07334 100644 --- a/src/except.cpp +++ b/src/except.cpp @@ -44,11 +44,18 @@ sql_error::sql_error( const string &_what, const string &_q ) { } -sql_error::~sql_error( ) throw( ){ +sql_error::~sql_error( ) throw( ) +{ } -const string& sql_error::query( ) const throw( ) { +const string& sql_error::query( ) const throw( ) +{ return m_q; } +database_locked::database_locked( ) + : failure( "database locked" ) +{ +} + } /* namespace sqlite3xx */ diff --git a/src/prepared_statement.cpp b/src/prepared_statement.cpp index b8074b3..674fbec 100644 --- a/src/prepared_statement.cpp +++ b/src/prepared_statement.cpp @@ -122,11 +122,20 @@ prepared_stmt::prepared_stmt( sqlite3 *db, string __sql ) : #else rc = sqlite3_prepare( db, _sql.c_str( ), -1, &_stmt, &tail ); #endif - if( rc != SQLITE_OK ) { - ostringstream s; - s << "sqlite3::prepared_stmt::prepared_stmt error: " << sqlite3_errmsg( _db ); - string msg = s.str( ); - throw sql_error( msg, _sql ); + switch( rc ) { + case SQLITE_BUSY: + throw database_locked( ); + + case SQLITE_OK: + break; + + default: + { + ostringstream s; + s << "sqlite3::prepared_stmt::prepared_stmt error: " << sqlite3_errmsg( _db ); + string msg = s.str( ); + throw sql_error( msg, _sql ); + } } } diff --git a/src/result.cpp b/src/result.cpp index f855a0a..5f989a9 100644 --- a/src/result.cpp +++ b/src/result.cpp @@ -74,7 +74,6 @@ ostream& operator<<( ostream& o, const result::field& f ) { void result::Step( ) { int rc; -TRY_AGAIN_STEP: rc = sqlite3_step( _stmt ); switch( rc ) { case SQLITE_DONE: @@ -95,8 +94,7 @@ TRY_AGAIN_STEP: /* don't fail if the sqlite file is locked by another writer, try again later */ case SQLITE_BUSY: -// sqlitexx_port_sleep( 1 ); - goto TRY_AGAIN_STEP; + throw database_locked( ); default: { ostringstream s; diff --git a/tests/test9.cpp b/tests/test9.cpp index 7c217cf..cf61508 100644 --- a/tests/test9.cpp +++ b/tests/test9.cpp @@ -33,8 +33,8 @@ using namespace sqlite3xx; using namespace std; -const int NOF_PRODUCERS = 10; -const int NOF_CONSUMERS = 10; +const int NOF_PRODUCERS = 1; +const int NOF_CONSUMERS = 1; const int NOF_PRODUCER_TRANSACTIONS = 100; const int NOF_PRODUCER_OPS = 100; const int NOF_CONSUMER_TRANSACTIONS = 100; @@ -45,6 +45,7 @@ const int NOF_CONSUMER_OPS = 100; static MUTEX_TYPE cout_mutex; static bool verbose = false; +static bool tracing = true; static THREAD_FUNC_DECL produce( void *thread_data ) { @@ -52,6 +53,7 @@ static THREAD_FUNC_DECL produce( void *thread_data ) try { connection c( "test9.db" ); + if( tracing ) c.trace( true ); c.prepare( "ins", "insert into x values( ? )" )( "integer", prepare::treat_direct ); for( int i = 0; i < NOF_PRODUCER_TRANSACTIONS; i++ ) { @@ -86,6 +88,7 @@ static THREAD_FUNC_DECL consume( void *thread_data ) try { connection c( "test9.db" ); c.prepare( "sel", "select * from x" ); + if( tracing ) c.trace( true ); for( int i = 0; i < NOF_CONSUMER_TRANSACTIONS; i++ ) { work t( c, "sel" ); @@ -127,6 +130,7 @@ int main( ) { try { cout << "creating DB.." << endl; connection c( "test9.db" ); + if( tracing ) c.trace( true ); cout << "connection object is " << c << endl; cout << "create table.." << endl; @@ -161,57 +165,3 @@ int main( ) { cerr << e.what( ) << ": " << e.query( ) << endl; } } - -/* - try { - - work wi( c, "insert" ); - cout << "insert some data.." << endl; - c.prepare( "ins", "insert into a( i, d, s, t ) values( NULL, NULL, NULL, ? )" ) - ( "text", sqlite3xx::prepare::treat_direct ); - result r = wi.prepared( "ins" )( "bla" ).exec( ); - cout << "inserted " << r.affected_rows( ) << " rows." << endl; - r = wi.prepared( "ins" )( "blu" ).exec( ); - cout << "inserted " << r.affected_rows( ) << " rows." << endl; - r = wi.prepared( "ins" )( "bli" ).exec( ); - cout << "inserted " << r.affected_rows( ) << " rows." << endl; - wi.commit( ); - - work wq( c, "query" ); - cout << "querying.." << endl; - c.prepare( "qry", "select * from a" ); - - r = wq.prepared( "qry" ).exec( ); - cout << "found " << r.size( ) << " records.." << endl; - cout << "found " << r.columns( ) << " columns.." << endl; - for( result::size_type i = 0; i < r.columns( ); i++ ) { - cout << "column " << i << ": " << r.column_type( i ) << endl; - } - assert( r.size( ) == 2 ); - - for( result::size_type i = 0; i < r.size( ); i++ ) { - cout << "i: " << i << endl; - - // by field number - cout << "(by col number) i: " << r[i][0] << " d: " << r[i][1] << " s: " << r[i][2] << " t: " << r[i][3] << endl; - - // associative array - cout << "(by col name) i: " << r[i]["i"] << " d: " << r[i]["d"] << " s: " << r[i]["s"] << " t: " << r[i]["t"] << endl; - - // map to variables of a given type - int value_i; - double value_d; - string value_s; - string value_t; - r[i]["i"].to( value_i ); - r[i]["d"].to( value_d ); - r[i]["s"].to( value_s ); - r[i]["t"].to( value_t ); - cout << "(mapping) i: " << value_i << " d: " << value_d << " s: " << value_s << " t: " << value_t << endl; - } - assert( r.size( ) == 3 ); - wq.commit( ); - - cout << "end." << endl; -} -*/ -- cgit v1.2.3-54-g00ecf