Some minor clean ups

This commit is contained in:
BeniS 2013-03-05 17:31:19 +13:00
parent f4263384bc
commit 15ee7962f1
4 changed files with 49 additions and 51 deletions

View File

@ -262,7 +262,8 @@ class DBInsert
DatabasePtr m_db; DatabasePtr m_db;
uint32 m_rows; uint32 m_rows;
std::string m_query, m_buf; std::string m_query;
std::string m_buf;
}; };
class DBTransaction class DBTransaction

View File

@ -28,13 +28,14 @@
class Database; class Database;
class DBResult; class DBResult;
typedef stdext::shared_object_ptr<Database> DatabasePtr;
typedef stdext::shared_object_ptr<DBResult> DBResultPtr;
// MySQL
class DatabaseMySQL; class DatabaseMySQL;
class MySQLResult; class MySQLResult;
typedef stdext::shared_object_ptr<Database> DatabasePtr;
typedef stdext::shared_object_ptr<DatabaseMySQL> DatabaseMySQLPtr; typedef stdext::shared_object_ptr<DatabaseMySQL> DatabaseMySQLPtr;
typedef stdext::shared_object_ptr<DBResult> DBResultPtr;
typedef stdext::shared_object_ptr<MySQLResult> MySQLResultPtr; typedef stdext::shared_object_ptr<MySQLResult> MySQLResultPtr;
#endif #endif

View File

@ -100,7 +100,7 @@ bool DatabaseMySQL::beginTransaction()
bool DatabaseMySQL::rollback() bool DatabaseMySQL::rollback()
{ {
if(mysql_rollback(m_handle)) { if(mysql_rollback(m_handle)) {
g_logger.error(stdext::format("DatabaseMySQL::rollback() ERROR: %s (%s)", mysql_error(m_handle), mysql_errno(m_handle))); g_logger.error(stdext::format("[DatabaseMySQL::rollback] ERROR: %s (%s)", mysql_error(m_handle), mysql_errno(m_handle)));
return false; return false;
} }
@ -110,7 +110,7 @@ bool DatabaseMySQL::rollback()
bool DatabaseMySQL::commit() bool DatabaseMySQL::commit()
{ {
if(mysql_commit(m_handle)) { if(mysql_commit(m_handle)) {
g_logger.error(stdext::format("DatabaseMySQL::commit() ERROR: %s (%s)", mysql_error(m_handle), mysql_errno(m_handle))); g_logger.error(stdext::format("[DatabaseMySQL::commit] ERROR: %s (%s)", mysql_error(m_handle), mysql_errno(m_handle)));
return false; return false;
} }
@ -130,13 +130,11 @@ bool DatabaseMySQL::internalExecuteQuery(const std::string &query)
bool DatabaseMySQL::executeQuery(const std::string &query) bool DatabaseMySQL::executeQuery(const std::string &query)
{ {
//LOG_ONDELAY(500);
if(internalExecuteQuery(query)) { if(internalExecuteQuery(query)) {
MYSQL_RES* m_res = mysql_store_result(m_handle); MYSQL_RES* res = mysql_store_result(m_handle);
if(m_res) { if(res) {
mysql_free_result(m_res); mysql_free_result(res);
} else if(mysql_errno(m_handle) != 0) { } else if(mysql_errno(m_handle) != 0) {
handleError(); handleError();
} }
@ -147,26 +145,24 @@ bool DatabaseMySQL::executeQuery(const std::string &query)
return false; return false;
} }
DBResultPtr DatabaseMySQL::storeQuery(const std::string &query) DBResultPtr DatabaseMySQL::storeQuery(const std::string& query)
{ {
//LOG_ONDELAY(500);
while(internalExecuteQuery(query)) { while(internalExecuteQuery(query)) {
MYSQL_RES* m_res = mysql_store_result(m_handle); MYSQL_RES* res = mysql_store_result(m_handle);
if(m_res) { if(res) {
DBResultPtr res = (DBResultPtr)MySQLResultPtr(new MySQLResult(m_res)); DBResultPtr result = (DBResultPtr)MySQLResultPtr(new MySQLResult(res));
if(!verifyResult(res)) if(!verifyResult(result))
break; break;
return res; return result;
} }
else if(mysql_errno(m_handle) != 0) { else if(mysql_errno(m_handle) != 0) {
if(!handleError()) if(!handleError())
break; break;
} }
//mSleep(10); stdext::millisleep(10);
} }
return nullptr; return nullptr;
@ -201,81 +197,81 @@ std::string DatabaseMySQL::escapeBlob(const char* s, uint32 length)
int32 MySQLResult::getDataInt(const std::string& s) int32 MySQLResult::getDataInt(const std::string& s)
{ {
listNames_t::iterator it = m_listNames.find(s); RowNames_t::iterator it = m_names.find(s);
if(it != m_listNames.end()) if(it != m_names.end())
return m_row[it->second] ? atoi(m_row[it->second]) : 0; return m_row[it->second] ? atoi(m_row[it->second]) : 0;
g_logger.error(stdext::format("MySQLResult::getDataInt() Error: %d", s)); g_logger.error(stdext::format("[MySQLResult::getDataInt] Error: %d", s));
return 0; // Failed return 0;
} }
int64 MySQLResult::getDataLong(const std::string& s) int64 MySQLResult::getDataLong(const std::string& s)
{ {
listNames_t::iterator it = m_listNames.find(s); RowNames_t::iterator it = m_names.find(s);
if(it != m_listNames.end()) if(it != m_names.end())
return m_row[it->second] ? atoll(m_row[it->second]) : 0; return m_row[it->second] ? atoll(m_row[it->second]) : 0;
g_logger.error(stdext::format("MySQLResult::getDataLong() Error: %d", s)); g_logger.error(stdext::format("[MySQLResult::getDataLong] Error: %d", s));
return 0; // Failed return 0;
} }
std::string MySQLResult::getDataString(const std::string& s) std::string MySQLResult::getDataString(const std::string& s)
{ {
listNames_t::iterator it = m_listNames.find(s); RowNames_t::iterator it = m_names.find(s);
if(it != m_listNames.end()) if(it != m_names.end())
return m_row[it->second] ? std::string(m_row[it->second]) : std::string(); return m_row[it->second] ? std::string(m_row[it->second]) : std::string();
g_logger.error(stdext::format("MySQLResult::getDataString() Error: %d", s)); g_logger.error(stdext::format("[MySQLResult::getDataString] Error: %d", s));
return std::string(); // Failed return std::string();
} }
const char* MySQLResult::getDataStream(const std::string& s, uint64& size) const char* MySQLResult::getDataStream(const std::string& s, uint64& size)
{ {
size = 0; size = 0;
listNames_t::iterator it = m_listNames.find(s); RowNames_t::iterator it = m_names.find(s);
if(it == m_listNames.end()) { if(it == m_names.end()) {
g_logger.error(stdext::format("MySQLResult::getDataStream() Error: %d", s)); g_logger.error(stdext::format("[MySQLResult::getDataStream] Error: %d", s));
return NULL; return NULL;
} }
if(!m_row[it->second]) if(!m_row[it->second])
return NULL; return NULL;
size = mysql_fetch_lengths(m_resultHandle)[it->second]; size = mysql_fetch_lengths(m_result)[it->second];
return m_row[it->second]; return m_row[it->second];
} }
void MySQLResult::free() void MySQLResult::free()
{ {
if(!m_resultHandle) { if(!m_result) {
g_logger.fatal("MySQLResult::free() Error trying to free already freed result"); g_logger.fatal("[MySQLResult::free] Error: trying to free already freed result");
return; return;
} }
mysql_free_result(m_resultHandle); mysql_free_result(m_result);
m_resultHandle = NULL; m_result = NULL;
} }
bool MySQLResult::next() bool MySQLResult::next()
{ {
m_row = mysql_fetch_row(m_resultHandle); m_row = mysql_fetch_row(m_result);
return (m_row != NULL); return (m_row != NULL);
} }
MySQLResult::~MySQLResult() MySQLResult::~MySQLResult()
{ {
if(m_resultHandle) if(m_result)
mysql_free_result(m_resultHandle); mysql_free_result(m_result);
} }
MySQLResult::MySQLResult(MYSQL_RES* result) MySQLResult::MySQLResult(MYSQL_RES* result)
{ {
m_resultHandle = result; m_result = result;
m_listNames.clear(); m_names.clear();
MYSQL_FIELD* field; MYSQL_FIELD* field;
int32 i = 0; int32 i = 0;
while((field = mysql_fetch_field(m_resultHandle))) { while((field = mysql_fetch_field(m_result))) {
m_listNames[field->name] = i++; m_names[field->name] = i++;
} }
} }

View File

@ -73,16 +73,16 @@ class MySQLResult : public DBResult
virtual void free(); virtual void free();
virtual bool next(); virtual bool next();
virtual int getRowCount() { return mysql_num_rows(m_resultHandle); } virtual int getRowCount() { return mysql_num_rows(m_result); }
protected: protected:
MySQLResult(MYSQL_RES* result); MySQLResult(MYSQL_RES* result);
virtual ~MySQLResult(); virtual ~MySQLResult();
typedef std::map<const std::string, uint32> listNames_t; typedef std::map<const std::string, uint32> RowNames_t;
listNames_t m_listNames; RowNames_t m_names;
MYSQL_RES* m_resultHandle; MYSQL_RES* m_result;
MYSQL_ROW m_row; MYSQL_ROW m_row;
}; };