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.

315 lines
8.5 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 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. 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. #include "../../components/base/basicFunctionality.h"
  179. CSTR SQLite_Connector::escapeString( CSTR value ){
  180. //CSTR buffer = NULL;
  181. int v_len = strlen(value);
  182. CSTR result = (CSTR) malloc( 3 * v_len );
  183. if( result ){
  184. /*
  185. int idx = 0;
  186. for( int i = 0; i < v_len; i++ ){
  187. //If character is printable
  188. if(isprint( value[i] ) != 0){
  189. strncat( result+idx, value+i, 1 );
  190. idx++;
  191. }else{
  192. char tB[4];
  193. snprintf( tB, 4, "#%02X", value[i]);
  194. strncat( result+idx, tB, 3 );
  195. idx+=3;
  196. };
  197. };
  198. buffer = ( CSTR ) malloc( idx+1 );
  199. if( buffer ) {
  200. strncat( buffer, result, idx );
  201. buffer[idx] = (char)0;
  202. };
  203. free(result);
  204. */
  205. escape_string( result, 3*v_len, value, v_len );
  206. };
  207. return result;
  208. };
  209. CSTR SQLite_Connector::escapeBinary( uint8_t *value, uint32_t Size ){
  210. CSTR buffer = NULL;
  211. if ( m_bIsConnected ){
  212. int v_len = Size;
  213. CSTR result = ( CSTR ) malloc( 3 * v_len );
  214. if( result ){
  215. int idx = 0;
  216. for( int i = 0; i < v_len; i++ ){
  217. //If character is printable
  218. if(isprint( value[i] ) != 0){
  219. memcpy( result+idx, value+i, 1 );
  220. idx++;
  221. }else{
  222. char tB[4];
  223. snprintf( tB, 4, "#%02X", value[i]);
  224. strncat( result+idx, tB, 3 );
  225. idx+=3;
  226. };
  227. };
  228. buffer = ( CSTR ) malloc( idx+1 );
  229. if( buffer ) {
  230. strncat( buffer, result, idx );
  231. buffer[idx] = (char)0;
  232. };
  233. free(result);
  234. };
  235. };
  236. return buffer;
  237. };
  238. uint64_t SQLite_Connector::QueryID( CSTR QS ){
  239. uint64_t RetVal = 0;
  240. uint64_t tVal = 0;
  241. if ( m_bIsConnected ) {
  242. Assoc * tElement;
  243. unsigned int num_Fields;
  244. sqlite3_stmt * query = NULL;
  245. char temp[256];
  246. int QSlen = strlen( QS );
  247. m_mAccessMutex.lock();
  248. if( !m_bPersistentConnection ){
  249. if( !ReConnect() ){
  250. m_mAccessMutex.unlock();
  251. return RetVal;
  252. };
  253. };
  254. if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, &query, NULL ) ){
  255. m_mAccessMutex.unlock();
  256. return RetVal;
  257. };
  258. #ifdef DEBUG_EXTRA
  259. LOG( sqlite3_expanded_sql( query ) );
  260. #endif
  261. tVal = sqlite3_last_insert_rowid( m_pDBLink );
  262. if( SQLITE_DONE != sqlite3_step( query )){
  263. sqlite3_finalize( query );
  264. m_mAccessMutex.unlock();
  265. return false;
  266. };
  267. RetVal = sqlite3_last_insert_rowid( m_pDBLink );
  268. sqlite3_finalize( query );
  269. if( !m_bPersistentConnection ){
  270. if( !Disconnect() ){
  271. m_mAccessMutex.unlock();
  272. return RetVal;
  273. };
  274. };
  275. m_mAccessMutex.unlock();
  276. if( RetVal == tVal ) RetVal = 0;
  277. };
  278. return RetVal;
  279. };
  280. void SQLite_Connector::deleteResult( AAssoc *&R ){
  281. if( R ){
  282. while( R->size() ){
  283. delete( (Assoc *) R->at(0) );
  284. R->erase( R->begin());
  285. };
  286. R->clear();
  287. delete( R );
  288. R = NULL;
  289. };
  290. };