1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-23 17:33:56 +03:00

feature #206: sqlite class implements the SqlDB interface

This commit is contained in:
Ruben S. Montero 2010-04-03 15:56:57 +02:00
parent 31ef166ee2
commit eacaa30e12

View File

@ -28,9 +28,28 @@
#include "Log.h" #include "Log.h"
#include "SqlDB.h" #include "SqlDB.h"
#include "ObjectSQL.h"
using namespace std; using namespace std;
extern "C" int sqlite_callback (
void * _obj,
int num,
char ** values,
char ** names)
{
ObjectSQL *obj;
obj = static_cast<ObjectSQL *>(_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 * SqliteDB class. Provides a wrapper to the sqlite3 database interface. It also
* provides "global" synchronization mechanism to use it in a multithread * provides "global" synchronization mechanism to use it in a multithread
@ -40,10 +59,7 @@ class SqliteDB : public SqlDB
{ {
public: public:
SqliteDB( SqliteDB(string& db_name, Log::LogFunction _log = 0):log(_log)
string& db_name,
Log::LogFunction _log = 0
):log(_log)
{ {
int rc; int rc;
@ -72,7 +88,7 @@ public:
* @param arg to pass to the callback function * @param arg to pass to the callback function
* @return 0 on success * @return 0 on success
*/ */
int exec(ostringstream& cmd, SqlCallback cbk=0, void * arg=0) int exec(ostringstream& cmd, ObjectSQL* obj=0)
{ {
int rc; int rc;
@ -84,16 +100,28 @@ public:
char * err_msg; char * err_msg;
char ** ptr = (log==0) ? 0 : &err_msg; char ** ptr = (log==0) ? 0 : &err_msg;
int (*callback)(void*,int,char**,char**);
void * arg;
str = cmd.str(); str = cmd.str();
c_str = str.c_str(); c_str = str.c_str();
callback = 0;
arg = 0;
if ((obj != 0)&&(obj->isCallBackSet()))
{
callback = sqlite_callback;
arg = static_cast<void *>(obj);
}
lock(); lock();
do do
{ {
counter++; 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) if (rc == SQLITE_BUSY || rc == SQLITE_IOERR_BLOCKED)
{ {
@ -102,11 +130,10 @@ public:
FD_ZERO(&zero); FD_ZERO(&zero);
timeout.tv_sec = 0; timeout.tv_sec = 0;
timeout.tv_usec = 100000; timeout.tv_usec = 250000;
select(0, &zero, &zero, &zero, &timeout); select(0, &zero, &zero, &zero, &timeout);
} }
}while( (rc == SQLITE_BUSY || rc == SQLITE_IOERR_BLOCKED) && }while( (rc == SQLITE_BUSY || rc == SQLITE_IOERR_BLOCKED) &&
(counter < 10)); (counter < 10));
@ -120,7 +147,10 @@ public:
oss << "SQL command was: " << c_str << ", error: " << err_msg; oss << "SQL command was: " << c_str << ", error: " << err_msg;
log("ONE",Log::ERROR,oss,0,Log::ERROR); log("ONE",Log::ERROR,oss,0,Log::ERROR);
}
if ( err_msg != 0)
{
sqlite3_free(err_msg); sqlite3_free(err_msg);
} }
@ -137,14 +167,14 @@ public:
* @param arg to pass to the callback function * @param arg to pass to the callback function
* @return 0 on success * @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; string cmd_str = cmd_c_str;
ostringstream cmd; ostringstream cmd;
cmd.str(cmd_str); cmd.str(cmd_str);
return exec(cmd, cbk, arg); return exec(cmd, obj);
}; };
/** /**