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

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. {
  97. char tB[1024];
  98. sprintf(tB, "(%d) %s", iErrCode, zMsg);
  99. basicLogCollection::getInstance()->Log("SQDB_G", pArg, tB);
  100. }
  101. SQLite_Connector::SQLite_Connector()
  102. {
  103. m_pLog = basicLogCollection::getInstance();
  104. m_bIsConnected = false;
  105. m_bPersistentConnection = false;
  106. m_pDBLink = NULL;
  107. if (! sqlite_global_config_is_set)
  108. {
  109. // if( SQLITE_OK != sqlite3_config(int, ...) ){ SQLITE_MISUSE
  110. sqlite3_config(SQLITE_CONFIG_LOG, errorLogCallback, this);
  111. sqlite3_initialize();
  112. sqlite_global_config_is_set = true;
  113. };
  114. sqlite_global_inctances_count++;
  115. }
  116. SQLite_Connector::~SQLite_Connector()
  117. {
  118. m_bIsConnected = ! Disconnect();
  119. if (sqlite_global_inctances_count == 1)
  120. {
  121. sqlite3_shutdown();
  122. };
  123. sqlite_global_inctances_count--;
  124. }
  125. bool SQLite_Connector::Connect(CSTR FileName, int AUX_FLAGS, bool Persistent)
  126. {
  127. m_mAccessMutex.lock();
  128. m_bPersistentConnection = Persistent;
  129. if (m_bIsConnected)
  130. {
  131. #ifdef DEBUG_ALL
  132. LOG("Connected -> Disconnect!");
  133. #endif
  134. m_bIsConnected = false;
  135. if (m_pDBLink != NULL)
  136. {
  137. if (SQLITE_OK != sqlite3_close(m_pDBLink))
  138. {
  139. m_mAccessMutex.unlock();
  140. return false;
  141. };
  142. };
  143. };
  144. sprintf(m_pDBName, "%s", FileName);
  145. m_nFLAGS = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | AUX_FLAGS;
  146. if (m_bPersistentConnection)
  147. {
  148. #ifdef DEBUG_ALL
  149. LOG("Making Connection....");
  150. #endif
  151. if (SQLITE_OK == sqlite3_open_v2(m_pDBName, &m_pDBLink, m_nFLAGS, NULL))
  152. {
  153. m_bIsConnected = true;
  154. }
  155. #ifdef DEBUG_ALL
  156. LOG("Done!");
  157. #endif
  158. }
  159. else
  160. {
  161. m_bIsConnected = true;
  162. };
  163. m_mAccessMutex.unlock();
  164. return m_bIsConnected;
  165. }
  166. bool SQLite_Connector::ReConnect(void)
  167. {
  168. if (m_pDBLink != NULL)
  169. {
  170. if (SQLITE_OK != sqlite3_close(m_pDBLink))
  171. {
  172. return false;
  173. }
  174. }
  175. if (SQLITE_OK == sqlite3_open_v2(m_pDBName, &m_pDBLink, m_nFLAGS, NULL))
  176. {
  177. return true;
  178. }
  179. return false;
  180. }
  181. bool SQLite_Connector::Disconnect(void)
  182. {
  183. if (m_pDBLink != NULL)
  184. {
  185. if (SQLITE_OK != sqlite3_close(m_pDBLink))
  186. {
  187. return false;
  188. }
  189. }
  190. m_pDBLink = NULL;
  191. return true;
  192. }
  193. bool SQLite_Connector::SilentQuery(CSTR QS)
  194. {
  195. if (m_bIsConnected)
  196. {
  197. int QSlen = strlen(QS);
  198. m_mAccessMutex.lock();
  199. if (! m_bPersistentConnection)
  200. {
  201. if (! ReConnect())
  202. {
  203. m_mAccessMutex.unlock();
  204. return false;
  205. };
  206. };
  207. sqlite3_stmt * query = NULL;
  208. // if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, NULL, NULL ) ){
  209. if (SQLITE_OK != sqlite3_prepare_v2(m_pDBLink, QS, QSlen, &query, NULL))
  210. {
  211. m_mAccessMutex.unlock();
  212. return false;
  213. };
  214. #ifdef DEBUG_ALL
  215. LOG(sqlite3_expanded_sql(query));
  216. #endif
  217. if (SQLITE_DONE != sqlite3_step(query))
  218. {
  219. sqlite3_finalize(query);
  220. m_mAccessMutex.unlock();
  221. return false;
  222. };
  223. sqlite3_finalize(query);
  224. if (! m_bPersistentConnection)
  225. {
  226. if (! Disconnect())
  227. {
  228. m_mAccessMutex.unlock();
  229. return false;
  230. };
  231. };
  232. m_mAccessMutex.unlock();
  233. return true;
  234. }
  235. else
  236. {
  237. LOG_ERROR("No Connection to DB!");
  238. };
  239. return false;
  240. }
  241. AAssoc * SQLite_Connector::Query(CSTR QS, bool useCache)
  242. {
  243. AAssoc * RetVal = NULL;
  244. Assoc * tElement;
  245. unsigned int num_Fields;
  246. sqlite3_stmt * query = NULL;
  247. char temp[256];
  248. int QSlen = strlen(QS);
  249. m_mAccessMutex.lock();
  250. if (! m_bPersistentConnection)
  251. {
  252. if (! ReConnect())
  253. {
  254. m_mAccessMutex.unlock();
  255. return RetVal;
  256. };
  257. };
  258. if (SQLITE_OK != sqlite3_prepare_v2(m_pDBLink, QS, QSlen, &query, NULL))
  259. {
  260. m_mAccessMutex.unlock();
  261. return RetVal;
  262. };
  263. #ifdef DEBUG_ALL
  264. LOG(sqlite3_expanded_sql(query));
  265. #endif
  266. RetVal = new AAssoc();
  267. while (sqlite3_step(query) != SQLITE_DONE)
  268. {
  269. string Key;
  270. string Val;
  271. uint32_t num_cols = sqlite3_column_count(query);
  272. if (num_cols)
  273. {
  274. tElement = new Assoc();
  275. if (tElement)
  276. {
  277. for (uint32_t i = 0; i < num_cols; i++)
  278. {
  279. Key = string(sqlite3_column_name(query, i));
  280. switch (sqlite3_column_type(query, i))
  281. {
  282. case SQLITE_INTEGER: Val = std::string((char *) sqlite3_column_text(query, i)); break;
  283. case SQLITE_FLOAT : Val = std::string((char *) sqlite3_column_text(query, i)); break;
  284. case SQLITE3_TEXT : Val = std::string((char *) sqlite3_column_text(query, i)); break;
  285. };
  286. (*tElement)[Key] = Val;
  287. };
  288. RetVal->push_back(tElement);
  289. };
  290. };
  291. };
  292. sqlite3_finalize(query);
  293. if (! m_bPersistentConnection)
  294. {
  295. if (! Disconnect())
  296. {
  297. m_mAccessMutex.unlock();
  298. return RetVal;
  299. };
  300. };
  301. m_mAccessMutex.unlock();
  302. return RetVal;
  303. }
  304. #include "../../components/base/basicFunctionality.h"
  305. CSTR SQLite_Connector::escapeString(CSTR value)
  306. {
  307. // CSTR buffer = NULL;
  308. int v_len = strlen(value);
  309. CSTR result = (CSTR) malloc(3 * v_len);
  310. if (result)
  311. {
  312. /*
  313. int idx = 0;
  314. for( int i = 0; i < v_len; i++ ){
  315. //If character is printable
  316. if(isprint( value[i] ) != 0){
  317. strncat( result+idx, value+i, 1 );
  318. idx++;
  319. }else{
  320. char tB[4];
  321. snprintf( tB, 4, "#%02X", value[i]);
  322. strncat( result+idx, tB, 3 );
  323. idx+=3;
  324. };
  325. };
  326. buffer = ( CSTR ) malloc( idx+1 );
  327. if( buffer ) {
  328. strncat( buffer, result, idx );
  329. buffer[idx] = (char)0;
  330. };
  331. free(result);
  332. */
  333. escape_string(result, 3 * v_len, value, v_len);
  334. };
  335. return result;
  336. }
  337. CSTR SQLite_Connector::escapeBinary(uint8_t * value, uint32_t Size)
  338. {
  339. CSTR buffer = NULL;
  340. if (m_bIsConnected)
  341. {
  342. int v_len = Size;
  343. CSTR result = (CSTR) malloc(3 * v_len);
  344. if (result)
  345. {
  346. int idx = 0;
  347. for (int i = 0; i < v_len; i++)
  348. {
  349. // If character is printable
  350. if (isprint(value[i]) != 0)
  351. {
  352. memcpy(result + idx, value + i, 1);
  353. idx++;
  354. }
  355. else
  356. {
  357. char tB[4];
  358. snprintf(tB, 4, "#%02X", value[i]);
  359. strncat(result + idx, tB, 3);
  360. idx += 3;
  361. };
  362. };
  363. buffer = (CSTR) malloc(idx + 1);
  364. if (buffer)
  365. {
  366. strncat(buffer, result, idx);
  367. buffer[idx] = (char) 0;
  368. };
  369. free(result);
  370. };
  371. };
  372. return buffer;
  373. }
  374. uint64_t SQLite_Connector::QueryID(CSTR QS)
  375. {
  376. uint64_t RetVal = 0;
  377. uint64_t tVal = 0;
  378. if (m_bIsConnected)
  379. {
  380. Assoc * tElement;
  381. unsigned int num_Fields;
  382. sqlite3_stmt * query = NULL;
  383. char temp[256];
  384. int QSlen = strlen(QS);
  385. m_mAccessMutex.lock();
  386. if (! m_bPersistentConnection)
  387. {
  388. if (! ReConnect())
  389. {
  390. m_mAccessMutex.unlock();
  391. return RetVal;
  392. };
  393. };
  394. if (SQLITE_OK != sqlite3_prepare_v2(m_pDBLink, QS, QSlen, &query, NULL))
  395. {
  396. m_mAccessMutex.unlock();
  397. return RetVal;
  398. };
  399. #ifdef DEBUG_ALL
  400. LOG(sqlite3_expanded_sql(query));
  401. #endif
  402. tVal = sqlite3_last_insert_rowid(m_pDBLink);
  403. if (SQLITE_DONE != sqlite3_step(query))
  404. {
  405. sqlite3_finalize(query);
  406. m_mAccessMutex.unlock();
  407. return RetVal;
  408. };
  409. RetVal = sqlite3_last_insert_rowid(m_pDBLink);
  410. sqlite3_finalize(query);
  411. if (! m_bPersistentConnection)
  412. {
  413. if (! Disconnect())
  414. {
  415. m_mAccessMutex.unlock();
  416. return RetVal;
  417. };
  418. };
  419. m_mAccessMutex.unlock();
  420. if (RetVal == tVal)
  421. {
  422. RetVal = 0;
  423. }
  424. };
  425. return RetVal;
  426. }
  427. void SQLite_Connector::deleteResult(AAssoc *& R)
  428. {
  429. if (R)
  430. {
  431. while (R->size())
  432. {
  433. delete((Assoc *) R->at(0));
  434. R->erase(R->begin());
  435. };
  436. R->clear();
  437. delete(R);
  438. R = NULL;
  439. };
  440. }