/* * sqlite3xx - sqlite3 C++ layer, following the ideas of libpqxx * Copyright (C) 2009 Andreas Baumann * * This copyrighted material is made available to anyone wishing to use, * modify, copy, or redistribute it subject to the terms and conditions of * the GNU Lesser General Public License, as published by the Free Software * Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA * */ #include #include #include "sqlite3xx/sqlite3xx" /* test needs 'unlink' from 'unistd.h' */ #if !defined _WIN32 #include #endif /* !defined _WIN32 */ #include "threads.h" using namespace sqlite3xx; using namespace std; const int NOF_PRODUCERS = 2; const int NOF_CONSUMERS = 2; const int NOF_PRODUCER_TRANSACTIONS = 10; const int NOF_PRODUCER_OPS = 10; const int NOF_CONSUMER_TRANSACTIONS = 10; const int NOF_CONSUMER_OPS = 10; #define UNUSED( x ) if( 0 && (x) ) { } MUTEX_TYPE cout_mutex; static THREAD_FUNC_DECL produce( void *thread_data ) { UNUSED( thread_data ); try { connection c( "test9.db" ); c.prepare( "ins", "insert into x values( ? )" )( "text", sqlite3xx::prepare::treat_direct ); for( int i = 0; i < NOF_PRODUCER_TRANSACTIONS; i++ ) { work t( c, "ins" ); MUTEX_LOCK( cout_mutex ); cout << "producer transaction " << i << " started" << endl; MUTEX_UNLOCK( cout_mutex ); for( int j = 0; j < NOF_PRODUCER_OPS; j++ ) { result r = t.prepared( "ins" )( i * 1000 + j ).exec( ); assert( r.affected_rows( ) == 1 ); } t.commit( ); } } catch( sql_error& e ) { cerr << "producer error, " << e.msg( ) << ": " << e.query( ) << endl; } THREAD_FUNC_RETURN; } static THREAD_FUNC_DECL consume( void *thread_data ) { UNUSED( thread_data ); try { connection c( "test9.db" ); c.prepare( "sel", "select * from x" ); for( int i = 0; i < NOF_CONSUMER_TRANSACTIONS; i++ ) { work t( c, "sel" ); MUTEX_LOCK( cout_mutex ); cout << "consumer transaction " << i << " started" << endl; MUTEX_UNLOCK( cout_mutex ); for( int j = 0; j < NOF_CONSUMER_OPS; j++ ) { result r = t.prepared( "sel" ).exec( ); for( result::const_iterator it = r.begin( ); it < r.end( ); it++ ) { int x; it["x"].to( x ); } MUTEX_LOCK( cout_mutex ); cout << "consumer transaction got " << r.size( ) << " rows." << endl; MUTEX_UNLOCK( cout_mutex ); } t.commit( ); } } catch( sql_error& e ) { cerr << "consumer error, " << e.msg( ) << ": " << e.query( ) << endl; } THREAD_FUNC_RETURN; } int main( ) { (void)unlink( "test9.db" ); try { cout << "creating DB.." << endl; connection c( "test9.db" ); cout << "connection object is " << c << endl; cout << "create table.." << endl; c.exec( "create table x( x integer )" ); // now have some threads creating data and some others // consuming it. stupid, but should nicely stress the thing THREAD_TYPE prod[NOF_PRODUCERS]; THREAD_TYPE cons[NOF_CONSUMERS]; MUTEX_SETUP( cout_mutex ); for( int i = 0; i < NOF_PRODUCERS; i++ ) { THREAD_CREATE( &prod[i], produce, NULL ); } for( int i = 0; i < NOF_CONSUMERS; i++ ) { THREAD_CREATE( &cons[i], consume, NULL ); } for( int i = 0; i < NOF_PRODUCERS; i++ ) { THREAD_JOIN( prod[i] ); } for( int i = 0; i < NOF_CONSUMERS; i++ ) { THREAD_JOIN( cons[i] ); } MUTEX_CLEANUP( cout_mutex ); } catch( sql_error& e ) { cerr << e.msg( ) << ": " << 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; } */