You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

310 lines
8.4 KiB

#include "sqlite_class.h"
#define LOG( X ) m_pLog->Log( "SQDB", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "SQDB", this, _LOG_ERROR, X )
static bool sqlite_global_config_is_set = false;
static uint8_t sqlite_global_inctances_count = 0;
static void errorLogCallback( void *pArg, int iErrCode, const char *zMsg ){
char tB[1024];
sprintf( tB, "(%d) %s", iErrCode, zMsg );
basicLogCollection::getInstance()->Log( "SQDB_G", pArg, tB );
};
SQLite_Connector::SQLite_Connector(){
m_pLog = basicLogCollection::getInstance();
m_bIsConnected = false;
m_bPersistentConnection = false;
m_pDBLink = NULL;
if( !sqlite_global_config_is_set ){
//if( SQLITE_OK != sqlite3_config(int, ...) ){ SQLITE_MISUSE
sqlite3_config( SQLITE_CONFIG_LOG, errorLogCallback, this );
sqlite3_initialize();
sqlite_global_config_is_set = true;
};
sqlite_global_inctances_count++;
};
SQLite_Connector::~SQLite_Connector(){
m_bIsConnected = !Disconnect();
if( sqlite_global_inctances_count == 1 ){
sqlite3_shutdown();
};
sqlite_global_inctances_count--;
};
bool SQLite_Connector::Connect( CSTR FileName, int AUX_FLAGS, bool Persistent ){
m_mAccessMutex.lock();
m_bPersistentConnection = Persistent;
if( m_bIsConnected ){
LOG("Connected -> Disconnect!");
m_bIsConnected = false;
if( m_pDBLink != NULL ){
if( SQLITE_OK != sqlite3_close( m_pDBLink ) ) {
m_mAccessMutex.unlock();
return false;
};
};
};
sprintf( m_pDBName, "%s", FileName );
m_nFLAGS = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | AUX_FLAGS;
if( m_bPersistentConnection ){
LOG("Making Connection....");
if( SQLITE_OK == sqlite3_open_v2( m_pDBName, &m_pDBLink, m_nFLAGS, NULL ) ) m_bIsConnected = true;
LOG("Done!");
} else {
m_bIsConnected = true;
};
m_mAccessMutex.unlock();
return m_bIsConnected;
};
bool SQLite_Connector::ReConnect( void ){
if( m_pDBLink != NULL ) if( SQLITE_OK != sqlite3_close( m_pDBLink ) ) return false;
if( SQLITE_OK == sqlite3_open_v2( m_pDBName, &m_pDBLink, m_nFLAGS, NULL ) ) return true;
return false;
};
bool SQLite_Connector::Disconnect( void ){
if( m_pDBLink != NULL ) if( SQLITE_OK != sqlite3_close( m_pDBLink ) ) return false;
m_pDBLink = NULL;
return true;
};
bool SQLite_Connector::SilentQuery( CSTR QS ){
if ( m_bIsConnected ) {
int QSlen = strlen( QS );
m_mAccessMutex.lock();
if( !m_bPersistentConnection ){
if( !ReConnect() ){
m_mAccessMutex.unlock();
return false;
};
};
sqlite3_stmt * query = NULL;
//if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, NULL, NULL ) ){
if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, &query, NULL ) ){
m_mAccessMutex.unlock();
return false;
};
#ifdef DEBUG_EXTRA
LOG( sqlite3_expanded_sql( query ) );
#endif
if( SQLITE_DONE != sqlite3_step( query )){
sqlite3_finalize( query );
m_mAccessMutex.unlock();
return false;
};
sqlite3_finalize( query );
if( !m_bPersistentConnection ){
if( !Disconnect() ){
m_mAccessMutex.unlock();
return false;
};
};
m_mAccessMutex.unlock();
return true;
};
return false;
};
AAssoc *SQLite_Connector::Query( CSTR QS, bool useCache ){
AAssoc * RetVal = NULL;
Assoc * tElement;
unsigned int num_Fields;
sqlite3_stmt * query = NULL;
char temp[256];
int QSlen = strlen( QS );
m_mAccessMutex.lock();
if( !m_bPersistentConnection ){
if( !ReConnect() ){
m_mAccessMutex.unlock();
return RetVal;
};
};
if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, &query, NULL ) ){
m_mAccessMutex.unlock();
return RetVal;
};
#ifdef DEBUG_EXTRA
LOG( sqlite3_expanded_sql( query ) );
#endif
RetVal = new AAssoc();
while( sqlite3_step( query ) != SQLITE_DONE ){
string *Key;
string *Val;
uint32_t num_cols = sqlite3_column_count( query );
if( num_cols ){
tElement = new Assoc();
if( tElement ){
for( uint32_t i = 0; i < num_cols; i++ ){
Key = NULL;
Val = NULL;
Key = new string( sqlite3_column_name( query, i ) );
if( Key!= NULL ) {
switch( sqlite3_column_type( query, i ) ){
case SQLITE_INTEGER:
Val = new std::string( (char*) sqlite3_column_text( query, i ) );
break;
case SQLITE_FLOAT:
Val = new std::string( (char*) sqlite3_column_text( query, i ) );
break;
case SQLITE3_TEXT:
Val = new std::string( (char*) sqlite3_column_text( query, i ) );
break;
case SQLITE_BLOB:
case SQLITE_NULL:
Val = new std::string( "" );
break;
default:
Val = new std::string( "" );
break;
};
if( Val != NULL ){
//(*tElement).insert( pair< string, string > ( *Key, *Val ) );
(*tElement)[*Key]=*Val;
} else {
LOG_ERROR( "Val Alloc Error" );
};
} else {
LOG_ERROR( "Key Alloc Error" );
};
};
RetVal->push_back( tElement );
};
};
};
sqlite3_finalize( query );
if( !m_bPersistentConnection ){
if( !Disconnect() ){
m_mAccessMutex.unlock();
return RetVal;
};
};
m_mAccessMutex.unlock();
return RetVal;
};
CSTR SQLite_Connector::escapeString( CSTR value ){
CSTR buffer = NULL;
int v_len = strlen(value);
CSTR result = (CSTR) malloc( 3 * v_len );
if( result ){
int idx = 0;
for( int i = 0; i < v_len; i++ ){
//If character is printable
if(isprint( value[i] ) != 0){
strncat( result+idx, value+i, 1 );
idx++;
}else{
char tB[4];
snprintf( tB, 4, "#%02X", value[i]);
strncat( result+idx, tB, 3 );
idx+=3;
};
};
buffer = ( CSTR ) malloc( idx+1 );
if( buffer ) {
strncat( buffer, result, idx );
buffer[idx] = (char)0;
};
free(result);
};
return buffer;
};
CSTR SQLite_Connector::escapeBinary( uint8_t *value, uint32_t Size ){
CSTR buffer = NULL;
if ( m_bIsConnected ){
int v_len = Size;
CSTR result = ( CSTR ) malloc( 3 * v_len );
if( result ){
int idx = 0;
for( int i = 0; i < v_len; i++ ){
//If character is printable
if(isprint( value[i] ) != 0){
memcpy( result+idx, value+i, 1 );
idx++;
}else{
char tB[4];
snprintf( tB, 4, "#%02X", value[i]);
strncat( result+idx, tB, 3 );
idx+=3;
};
};
buffer = ( CSTR ) malloc( idx+1 );
if( buffer ) {
strncat( buffer, result, idx );
buffer[idx] = (char)0;
};
free(result);
};
};
return buffer;
};
uint64_t SQLite_Connector::QueryID( CSTR QS ){
uint64_t RetVal = 0;
uint64_t tVal = 0;
if ( m_bIsConnected ) {
Assoc * tElement;
unsigned int num_Fields;
sqlite3_stmt * query = NULL;
char temp[256];
int QSlen = strlen( QS );
m_mAccessMutex.lock();
if( !m_bPersistentConnection ){
if( !ReConnect() ){
m_mAccessMutex.unlock();
return RetVal;
};
};
if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, &query, NULL ) ){
m_mAccessMutex.unlock();
return RetVal;
};
#ifdef DEBUG_EXTRA
LOG( sqlite3_expanded_sql( query ) );
#endif
tVal = sqlite3_last_insert_rowid( m_pDBLink );
if( SQLITE_DONE != sqlite3_step( query )){
sqlite3_finalize( query );
m_mAccessMutex.unlock();
return false;
};
RetVal = sqlite3_last_insert_rowid( m_pDBLink );
sqlite3_finalize( query );
if( !m_bPersistentConnection ){
if( !Disconnect() ){
m_mAccessMutex.unlock();
return RetVal;
};
};
m_mAccessMutex.unlock();
if( RetVal == tVal ) RetVal = 0;
};
return RetVal;
};
void SQLite_Connector::deleteResult( AAssoc *&R ){
if( R ){
while( R->size() ){
delete( (Assoc *) R->at(0) );
R->erase( R->begin());
};
R->clear();
delete( R );
R = NULL;
};
};