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.

402 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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 = string( sqlite3_column_name( query, i ) );
  230. switch( sqlite3_column_type( query, i ) ){
  231. case SQLITE_INTEGER:
  232. Val = std::string( (char*) sqlite3_column_text( query, i ) );
  233. break;
  234. case SQLITE_FLOAT:
  235. Val = std::string( (char*) sqlite3_column_text( query, i ) );
  236. break;
  237. case SQLITE3_TEXT:
  238. Val = std::string( (char*) sqlite3_column_text( query, i ) );
  239. break;
  240. };
  241. (*tElement)[Key]=Val;
  242. };
  243. RetVal->push_back( tElement );
  244. };
  245. };
  246. };
  247. sqlite3_finalize( query );
  248. if( !m_bPersistentConnection ){
  249. if( !Disconnect() ){
  250. m_mAccessMutex.unlock();
  251. return RetVal;
  252. };
  253. };
  254. m_mAccessMutex.unlock();
  255. return RetVal;
  256. };
  257. #include "../../components/base/basicFunctionality.h"
  258. CSTR SQLite_Connector::escapeString( CSTR value ){
  259. //CSTR buffer = NULL;
  260. int v_len = strlen(value);
  261. CSTR result = (CSTR) malloc( 3 * v_len );
  262. if( result ){
  263. /*
  264. int idx = 0;
  265. for( int i = 0; i < v_len; i++ ){
  266. //If character is printable
  267. if(isprint( value[i] ) != 0){
  268. strncat( result+idx, value+i, 1 );
  269. idx++;
  270. }else{
  271. char tB[4];
  272. snprintf( tB, 4, "#%02X", value[i]);
  273. strncat( result+idx, tB, 3 );
  274. idx+=3;
  275. };
  276. };
  277. buffer = ( CSTR ) malloc( idx+1 );
  278. if( buffer ) {
  279. strncat( buffer, result, idx );
  280. buffer[idx] = (char)0;
  281. };
  282. free(result);
  283. */
  284. escape_string( result, 3*v_len, value, v_len );
  285. };
  286. return result;
  287. };
  288. CSTR SQLite_Connector::escapeBinary( uint8_t *value, uint32_t Size ){
  289. CSTR buffer = NULL;
  290. if ( m_bIsConnected ){
  291. int v_len = Size;
  292. CSTR result = ( CSTR ) malloc( 3 * v_len );
  293. if( result ){
  294. int idx = 0;
  295. for( int i = 0; i < v_len; i++ ){
  296. //If character is printable
  297. if(isprint( value[i] ) != 0){
  298. memcpy( result+idx, value+i, 1 );
  299. idx++;
  300. }else{
  301. char tB[4];
  302. snprintf( tB, 4, "#%02X", value[i]);
  303. strncat( result+idx, tB, 3 );
  304. idx+=3;
  305. };
  306. };
  307. buffer = ( CSTR ) malloc( idx+1 );
  308. if( buffer ) {
  309. strncat( buffer, result, idx );
  310. buffer[idx] = (char)0;
  311. };
  312. free(result);
  313. };
  314. };
  315. return buffer;
  316. };
  317. uint64_t SQLite_Connector::QueryID( CSTR QS ){
  318. uint64_t RetVal = 0;
  319. uint64_t tVal = 0;
  320. if ( m_bIsConnected ) {
  321. Assoc * tElement;
  322. unsigned int num_Fields;
  323. sqlite3_stmt * query = NULL;
  324. char temp[256];
  325. int QSlen = strlen( QS );
  326. m_mAccessMutex.lock();
  327. if( !m_bPersistentConnection ){
  328. if( !ReConnect() ){
  329. m_mAccessMutex.unlock();
  330. return RetVal;
  331. };
  332. };
  333. if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, &query, NULL ) ){
  334. m_mAccessMutex.unlock();
  335. return RetVal;
  336. };
  337. #ifdef DEBUG_ALL
  338. LOG( sqlite3_expanded_sql( query ) );
  339. #endif
  340. tVal = sqlite3_last_insert_rowid( m_pDBLink );
  341. if( SQLITE_DONE != sqlite3_step( query )){
  342. sqlite3_finalize( query );
  343. m_mAccessMutex.unlock();
  344. return RetVal;
  345. };
  346. RetVal = sqlite3_last_insert_rowid( m_pDBLink );
  347. sqlite3_finalize( query );
  348. if( !m_bPersistentConnection ){
  349. if( !Disconnect() ){
  350. m_mAccessMutex.unlock();
  351. return RetVal;
  352. };
  353. };
  354. m_mAccessMutex.unlock();
  355. if( RetVal == tVal ) RetVal = 0;
  356. };
  357. return RetVal;
  358. };
  359. void SQLite_Connector::deleteResult( AAssoc *&R ){
  360. if( R ){
  361. while( R->size() ){
  362. delete( (Assoc *) R->at(0) );
  363. R->erase( R->begin());
  364. };
  365. R->clear();
  366. delete( R );
  367. R = NULL;
  368. };
  369. };