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.

310 lines
8.4 KiB

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. static void errorLogCallback( void *pArg, int iErrCode, const char *zMsg ){
  7. char tB[1024];
  8. sprintf( tB, "(%d) %s", iErrCode, zMsg );
  9. basicLogCollection::getInstance()->Log( "SQDB_G", pArg, tB );
  10. };
  11. SQLite_Connector::SQLite_Connector(){
  12. m_pLog = basicLogCollection::getInstance();
  13. m_bIsConnected = false;
  14. m_bPersistentConnection = false;
  15. m_pDBLink = NULL;
  16. if( !sqlite_global_config_is_set ){
  17. //if( SQLITE_OK != sqlite3_config(int, ...) ){ SQLITE_MISUSE
  18. sqlite3_config( SQLITE_CONFIG_LOG, errorLogCallback, this );
  19. sqlite3_initialize();
  20. sqlite_global_config_is_set = true;
  21. };
  22. sqlite_global_inctances_count++;
  23. };
  24. SQLite_Connector::~SQLite_Connector(){
  25. m_bIsConnected = !Disconnect();
  26. if( sqlite_global_inctances_count == 1 ){
  27. sqlite3_shutdown();
  28. };
  29. sqlite_global_inctances_count--;
  30. };
  31. bool SQLite_Connector::Connect( CSTR FileName, int AUX_FLAGS, bool Persistent ){
  32. m_mAccessMutex.lock();
  33. m_bPersistentConnection = Persistent;
  34. if( m_bIsConnected ){
  35. LOG("Connected -> Disconnect!");
  36. m_bIsConnected = false;
  37. if( m_pDBLink != NULL ){
  38. if( SQLITE_OK != sqlite3_close( m_pDBLink ) ) {
  39. m_mAccessMutex.unlock();
  40. return false;
  41. };
  42. };
  43. };
  44. sprintf( m_pDBName, "%s", FileName );
  45. m_nFLAGS = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | AUX_FLAGS;
  46. if( m_bPersistentConnection ){
  47. LOG("Making Connection....");
  48. if( SQLITE_OK == sqlite3_open_v2( m_pDBName, &m_pDBLink, m_nFLAGS, NULL ) ) m_bIsConnected = true;
  49. LOG("Done!");
  50. } else {
  51. m_bIsConnected = true;
  52. };
  53. m_mAccessMutex.unlock();
  54. return m_bIsConnected;
  55. };
  56. bool SQLite_Connector::ReConnect( void ){
  57. if( m_pDBLink != NULL ) if( SQLITE_OK != sqlite3_close( m_pDBLink ) ) return false;
  58. if( SQLITE_OK == sqlite3_open_v2( m_pDBName, &m_pDBLink, m_nFLAGS, NULL ) ) return true;
  59. return false;
  60. };
  61. bool SQLite_Connector::Disconnect( void ){
  62. if( m_pDBLink != NULL ) if( SQLITE_OK != sqlite3_close( m_pDBLink ) ) return false;
  63. m_pDBLink = NULL;
  64. return true;
  65. };
  66. bool SQLite_Connector::SilentQuery( CSTR QS ){
  67. if ( m_bIsConnected ) {
  68. int QSlen = strlen( QS );
  69. m_mAccessMutex.lock();
  70. if( !m_bPersistentConnection ){
  71. if( !ReConnect() ){
  72. m_mAccessMutex.unlock();
  73. return false;
  74. };
  75. };
  76. sqlite3_stmt * query = NULL;
  77. //if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, NULL, NULL ) ){
  78. if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, &query, NULL ) ){
  79. m_mAccessMutex.unlock();
  80. return false;
  81. };
  82. #ifdef DEBUG_EXTRA
  83. LOG( sqlite3_expanded_sql( query ) );
  84. #endif
  85. if( SQLITE_DONE != sqlite3_step( query )){
  86. sqlite3_finalize( query );
  87. m_mAccessMutex.unlock();
  88. return false;
  89. };
  90. sqlite3_finalize( query );
  91. if( !m_bPersistentConnection ){
  92. if( !Disconnect() ){
  93. m_mAccessMutex.unlock();
  94. return false;
  95. };
  96. };
  97. m_mAccessMutex.unlock();
  98. return true;
  99. };
  100. return false;
  101. };
  102. AAssoc *SQLite_Connector::Query( CSTR QS, bool useCache ){
  103. AAssoc * RetVal = NULL;
  104. Assoc * tElement;
  105. unsigned int num_Fields;
  106. sqlite3_stmt * query = NULL;
  107. char temp[256];
  108. int QSlen = strlen( QS );
  109. m_mAccessMutex.lock();
  110. if( !m_bPersistentConnection ){
  111. if( !ReConnect() ){
  112. m_mAccessMutex.unlock();
  113. return RetVal;
  114. };
  115. };
  116. if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, &query, NULL ) ){
  117. m_mAccessMutex.unlock();
  118. return RetVal;
  119. };
  120. #ifdef DEBUG_EXTRA
  121. LOG( sqlite3_expanded_sql( query ) );
  122. #endif
  123. RetVal = new AAssoc();
  124. while( sqlite3_step( query ) != SQLITE_DONE ){
  125. string *Key;
  126. string *Val;
  127. uint32_t num_cols = sqlite3_column_count( query );
  128. if( num_cols ){
  129. tElement = new Assoc();
  130. if( tElement ){
  131. for( uint32_t i = 0; i < num_cols; i++ ){
  132. Key = NULL;
  133. Val = NULL;
  134. Key = new string( sqlite3_column_name( query, i ) );
  135. if( Key!= NULL ) {
  136. switch( sqlite3_column_type( query, i ) ){
  137. case SQLITE_INTEGER:
  138. Val = new std::string( (char*) sqlite3_column_text( query, i ) );
  139. break;
  140. case SQLITE_FLOAT:
  141. Val = new std::string( (char*) sqlite3_column_text( query, i ) );
  142. break;
  143. case SQLITE3_TEXT:
  144. Val = new std::string( (char*) sqlite3_column_text( query, i ) );
  145. break;
  146. case SQLITE_BLOB:
  147. case SQLITE_NULL:
  148. Val = new std::string( "" );
  149. break;
  150. default:
  151. Val = new std::string( "" );
  152. break;
  153. };
  154. if( Val != NULL ){
  155. //(*tElement).insert( pair< string, string > ( *Key, *Val ) );
  156. (*tElement)[*Key]=*Val;
  157. } else {
  158. LOG_ERROR( "Val Alloc Error" );
  159. };
  160. } else {
  161. LOG_ERROR( "Key Alloc Error" );
  162. };
  163. };
  164. RetVal->push_back( tElement );
  165. };
  166. };
  167. };
  168. sqlite3_finalize( query );
  169. if( !m_bPersistentConnection ){
  170. if( !Disconnect() ){
  171. m_mAccessMutex.unlock();
  172. return RetVal;
  173. };
  174. };
  175. m_mAccessMutex.unlock();
  176. return RetVal;
  177. };
  178. CSTR SQLite_Connector::escapeString( CSTR value ){
  179. CSTR buffer = NULL;
  180. int v_len = strlen(value);
  181. CSTR result = (CSTR) malloc( 3 * v_len );
  182. if( result ){
  183. int idx = 0;
  184. for( int i = 0; i < v_len; i++ ){
  185. //If character is printable
  186. if(isprint( value[i] ) != 0){
  187. strncat( result+idx, value+i, 1 );
  188. idx++;
  189. }else{
  190. char tB[4];
  191. snprintf( tB, 4, "#%02X", value[i]);
  192. strncat( result+idx, tB, 3 );
  193. idx+=3;
  194. };
  195. };
  196. buffer = ( CSTR ) malloc( idx+1 );
  197. if( buffer ) {
  198. strncat( buffer, result, idx );
  199. buffer[idx] = (char)0;
  200. };
  201. free(result);
  202. };
  203. return buffer;
  204. };
  205. CSTR SQLite_Connector::escapeBinary( uint8_t *value, uint32_t Size ){
  206. CSTR buffer = NULL;
  207. if ( m_bIsConnected ){
  208. int v_len = Size;
  209. CSTR result = ( CSTR ) malloc( 3 * v_len );
  210. if( result ){
  211. int idx = 0;
  212. for( int i = 0; i < v_len; i++ ){
  213. //If character is printable
  214. if(isprint( value[i] ) != 0){
  215. memcpy( result+idx, value+i, 1 );
  216. idx++;
  217. }else{
  218. char tB[4];
  219. snprintf( tB, 4, "#%02X", value[i]);
  220. strncat( result+idx, tB, 3 );
  221. idx+=3;
  222. };
  223. };
  224. buffer = ( CSTR ) malloc( idx+1 );
  225. if( buffer ) {
  226. strncat( buffer, result, idx );
  227. buffer[idx] = (char)0;
  228. };
  229. free(result);
  230. };
  231. };
  232. return buffer;
  233. };
  234. uint64_t SQLite_Connector::QueryID( CSTR QS ){
  235. uint64_t RetVal = 0;
  236. uint64_t tVal = 0;
  237. if ( m_bIsConnected ) {
  238. Assoc * tElement;
  239. unsigned int num_Fields;
  240. sqlite3_stmt * query = NULL;
  241. char temp[256];
  242. int QSlen = strlen( QS );
  243. m_mAccessMutex.lock();
  244. if( !m_bPersistentConnection ){
  245. if( !ReConnect() ){
  246. m_mAccessMutex.unlock();
  247. return RetVal;
  248. };
  249. };
  250. if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, &query, NULL ) ){
  251. m_mAccessMutex.unlock();
  252. return RetVal;
  253. };
  254. #ifdef DEBUG_EXTRA
  255. LOG( sqlite3_expanded_sql( query ) );
  256. #endif
  257. tVal = sqlite3_last_insert_rowid( m_pDBLink );
  258. if( SQLITE_DONE != sqlite3_step( query )){
  259. sqlite3_finalize( query );
  260. m_mAccessMutex.unlock();
  261. return false;
  262. };
  263. RetVal = sqlite3_last_insert_rowid( m_pDBLink );
  264. sqlite3_finalize( query );
  265. if( !m_bPersistentConnection ){
  266. if( !Disconnect() ){
  267. m_mAccessMutex.unlock();
  268. return RetVal;
  269. };
  270. };
  271. m_mAccessMutex.unlock();
  272. if( RetVal == tVal ) RetVal = 0;
  273. };
  274. return RetVal;
  275. };
  276. void SQLite_Connector::deleteResult( AAssoc *&R ){
  277. if( R ){
  278. while( R->size() ){
  279. delete( (Assoc *) R->at(0) );
  280. R->erase( R->begin());
  281. };
  282. R->clear();
  283. delete( R );
  284. R = NULL;
  285. };
  286. };