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
251 lines
11 KiB
#include "IM_DataBaseAdapter.h"
|
|
#include "../../lib/alg/generic/md5.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "IMDBA", this, X )
|
|
#ifdef DEBUG
|
|
#define LOG_ERROR( X ) {\
|
|
char _tBfrErrMSG234[256];\
|
|
sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
|
|
m_pLog->Log( "IMDBA", this, _LOG_ERROR, _tBfrErrMSG234 );\
|
|
}
|
|
#else
|
|
#define LOG_ERROR( X ) m_pLog->Log( "IMDBA", this, _LOG_ERROR, X );
|
|
#endif
|
|
|
|
IM_DataBaseAdapter* IM_DataBaseAdapter::_instance = NULL;
|
|
|
|
/**
|
|
* Returns the single instance of the object.
|
|
*/
|
|
IM_DataBaseAdapter* IM_DataBaseAdapter::getInstance( config_setting_t * Settings ) {
|
|
// check if the instance has been created yet
|
|
if( NULL == _instance ){
|
|
// if not, then create it
|
|
_instance = new IM_DataBaseAdapter( Settings );
|
|
};
|
|
// return the single instance
|
|
return _instance;
|
|
};
|
|
|
|
// Constructor implementation
|
|
inline IM_DataBaseAdapter::IM_DataBaseAdapter( config_setting_t * Settings ){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DBC" );
|
|
m_bIsConnected = false;
|
|
if( m_pSettings!= NULL ) {
|
|
config_setting_t * tSetting;
|
|
char tDBHost[128];
|
|
char tDBName[128];
|
|
char tDBUser[128];
|
|
char tDBPass[128];
|
|
uint16_t tDBPort = 3306;
|
|
tSetting = config_setting_get_member( m_pSettings, "DATABASE_HOST" );
|
|
if( tSetting == NULL ){
|
|
tSetting = config_setting_add(m_pSettings, "DATABASE_HOST", CONFIG_TYPE_STRING );
|
|
config_setting_set_string( tSetting, "localhost" );
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
LOG("No DB Host Setting! Using default!");
|
|
};
|
|
sprintf(tDBHost, "%s", config_setting_get_string(tSetting));
|
|
tSetting = config_setting_get_member( m_pSettings, "DATABASE_NAME" );
|
|
if( tSetting == NULL ){
|
|
tSetting = config_setting_add(m_pSettings, "DATABASE_NAME", CONFIG_TYPE_STRING );
|
|
config_setting_set_string( tSetting, "dfsl_gunter" );
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
LOG("No DB Database Setting! Using default!");
|
|
};
|
|
sprintf(tDBName, "%s", config_setting_get_string(tSetting));
|
|
tSetting = config_setting_get_member( m_pSettings, "DATABASE_USER" );
|
|
if( tSetting == NULL ){
|
|
tSetting = config_setting_add(m_pSettings, "DATABASE_USER", CONFIG_TYPE_STRING );
|
|
config_setting_set_string( tSetting, "dbroot" );
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
LOG("No DB User Setting! Using default!");
|
|
};
|
|
sprintf(tDBUser, "%s", config_setting_get_string(tSetting));
|
|
tSetting = config_setting_get_member( m_pSettings, "DATABASE_PASSWORD" );
|
|
if( tSetting == NULL ){
|
|
tSetting = config_setting_add(m_pSettings, "DATABASE_PASSWORD", CONFIG_TYPE_STRING );
|
|
config_setting_set_string( tSetting, "123567" );
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
LOG("No DB Password Setting! Using default!");
|
|
};
|
|
sprintf(tDBPass, "%s", config_setting_get_string(tSetting));
|
|
tSetting = config_setting_get_member( m_pSettings, "DATABASE_PORT" );
|
|
if( tSetting == NULL ){
|
|
tSetting = config_setting_add(m_pSettings, "DATABASE_PORT", CONFIG_TYPE_INT );
|
|
config_setting_set_int( tSetting, 3306 );
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
LOG("No DB Port Setting! Using default!");
|
|
};
|
|
tDBPort = config_setting_get_int( tSetting );
|
|
basicSettingsProvider::getInstance()->validate();
|
|
m_pConnectorClass = new MySQL_Connector( tDBName, tDBHost, tDBUser, tDBPass, tDBPort );
|
|
m_bIsConnected = ( m_pConnectorClass != NULL );
|
|
} else {
|
|
LOG_ERROR("No Settings Provided!");
|
|
};
|
|
};
|
|
|
|
// Destructor implementation
|
|
inline IM_DataBaseAdapter::~IM_DataBaseAdapter(){
|
|
if( m_pConnectorClass ) delete( m_pConnectorClass );
|
|
};
|
|
|
|
IM_ITEM_DATA *IM_DataBaseAdapter::getItemDataById( uint32_t itemID ){
|
|
IM_ITEM_DATA *tVal = NULL;
|
|
if( m_pConnectorClass ) {
|
|
char tQBuf[CTEXT];
|
|
AAssoc *result;
|
|
sprintf(tQBuf, "SELECT `id`, `md5`, `width`, `height`, `4cc` FROM `items` WHERE `type`=2 AND `id`=%d", itemID );
|
|
#ifdef DEBUG
|
|
LOG(tQBuf);
|
|
#endif
|
|
result = m_pConnectorClass->Query( tQBuf );
|
|
if( result!= NULL ) {
|
|
if( result->size() > 0 ) {
|
|
Assoc *cArr;
|
|
tVal = (IM_ITEM_DATA*) malloc(sizeof(IM_ITEM_DATA));
|
|
if( tVal != NULL ) {
|
|
Assoc::iterator it;
|
|
|
|
memset( tVal, 0, sizeof(IM_ITEM_DATA) );
|
|
cArr = result->at(0);
|
|
it = cArr->find("id");
|
|
if( it != cArr->end() ){
|
|
tVal->item_id = atoi( it->second.c_str() );
|
|
};
|
|
it = cArr->find("md5");
|
|
if( it != cArr->end() ){
|
|
strncpy( tVal->md5, it->second.c_str(), 32 );
|
|
};
|
|
it = cArr->find("width");
|
|
if( it != cArr->end() ){
|
|
tVal->width = atoi( it->second.c_str() );
|
|
};
|
|
it = cArr->find("height");
|
|
if( it != cArr->end() ){
|
|
tVal->height = atoi( it->second.c_str() );
|
|
};
|
|
it = cArr->find("4cc");
|
|
if( it != cArr->end() ){
|
|
if( strstr( it->second.c_str(),"JPEG" ) ) tVal->type = IM_ITEM_TYPE_JPEG;
|
|
if( strstr( it->second.c_str(),"PNG" ) ) tVal->type = IM_ITEM_TYPE_PNG;
|
|
if( strstr( it->second.c_str(),"GIF" ) ) tVal->type = IM_ITEM_TYPE_GIF;
|
|
if( strstr( it->second.c_str(),"TIFF" ) ) tVal->type = IM_ITEM_TYPE_TIFF;
|
|
if( strstr( it->second.c_str(),"AGIF" ) ) tVal->type = IM_ITEM_TYPE_AGIF;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
deleteResult( result );
|
|
};
|
|
return tVal;
|
|
};
|
|
|
|
IM_ITEM_DATA *IM_DataBaseAdapter::getItemDataByMd5( CSTR md5 ){
|
|
IM_ITEM_DATA *tVal = NULL;
|
|
if( m_pConnectorClass ) {
|
|
char tQBuf[CTEXT];
|
|
AAssoc *result;
|
|
sprintf(tQBuf, "SELECT `id`, `md5`, `width`, `height`, `4cc` FROM `items` WHERE `type`=2 AND `md5`='%s'", md5 );
|
|
#ifdef DEBUG
|
|
LOG(tQBuf);
|
|
#endif
|
|
result = m_pConnectorClass->Query( tQBuf );
|
|
if( result!= NULL ) {
|
|
if( result->size() > 0 ) {
|
|
Assoc *cArr;
|
|
tVal = (IM_ITEM_DATA*) malloc(sizeof(IM_ITEM_DATA));
|
|
if( tVal != NULL ) {
|
|
Assoc::iterator it;
|
|
|
|
memset( tVal, 0, sizeof(IM_ITEM_DATA) );
|
|
cArr = result->at(0);
|
|
it = cArr->find("id");
|
|
if( it != cArr->end() ){
|
|
tVal->item_id = atoi( it->second.c_str() );
|
|
};
|
|
it = cArr->find("md5");
|
|
if( it != cArr->end() ){
|
|
strncpy( tVal->md5, it->second.c_str(), 32 );
|
|
};
|
|
it = cArr->find("width");
|
|
if( it != cArr->end() ){
|
|
tVal->width = atoi( it->second.c_str() );
|
|
};
|
|
it = cArr->find("height");
|
|
if( it != cArr->end() ){
|
|
tVal->height = atoi( it->second.c_str() );
|
|
};
|
|
it = cArr->find("4cc");
|
|
if( it != cArr->end() ){
|
|
if( strstr( it->second.c_str(),"JPEG" ) ) tVal->type = IM_ITEM_TYPE_JPEG;
|
|
if( strstr( it->second.c_str(),"PNG" ) ) tVal->type = IM_ITEM_TYPE_PNG;
|
|
if( strstr( it->second.c_str(),"GIF" ) ) tVal->type = IM_ITEM_TYPE_GIF;
|
|
if( strstr( it->second.c_str(),"TIFF" ) ) tVal->type = IM_ITEM_TYPE_TIFF;
|
|
if( strstr( it->second.c_str(),"AGIF" ) ) tVal->type = IM_ITEM_TYPE_AGIF;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
deleteResult( result );
|
|
};
|
|
return tVal;
|
|
};
|
|
|
|
void IM_DataBaseAdapter::insertItemROI( uint32_t itemID, uint32_t classID, float weight, uint32_t x, uint32_t y, uint32_t w, uint32_t h ){
|
|
if( m_pConnectorClass ) {
|
|
char tQBuf[CTEXT];
|
|
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 );
|
|
#ifdef DEBUG
|
|
LOG(tQBuf);
|
|
#endif
|
|
m_pConnectorClass->SilentQuery( tQBuf );
|
|
};
|
|
};
|
|
|
|
uint32_t IM_DataBaseAdapter::insertItemFace( uint32_t itemID, CSTR md5, uint32_t x, uint32_t y, uint32_t w, uint32_t h ){
|
|
uint32_t RetVal = 0;
|
|
if( m_pConnectorClass ) {
|
|
char tQBuf[CTEXT];
|
|
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 );
|
|
#ifdef DEBUG
|
|
LOG(tQBuf);
|
|
#endif
|
|
RetVal = m_pConnectorClass->QueryID( tQBuf );
|
|
if( RetVal == 0 ) {
|
|
AAssoc *result;
|
|
sprintf(tQBuf, "SELECT `id` FROM `faces_chips` WHERE `md5`='%s' ", md5 );
|
|
result = m_pConnectorClass->Query( tQBuf );
|
|
if( result!= NULL ) {
|
|
if( result->size() > 0 ) {
|
|
Assoc *cArr;
|
|
Assoc::iterator it;
|
|
cArr = result->at(0);
|
|
it = cArr->find("id");
|
|
if( it != cArr->end() ){
|
|
RetVal = atoi( it->second.c_str() );
|
|
};
|
|
};
|
|
};
|
|
deleteResult( result );
|
|
};
|
|
};
|
|
return RetVal;
|
|
};
|
|
|
|
void IM_DataBaseAdapter::updateItemFace( uint32_t itemID, uint8_t dID, float P ){
|
|
if( m_pConnectorClass ) {
|
|
char tQBuf[CTEXT];
|
|
sprintf(tQBuf, "UPDATE `faces_chips` SET `a_%d`=%.5f WHERE `id`=%d", dID, P, itemID );
|
|
#ifdef DEBUG
|
|
LOG(tQBuf);
|
|
#endif
|
|
m_pConnectorClass->SilentQuery( tQBuf );
|
|
};
|
|
};
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|
|
|