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.

412 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
​
​
​
​
​
​
​
​
​
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. LOG("Connected -> Disconnect!");
  125. m_bIsConnected = false;
  126. if( m_pDBLink != NULL ){
  127. if( SQLITE_OK != sqlite3_close( m_pDBLink ) ) {
  128. m_mAccessMutex.unlock();
  129. return false;
  130. };
  131. };
  132. };
  133. sprintf( m_pDBName, "%s", FileName );
  134. m_nFLAGS = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | AUX_FLAGS;
  135. if( m_bPersistentConnection ){
  136. LOG("Making Connection....");
  137. if( SQLITE_OK == sqlite3_open_v2( m_pDBName, &m_pDBLink, m_nFLAGS, NULL ) ) m_bIsConnected = true;
  138. LOG("Done!");
  139. } else {
  140. m_bIsConnected = true;
  141. };
  142. m_mAccessMutex.unlock();
  143. return m_bIsConnected;
  144. };
  145. bool SQLite_Connector::ReConnect( void ){
  146. if( m_pDBLink != NULL ) if( SQLITE_OK != sqlite3_close( m_pDBLink ) ) return false;
  147. if( SQLITE_OK == sqlite3_open_v2( m_pDBName, &m_pDBLink, m_nFLAGS, NULL ) ) return true;
  148. return false;
  149. };
  150. bool SQLite_Connector::Disconnect( void ){
  151. if( m_pDBLink != NULL ) if( SQLITE_OK != sqlite3_close( m_pDBLink ) ) return false;
  152. m_pDBLink = NULL;
  153. return true;
  154. };
  155. bool SQLite_Connector::SilentQuery( CSTR QS ){
  156. if ( m_bIsConnected ) {
  157. int QSlen = strlen( QS );
  158. m_mAccessMutex.lock();
  159. if( !m_bPersistentConnection ){
  160. if( !ReConnect() ){
  161. m_mAccessMutex.unlock();
  162. return false;
  163. };
  164. };
  165. sqlite3_stmt * query = NULL;
  166. //if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, NULL, NULL ) ){
  167. if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, &query, NULL ) ){
  168. m_mAccessMutex.unlock();
  169. return false;
  170. };
  171. #ifdef DEBUG_EXTRA
  172. LOG( sqlite3_expanded_sql( query ) );
  173. #endif
  174. if( SQLITE_DONE != sqlite3_step( query )){
  175. sqlite3_finalize( query );
  176. m_mAccessMutex.unlock();
  177. return false;
  178. };
  179. sqlite3_finalize( query );
  180. if( !m_bPersistentConnection ){
  181. if( !Disconnect() ){
  182. m_mAccessMutex.unlock();
  183. return false;
  184. };
  185. };
  186. m_mAccessMutex.unlock();
  187. return true;
  188. };
  189. return false;
  190. };
  191. AAssoc *SQLite_Connector::Query( CSTR QS, bool useCache ){
  192. AAssoc * RetVal = NULL;
  193. Assoc * tElement;
  194. unsigned int num_Fields;
  195. sqlite3_stmt * query = NULL;
  196. char temp[256];
  197. int QSlen = strlen( QS );
  198. m_mAccessMutex.lock();
  199. if( !m_bPersistentConnection ){
  200. if( !ReConnect() ){
  201. m_mAccessMutex.unlock();
  202. return RetVal;
  203. };
  204. };
  205. if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, &query, NULL ) ){
  206. m_mAccessMutex.unlock();
  207. return RetVal;
  208. };
  209. #ifdef DEBUG_EXTRA
  210. LOG( sqlite3_expanded_sql( query ) );
  211. #endif
  212. RetVal = new AAssoc();
  213. while( sqlite3_step( query ) != SQLITE_DONE ){
  214. string *Key;
  215. string *Val;
  216. uint32_t num_cols = sqlite3_column_count( query );
  217. if( num_cols ){
  218. tElement = new Assoc();
  219. if( tElement ){
  220. for( uint32_t i = 0; i < num_cols; i++ ){
  221. Key = NULL;
  222. Val = NULL;
  223. Key = new string( sqlite3_column_name( query, i ) );
  224. if( Key!= NULL ) {
  225. switch( sqlite3_column_type( query, i ) ){
  226. case SQLITE_INTEGER:
  227. Val = new std::string( (char*) sqlite3_column_text( query, i ) );
  228. break;
  229. case SQLITE_FLOAT:
  230. Val = new std::string( (char*) sqlite3_column_text( query, i ) );
  231. break;
  232. case SQLITE3_TEXT:
  233. Val = new std::string( (char*) sqlite3_column_text( query, i ) );
  234. break;
  235. case SQLITE_BLOB:
  236. case SQLITE_NULL:
  237. Val = new std::string( "" );
  238. break;
  239. default:
  240. Val = new std::string( "" );
  241. break;
  242. };
  243. if( Val != NULL ){
  244. //(*tElement).insert( pair< string, string > ( *Key, *Val ) );
  245. (*tElement)[*Key]=*Val;
  246. } else {
  247. LOG_ERROR( "Val Alloc Error" );
  248. };
  249. } else {
  250. LOG_ERROR( "Key Alloc Error" );
  251. };
  252. };
  253. RetVal->push_back( tElement );
  254. };
  255. };
  256. };
  257. sqlite3_finalize( query );
  258. if( !m_bPersistentConnection ){
  259. if( !Disconnect() ){
  260. m_mAccessMutex.unlock();
  261. return RetVal;
  262. };
  263. };
  264. m_mAccessMutex.unlock();
  265. return RetVal;
  266. };
  267. #include "../../components/base/basicFunctionality.h"
  268. CSTR SQLite_Connector::escapeString( CSTR value ){
  269. //CSTR buffer = NULL;
  270. int v_len = strlen(value);
  271. CSTR result = (CSTR) malloc( 3 * v_len );
  272. if( result ){
  273. /*
  274. int idx = 0;
  275. for( int i = 0; i < v_len; i++ ){
  276. //If character is printable
  277. if(isprint( value[i] ) != 0){
  278. strncat( result+idx, value+i, 1 );
  279. idx++;
  280. }else{
  281. char tB[4];
  282. snprintf( tB, 4, "#%02X", value[i]);
  283. strncat( result+idx, tB, 3 );
  284. idx+=3;
  285. };
  286. };
  287. buffer = ( CSTR ) malloc( idx+1 );
  288. if( buffer ) {
  289. strncat( buffer, result, idx );
  290. buffer[idx] = (char)0;
  291. };
  292. free(result);
  293. */
  294. escape_string( result, 3*v_len, value, v_len );
  295. };
  296. return result;
  297. };
  298. CSTR SQLite_Connector::escapeBinary( uint8_t *value, uint32_t Size ){
  299. CSTR buffer = NULL;
  300. if ( m_bIsConnected ){
  301. int v_len = Size;
  302. CSTR result = ( CSTR ) malloc( 3 * v_len );
  303. if( result ){
  304. int idx = 0;
  305. for( int i = 0; i < v_len; i++ ){
  306. //If character is printable
  307. if(isprint( value[i] ) != 0){
  308. memcpy( result+idx, value+i, 1 );
  309. idx++;
  310. }else{
  311. char tB[4];
  312. snprintf( tB, 4, "#%02X", value[i]);
  313. strncat( result+idx, tB, 3 );
  314. idx+=3;
  315. };
  316. };
  317. buffer = ( CSTR ) malloc( idx+1 );
  318. if( buffer ) {
  319. strncat( buffer, result, idx );
  320. buffer[idx] = (char)0;
  321. };
  322. free(result);
  323. };
  324. };
  325. return buffer;
  326. };
  327. uint64_t SQLite_Connector::QueryID( CSTR QS ){
  328. uint64_t RetVal = 0;
  329. uint64_t tVal = 0;
  330. if ( m_bIsConnected ) {
  331. Assoc * tElement;
  332. unsigned int num_Fields;
  333. sqlite3_stmt * query = NULL;
  334. char temp[256];
  335. int QSlen = strlen( QS );
  336. m_mAccessMutex.lock();
  337. if( !m_bPersistentConnection ){
  338. if( !ReConnect() ){
  339. m_mAccessMutex.unlock();
  340. return RetVal;
  341. };
  342. };
  343. if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, &query, NULL ) ){
  344. m_mAccessMutex.unlock();
  345. return RetVal;
  346. };
  347. #ifdef DEBUG_EXTRA
  348. LOG( sqlite3_expanded_sql( query ) );
  349. #endif
  350. tVal = sqlite3_last_insert_rowid( m_pDBLink );
  351. if( SQLITE_DONE != sqlite3_step( query )){
  352. sqlite3_finalize( query );
  353. m_mAccessMutex.unlock();
  354. return RetVal;
  355. };
  356. RetVal = sqlite3_last_insert_rowid( m_pDBLink );
  357. sqlite3_finalize( query );
  358. if( !m_bPersistentConnection ){
  359. if( !Disconnect() ){
  360. m_mAccessMutex.unlock();
  361. return RetVal;
  362. };
  363. };
  364. m_mAccessMutex.unlock();
  365. if( RetVal == tVal ) RetVal = 0;
  366. };
  367. return RetVal;
  368. };
  369. void SQLite_Connector::deleteResult( AAssoc *&R ){
  370. if( R ){
  371. while( R->size() ){
  372. delete( (Assoc *) R->at(0) );
  373. R->erase( R->begin());
  374. };
  375. R->clear();
  376. delete( R );
  377. R = NULL;
  378. };
  379. };