From eacaa30e12ef087fc287f2b2ab0eeb111e7979a1 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sat, 3 Apr 2010 15:56:57 +0200 Subject: [PATCH] feature #206: sqlite class implements the SqlDB interface --- include/SqliteDB.h | 50 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/include/SqliteDB.h b/include/SqliteDB.h index 7d299c03c0..2702fea4d0 100644 --- a/include/SqliteDB.h +++ b/include/SqliteDB.h @@ -28,9 +28,28 @@ #include "Log.h" #include "SqlDB.h" +#include "ObjectSQL.h" using namespace std; +extern "C" int sqlite_callback ( + void * _obj, + int num, + char ** values, + char ** names) +{ + ObjectSQL *obj; + + obj = static_cast(_obj); + + if (obj == 0) + { + return -1; + } + + return obj->do_callback(num,values,names); +}; + /** * SqliteDB class. Provides a wrapper to the sqlite3 database interface. It also * provides "global" synchronization mechanism to use it in a multithread @@ -40,10 +59,7 @@ class SqliteDB : public SqlDB { public: - SqliteDB( - string& db_name, - Log::LogFunction _log = 0 - ):log(_log) + SqliteDB(string& db_name, Log::LogFunction _log = 0):log(_log) { int rc; @@ -72,7 +88,7 @@ public: * @param arg to pass to the callback function * @return 0 on success */ - int exec(ostringstream& cmd, SqlCallback cbk=0, void * arg=0) + int exec(ostringstream& cmd, ObjectSQL* obj=0) { int rc; @@ -84,16 +100,28 @@ public: char * err_msg; char ** ptr = (log==0) ? 0 : &err_msg; + int (*callback)(void*,int,char**,char**); + void * arg; + str = cmd.str(); c_str = str.c_str(); + callback = 0; + arg = 0; + + if ((obj != 0)&&(obj->isCallBackSet())) + { + callback = sqlite_callback; + arg = static_cast(obj); + } + lock(); do { counter++; - rc = sqlite3_exec(db, c_str, cbk, arg, ptr); + rc = sqlite3_exec(db, c_str, callback, arg, ptr); if (rc == SQLITE_BUSY || rc == SQLITE_IOERR_BLOCKED) { @@ -102,11 +130,10 @@ public: FD_ZERO(&zero); timeout.tv_sec = 0; - timeout.tv_usec = 100000; + timeout.tv_usec = 250000; select(0, &zero, &zero, &zero, &timeout); } - }while( (rc == SQLITE_BUSY || rc == SQLITE_IOERR_BLOCKED) && (counter < 10)); @@ -120,7 +147,10 @@ public: oss << "SQL command was: " << c_str << ", error: " << err_msg; log("ONE",Log::ERROR,oss,0,Log::ERROR); + } + if ( err_msg != 0) + { sqlite3_free(err_msg); } @@ -137,14 +167,14 @@ public: * @param arg to pass to the callback function * @return 0 on success */ - int exec(const char * cmd_c_str, SqlCallback cbk=0, void * arg=0) + int exec(const char * cmd_c_str, ObjectSQL* obj=0) { string cmd_str = cmd_c_str; ostringstream cmd; cmd.str(cmd_str); - return exec(cmd, cbk, arg); + return exec(cmd, obj); }; /**