diff --git a/CMakeLists.txt b/CMakeLists.txt index d47702bb..3bf7fa78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ set(FRAMEWORK_SOUND ON) set(FRAMEWORK_GRAPHICS ON) set(FRAMEWORK_XML ON) set(FRAMEWORK_NET ON) +set(FRAMEWORK_SQL ON) include(src/framework/CMakeLists.txt) include(src/client/CMakeLists.txt) diff --git a/src/framework/CMakeLists.txt b/src/framework/CMakeLists.txt index 5829320c..7198eb20 100644 --- a/src/framework/CMakeLists.txt +++ b/src/framework/CMakeLists.txt @@ -3,6 +3,7 @@ # FRAMEWORK_GRAPHICS # FRAMEWORK_NET # FRAMEWORK_XML +# FRAMEWORK_SQL # CMAKE_CURRENT_LIST_DIR cmake 2.6 compatibility if(${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 6) diff --git a/src/framework/cmake/FindMySQL.cmake b/src/framework/cmake/FindMySQL.cmake index 0178a4d8..1b4f71fe 100644 --- a/src/framework/cmake/FindMySQL.cmake +++ b/src/framework/cmake/FindMySQL.cmake @@ -4,8 +4,14 @@ # MYSQL_LIBRARY - the mysql library FIND_PATH(MYSQL_INCLUDE_DIR NAMES mysql.h PATH_SUFFIXES mysql) -SET(_MYSQL_STATIC_LIBS libmysqlclient.a libmysqlclient_r.a) -SET(_MYSQL_SHARED_LIBS libmysqlclient.dll.a libmysqlclient_r.dll.a mysqlclient mysqlclient_r) +IF(WIN32) + SET(_MYSQL_STATIC_LIBS libmysql.a libmysql_r.a) + SET(_MYSQL_SHARED_LIBS libmysql.dll.a libmysql_r.dll.a libmysql libmysql_r) +ELSE() + SET(_MYSQL_STATIC_LIBS libmysqlclient.a libmysqlclient_r.a) + SET(_MYSQL_SHARED_LIBS libmysqlclient.dll.a libmysqlclient_r.dll.a mysqlclient mysqlclient_r) +ENDIF() + IF(USE_STATIC_LIBS) FIND_LIBRARY(MYSQL_LIBRARY NAMES ${_MYSQL_STATIC_LIBS} ${_MYSQL_SHARED_LIBS}) ELSE() @@ -14,3 +20,4 @@ ENDIF() INCLUDE(FindPackageHandleStandardArgs) FIND_PACKAGE_HANDLE_STANDARD_ARGS(MySQL DEFAULT_MSG MYSQL_LIBRARY MYSQL_INCLUDE_DIR) MARK_AS_ADVANCED(MYSQL_LIBRARY MYSQL_INCLUDE_DIR) + diff --git a/src/framework/const.h b/src/framework/const.h index de4b6740..1dc5f190 100644 --- a/src/framework/const.h +++ b/src/framework/const.h @@ -288,6 +288,13 @@ namespace Fw BackgroundPane = 2, BothPanes = 3 }; + +#ifdef FW_SQL + enum DatabaseEngine { + DatabaseNone = 0, + DatabaseMySQL + }; +#endif } #endif diff --git a/src/framework/sql/database.cpp b/src/framework/sql/database.cpp index 76bb2719..ff3f052f 100644 --- a/src/framework/sql/database.cpp +++ b/src/framework/sql/database.cpp @@ -21,11 +21,69 @@ */ #include "database.h" +#include "mysql.h" -Database::Database() +boost::recursive_mutex DBQuery::databaseLock; +DatabasePtr Database::m_instance = nullptr; + +DatabasePtr Database::getInstance() +{ + if(!m_instance) + m_instance = (DatabasePtr)DatabaseMySQLPtr(new DatabaseMySQL()); + + m_instance->use(); + return m_instance; +} + +DBResultPtr Database::verifyResult(DBResultPtr result) +{ + if(result->next()) + return result; + + result->free(); + return nullptr; +} + +void DBInsert::setQuery(const std::string& query) { + m_query = query; + m_buf = ""; + m_rows = 0; } -Database::~Database() +bool DBInsert::addRow(const std::string& row) { + ++m_rows; + if(m_buf.empty()) { + m_buf = "(" + row + ")"; + } + else if(m_buf.length() > 8192) + { + if(!execute()) + return false; + + m_buf = "(" + row + ")"; + } + else + m_buf += ",(" + row + ")"; + + return true; +} + +bool DBInsert::addRow(std::stringstream& row) +{ + bool ret = addRow(row.str()); + row.str(""); + return ret; +} + +bool DBInsert::execute() +{ + if(m_buf.empty() || !m_rows) + return true; + + m_rows = 0; + bool ret = m_db->executeQuery(m_query + m_buf); + m_buf = ""; + return ret; } diff --git a/src/framework/sql/database.h b/src/framework/sql/database.h index 613e26bb..a5a72751 100644 --- a/src/framework/sql/database.h +++ b/src/framework/sql/database.h @@ -22,16 +22,299 @@ #ifndef DATABASE_H #define DATABASE_H - #include "declarations.h" -#include + +#include #include +#include +#include + class Database : public LuaObject { -public: - Database(); - ~Database(); + public: + friend class DBTransaction; + + /** + * Singleton implementation. + * + * Retruns instance of database handler. Don't create database (or drivers) instances in your code - instead of it use Database::getInstance()-> + * This method stores static instance of connection class internaly to make sure exactly one instance of connection is created for entire system. + * + * @return database connection handler singleton + */ + static DatabasePtr getInstance(); + + /** + * Database ... + */ + virtual void use() {m_use = g_clock.millis();} + + /** + * Database connector. + * + * Connects the database to the source host. + */ + virtual void connect(const std::string& host, const std::string& user, const std::string& pass, + const std::string& db, uint16 port, const std::string& unix_socket = "") {} + + /** + * Transaction related methods. + * + * Methods for starting, commiting and rolling back transaction. Each of the returns boolean value. + * + * @return true on success, false on error + * @note#include + * If your database system doesn't support transactions you should return true - it's not feature test, code should work without transaction, just will lack integrity. + */ + + virtual bool beginTransaction() {return false;} + virtual bool rollback() {return false;} + virtual bool commit() {return false;} + + /** + * Executes command. + * + * Executes query which doesn't generates results (eg. INSERT, UPDATE, DELETE...). + * + * @param std::string query command + * @return true on success, false on error + */ + virtual bool executeQuery(const std::string& query) {return false;} + + /** + * Queries database. + * + * Executes query which generates results (mostly SELECT). + * + * @param std::string query + * @return results object (null on error) + */ + virtual DBResultPtr storeQuery(const std::string& query) {return nullptr;} + + /** + * Escapes string for query. + * + * Prepares string to fit SQL queries including quoting it. + * + * @param std::string string to be escaped + * @return quoted string + */ + virtual std::string escapeString(const std::string&) {return "''";} + + /** + * Escapes binary stream for query. + * + * Prepares binary stream to fit SQL queries. + * + * @param char* binary stream + * @param long stream length + * @return quoted string + */ + virtual std::string escapeBlob(const char*, uint32) {return "''";} + + /** + * Retrieve id of last inserted row + * + * @return id on success, 0 if last query did not result on any rows with auto_increment keys + */ + virtual uint64 getLastInsertedRowID() {return 0;} + + /** + * Get case insensitive string comparison operator + * + * @return the case insensitive operator + */ + virtual std::string getStringComparer() {return "= ";} + virtual std::string getUpdateLimiter() {return " LIMIT 1;";} + + /** + * Get database engine + * + * @return the database engine type + */ + virtual Fw::DatabaseEngine getDatabaseEngine() {return Fw::DatabaseNone;} + + /** + * Database connected. + * + * Returns whether or not the database is connected. + * + * @return whether or not the database is connected. + */ + bool isConnected() const {return m_connected;} + + /** + * Database set connected. + * + * Sets the database to know that it is connected. + */ + void setConnected(bool connected) { m_connected = connected; } + + protected: + virtual bool handleError() {return false;} + virtual bool internalExecuteQuery(const std::string &query) {return false;} + + DBResultPtr verifyResult(DBResultPtr result); + + Database(): m_connected(false) {} + virtual ~Database() {m_connected = false;} + + ticks_t m_use; + bool m_connected; + + private: + static DatabasePtr m_instance; +}; + +class DBResult : public LuaObject +{ + public: + /** Get the Integer value of a field in database + *\returns The Integer value of the selected field and row + *\param s The name of the field + */ + virtual int32 getDataInt(const std::string&) {return 0;} + + /** Get the Long value of a field in database + *\returns The Long value of the selected field and row + *\param s The name of the field + */ + virtual int64 getDataLong(const std::string&) {return 0;} + + /** Get the String of a field in database + *\returns The String of the selected field and row + *\param s The name of the field + */ + virtual std::string getDataString(const std::string&) {return "";} + + /** Get the blob of a field in database + *\returns a PropStream that is initiated with the blob data field, if not exist it returns NULL. + *\param s The name of the field + */ + virtual const char* getDataStream(const std::string&, uint64&) {return "";} + + /** Result freeing + */ + virtual void free() {} + + /** Moves to next result in set + *\returns true if moved, false if there are no more results. + */ + virtual bool next() {return false;} + + /** Returned the number of rows from result + *\returns integer value of row amount, 0 if result is empty. + */ + virtual int getRowCount() { return 0; } + + protected: + DBResult() {} + virtual ~DBResult() {} +}; + +/** + * Thread locking hack. + * + * By using this class for your queries you lock and unlock database for threads. +*/ +class DBQuery : public std::stringstream +{ + friend class Database; + public: + DBQuery() {databaseLock.lock();} + ~DBQuery() {databaseLock.unlock();} + + protected: + static boost::recursive_mutex databaseLock; +}; + +/** + * INSERT statement. + * + * Gives possibility to optimize multiple INSERTs on databases that support multiline INSERTs. + */ +class DBInsert +{ + public: + /** + * Associates with given database handler. + * + * @param Database* database wrapper + */ + DBInsert(DatabasePtr db): m_db(db), m_rows(0) {} + ~DBInsert() {} + + /** + * Sets query prototype. + * + * @param std::string& INSERT query + */ + void setQuery(const std::string& query); + + /** + * Adds new row to INSERT statement. + * + * On databases that doesn't support multiline INSERTs it simply execute INSERT for each row. + * + * @param std::string& row data + */ + bool addRow(const std::string& row); + /** + * Allows to use addRow() with stringstream as parameter. + */ + bool addRow(std::stringstream& row); + + /** + * Executes current buffer. + */ + bool execute(); + + protected: + DatabasePtr m_db; + + uint32 m_rows; + std::string m_query, m_buf; +}; + +class DBTransaction +{ + public: + DBTransaction(DatabasePtr database) + { + m_db = database; + m_state = STATE_FRESH; + } + + ~DBTransaction() + { + if(m_state == STATE_READY) + m_db->rollback(); + } + + bool begin() + { + m_state = STATE_READY; + return m_db->beginTransaction(); + } + + bool commit() + { + if(m_state != STATE_READY) + return false; + + m_state = STATE_DONE; + return m_db->commit(); + } + + private: + DatabasePtr m_db; + enum TransactionStates_t + { + STATE_FRESH, + STATE_READY, + STATE_DONE + } m_state; }; #endif diff --git a/src/framework/sql/declarations.h b/src/framework/sql/declarations.h index 928e98dc..55eb62d7 100644 --- a/src/framework/sql/declarations.h +++ b/src/framework/sql/declarations.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2013 OTClient + * Copyright (c) 2010-2012 TitanCore * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -25,10 +25,16 @@ #include +class Database; class DBResult; + class DatabaseMySQL; +class MySQLResult; -typedef stdext::shared_object_ptr DBResultPtr; +typedef stdext::shared_object_ptr DatabasePtr; typedef stdext::shared_object_ptr DatabaseMySQLPtr; +typedef stdext::shared_object_ptr DBResultPtr; +typedef stdext::shared_object_ptr MySQLResultPtr; + #endif diff --git a/src/framework/sql/mysql.cpp b/src/framework/sql/mysql.cpp index 69f05452..b3305b39 100644 --- a/src/framework/sql/mysql.cpp +++ b/src/framework/sql/mysql.cpp @@ -20,46 +20,50 @@ * THE SOFTWARE. */ -#ifdef WIN32 -#include -#endif #include "mysql.h" + #include -#include + #include -DatabaseMySQL::DatabaseMySQL() : m_running(false) +DatabaseMySQL::DatabaseMySQL() { - if(!mysql_init(&m_mysqlHandle)) + m_handle = new MYSQL(); + if(!mysql_init(m_handle)) { g_logger.fatal("Failed to initialize MySQL connection handle."); + } my_bool reconnect = true; - mysql_options(&m_mysqlHandle, MYSQL_OPT_RECONNECT, &reconnect); + mysql_options(m_handle, MYSQL_OPT_RECONNECT, &reconnect); } DatabaseMySQL::~DatabaseMySQL() { - mysql_close(&m_mysqlHandle); + mysql_close(m_handle); + delete m_handle; } void DatabaseMySQL::connect(const std::string& host, const std::string& user, const std::string& pass, - const std::string& db, uint16_t port, const std::string& unix_socket) + const std::string& db, uint16 port, const std::string& unix_socket) { - if(!mysql_real_connect(&m_mysqlHandle, + if(!mysql_real_connect(m_handle, host.c_str(), user.c_str(), pass.c_str(), db.c_str(), port, unix_socket.empty() ? NULL : unix_socket.c_str(), 0)) { - g_logger.error(stdext::format("Failed to connect to database. MYSQL ERROR: %s", mysql_error(&m_mysqlHandle))); + g_logger.error(stdext::format("Failed to connect to database. MYSQL ERROR: %s", mysql_error(m_handle))); + } + else { + setConnected(true); } } bool DatabaseMySQL::handleError() { - unsigned int error = mysql_errno(&m_mysqlHandle); - g_logger.error(stdext::format("MYSQL error code = %d, message: %s", error, mysql_error(&m_mysqlHandle))); + unsigned int error = mysql_errno(m_handle); + g_logger.error(stdext::format("MYSQL error code = %d, message: %s", error, mysql_error(m_handle))); if(error == CR_SOCKET_CREATE_ERROR || error == CR_CONNECTION_ERROR || @@ -70,18 +74,19 @@ bool DatabaseMySQL::handleError() error == CR_SERVER_LOST || error == CR_SERVER_HANDSHAKE_ERR) { g_logger.error("MYSQL connection lost, trying to reconnect..."); + setConnected(false); - //int64_t startTime = g_clock.millis(); - - /*while(true) { - bool connected = (mysql_ping(&m_mysqlHandle) == 0); - uint32_t diffTime = (mTime() - startTime); + ticks_t startTime = g_clock.millis(); + while(true) { + bool connected = (mysql_ping(m_handle) == 0); + ticks_t diffTime = (g_clock.millis() - startTime); if(connected) { g_logger.info(stdext::format("MySQL reconneted in %d ms", diffTime)); + setConnected(true); return true; } - mSleep(100); - }*/ + stdext::millisleep(100); + } } return false; @@ -94,8 +99,8 @@ bool DatabaseMySQL::beginTransaction() bool DatabaseMySQL::rollback() { - if(mysql_rollback(&m_mysqlHandle) != 0) { - g_logger.error(mysql_error(&m_mysqlHandle)); + if(mysql_rollback(m_handle)) { + g_logger.error(stdext::format("DatabaseMySQL::rollback() ERROR: %s (%s)", mysql_error(m_handle), mysql_errno(m_handle))); return false; } @@ -104,8 +109,8 @@ bool DatabaseMySQL::rollback() bool DatabaseMySQL::commit() { - if(mysql_commit(&m_mysqlHandle) != 0) { - g_logger.error(mysql_error(&m_mysqlHandle)); + if(mysql_commit(m_handle)) { + g_logger.error(stdext::format("DatabaseMySQL::commit() ERROR: %s (%s)", mysql_error(m_handle), mysql_errno(m_handle))); return false; } @@ -114,7 +119,7 @@ bool DatabaseMySQL::commit() bool DatabaseMySQL::internalExecuteQuery(const std::string &query) { - while(mysql_real_query(&m_mysqlHandle, query.c_str(), query.length()) != 0) { + while(mysql_real_query(m_handle, query.c_str(), query.length()) != 0) { if(!handleError()) { return false; } @@ -128,11 +133,11 @@ bool DatabaseMySQL::executeQuery(const std::string &query) //LOG_ONDELAY(500); if(internalExecuteQuery(query)) { - MYSQL_RES *m_res = mysql_store_result(&m_mysqlHandle); + MYSQL_RES* m_res = mysql_store_result(m_handle); if(m_res) { mysql_free_result(m_res); - } else if(mysql_errno(&m_mysqlHandle) != 0) { + } else if(mysql_errno(m_handle) != 0) { handleError(); } @@ -147,124 +152,132 @@ DBResultPtr DatabaseMySQL::storeQuery(const std::string &query) //LOG_ONDELAY(500); while(internalExecuteQuery(query)) { - MYSQL_RES *m_res = mysql_store_result(&m_mysqlHandle); + MYSQL_RES* m_res = mysql_store_result(m_handle); if(m_res) { - DBResultPtr res = DBResultPtr(new DBResult(m_res)); - if(res->next()) { - return res; - } else { - //delete res; + DBResultPtr res = (DBResultPtr)MySQLResultPtr(new MySQLResult(m_res)); + if(!verifyResult(res)) break; - } - } else if(mysql_errno(&m_mysqlHandle) != 0) { - if(!handleError()) { + + return res; + } + else if(mysql_errno(m_handle) != 0) { + if(!handleError()) break; - } } //mSleep(10); } - return NULL; + return nullptr; } -uint64_t DatabaseMySQL::getLastInsertedRowID() +uint64 DatabaseMySQL::getLastInsertedRowID() { - return (uint64_t)mysql_insert_id(&m_mysqlHandle); + return (uint64)mysql_insert_id(m_handle); } std::string DatabaseMySQL::escapeString(const std::string &s) { - return escapeBlob(s.c_str(), s.length()); + return escapeBlob( s.c_str(), s.length() ); } -std::string DatabaseMySQL::escapeBlob(const char* s, uint32_t length) +std::string DatabaseMySQL::escapeBlob(const char* s, uint32 length) { - if(!s) - return std::string("''"); + if(!s) { + return std::string(); + } char* output = new char[length * 2 + 1]; + mysql_real_escape_string(m_handle, output, s, length); + + std::string res = "'"; + res += output; + res += "'"; - mysql_real_escape_string(&m_mysqlHandle, output, s, length); - std::string r = "'"; - r += output; - r += "'"; delete[] output; - return r; + return res; } -void DatabaseMySQL::freeResult(DBResult* res) +int32 MySQLResult::getDataInt(const std::string& s) { - delete res; + listNames_t::iterator it = m_listNames.find(s); + if(it != m_listNames.end()) + return m_row[it->second] ? atoi(m_row[it->second]) : 0; + + g_logger.error(stdext::format("MySQLResult::getDataInt() Error: %d", s)); + return 0; // Failed } -DBResult::DBResult(MYSQL_RES* res) +int64 MySQLResult::getDataLong(const std::string& s) { - m_res = res; - m_listNames.clear(); + listNames_t::iterator it = m_listNames.find(s); + if(it != m_listNames.end()) + return m_row[it->second] ? atoll(m_row[it->second]) : 0; - MYSQL_FIELD* field; - int32_t i = 0; - while((field = mysql_fetch_field(m_res))) { - m_listNames[field->name] = i; - i++; - } + g_logger.error(stdext::format("MySQLResult::getDataLong() Error: %d", s)); + return 0; // Failed } -DBResult::~DBResult() +std::string MySQLResult::getDataString(const std::string& s) { - mysql_free_result(m_res); + listNames_t::iterator it = m_listNames.find(s); + if(it != m_listNames.end()) + return m_row[it->second] ? std::string(m_row[it->second]) : std::string(); + + g_logger.error(stdext::format("MySQLResult::getDataString() Error: %d", s)); + return std::string(); // Failed } -int32_t DBResult::getDataInt(const std::string &s) +const char* MySQLResult::getDataStream(const std::string& s, uint64& size) { - ListNames::iterator it = m_listNames.find(s); - if(it != m_listNames.end() ) { - if(m_row[it->second] == NULL) { - return 0; - } - else { - return atoi(m_row[it->second]); - } + size = 0; + listNames_t::iterator it = m_listNames.find(s); + if(it == m_listNames.end()) { + g_logger.error(stdext::format("MySQLResult::getDataStream() Error: %d", s)); + return NULL; } - g_logger.error(stdext::format("error during getDataInt(%s).", s)); - return 0; + if(!m_row[it->second]) + return NULL; + + size = mysql_fetch_lengths(m_resultHandle)[it->second]; + return m_row[it->second]; } -int64_t DBResult::getDataLong(const std::string &s) +void MySQLResult::free() { - ListNames::iterator it = m_listNames.find(s); - if(it != m_listNames.end()) { - if(m_row[it->second] == NULL) { - return 0; - } - else { - return atoll(m_row[it->second]); - } + if(!m_resultHandle) { + g_logger.fatal("MySQLResult::free() Error trying to free already freed result"); + return; } - g_logger.error(stdext::format("error during getDataLong(%s).", s)); - return 0; + mysql_free_result(m_resultHandle); + + delete m_resultHandle; + m_resultHandle = NULL; } -std::string DBResult::getDataString(const std::string &s) +bool MySQLResult::next() { - ListNames::iterator it = m_listNames.find(s); - if(it != m_listNames.end() ) { - if(m_row[it->second] == NULL) - return std::string(""); - else - return std::string(m_row[it->second]); - } + m_row = mysql_fetch_row(m_resultHandle); + return (m_row != NULL); +} - g_logger.error(stdext::format("error during getDataString(%s).", s)); - return std::string(""); +MySQLResult::~MySQLResult() +{ + if(m_resultHandle) + mysql_free_result(m_resultHandle); } -bool DBResult::next() +MySQLResult::MySQLResult(MYSQL_RES* result) { - m_row = mysql_fetch_row(m_res); - return m_row != NULL; + m_resultHandle = result; + m_listNames.clear(); + + MYSQL_FIELD* field; + int32 i = 0; + while((field = mysql_fetch_field(m_resultHandle))) { + m_listNames[field->name] = i++; + } } diff --git a/src/framework/sql/mysql.h b/src/framework/sql/mysql.h index dfa32f6f..74c57875 100644 --- a/src/framework/sql/mysql.h +++ b/src/framework/sql/mysql.h @@ -20,104 +20,70 @@ * THE SOFTWARE. */ -#ifndef DATABASEMYSQL_H -#define DATABASEMYSQL_H +#ifndef __DATABASE_MYSQL__ +#define __DATABASE_MYSQL__ #include "database.h" + #include -#include -class DBResult; +#ifdef WINDOWS + #include +#endif + +#include class DatabaseMySQL : public Database { -public: - DatabaseMySQL(); - ~DatabaseMySQL(); + public: + DatabaseMySQL(); + virtual ~DatabaseMySQL(); - void connect(const std::string& host, const std::string& user, const std::string& pass, - const std::string& db, uint16_t port, const std::string& unix_socket = ""); + virtual void connect(const std::string& host, const std::string& user, const std::string& pass, + const std::string& db, uint16 port, const std::string& unix_socket = ""); - bool beginTransaction(); - bool rollback(); - bool commit(); + virtual bool beginTransaction(); + virtual bool rollback(); + virtual bool commit(); - bool executeQuery(const std::string &query); - DBResultPtr storeQuery(const std::string &query); + virtual bool executeQuery(const std::string& query); + virtual DBResultPtr storeQuery(const std::string& query); - uint64_t getLastInsertedRowID(); + virtual std::string escapeString(const std::string &s); + virtual std::string escapeBlob(const char* s, uint32 length); - std::string escapeString(const std::string &s); - std::string escapeBlob(const char* s, uint32_t length); + virtual uint64 getLastInsertedRowID(); + virtual Fw::DatabaseEngine getDatabaseEngine() {return Fw::DatabaseMySQL;} - void freeResult(DBResult *res); + protected: + bool handleError(); + bool internalExecuteQuery(const std::string &query); -protected: - bool handleError(); - bool internalExecuteQuery(const std::string &query); - - bool m_running; - MYSQL m_mysqlHandle; + MYSQL* m_handle; }; -class DBResult : public LuaObject +class MySQLResult : public DBResult { -public: - DBResult(MYSQL_RES* res); - ~DBResult(); - friend class DatabaseMySQL; + public: + virtual int32 getDataInt(const std::string& s); + virtual int64 getDataLong(const std::string& s); + virtual std::string getDataString(const std::string& s); + virtual const char* getDataStream(const std::string& s, uint64& size); - int32_t getDataInt(const std::string &s); - int64_t getDataLong(const std::string &s); - std::string getDataString(const std::string &s); + virtual void free(); + virtual bool next(); + virtual int getRowCount() { return mysql_num_rows(m_resultHandle); } - bool next(); - int getRowCount() { return mysql_num_rows(m_res); } + protected: + MySQLResult(MYSQL_RES* result); + virtual ~MySQLResult(); -private: - typedef std::map ListNames; - ListNames m_listNames; + typedef std::map listNames_t; + listNames_t m_listNames; - MYSQL_RES* m_res; - MYSQL_ROW m_row; -}; - -class DBTransaction -{ -public: - DBTransaction(DatabaseMySQL* database) { - m_database = database; - m_state = STATE_NO_START; - } - - ~DBTransaction() { - if(m_state == STATE_START) { - m_database->rollback(); - } - } - - bool begin() { - m_state = STATE_START; - return m_database->beginTransaction(); - } - - bool commit() { - if(m_state == STATE_START) { - m_state = STEATE_COMMIT; - return m_database->commit(); - } else { - return false; - } - } - -private: - enum TransactionStates_t { - STATE_NO_START, STATE_START, STEATE_COMMIT - }; - - TransactionStates_t m_state; - DatabaseMySQL* m_database; + MYSQL_RES* m_resultHandle; + MYSQL_ROW m_row; }; #endif