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.

414 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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. } else {
  189. LOG_ERROR("No Connection to DB!");
  190. };
  191. return false;
  192. };
  193. AAssoc *SQLite_Connector::Query( CSTR QS, bool useCache ){
  194. AAssoc * RetVal = NULL;
  195. Assoc * tElement;
  196. unsigned int num_Fields;
  197. sqlite3_stmt * query = NULL;
  198. char temp[256];
  199. int QSlen = strlen( QS );
  200. m_mAccessMutex.lock();
  201. if( !m_bPersistentConnection ){
  202. if( !ReConnect() ){
  203. m_mAccessMutex.unlock();
  204. return RetVal;
  205. };
  206. };
  207. if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, &query, NULL ) ){
  208. m_mAccessMutex.unlock();
  209. return RetVal;
  210. };
  211. #ifdef DEBUG_EXTRA
  212. LOG( sqlite3_expanded_sql( query ) );
  213. #endif
  214. RetVal = new AAssoc();
  215. while( sqlite3_step( query ) != SQLITE_DONE ){
  216. string *Key;
  217. string *Val;
  218. uint32_t num_cols = sqlite3_column_count( query );
  219. if( num_cols ){
  220. tElement = new Assoc();
  221. if( tElement ){
  222. for( uint32_t i = 0; i < num_cols; i++ ){
  223. Key = NULL;
  224. Val = NULL;
  225. Key = new string( sqlite3_column_name( query, i ) );
  226. if( Key!= NULL ) {
  227. switch( sqlite3_column_type( query, i ) ){
  228. case SQLITE_INTEGER:
  229. Val = new std::string( (char*) sqlite3_column_text( query, i ) );
  230. break;
  231. case SQLITE_FLOAT:
  232. Val = new std::string( (char*) sqlite3_column_text( query, i ) );
  233. break;
  234. case SQLITE3_TEXT:
  235. Val = new std::string( (char*) sqlite3_column_text( query, i ) );
  236. break;
  237. case SQLITE_BLOB:
  238. case SQLITE_NULL:
  239. Val = new std::string( "" );
  240. break;
  241. default:
  242. Val = new std::string( "" );
  243. break;
  244. };
  245. if( Val != NULL ){
  246. //(*tElement).insert( pair< string, string > ( *Key, *Val ) );
  247. (*tElement)[*Key]=*Val;
  248. } else {
  249. LOG_ERROR( "Val Alloc Error" );
  250. };
  251. } else {
  252. LOG_ERROR( "Key Alloc Error" );
  253. };
  254. };
  255. RetVal->push_back( tElement );
  256. };
  257. };
  258. };
  259. sqlite3_finalize( query );
  260. if( !m_bPersistentConnection ){
  261. if( !Disconnect() ){
  262. m_mAccessMutex.unlock();
  263. return RetVal;
  264. };
  265. };
  266. m_mAccessMutex.unlock();
  267. return RetVal;
  268. };
  269. #include "../../components/base/basicFunctionality.h"
  270. CSTR SQLite_Connector::escapeString( CSTR value ){
  271. //CSTR buffer = NULL;
  272. int v_len = strlen(value);
  273. CSTR result = (CSTR) malloc( 3 * v_len );
  274. if( result ){
  275. /*
  276. int idx = 0;
  277. for( int i = 0; i < v_len; i++ ){
  278. //If character is printable
  279. if(isprint( value[i] ) != 0){
  280. strncat( result+idx, value+i, 1 );
  281. idx++;
  282. }else{
  283. char tB[4];
  284. snprintf( tB, 4, "#%02X", value[i]);
  285. strncat( result+idx, tB, 3 );
  286. idx+=3;
  287. };
  288. };
  289. buffer = ( CSTR ) malloc( idx+1 );
  290. if( buffer ) {
  291. strncat( buffer, result, idx );
  292. buffer[idx] = (char)0;
  293. };
  294. free(result);
  295. */
  296. escape_string( result, 3*v_len, value, v_len );
  297. };
  298. return result;
  299. };
  300. CSTR SQLite_Connector::escapeBinary( uint8_t *value, uint32_t Size ){
  301. CSTR buffer = NULL;
  302. if ( m_bIsConnected ){
  303. int v_len = Size;
  304. CSTR result = ( CSTR ) malloc( 3 * v_len );
  305. if( result ){
  306. int idx = 0;
  307. for( int i = 0; i < v_len; i++ ){
  308. //If character is printable
  309. if(isprint( value[i] ) != 0){
  310. memcpy( result+idx, value+i, 1 );
  311. idx++;
  312. }else{
  313. char tB[4];
  314. snprintf( tB, 4, "#%02X", value[i]);
  315. strncat( result+idx, tB, 3 );
  316. idx+=3;
  317. };
  318. };
  319. buffer = ( CSTR ) malloc( idx+1 );
  320. if( buffer ) {
  321. strncat( buffer, result, idx );
  322. buffer[idx] = (char)0;
  323. };
  324. free(result);
  325. };
  326. };
  327. return buffer;
  328. };
  329. uint64_t SQLite_Connector::QueryID( CSTR QS ){
  330. uint64_t RetVal = 0;
  331. uint64_t tVal = 0;
  332. if ( m_bIsConnected ) {
  333. Assoc * tElement;
  334. unsigned int num_Fields;
  335. sqlite3_stmt * query = NULL;
  336. char temp[256];
  337. int QSlen = strlen( QS );
  338. m_mAccessMutex.lock();
  339. if( !m_bPersistentConnection ){
  340. if( !ReConnect() ){
  341. m_mAccessMutex.unlock();
  342. return RetVal;
  343. };
  344. };
  345. if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, &query, NULL ) ){
  346. m_mAccessMutex.unlock();
  347. return RetVal;
  348. };
  349. #ifdef DEBUG_EXTRA
  350. LOG( sqlite3_expanded_sql( query ) );
  351. #endif
  352. tVal = sqlite3_last_insert_rowid( m_pDBLink );
  353. if( SQLITE_DONE != sqlite3_step( query )){
  354. sqlite3_finalize( query );
  355. m_mAccessMutex.unlock();
  356. return RetVal;
  357. };
  358. RetVal = sqlite3_last_insert_rowid( m_pDBLink );
  359. sqlite3_finalize( query );
  360. if( !m_bPersistentConnection ){
  361. if( !Disconnect() ){
  362. m_mAccessMutex.unlock();
  363. return RetVal;
  364. };
  365. };
  366. m_mAccessMutex.unlock();
  367. if( RetVal == tVal ) RetVal = 0;
  368. };
  369. return RetVal;
  370. };
  371. void SQLite_Connector::deleteResult( AAssoc *&R ){
  372. if( R ){
  373. while( R->size() ){
  374. delete( (Assoc *) R->at(0) );
  375. R->erase( R->begin());
  376. };
  377. R->clear();
  378. delete( R );
  379. R = NULL;
  380. };
  381. };