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.

420 lines
12 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
3 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "sqlite_class.h"
  2. #define LOG( X ) m_pLog->Log( "SQDB", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "SQDB", this, _LOG_ERROR, X )
  4. static bool sqlite_global_config_is_set = false;
  5. static uint8_t sqlite_global_inctances_count = 0;
  6. //https://www.sqlite.org/recovery.html
  7. /*
  8. // *
  9. // ** This function is called to recover data from the database.
  10. // * /
  11. static int recoverDatabase(sqlite3 *db){
  12. int rc; /* Return code from this routine * /
  13. const char *zLAF = "lost_and_found"; /* Name of "lost_and_found" table * /
  14. int bFreelist = 1; /* True to scan the freelist * /
  15. int bRowids = 1; /* True to restore ROWID values * /
  16. sqlite3_recover *p; /* The recovery object * /
  17. p = sqlite3_recover_init_sql(db, "main", recoverSqlCb, 0);
  18. sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
  19. sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
  20. sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
  21. sqlite3_recover_run(p);
  22. if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
  23. const char *zErr = sqlite3_recover_errmsg(p);
  24. int errCode = sqlite3_recover_errcode(p);
  25. if( eVerbosity>0 ){
  26. printf("recovery error: %s (%d)\n", zErr, errCode);
  27. }
  28. }
  29. rc = sqlite3_recover_finish(p);
  30. if( eVerbosity>0 && rc ){
  31. printf("recovery returns error code %d\n", rc);
  32. }
  33. return rc;
  34. }
  35. // *
  36. // ** This function is called to recover data from the database. A script
  37. // ** to construct a new database containing all recovered data is output
  38. // ** on stream pState->out.
  39. // * /
  40. static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
  41. int rc = SQLITE_OK;
  42. const char *zRecoveryDb = ""; /* Name of "recovery" database. Debug only * /
  43. const char *zLAF = "lost_and_found";
  44. int bFreelist = 1; /* 0 if --ignore-freelist is specified * /
  45. int bRowids = 1; /* 0 if --no-rowids * /
  46. sqlite3_recover *p = 0;
  47. int i = 0;
  48. for(i=1; i<nArg; i++){
  49. char *z = azArg[i];
  50. int n;
  51. if( z[0]=='-' && z[1]=='-' ) z++;
  52. n = strlen30(z);
  53. if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){
  54. bFreelist = 0;
  55. }else
  56. if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
  57. /* This option determines the name of the ATTACH-ed database used
  58. ** internally by the recovery extension. The default is "" which
  59. ** means to use a temporary database that is automatically deleted
  60. ** when closed. This option is undocumented and might disappear at
  61. ** any moment. * /
  62. i++;
  63. zRecoveryDb = azArg[i];
  64. }else
  65. if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
  66. i++;
  67. zLAF = azArg[i];
  68. }else
  69. if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
  70. bRowids = 0;
  71. }
  72. else{
  73. utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
  74. showHelp(pState->out, azArg[0]);
  75. return 1;
  76. }
  77. }
  78. p = sqlite3_recover_init_sql(
  79. pState->db, "main", recoverSqlCb, (void*)pState
  80. );
  81. sqlite3_recover_config(p, 789, (void*)zRecoveryDb); /* Debug use only * /
  82. sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
  83. sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
  84. sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
  85. sqlite3_recover_run(p);
  86. if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
  87. const char *zErr = sqlite3_recover_errmsg(p);
  88. int errCode = sqlite3_recover_errcode(p);
  89. raw_printf(stderr, "sql error: %s (%d)\n", zErr, errCode);
  90. }
  91. rc = sqlite3_recover_finish(p);
  92. return rc;
  93. }
  94. */
  95. static void errorLogCallback( void *pArg, int iErrCode, const char *zMsg ){
  96. char tB[1024];
  97. sprintf( tB, "(%d) %s", iErrCode, zMsg );
  98. basicLogCollection::getInstance()->Log( "SQDB_G", pArg, tB );
  99. };
  100. SQLite_Connector::SQLite_Connector(){
  101. m_pLog = basicLogCollection::getInstance();
  102. m_bIsConnected = false;
  103. m_bPersistentConnection = false;
  104. m_pDBLink = NULL;
  105. if( !sqlite_global_config_is_set ){
  106. //if( SQLITE_OK != sqlite3_config(int, ...) ){ SQLITE_MISUSE
  107. sqlite3_config( SQLITE_CONFIG_LOG, errorLogCallback, this );
  108. sqlite3_initialize();
  109. sqlite_global_config_is_set = true;
  110. };
  111. sqlite_global_inctances_count++;
  112. };
  113. SQLite_Connector::~SQLite_Connector(){
  114. m_bIsConnected = !Disconnect();
  115. if( sqlite_global_inctances_count == 1 ){
  116. sqlite3_shutdown();
  117. };
  118. sqlite_global_inctances_count--;
  119. };
  120. bool SQLite_Connector::Connect( CSTR FileName, int AUX_FLAGS, bool Persistent ){
  121. m_mAccessMutex.lock();
  122. m_bPersistentConnection = Persistent;
  123. if( m_bIsConnected ){
  124. #ifdef DEBUG_ALL
  125. LOG("Connected -> Disconnect!");
  126. #endif
  127. m_bIsConnected = false;
  128. if( m_pDBLink != NULL ){
  129. if( SQLITE_OK != sqlite3_close( m_pDBLink ) ) {
  130. m_mAccessMutex.unlock();
  131. return false;
  132. };
  133. };
  134. };
  135. sprintf( m_pDBName, "%s", FileName );
  136. m_nFLAGS = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | AUX_FLAGS;
  137. if( m_bPersistentConnection ){
  138. #ifdef DEBUG_ALL
  139. LOG("Making Connection....");
  140. #endif
  141. if( SQLITE_OK == sqlite3_open_v2( m_pDBName, &m_pDBLink, m_nFLAGS, NULL ) ) m_bIsConnected = true;
  142. #ifdef DEBUG_ALL
  143. LOG("Done!");
  144. #endif
  145. } else {
  146. m_bIsConnected = true;
  147. };
  148. m_mAccessMutex.unlock();
  149. return m_bIsConnected;
  150. };
  151. bool SQLite_Connector::ReConnect( void ){
  152. if( m_pDBLink != NULL ) if( SQLITE_OK != sqlite3_close( m_pDBLink ) ) return false;
  153. if( SQLITE_OK == sqlite3_open_v2( m_pDBName, &m_pDBLink, m_nFLAGS, NULL ) ) return true;
  154. return false;
  155. };
  156. bool SQLite_Connector::Disconnect( void ){
  157. if( m_pDBLink != NULL ) if( SQLITE_OK != sqlite3_close( m_pDBLink ) ) return false;
  158. m_pDBLink = NULL;
  159. return true;
  160. };
  161. bool SQLite_Connector::SilentQuery( CSTR QS ){
  162. if ( m_bIsConnected ) {
  163. int QSlen = strlen( QS );
  164. m_mAccessMutex.lock();
  165. if( !m_bPersistentConnection ){
  166. if( !ReConnect() ){
  167. m_mAccessMutex.unlock();
  168. return false;
  169. };
  170. };
  171. sqlite3_stmt * query = NULL;
  172. //if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, NULL, NULL ) ){
  173. if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, &query, NULL ) ){
  174. m_mAccessMutex.unlock();
  175. return false;
  176. };
  177. #ifdef DEBUG_ALL
  178. LOG( sqlite3_expanded_sql( query ) );
  179. #endif
  180. if( SQLITE_DONE != sqlite3_step( query )){
  181. sqlite3_finalize( query );
  182. m_mAccessMutex.unlock();
  183. return false;
  184. };
  185. sqlite3_finalize( query );
  186. if( !m_bPersistentConnection ){
  187. if( !Disconnect() ){
  188. m_mAccessMutex.unlock();
  189. return false;
  190. };
  191. };
  192. m_mAccessMutex.unlock();
  193. return true;
  194. } else {
  195. LOG_ERROR("No Connection to DB!");
  196. };
  197. return false;
  198. };
  199. AAssoc *SQLite_Connector::Query( CSTR QS, bool useCache ){
  200. AAssoc * RetVal = NULL;
  201. Assoc * tElement;
  202. unsigned int num_Fields;
  203. sqlite3_stmt * query = NULL;
  204. char temp[256];
  205. int QSlen = strlen( QS );
  206. m_mAccessMutex.lock();
  207. if( !m_bPersistentConnection ){
  208. if( !ReConnect() ){
  209. m_mAccessMutex.unlock();
  210. return RetVal;
  211. };
  212. };
  213. if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, &query, NULL ) ){
  214. m_mAccessMutex.unlock();
  215. return RetVal;
  216. };
  217. #ifdef DEBUG_ALL
  218. LOG( sqlite3_expanded_sql( query ) );
  219. #endif
  220. RetVal = new AAssoc();
  221. while( sqlite3_step( query ) != SQLITE_DONE ){
  222. string *Key;
  223. string *Val;
  224. uint32_t num_cols = sqlite3_column_count( query );
  225. if( num_cols ){
  226. tElement = new Assoc();
  227. if( tElement ){
  228. for( uint32_t i = 0; i < num_cols; i++ ){
  229. Key = NULL;
  230. Val = NULL;
  231. Key = new string( sqlite3_column_name( query, i ) );
  232. if( Key!= NULL ) {
  233. switch( sqlite3_column_type( query, i ) ){
  234. case SQLITE_INTEGER:
  235. Val = new std::string( (char*) sqlite3_column_text( query, i ) );
  236. break;
  237. case SQLITE_FLOAT:
  238. Val = new std::string( (char*) sqlite3_column_text( query, i ) );
  239. break;
  240. case SQLITE3_TEXT:
  241. Val = new std::string( (char*) sqlite3_column_text( query, i ) );
  242. break;
  243. case SQLITE_BLOB:
  244. case SQLITE_NULL:
  245. Val = new std::string( "" );
  246. break;
  247. default:
  248. Val = new std::string( "" );
  249. break;
  250. };
  251. if( Val != NULL ){
  252. //(*tElement).insert( pair< string, string > ( *Key, *Val ) );
  253. (*tElement)[*Key]=*Val;
  254. } else {
  255. LOG_ERROR( "Val Alloc Error" );
  256. };
  257. } else {
  258. LOG_ERROR( "Key Alloc Error" );
  259. };
  260. };
  261. RetVal->push_back( tElement );
  262. };
  263. };
  264. };
  265. sqlite3_finalize( query );
  266. if( !m_bPersistentConnection ){
  267. if( !Disconnect() ){
  268. m_mAccessMutex.unlock();
  269. return RetVal;
  270. };
  271. };
  272. m_mAccessMutex.unlock();
  273. return RetVal;
  274. };
  275. #include "../../components/base/basicFunctionality.h"
  276. CSTR SQLite_Connector::escapeString( CSTR value ){
  277. //CSTR buffer = NULL;
  278. int v_len = strlen(value);
  279. CSTR result = (CSTR) malloc( 3 * v_len );
  280. if( result ){
  281. /*
  282. int idx = 0;
  283. for( int i = 0; i < v_len; i++ ){
  284. //If character is printable
  285. if(isprint( value[i] ) != 0){
  286. strncat( result+idx, value+i, 1 );
  287. idx++;
  288. }else{
  289. char tB[4];
  290. snprintf( tB, 4, "#%02X", value[i]);
  291. strncat( result+idx, tB, 3 );
  292. idx+=3;
  293. };
  294. };
  295. buffer = ( CSTR ) malloc( idx+1 );
  296. if( buffer ) {
  297. strncat( buffer, result, idx );
  298. buffer[idx] = (char)0;
  299. };
  300. free(result);
  301. */
  302. escape_string( result, 3*v_len, value, v_len );
  303. };
  304. return result;
  305. };
  306. CSTR SQLite_Connector::escapeBinary( uint8_t *value, uint32_t Size ){
  307. CSTR buffer = NULL;
  308. if ( m_bIsConnected ){
  309. int v_len = Size;
  310. CSTR result = ( CSTR ) malloc( 3 * v_len );
  311. if( result ){
  312. int idx = 0;
  313. for( int i = 0; i < v_len; i++ ){
  314. //If character is printable
  315. if(isprint( value[i] ) != 0){
  316. memcpy( result+idx, value+i, 1 );
  317. idx++;
  318. }else{
  319. char tB[4];
  320. snprintf( tB, 4, "#%02X", value[i]);
  321. strncat( result+idx, tB, 3 );
  322. idx+=3;
  323. };
  324. };
  325. buffer = ( CSTR ) malloc( idx+1 );
  326. if( buffer ) {
  327. strncat( buffer, result, idx );
  328. buffer[idx] = (char)0;
  329. };
  330. free(result);
  331. };
  332. };
  333. return buffer;
  334. };
  335. uint64_t SQLite_Connector::QueryID( CSTR QS ){
  336. uint64_t RetVal = 0;
  337. uint64_t tVal = 0;
  338. if ( m_bIsConnected ) {
  339. Assoc * tElement;
  340. unsigned int num_Fields;
  341. sqlite3_stmt * query = NULL;
  342. char temp[256];
  343. int QSlen = strlen( QS );
  344. m_mAccessMutex.lock();
  345. if( !m_bPersistentConnection ){
  346. if( !ReConnect() ){
  347. m_mAccessMutex.unlock();
  348. return RetVal;
  349. };
  350. };
  351. if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, &query, NULL ) ){
  352. m_mAccessMutex.unlock();
  353. return RetVal;
  354. };
  355. #ifdef DEBUG_ALL
  356. LOG( sqlite3_expanded_sql( query ) );
  357. #endif
  358. tVal = sqlite3_last_insert_rowid( m_pDBLink );
  359. if( SQLITE_DONE != sqlite3_step( query )){
  360. sqlite3_finalize( query );
  361. m_mAccessMutex.unlock();
  362. return RetVal;
  363. };
  364. RetVal = sqlite3_last_insert_rowid( m_pDBLink );
  365. sqlite3_finalize( query );
  366. if( !m_bPersistentConnection ){
  367. if( !Disconnect() ){
  368. m_mAccessMutex.unlock();
  369. return RetVal;
  370. };
  371. };
  372. m_mAccessMutex.unlock();
  373. if( RetVal == tVal ) RetVal = 0;
  374. };
  375. return RetVal;
  376. };
  377. void SQLite_Connector::deleteResult( AAssoc *&R ){
  378. if( R ){
  379. while( R->size() ){
  380. delete( (Assoc *) R->at(0) );
  381. R->erase( R->begin());
  382. };
  383. R->clear();
  384. delete( R );
  385. R = NULL;
  386. };
  387. };