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.
472 lines
10 KiB
472 lines
10 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;
|
|
|
|
// https://www.sqlite.org/recovery.html
|
|
/*
|
|
|
|
// *
|
|
// ** This function is called to recover data from the database.
|
|
// * /
|
|
static int recoverDatabase(sqlite3 *db){
|
|
int rc; // Return code from this routine
|
|
const char *zLAF = "lost_and_found"; // Name of "lost_and_found" table
|
|
int bFreelist = 1; // True to scan the freelist
|
|
int bRowids = 1; // True to restore ROWID values
|
|
sqlite3_recover *p; // The recovery object
|
|
|
|
p = sqlite3_recover_init_sql(db, "main", recoverSqlCb, 0);
|
|
sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
|
|
sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
|
|
sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
|
|
sqlite3_recover_run(p);
|
|
if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
|
|
const char *zErr = sqlite3_recover_errmsg(p);
|
|
int errCode = sqlite3_recover_errcode(p);
|
|
if( eVerbosity>0 ){
|
|
printf("recovery error: %s (%d)\n", zErr, errCode);
|
|
}
|
|
}
|
|
rc = sqlite3_recover_finish(p);
|
|
if( eVerbosity>0 && rc ){
|
|
printf("recovery returns error code %d\n", rc);
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
// *
|
|
// ** This function is called to recover data from the database. A script
|
|
// ** to construct a new database containing all recovered data is output
|
|
// ** on stream pState->out.
|
|
// * /
|
|
static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
|
|
int rc = SQLITE_OK;
|
|
const char *zRecoveryDb = ""; // Name of "recovery" database. Debug only
|
|
const char *zLAF = "lost_and_found";
|
|
int bFreelist = 1; // 0 if --ignore-freelist is specified
|
|
int bRowids = 1; // 0 if --no-rowids
|
|
sqlite3_recover *p = 0;
|
|
int i = 0;
|
|
|
|
for(i=1; i<nArg; i++){
|
|
char *z = azArg[i];
|
|
int n;
|
|
if( z[0]=='-' && z[1]=='-' ) z++;
|
|
n = strlen30(z);
|
|
if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){
|
|
bFreelist = 0;
|
|
}else
|
|
if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
|
|
// This option determines the name of the ATTACH-ed database used
|
|
// internally by the recovery extension. The default is "" which
|
|
// means to use a temporary database that is automatically deleted
|
|
// when closed. This option is undocumented and might disappear at
|
|
// any moment.
|
|
i++;
|
|
zRecoveryDb = azArg[i];
|
|
}else
|
|
if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
|
|
i++;
|
|
zLAF = azArg[i];
|
|
}else
|
|
if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
|
|
bRowids = 0;
|
|
}
|
|
else{
|
|
utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
|
|
showHelp(pState->out, azArg[0]);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
p = sqlite3_recover_init_sql(
|
|
pState->db, "main", recoverSqlCb, (void*)pState
|
|
);
|
|
|
|
sqlite3_recover_config(p, 789, (void*)zRecoveryDb); // Debug use only
|
|
sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
|
|
sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
|
|
sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
|
|
|
|
sqlite3_recover_run(p);
|
|
if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
|
|
const char *zErr = sqlite3_recover_errmsg(p);
|
|
int errCode = sqlite3_recover_errcode(p);
|
|
raw_printf(stderr, "sql error: %s (%d)\n", zErr, errCode);
|
|
}
|
|
rc = sqlite3_recover_finish(p);
|
|
return rc;
|
|
}
|
|
|
|
*/
|
|
|
|
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)
|
|
{
|
|
#ifdef DEBUG_ALL
|
|
LOG("Connected -> Disconnect!");
|
|
#endif
|
|
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)
|
|
{
|
|
#ifdef DEBUG_ALL
|
|
LOG("Making Connection....");
|
|
#endif
|
|
if (SQLITE_OK == sqlite3_open_v2(m_pDBName, &m_pDBLink, m_nFLAGS, NULL))
|
|
{
|
|
m_bIsConnected = true;
|
|
}
|
|
#ifdef DEBUG_ALL
|
|
LOG("Done!");
|
|
#endif
|
|
}
|
|
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_ALL
|
|
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;
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("No Connection to DB!");
|
|
};
|
|
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];
|
|
if (useCache)useCache=false;
|
|
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_ALL
|
|
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 = string(sqlite3_column_name(query, i));
|
|
switch (sqlite3_column_type(query, i))
|
|
{
|
|
case SQLITE_INTEGER: Val = std::string((char *) sqlite3_column_text(query, i)); break;
|
|
case SQLITE_FLOAT : Val = std::string((char *) sqlite3_column_text(query, i)); break;
|
|
case SQLITE3_TEXT : Val = std::string((char *) sqlite3_column_text(query, i)); break;
|
|
};
|
|
(*tElement)[Key] = Val;
|
|
};
|
|
RetVal->push_back(tElement);
|
|
};
|
|
};
|
|
};
|
|
|
|
sqlite3_finalize(query);
|
|
|
|
if (! m_bPersistentConnection)
|
|
{
|
|
if (! Disconnect())
|
|
{
|
|
m_mAccessMutex.unlock();
|
|
return RetVal;
|
|
};
|
|
};
|
|
m_mAccessMutex.unlock();
|
|
return RetVal;
|
|
}
|
|
|
|
#include "../../components/base/basicFunctionality.h"
|
|
|
|
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);
|
|
*/
|
|
escape_string(result, 3 * v_len, value, v_len);
|
|
};
|
|
return result;
|
|
}
|
|
|
|
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_ALL
|
|
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 RetVal;
|
|
};
|
|
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;
|
|
};
|
|
}
|