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. if (useCache)useCache=false;
  249. int QSlen = strlen(QS);
  250. m_mAccessMutex.lock();
  251. if (! m_bPersistentConnection)
  252. {
  253. if (! ReConnect())
  254. {
  255. m_mAccessMutex.unlock();
  256. return RetVal;
  257. };
  258. };
  259. if (SQLITE_OK != sqlite3_prepare_v2(m_pDBLink, QS, QSlen, &query, NULL))
  260. {
  261. m_mAccessMutex.unlock();
  262. return RetVal;
  263. };
  264. #ifdef DEBUG_ALL
  265. LOG(sqlite3_expanded_sql(query));
  266. #endif
  267. RetVal = new AAssoc();
  268. while (sqlite3_step(query) != SQLITE_DONE)
  269. {
  270. string Key;
  271. string Val;
  272. uint32_t num_cols = sqlite3_column_count(query);
  273. if (num_cols)
  274. {
  275. tElement = new Assoc();
  276. if (tElement)
  277. {
  278. for (uint32_t i = 0; i < num_cols; i++)
  279. {
  280. Key = string(sqlite3_column_name(query, i));
  281. switch (sqlite3_column_type(query, i))
  282. {
  283. case SQLITE_INTEGER: Val = std::string((char *) sqlite3_column_text(query, i)); break;
  284. case SQLITE_FLOAT : Val = std::string((char *) sqlite3_column_text(query, i)); break;
  285. case SQLITE3_TEXT : Val = std::string((char *) sqlite3_column_text(query, i)); break;
  286. };
  287. (*tElement)[Key] = Val;
  288. };
  289. RetVal->push_back(tElement);
  290. };
  291. };
  292. };
  293. sqlite3_finalize(query);
  294. if (! m_bPersistentConnection)
  295. {
  296. if (! Disconnect())
  297. {
  298. m_mAccessMutex.unlock();
  299. return RetVal;
  300. };
  301. };
  302. m_mAccessMutex.unlock();
  303. return RetVal;
  304. }
  305. #include "../../components/base/basicFunctionality.h"
  306. CSTR SQLite_Connector::escapeString(CSTR value)
  307. {
  308. // CSTR buffer = NULL;
  309. int v_len = strlen(value);
  310. CSTR result = (CSTR) malloc(3 * v_len);
  311. if (result)
  312. {
  313. /*
  314. int idx = 0;
  315. for( int i = 0; i < v_len; i++ ){
  316. //If character is printable
  317. if(isprint( value[i] ) != 0){
  318. strncat( result+idx, value+i, 1 );
  319. idx++;
  320. }else{
  321. char tB[4];
  322. snprintf( tB, 4, "#%02X", value[i]);
  323. strncat( result+idx, tB, 3 );
  324. idx+=3;
  325. };
  326. };
  327. buffer = ( CSTR ) malloc( idx+1 );
  328. if( buffer ) {
  329. strncat( buffer, result, idx );
  330. buffer[idx] = (char)0;
  331. };
  332. free(result);
  333. */
  334. escape_string(result, 3 * v_len, value, v_len);
  335. };
  336. return result;
  337. }
  338. CSTR SQLite_Connector::escapeBinary(uint8_t * value, uint32_t Size)
  339. {
  340. CSTR buffer = NULL;
  341. if (m_bIsConnected)
  342. {
  343. int v_len = Size;
  344. CSTR result = (CSTR) malloc(3 * v_len);
  345. if (result)
  346. {
  347. int idx = 0;
  348. for (int i = 0; i < v_len; i++)
  349. {
  350. // If character is printable
  351. if (isprint(value[i]) != 0)
  352. {
  353. memcpy(result + idx, value + i, 1);
  354. idx++;
  355. }
  356. else
  357. {
  358. char tB[4];
  359. snprintf(tB, 4, "#%02X", value[i]);
  360. strncat(result + idx, tB, 3);
  361. idx += 3;
  362. };
  363. };
  364. buffer = (CSTR) malloc(idx + 1);
  365. if (buffer)
  366. {
  367. strncat(buffer, result, idx);
  368. buffer[idx] = (char) 0;
  369. };
  370. free(result);
  371. };
  372. };
  373. return buffer;
  374. }
  375. uint64_t SQLite_Connector::QueryID(CSTR QS)
  376. {
  377. uint64_t RetVal = 0;
  378. uint64_t tVal = 0;
  379. if (m_bIsConnected)
  380. {
  381. //Assoc * tElement;
  382. //unsigned int num_Fields;
  383. sqlite3_stmt * query = NULL;
  384. //char temp[256];
  385. int QSlen = strlen(QS);
  386. m_mAccessMutex.lock();
  387. if (! m_bPersistentConnection)
  388. {
  389. if (! ReConnect())
  390. {
  391. m_mAccessMutex.unlock();
  392. return RetVal;
  393. };
  394. };
  395. if (SQLITE_OK != sqlite3_prepare_v2(m_pDBLink, QS, QSlen, &query, NULL))
  396. {
  397. m_mAccessMutex.unlock();
  398. return RetVal;
  399. };
  400. #ifdef DEBUG_ALL
  401. LOG(sqlite3_expanded_sql(query));
  402. #endif
  403. tVal = sqlite3_last_insert_rowid(m_pDBLink);
  404. if (SQLITE_DONE != sqlite3_step(query))
  405. {
  406. sqlite3_finalize(query);
  407. m_mAccessMutex.unlock();
  408. return RetVal;
  409. };
  410. RetVal = sqlite3_last_insert_rowid(m_pDBLink);
  411. sqlite3_finalize(query);
  412. if (! m_bPersistentConnection)
  413. {
  414. if (! Disconnect())
  415. {
  416. m_mAccessMutex.unlock();
  417. return RetVal;
  418. };
  419. };
  420. m_mAccessMutex.unlock();
  421. if (RetVal == tVal)
  422. {
  423. RetVal = 0;
  424. }
  425. };
  426. return RetVal;
  427. }
  428. void SQLite_Connector::deleteResult(AAssoc *& R)
  429. {
  430. if (R)
  431. {
  432. while (R->size())
  433. {
  434. delete((Assoc *) R->at(0));
  435. R->erase(R->begin());
  436. };
  437. R->clear();
  438. delete(R);
  439. R = NULL;
  440. };
  441. }