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.

251 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "IM_DataBaseAdapter.h"
  2. #include "../../lib/alg/generic/md5.h"
  3. #define LOG( X ) m_pLog->Log( "IMDBA", this, X )
  4. #ifdef DEBUG
  5. #define LOG_ERROR( X ) {\
  6. char _tBfrErrMSG234[256];\
  7. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  8. m_pLog->Log( "IMDBA", this, _LOG_ERROR, _tBfrErrMSG234 );\
  9. }
  10. #else
  11. #define LOG_ERROR( X ) m_pLog->Log( "IMDBA", this, _LOG_ERROR, X );
  12. #endif
  13. IM_DataBaseAdapter* IM_DataBaseAdapter::_instance = NULL;
  14. /**
  15. * Returns the single instance of the object.
  16. */
  17. IM_DataBaseAdapter* IM_DataBaseAdapter::getInstance( config_setting_t * Settings ) {
  18. // check if the instance has been created yet
  19. if( NULL == _instance ){
  20. // if not, then create it
  21. _instance = new IM_DataBaseAdapter( Settings );
  22. };
  23. // return the single instance
  24. return _instance;
  25. };
  26. // Constructor implementation
  27. inline IM_DataBaseAdapter::IM_DataBaseAdapter( config_setting_t * Settings ){
  28. m_pLog = basicLogCollection::getInstance();
  29. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DBC" );
  30. m_bIsConnected = false;
  31. if( m_pSettings!= NULL ) {
  32. config_setting_t * tSetting;
  33. char tDBHost[128];
  34. char tDBName[128];
  35. char tDBUser[128];
  36. char tDBPass[128];
  37. uint16_t tDBPort = 3306;
  38. tSetting = config_setting_get_member( m_pSettings, "DATABASE_HOST" );
  39. if( tSetting == NULL ){
  40. tSetting = config_setting_add(m_pSettings, "DATABASE_HOST", CONFIG_TYPE_STRING );
  41. config_setting_set_string( tSetting, "localhost" );
  42. basicSettingsProvider::getInstance()->compromise();
  43. LOG("No DB Host Setting! Using default!");
  44. };
  45. sprintf(tDBHost, "%s", config_setting_get_string(tSetting));
  46. tSetting = config_setting_get_member( m_pSettings, "DATABASE_NAME" );
  47. if( tSetting == NULL ){
  48. tSetting = config_setting_add(m_pSettings, "DATABASE_NAME", CONFIG_TYPE_STRING );
  49. config_setting_set_string( tSetting, "dfsl_gunter" );
  50. basicSettingsProvider::getInstance()->compromise();
  51. LOG("No DB Database Setting! Using default!");
  52. };
  53. sprintf(tDBName, "%s", config_setting_get_string(tSetting));
  54. tSetting = config_setting_get_member( m_pSettings, "DATABASE_USER" );
  55. if( tSetting == NULL ){
  56. tSetting = config_setting_add(m_pSettings, "DATABASE_USER", CONFIG_TYPE_STRING );
  57. config_setting_set_string( tSetting, "dbroot" );
  58. basicSettingsProvider::getInstance()->compromise();
  59. LOG("No DB User Setting! Using default!");
  60. };
  61. sprintf(tDBUser, "%s", config_setting_get_string(tSetting));
  62. tSetting = config_setting_get_member( m_pSettings, "DATABASE_PASSWORD" );
  63. if( tSetting == NULL ){
  64. tSetting = config_setting_add(m_pSettings, "DATABASE_PASSWORD", CONFIG_TYPE_STRING );
  65. config_setting_set_string( tSetting, "123567" );
  66. basicSettingsProvider::getInstance()->compromise();
  67. LOG("No DB Password Setting! Using default!");
  68. };
  69. sprintf(tDBPass, "%s", config_setting_get_string(tSetting));
  70. tSetting = config_setting_get_member( m_pSettings, "DATABASE_PORT" );
  71. if( tSetting == NULL ){
  72. tSetting = config_setting_add(m_pSettings, "DATABASE_PORT", CONFIG_TYPE_INT );
  73. config_setting_set_int( tSetting, 3306 );
  74. basicSettingsProvider::getInstance()->compromise();
  75. LOG("No DB Port Setting! Using default!");
  76. };
  77. tDBPort = config_setting_get_int( tSetting );
  78. basicSettingsProvider::getInstance()->validate();
  79. m_pConnectorClass = new MySQL_Connector( tDBName, tDBHost, tDBUser, tDBPass, tDBPort );
  80. m_bIsConnected = ( m_pConnectorClass != NULL );
  81. } else {
  82. LOG_ERROR("No Settings Provided!");
  83. };
  84. };
  85. // Destructor implementation
  86. inline IM_DataBaseAdapter::~IM_DataBaseAdapter(){
  87. if( m_pConnectorClass ) delete( m_pConnectorClass );
  88. };
  89. IM_ITEM_DATA *IM_DataBaseAdapter::getItemDataById( uint32_t itemID ){
  90. IM_ITEM_DATA *tVal = NULL;
  91. if( m_pConnectorClass ) {
  92. char tQBuf[CTEXT];
  93. AAssoc *result;
  94. sprintf(tQBuf, "SELECT `id`, `md5`, `width`, `height`, `4cc` FROM `items` WHERE `type`=2 AND `id`=%d", itemID );
  95. #ifdef DEBUG
  96. LOG(tQBuf);
  97. #endif
  98. result = m_pConnectorClass->Query( tQBuf );
  99. if( result!= NULL ) {
  100. if( result->size() > 0 ) {
  101. Assoc *cArr;
  102. tVal = (IM_ITEM_DATA*) malloc(sizeof(IM_ITEM_DATA));
  103. if( tVal != NULL ) {
  104. Assoc::iterator it;
  105. memset( tVal, 0, sizeof(IM_ITEM_DATA) );
  106. cArr = result->at(0);
  107. it = cArr->find("id");
  108. if( it != cArr->end() ){
  109. tVal->item_id = atoi( it->second.c_str() );
  110. };
  111. it = cArr->find("md5");
  112. if( it != cArr->end() ){
  113. strncpy( tVal->md5, it->second.c_str(), 32 );
  114. };
  115. it = cArr->find("width");
  116. if( it != cArr->end() ){
  117. tVal->width = atoi( it->second.c_str() );
  118. };
  119. it = cArr->find("height");
  120. if( it != cArr->end() ){
  121. tVal->height = atoi( it->second.c_str() );
  122. };
  123. it = cArr->find("4cc");
  124. if( it != cArr->end() ){
  125. if( strstr( it->second.c_str(),"JPEG" ) ) tVal->type = IM_ITEM_TYPE_JPEG;
  126. if( strstr( it->second.c_str(),"PNG" ) ) tVal->type = IM_ITEM_TYPE_PNG;
  127. if( strstr( it->second.c_str(),"GIF" ) ) tVal->type = IM_ITEM_TYPE_GIF;
  128. if( strstr( it->second.c_str(),"TIFF" ) ) tVal->type = IM_ITEM_TYPE_TIFF;
  129. if( strstr( it->second.c_str(),"AGIF" ) ) tVal->type = IM_ITEM_TYPE_AGIF;
  130. };
  131. };
  132. };
  133. };
  134. deleteResult( result );
  135. };
  136. return tVal;
  137. };
  138. IM_ITEM_DATA *IM_DataBaseAdapter::getItemDataByMd5( CSTR md5 ){
  139. IM_ITEM_DATA *tVal = NULL;
  140. if( m_pConnectorClass ) {
  141. char tQBuf[CTEXT];
  142. AAssoc *result;
  143. sprintf(tQBuf, "SELECT `id`, `md5`, `width`, `height`, `4cc` FROM `items` WHERE `type`=2 AND `md5`='%s'", md5 );
  144. #ifdef DEBUG
  145. LOG(tQBuf);
  146. #endif
  147. result = m_pConnectorClass->Query( tQBuf );
  148. if( result!= NULL ) {
  149. if( result->size() > 0 ) {
  150. Assoc *cArr;
  151. tVal = (IM_ITEM_DATA*) malloc(sizeof(IM_ITEM_DATA));
  152. if( tVal != NULL ) {
  153. Assoc::iterator it;
  154. memset( tVal, 0, sizeof(IM_ITEM_DATA) );
  155. cArr = result->at(0);
  156. it = cArr->find("id");
  157. if( it != cArr->end() ){
  158. tVal->item_id = atoi( it->second.c_str() );
  159. };
  160. it = cArr->find("md5");
  161. if( it != cArr->end() ){
  162. strncpy( tVal->md5, it->second.c_str(), 32 );
  163. };
  164. it = cArr->find("width");
  165. if( it != cArr->end() ){
  166. tVal->width = atoi( it->second.c_str() );
  167. };
  168. it = cArr->find("height");
  169. if( it != cArr->end() ){
  170. tVal->height = atoi( it->second.c_str() );
  171. };
  172. it = cArr->find("4cc");
  173. if( it != cArr->end() ){
  174. if( strstr( it->second.c_str(),"JPEG" ) ) tVal->type = IM_ITEM_TYPE_JPEG;
  175. if( strstr( it->second.c_str(),"PNG" ) ) tVal->type = IM_ITEM_TYPE_PNG;
  176. if( strstr( it->second.c_str(),"GIF" ) ) tVal->type = IM_ITEM_TYPE_GIF;
  177. if( strstr( it->second.c_str(),"TIFF" ) ) tVal->type = IM_ITEM_TYPE_TIFF;
  178. if( strstr( it->second.c_str(),"AGIF" ) ) tVal->type = IM_ITEM_TYPE_AGIF;
  179. };
  180. };
  181. };
  182. };
  183. deleteResult( result );
  184. };
  185. return tVal;
  186. };
  187. void IM_DataBaseAdapter::insertItemROI( uint32_t itemID, uint32_t classID, float weight, uint32_t x, uint32_t y, uint32_t w, uint32_t h ){
  188. if( m_pConnectorClass ) {
  189. char tQBuf[CTEXT];
  190. sprintf(tQBuf, "INSERT INTO `item_rois` SET `item_id`=%d, `class_id`=%d, `probability`=%.5f, `x`=%d, `y`=%d, `width`=%d, `height`=%d", itemID, classID, weight, x, y, w, h );
  191. #ifdef DEBUG
  192. LOG(tQBuf);
  193. #endif
  194. m_pConnectorClass->SilentQuery( tQBuf );
  195. };
  196. };
  197. uint32_t IM_DataBaseAdapter::insertItemFace( uint32_t itemID, CSTR md5, uint32_t x, uint32_t y, uint32_t w, uint32_t h ){
  198. uint32_t RetVal = 0;
  199. if( m_pConnectorClass ) {
  200. char tQBuf[CTEXT];
  201. sprintf(tQBuf, "INSERT INTO `faces_chips` SET `item_id`=%d, `md5`='%s', `x`=%d, `y`=%d, `w`=%d, `h`=%d ON DUPLICATE KEY UPDATE `item_id`=%d, `x`=%d, `y`=%d, `w`=%d", itemID, md5, x, y, w, h, itemID, x, y, w, h );
  202. #ifdef DEBUG
  203. LOG(tQBuf);
  204. #endif
  205. RetVal = m_pConnectorClass->QueryID( tQBuf );
  206. if( RetVal == 0 ) {
  207. AAssoc *result;
  208. sprintf(tQBuf, "SELECT `id` FROM `faces_chips` WHERE `md5`='%s' ", md5 );
  209. result = m_pConnectorClass->Query( tQBuf );
  210. if( result!= NULL ) {
  211. if( result->size() > 0 ) {
  212. Assoc *cArr;
  213. Assoc::iterator it;
  214. cArr = result->at(0);
  215. it = cArr->find("id");
  216. if( it != cArr->end() ){
  217. RetVal = atoi( it->second.c_str() );
  218. };
  219. };
  220. };
  221. deleteResult( result );
  222. };
  223. };
  224. return RetVal;
  225. };
  226. void IM_DataBaseAdapter::updateItemFace( uint32_t itemID, uint8_t dID, float P ){
  227. if( m_pConnectorClass ) {
  228. char tQBuf[CTEXT];
  229. sprintf(tQBuf, "UPDATE `faces_chips` SET `a_%d`=%.5f WHERE `id`=%d", dID, P, itemID );
  230. #ifdef DEBUG
  231. LOG(tQBuf);
  232. #endif
  233. m_pConnectorClass->SilentQuery( tQBuf );
  234. };
  235. };
  236. #undef LOG
  237. #undef LOG_ERROR