From 02d32565e98d04909941503d68c89bb66d31833b Mon Sep 17 00:00:00 2001 From: BeniS Date: Sun, 3 Mar 2013 19:19:22 +1300 Subject: [PATCH 01/10] Work on sql framework classes --- CMakeLists.txt | 1 + src/framework/CMakeLists.txt | 1 + src/framework/cmake/FindMySQL.cmake | 11 +- src/framework/const.h | 7 + src/framework/sql/database.cpp | 62 +++++- src/framework/sql/database.h | 293 +++++++++++++++++++++++++++- src/framework/sql/declarations.h | 12 +- src/framework/sql/mysql.cpp | 241 ++++++++++++----------- src/framework/sql/mysql.h | 120 ++++-------- 9 files changed, 545 insertions(+), 203 deletions(-) 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; } -Database::~Database() +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; +} + +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; -typedef stdext::shared_object_ptr DBResultPtr; +class DatabaseMySQL; +class MySQLResult; + +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; + listNames_t::iterator it = m_listNames.find(s); + if(it != m_listNames.end()) + return m_row[it->second] ? atoll(m_row[it->second]) : 0; + + g_logger.error(stdext::format("MySQLResult::getDataLong() Error: %d", s)); + return 0; // Failed +} + +std::string MySQLResult::getDataString(const std::string& s) +{ + 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 +} + +const char* MySQLResult::getDataStream(const std::string& s, uint64& size) +{ + 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; + } + + if(!m_row[it->second]) + return NULL; + + size = mysql_fetch_lengths(m_resultHandle)[it->second]; + return m_row[it->second]; +} + +void MySQLResult::free() +{ + if(!m_resultHandle) { + g_logger.fatal("MySQLResult::free() Error trying to free already freed result"); + return; + } + + mysql_free_result(m_resultHandle); + + delete m_resultHandle; + m_resultHandle = NULL; +} + +bool MySQLResult::next() +{ + m_row = mysql_fetch_row(m_resultHandle); + return (m_row != NULL); +} + +MySQLResult::~MySQLResult() +{ + if(m_resultHandle) + mysql_free_result(m_resultHandle); +} + +MySQLResult::MySQLResult(MYSQL_RES* result) +{ + m_resultHandle = result; m_listNames.clear(); MYSQL_FIELD* field; - int32_t i = 0; - while((field = mysql_fetch_field(m_res))) { - m_listNames[field->name] = i; - i++; + int32 i = 0; + while((field = mysql_fetch_field(m_resultHandle))) { + m_listNames[field->name] = i++; } } - -DBResult::~DBResult() -{ - mysql_free_result(m_res); -} - -int32_t DBResult::getDataInt(const std::string &s) -{ - 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]); - } - } - - g_logger.error(stdext::format("error during getDataInt(%s).", s)); - return 0; -} - -int64_t DBResult::getDataLong(const std::string &s) -{ - 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]); - } - } - - g_logger.error(stdext::format("error during getDataLong(%s).", s)); - return 0; -} - -std::string DBResult::getDataString(const std::string &s) -{ - 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]); - } - - g_logger.error(stdext::format("error during getDataString(%s).", s)); - return std::string(""); -} - -bool DBResult::next() -{ - m_row = mysql_fetch_row(m_res); - return m_row != NULL; -} 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; +#include + +#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 From 8db85e4e3ac7592f9caa224614a7623c4d7dc097 Mon Sep 17 00:00:00 2001 From: BeniS Date: Sun, 3 Mar 2013 19:43:25 +1300 Subject: [PATCH 02/10] Use stdext::millis not g_clock.millis --- src/framework/sql/database.h | 3 +-- src/framework/sql/declarations.h | 2 +- src/framework/sql/mysql.cpp | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/framework/sql/database.h b/src/framework/sql/database.h index a5a72751..8161cdd5 100644 --- a/src/framework/sql/database.h +++ b/src/framework/sql/database.h @@ -24,7 +24,6 @@ #define DATABASE_H #include "declarations.h" -#include #include #include @@ -48,7 +47,7 @@ class Database : public LuaObject /** * Database ... */ - virtual void use() {m_use = g_clock.millis();} + virtual void use() {m_use = stdext::millis();} /** * Database connector. diff --git a/src/framework/sql/declarations.h b/src/framework/sql/declarations.h index 55eb62d7..8111389a 100644 --- a/src/framework/sql/declarations.h +++ b/src/framework/sql/declarations.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2012 TitanCore + * Copyright (c) 2010-2012 OTClient * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/framework/sql/mysql.cpp b/src/framework/sql/mysql.cpp index b3305b39..7d6164ac 100644 --- a/src/framework/sql/mysql.cpp +++ b/src/framework/sql/mysql.cpp @@ -76,10 +76,10 @@ bool DatabaseMySQL::handleError() g_logger.error("MYSQL connection lost, trying to reconnect..."); setConnected(false); - ticks_t startTime = g_clock.millis(); + ticks_t startTime = stdext::millis(); while(true) { bool connected = (mysql_ping(m_handle) == 0); - ticks_t diffTime = (g_clock.millis() - startTime); + ticks_t diffTime = (stdext::millis() - startTime); if(connected) { g_logger.info(stdext::format("MySQL reconneted in %d ms", diffTime)); setConnected(true); From 48ac91d173a79d34ae9325bd1547055cc39380bd Mon Sep 17 00:00:00 2001 From: BeniS Date: Mon, 4 Mar 2013 01:27:07 +1300 Subject: [PATCH 03/10] A few clean ups and also tested, its working fine --- src/framework/sql/database.h | 2 +- src/framework/sql/declarations.h | 2 +- src/framework/sql/mysql.cpp | 2 -- src/framework/sql/mysql.h | 4 ++-- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/framework/sql/database.h b/src/framework/sql/database.h index 8161cdd5..9b189877 100644 --- a/src/framework/sql/database.h +++ b/src/framework/sql/database.h @@ -239,7 +239,7 @@ class DBInsert /** * Associates with given database handler. * - * @param Database* database wrapper + * @param DatabasePtr database wrapper */ DBInsert(DatabasePtr db): m_db(db), m_rows(0) {} ~DBInsert() {} diff --git a/src/framework/sql/declarations.h b/src/framework/sql/declarations.h index 8111389a..9df1d34f 100644 --- a/src/framework/sql/declarations.h +++ b/src/framework/sql/declarations.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2012 OTClient + * Copyright (c) 2010-2013 OTClient * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/framework/sql/mysql.cpp b/src/framework/sql/mysql.cpp index 7d6164ac..2d127f20 100644 --- a/src/framework/sql/mysql.cpp +++ b/src/framework/sql/mysql.cpp @@ -253,8 +253,6 @@ void MySQLResult::free() } mysql_free_result(m_resultHandle); - - delete m_resultHandle; m_resultHandle = NULL; } diff --git a/src/framework/sql/mysql.h b/src/framework/sql/mysql.h index 74c57875..5a90a207 100644 --- a/src/framework/sql/mysql.h +++ b/src/framework/sql/mysql.h @@ -20,8 +20,8 @@ * THE SOFTWARE. */ -#ifndef __DATABASE_MYSQL__ -#define __DATABASE_MYSQL__ +#ifndef DATABASEMYSQL_H +#define DATABASEMYSQL_H #include "database.h" From f50c63e9e53c312eb7ef29bbf19171c4f8e299f4 Mon Sep 17 00:00:00 2001 From: BeniS Date: Mon, 4 Mar 2013 04:11:29 +1300 Subject: [PATCH 04/10] Removed singleton instance & added more lua bindings * Some tidying up --- src/framework/luafunctions.cpp | 21 ++++++++-- src/framework/sql/database.cpp | 11 ------ src/framework/sql/database.h | 71 ++++++++++++++-------------------- 3 files changed, 48 insertions(+), 55 deletions(-) diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index 43f477a9..1f11ef09 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -840,6 +840,19 @@ void Application::registerLuaFunctions() #ifdef FW_SQL // Database g_lua.registerClass(); + g_lua.bindClassMemberFunction("getDatabaseEngine", &Database::getDatabaseEngine); + g_lua.bindClassMemberFunction("isConnected", &Database::isConnected); + g_lua.bindClassMemberFunction("getStringComparer", &Database::getStringComparer); + g_lua.bindClassMemberFunction("getUpdateLimiter", &Database::getUpdateLimiter); + g_lua.bindClassMemberFunction("getLastInsertedRowID", &Database::getLastInsertedRowID); + g_lua.bindClassMemberFunction("escapeString", &Database::escapeString); + //g_lua.bindClassMemberFunction("escapeBlob", &Database::escapeBlob); // need to write a cast for this type to work (if needed) + + // DBQuery (not sure if this class will work as a luafunction) + /*g_lua.registerClass(); + g_lua.bindClassStaticFunction("create", []{ return DBQuery(); }); + g_lua.bindClassMemberFunction("append", &DBQuery::append); + g_lua.bindClassMemberFunction("set", &DBQuery::set);*/ // DBResult g_lua.registerClass(); @@ -847,15 +860,17 @@ void Application::registerLuaFunctions() g_lua.bindClassMemberFunction("getDataLong", &DBResult::getDataLong); g_lua.bindClassMemberFunction("getDataString", &DBResult::getDataString); g_lua.bindClassMemberFunction("getRowCount", &DBResult::getRowCount); + g_lua.bindClassMemberFunction("free", &DBResult::free); g_lua.bindClassMemberFunction("next", &DBResult::next); - - // Mysql + // MySQL g_lua.registerClass(); g_lua.bindClassStaticFunction("create", []{ return DatabaseMySQLPtr(new DatabaseMySQL); }); g_lua.bindClassMemberFunction("connect", &DatabaseMySQL::connect); + g_lua.bindClassMemberFunction("beginTransaction", &DatabaseMySQL::beginTransaction); + g_lua.bindClassMemberFunction("rollback", &DatabaseMySQL::rollback); + g_lua.bindClassMemberFunction("commit", &DatabaseMySQL::commit); g_lua.bindClassMemberFunction("executeQuery", &DatabaseMySQL::executeQuery); g_lua.bindClassMemberFunction("storeQuery", &DatabaseMySQL::storeQuery); - g_lua.bindClassMemberFunction("escapeString", &DatabaseMySQL::escapeString); #endif } diff --git a/src/framework/sql/database.cpp b/src/framework/sql/database.cpp index ff3f052f..f1fbf1a2 100644 --- a/src/framework/sql/database.cpp +++ b/src/framework/sql/database.cpp @@ -21,19 +21,8 @@ */ #include "database.h" -#include "mysql.h" 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) { diff --git a/src/framework/sql/database.h b/src/framework/sql/database.h index 9b189877..9392c983 100644 --- a/src/framework/sql/database.h +++ b/src/framework/sql/database.h @@ -32,23 +32,6 @@ class Database : public LuaObject { 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 = stdext::millis();} - /** * Database connector. * @@ -67,9 +50,9 @@ class Database : public LuaObject * 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;} + virtual bool beginTransaction() { return false; } + virtual bool rollback() { return false; } + virtual bool commit() { return false; } /** * Executes command. @@ -79,7 +62,7 @@ class Database : public LuaObject * @param std::string query command * @return true on success, false on error */ - virtual bool executeQuery(const std::string& query) {return false;} + virtual bool executeQuery(const std::string& query) { return false; } /** * Queries database. @@ -89,7 +72,7 @@ class Database : public LuaObject * @param std::string query * @return results object (null on error) */ - virtual DBResultPtr storeQuery(const std::string& query) {return nullptr;} + virtual DBResultPtr storeQuery(const std::string& query) { return nullptr; } /** * Escapes string for query. @@ -99,7 +82,7 @@ class Database : public LuaObject * @param std::string string to be escaped * @return quoted string */ - virtual std::string escapeString(const std::string&) {return "''";} + virtual std::string escapeString(const std::string&) { return "''"; } /** * Escapes binary stream for query. @@ -110,29 +93,29 @@ class Database : public LuaObject * @param long stream length * @return quoted string */ - virtual std::string escapeBlob(const char*, uint32) {return "''";} + 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;} + 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;";} + 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;} + virtual Fw::DatabaseEngine getDatabaseEngine() { return Fw::DatabaseNone; } /** * Database connected. @@ -141,8 +124,11 @@ class Database : public LuaObject * * @return whether or not the database is connected. */ - bool isConnected() const {return m_connected;} + bool isConnected() { return m_connected; } + friend class DBTransaction; + + protected: /** * Database set connected. * @@ -150,14 +136,13 @@ class Database : public LuaObject */ void setConnected(bool connected) { m_connected = connected; } - protected: - virtual bool handleError() {return false;} - virtual bool internalExecuteQuery(const std::string &query) {return false;} + 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;} + virtual ~Database() { m_connected = false; } ticks_t m_use; bool m_connected; @@ -173,25 +158,25 @@ class DBResult : public LuaObject *\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;} + 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;} + 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 "";} + 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 "";} + virtual const char* getDataStream(const std::string&, uint64&) { return ""; } /** Result freeing */ @@ -200,7 +185,7 @@ class DBResult : public LuaObject /** Moves to next result in set *\returns true if moved, false if there are no more results. */ - virtual bool next() {return false;} + virtual bool next() { return false; } /** Returned the number of rows from result *\returns integer value of row amount, 0 if result is empty. @@ -217,12 +202,15 @@ class DBResult : public LuaObject * * By using this class for your queries you lock and unlock database for threads. */ -class DBQuery : public std::stringstream +class DBQuery : public std::stringstream, public LuaObject { friend class Database; public: - DBQuery() {databaseLock.lock();} - ~DBQuery() {databaseLock.unlock();} + DBQuery() { databaseLock.lock(); } + ~DBQuery() { databaseLock.unlock(); } + + void set(std::string& query) { str(query); } + void append(char query) { putback(query); } protected: static boost::recursive_mutex databaseLock; @@ -259,6 +247,7 @@ class DBInsert * @param std::string& row data */ bool addRow(const std::string& row); + /** * Allows to use addRow() with stringstream as parameter. */ From f4263384bc5fb877b9da579502d007477b22c818 Mon Sep 17 00:00:00 2001 From: BeniS Date: Mon, 4 Mar 2013 18:03:04 +1300 Subject: [PATCH 05/10] Fix boost issue on Ubuntu and fix MySQL lib issue * Add framework options for configuration * libboost_chrono-mt.a dependency issue (with clock_gettime) * MySQL lib must be added before zlib --- CMakeLists.txt | 11 ++++++----- src/framework/CMakeLists.txt | 11 +++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3bf7fa78..0b1ef98f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,11 +3,12 @@ project(otclient) set(VERSION "0.6.2") -set(FRAMEWORK_SOUND ON) -set(FRAMEWORK_GRAPHICS ON) -set(FRAMEWORK_XML ON) -set(FRAMEWORK_NET ON) -set(FRAMEWORK_SQL ON) +option(FRAMEWORK_SOUND "Use SOUND " ON) +option(FRAMEWORK_GRAPHICS "Use GRAPHICS " ON) +option(FRAMEWORK_XML "Use XML " ON) +option(FRAMEWORK_NET "Use NET " ON) +option(FRAMEWORK_SQL "Use 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 7198eb20..2b5b2aa3 100644 --- a/src/framework/CMakeLists.txt +++ b/src/framework/CMakeLists.txt @@ -219,14 +219,12 @@ message(STATUS "LuaJIT: " ${LUAJIT}) find_package(PhysFS REQUIRED) find_package(OpenSSL REQUIRED) -find_package(ZLIB REQUIRED) set(framework_LIBRARIES ${framework_LIBRARIES} ${Boost_LIBRARIES} ${LUA_LIBRARY} ${PHYSFS_LIBRARY} ${OPENSSL_LIBRARIES} - ${ZLIB_LIBRARY} ) set(framework_INCLUDE_DIRS ${framework_INCLUDE_DIRS} @@ -235,7 +233,6 @@ set(framework_INCLUDE_DIRS ${framework_INCLUDE_DIRS} ${LUA_INCLUDE_DIR} ${PHYSFS_INCLUDE_DIR} ${OpenSSL_INCLUDE_DIR} - ${ZLIB_INCLUDE_DIR} ) if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") @@ -277,7 +274,7 @@ else() else() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -rdynamic -Wl,-rpath,./libs") # rdynamic is needed by backtrace.h used in crash handler - set(SYSTEM_LIBRARIES dl) + set(SYSTEM_LIBRARIES dl rt) endif() set(framework_LIBRARIES ${framework_LIBRARIES} ${SYSTEM_LIBRARIES}) endif() @@ -462,8 +459,6 @@ if(FRAMEWORK_SOUND) set(framework_LIBRARIES ${framework_LIBRARIES} winmm) elseif(APPLE) set(framework_LIBRARIES ${framework_LIBRARIES} System) - else() - set(framework_LIBRARIES ${framework_LIBRARIES} rt) endif() set(framework_SOURCES ${framework_SOURCES} @@ -542,6 +537,10 @@ if(FRAMEWORK_SQL) set(framework_DEFINITIONS ${framework_DEFINITIONS} -DFW_SQL) endif() +find_package(ZLIB REQUIRED) +set(framework_LIBRARIES ${framework_LIBRARIES} ${ZLIB_LIBRARY}) +set(framework_INCLUDE_DIRS ${framework_INCLUDE_DIRS} ${ZLIB_INCLUDE_DIR}) + include_directories(${framework_INCLUDE_DIRS}) add_definitions(${framework_DEFINITIONS}) From 15ee7962f1e8f6011eeb7afdd7cb5053e5e77826 Mon Sep 17 00:00:00 2001 From: BeniS Date: Tue, 5 Mar 2013 17:31:19 +1300 Subject: [PATCH 06/10] Some minor clean ups --- src/framework/sql/database.h | 3 +- src/framework/sql/declarations.h | 7 +-- src/framework/sql/mysql.cpp | 82 +++++++++++++++----------------- src/framework/sql/mysql.h | 8 ++-- 4 files changed, 49 insertions(+), 51 deletions(-) diff --git a/src/framework/sql/database.h b/src/framework/sql/database.h index 9392c983..c2721796 100644 --- a/src/framework/sql/database.h +++ b/src/framework/sql/database.h @@ -262,7 +262,8 @@ class DBInsert DatabasePtr m_db; uint32 m_rows; - std::string m_query, m_buf; + std::string m_query; + std::string m_buf; }; class DBTransaction diff --git a/src/framework/sql/declarations.h b/src/framework/sql/declarations.h index 9df1d34f..04ab4889 100644 --- a/src/framework/sql/declarations.h +++ b/src/framework/sql/declarations.h @@ -28,13 +28,14 @@ class Database; class DBResult; +typedef stdext::shared_object_ptr DatabasePtr; +typedef stdext::shared_object_ptr DBResultPtr; + +// MySQL class DatabaseMySQL; class MySQLResult; -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 2d127f20..f5c9fcf1 100644 --- a/src/framework/sql/mysql.cpp +++ b/src/framework/sql/mysql.cpp @@ -100,7 +100,7 @@ bool DatabaseMySQL::beginTransaction() bool DatabaseMySQL::rollback() { 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; } @@ -110,7 +110,7 @@ bool DatabaseMySQL::rollback() bool DatabaseMySQL::commit() { 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; } @@ -130,13 +130,11 @@ bool DatabaseMySQL::internalExecuteQuery(const std::string &query) bool DatabaseMySQL::executeQuery(const std::string &query) { - //LOG_ONDELAY(500); - if(internalExecuteQuery(query)) { - MYSQL_RES* m_res = mysql_store_result(m_handle); + MYSQL_RES* res = mysql_store_result(m_handle); - if(m_res) { - mysql_free_result(m_res); + if(res) { + mysql_free_result(res); } else if(mysql_errno(m_handle) != 0) { handleError(); } @@ -147,26 +145,24 @@ bool DatabaseMySQL::executeQuery(const std::string &query) return false; } -DBResultPtr DatabaseMySQL::storeQuery(const std::string &query) +DBResultPtr DatabaseMySQL::storeQuery(const std::string& query) { - //LOG_ONDELAY(500); - while(internalExecuteQuery(query)) { - MYSQL_RES* m_res = mysql_store_result(m_handle); + MYSQL_RES* res = mysql_store_result(m_handle); - if(m_res) { - DBResultPtr res = (DBResultPtr)MySQLResultPtr(new MySQLResult(m_res)); - if(!verifyResult(res)) + if(res) { + DBResultPtr result = (DBResultPtr)MySQLResultPtr(new MySQLResult(res)); + if(!verifyResult(result)) break; - return res; + return result; } else if(mysql_errno(m_handle) != 0) { if(!handleError()) break; } - //mSleep(10); + stdext::millisleep(10); } return nullptr; @@ -201,81 +197,81 @@ std::string DatabaseMySQL::escapeBlob(const char* s, uint32 length) int32 MySQLResult::getDataInt(const std::string& s) { - listNames_t::iterator it = m_listNames.find(s); - if(it != m_listNames.end()) + RowNames_t::iterator it = m_names.find(s); + if(it != m_names.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 + g_logger.error(stdext::format("[MySQLResult::getDataInt] Error: %d", s)); + return 0; } int64 MySQLResult::getDataLong(const std::string& s) { - listNames_t::iterator it = m_listNames.find(s); - if(it != m_listNames.end()) + RowNames_t::iterator it = m_names.find(s); + if(it != m_names.end()) return m_row[it->second] ? atoll(m_row[it->second]) : 0; - g_logger.error(stdext::format("MySQLResult::getDataLong() Error: %d", s)); - return 0; // Failed + g_logger.error(stdext::format("[MySQLResult::getDataLong] Error: %d", s)); + return 0; } std::string MySQLResult::getDataString(const std::string& s) { - listNames_t::iterator it = m_listNames.find(s); - if(it != m_listNames.end()) + RowNames_t::iterator it = m_names.find(s); + if(it != m_names.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 + g_logger.error(stdext::format("[MySQLResult::getDataString] Error: %d", s)); + return std::string(); } const char* MySQLResult::getDataStream(const std::string& s, uint64& size) { 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)); + RowNames_t::iterator it = m_names.find(s); + if(it == m_names.end()) { + g_logger.error(stdext::format("[MySQLResult::getDataStream] Error: %d", s)); return NULL; } if(!m_row[it->second]) return NULL; - size = mysql_fetch_lengths(m_resultHandle)[it->second]; + size = mysql_fetch_lengths(m_result)[it->second]; return m_row[it->second]; } void MySQLResult::free() { - if(!m_resultHandle) { - g_logger.fatal("MySQLResult::free() Error trying to free already freed result"); + if(!m_result) { + g_logger.fatal("[MySQLResult::free] Error: trying to free already freed result"); return; } - mysql_free_result(m_resultHandle); - m_resultHandle = NULL; + mysql_free_result(m_result); + m_result = NULL; } bool MySQLResult::next() { - m_row = mysql_fetch_row(m_resultHandle); + m_row = mysql_fetch_row(m_result); return (m_row != NULL); } MySQLResult::~MySQLResult() { - if(m_resultHandle) - mysql_free_result(m_resultHandle); + if(m_result) + mysql_free_result(m_result); } MySQLResult::MySQLResult(MYSQL_RES* result) { - m_resultHandle = result; - m_listNames.clear(); + m_result = result; + m_names.clear(); MYSQL_FIELD* field; int32 i = 0; - while((field = mysql_fetch_field(m_resultHandle))) { - m_listNames[field->name] = i++; + while((field = mysql_fetch_field(m_result))) { + m_names[field->name] = i++; } } diff --git a/src/framework/sql/mysql.h b/src/framework/sql/mysql.h index 5a90a207..632bcfb7 100644 --- a/src/framework/sql/mysql.h +++ b/src/framework/sql/mysql.h @@ -73,16 +73,16 @@ class MySQLResult : public DBResult virtual void free(); virtual bool next(); - virtual int getRowCount() { return mysql_num_rows(m_resultHandle); } + virtual int getRowCount() { return mysql_num_rows(m_result); } protected: MySQLResult(MYSQL_RES* result); virtual ~MySQLResult(); - typedef std::map listNames_t; - listNames_t m_listNames; + typedef std::map RowNames_t; + RowNames_t m_names; - MYSQL_RES* m_resultHandle; + MYSQL_RES* m_result; MYSQL_ROW m_row; }; From 9305053e3486c5dd8e564cad73cf1f2e5251e74d Mon Sep 17 00:00:00 2001 From: BeniS Date: Tue, 5 Mar 2013 17:47:43 +1300 Subject: [PATCH 07/10] Few issues with compilation and layout --- src/framework/sql/database.h | 419 +++++++++++++++++------------------ src/framework/sql/mysql.h | 70 +++--- 2 files changed, 245 insertions(+), 244 deletions(-) diff --git a/src/framework/sql/database.h b/src/framework/sql/database.h index c2721796..cc635d82 100644 --- a/src/framework/sql/database.h +++ b/src/framework/sql/database.h @@ -31,170 +31,169 @@ class Database : public LuaObject { - public: - /** - * 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 = "") {} +public: + friend class DBTransaction; - /** - * 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. - */ + Database(): m_connected(false) {} + virtual ~Database() { m_connected = false; } - virtual bool beginTransaction() { return false; } - virtual bool rollback() { return false; } - virtual bool commit() { return false; } + /** + * 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 = "") {} - /** - * 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; } + /** + * 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. + */ - /** - * 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; } + virtual bool beginTransaction() { return false; } + virtual bool rollback() { return false; } + virtual bool commit() { return false; } - /** - * 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 "''"; } + /** + * 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; } - /** - * 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 "''"; } + /** + * 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; } - /** - * 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; } + /** + * 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 "''"; } - /** - * Get case insensitive string comparison operator - * - * @return the case insensitive operator - */ - virtual std::string getStringComparer() { return "= "; } - virtual std::string getUpdateLimiter() { return " LIMIT 1;"; } + /** + * 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 "''"; } - /** - * Get database engine - * - * @return the database engine type - */ - virtual Fw::DatabaseEngine getDatabaseEngine() { return Fw::DatabaseNone; } + /** + * 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; } - /** - * Database connected. - * - * Returns whether or not the database is connected. - * - * @return whether or not the database is connected. - */ - bool isConnected() { return m_connected; } + /** + * Get case insensitive string comparison operator + * + * @return the case insensitive operator + */ + virtual std::string getStringComparer() { return "= "; } + virtual std::string getUpdateLimiter() { return " LIMIT 1;"; } - friend class DBTransaction; + /** + * Get database engine + * + * @return the database engine type + */ + virtual Fw::DatabaseEngine getDatabaseEngine() { return Fw::DatabaseNone; } - protected: - /** - * Database set connected. - * - * Sets the database to know that it is connected. - */ - void setConnected(bool connected) { m_connected = connected; } + /** + * Database connected. + * + * Returns whether or not the database is connected. + * + * @return whether or not the database is connected. + */ + bool isConnected() { return m_connected; } - virtual bool handleError() { return false; } - virtual bool internalExecuteQuery(const std::string &query) { return false; } +protected: + /** + * Database set connected. + * + * Sets the database to know that it is connected. + */ + void setConnected(bool connected) { m_connected = connected; } - DBResultPtr verifyResult(DBResultPtr result); + virtual bool handleError() { return false; } + virtual bool internalExecuteQuery(const std::string &query) { return false; } - Database(): m_connected(false) {} - virtual ~Database() { m_connected = false; } + DBResultPtr verifyResult(DBResultPtr result); - ticks_t m_use; - bool m_connected; + ticks_t m_use; + bool m_connected; - private: - static DatabasePtr m_instance; +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; } +public: + DBResult() {} + virtual ~DBResult() {} - /** 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 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 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 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 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 ""; } + /** 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 ""; } - /** Result freeing - */ - virtual void free() {} + /** 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 ""; } - /** Moves to next result in set - *\returns true if moved, false if there are no more results. - */ - virtual bool next() { return false; } + /** Result freeing + */ + virtual void free() {} - /** Returned the number of rows from result - *\returns integer value of row amount, 0 if result is empty. - */ - virtual int getRowCount() { return 0; } + /** Moves to next result in set + *\returns true if moved, false if there are no more results. + */ + virtual bool next() { return false; } - protected: - DBResult() {} - virtual ~DBResult() {} + /** Returned the number of rows from result + *\returns integer value of row amount, 0 if result is empty. + */ + virtual int getRowCount() { return 0; } }; /** @@ -204,16 +203,16 @@ class DBResult : public LuaObject */ class DBQuery : public std::stringstream, public LuaObject { - friend class Database; - public: - DBQuery() { databaseLock.lock(); } - ~DBQuery() { databaseLock.unlock(); } +friend class Database; +public: + DBQuery() { databaseLock.lock(); } + ~DBQuery() { databaseLock.unlock(); } - void set(std::string& query) { str(query); } - void append(char query) { putback(query); } + void set(std::string& query) { str(query); } + void append(char query) { putback(query); } - protected: - static boost::recursive_mutex databaseLock; +protected: + static boost::recursive_mutex databaseLock; }; /** @@ -223,87 +222,87 @@ class DBQuery : public std::stringstream, public LuaObject */ class DBInsert { - public: - /** - * Associates with given database handler. - * - * @param DatabasePtr database wrapper - */ - DBInsert(DatabasePtr db): m_db(db), m_rows(0) {} - ~DBInsert() {} +public: + /** + * Associates with given database handler. + * + * @param DatabasePtr database wrapper + */ + DBInsert(const DatabasePtr& db): m_db(db), m_rows(0) {} + ~DBInsert() {} - /** - * Sets query prototype. - * - * @param std::string& INSERT query - */ - void setQuery(const std::string& query); + /** + * 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); + /** + * 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); + /** + * Allows to use addRow() with stringstream as parameter. + */ + bool addRow(std::stringstream& row); - /** - * Executes current buffer. - */ - bool execute(); + /** + * Executes current buffer. + */ + bool execute(); - protected: - DatabasePtr m_db; +protected: + DatabasePtr m_db; - uint32 m_rows; - std::string m_query; - std::string m_buf; + uint32 m_rows; + std::string m_query; + std::string m_buf; }; class DBTransaction { - public: - DBTransaction(DatabasePtr database) - { - m_db = database; - m_state = STATE_FRESH; - } +public: + DBTransaction(DatabasePtr database) + { + m_db = database; + m_state = STATE_FRESH; + } - ~DBTransaction() - { - if(m_state == STATE_READY) - m_db->rollback(); - } + ~DBTransaction() + { + if(m_state == STATE_READY) + m_db->rollback(); + } - bool begin() - { - m_state = STATE_READY; - return m_db->beginTransaction(); - } + bool begin() + { + m_state = STATE_READY; + return m_db->beginTransaction(); + } - bool commit() - { - if(m_state != STATE_READY) - return false; + bool commit() + { + if(m_state != STATE_READY) + return false; - m_state = STATE_DONE; - return m_db->commit(); - } + m_state = STATE_DONE; + return m_db->commit(); + } - private: - DatabasePtr m_db; - enum TransactionStates_t - { - STATE_FRESH, - STATE_READY, - STATE_DONE - } m_state; +private: + DatabasePtr m_db; + enum TransactionStates_t + { + STATE_FRESH, + STATE_READY, + STATE_DONE + } m_state; }; #endif diff --git a/src/framework/sql/mysql.h b/src/framework/sql/mysql.h index 632bcfb7..0660dcf3 100644 --- a/src/framework/sql/mysql.h +++ b/src/framework/sql/mysql.h @@ -35,55 +35,57 @@ class DatabaseMySQL : public Database { - public: - DatabaseMySQL(); - virtual ~DatabaseMySQL(); +public: + DatabaseMySQL(); + virtual ~DatabaseMySQL(); - 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 = ""); + 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 = ""); - virtual bool beginTransaction(); - virtual bool rollback(); - virtual bool commit(); + virtual bool beginTransaction(); + virtual bool rollback(); + virtual bool commit(); - virtual bool executeQuery(const std::string& query); - virtual DBResultPtr storeQuery(const std::string& query); + virtual bool executeQuery(const std::string& query); + virtual DBResultPtr storeQuery(const std::string& query); - virtual std::string escapeString(const std::string &s); - virtual std::string escapeBlob(const char* s, uint32 length); + virtual std::string escapeString(const std::string &s); + virtual std::string escapeBlob(const char* s, uint32 length); - virtual uint64 getLastInsertedRowID(); - virtual Fw::DatabaseEngine getDatabaseEngine() {return Fw::DatabaseMySQL;} + virtual uint64 getLastInsertedRowID(); + virtual Fw::DatabaseEngine getDatabaseEngine() {return Fw::DatabaseMySQL;} - protected: - bool handleError(); - bool internalExecuteQuery(const std::string &query); +protected: + bool handleError(); + bool internalExecuteQuery(const std::string &query); - MYSQL* m_handle; + MYSQL* m_handle; }; class MySQLResult : public 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); - virtual void free(); - virtual bool next(); - virtual int getRowCount() { return mysql_num_rows(m_result); } +friend class DatabaseMySQL; - protected: - MySQLResult(MYSQL_RES* result); - virtual ~MySQLResult(); +public: + MySQLResult(MYSQL_RES* result); + virtual ~MySQLResult(); - typedef std::map RowNames_t; - RowNames_t m_names; + 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); - MYSQL_RES* m_result; - MYSQL_ROW m_row; + virtual void free(); + virtual bool next(); + virtual int getRowCount() { return mysql_num_rows(m_result); } + +protected: + typedef std::map RowNames_t; + RowNames_t m_names; + + MYSQL_RES* m_result; + MYSQL_ROW m_row; }; #endif From a7b27ef8dd5425675fdcba228a96300aaab580b6 Mon Sep 17 00:00:00 2001 From: BeniS Date: Tue, 5 Mar 2013 20:33:27 +1300 Subject: [PATCH 08/10] Forgot to add MySQLResult lua binding --- src/framework/luafunctions.cpp | 19 ++++++++++++++++--- src/framework/sql/database.h | 1 - 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index 1f11ef09..0076649e 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -848,17 +848,20 @@ void Application::registerLuaFunctions() g_lua.bindClassMemberFunction("escapeString", &Database::escapeString); //g_lua.bindClassMemberFunction("escapeBlob", &Database::escapeBlob); // need to write a cast for this type to work (if needed) - // DBQuery (not sure if this class will work as a luafunction) - /*g_lua.registerClass(); + // DBQuery + /* (not sure if this class will work as a luafunction) + g_lua.registerClass(); g_lua.bindClassStaticFunction("create", []{ return DBQuery(); }); g_lua.bindClassMemberFunction("append", &DBQuery::append); - g_lua.bindClassMemberFunction("set", &DBQuery::set);*/ + g_lua.bindClassMemberFunction("set", &DBQuery::set); + */ // DBResult g_lua.registerClass(); g_lua.bindClassMemberFunction("getDataInt", &DBResult::getDataInt); g_lua.bindClassMemberFunction("getDataLong", &DBResult::getDataLong); g_lua.bindClassMemberFunction("getDataString", &DBResult::getDataString); + //g_lua.bindClassMemberFunction("getDataStream", &DBResult::getDataStream); // need to write a cast for this type to work (if needed) g_lua.bindClassMemberFunction("getRowCount", &DBResult::getRowCount); g_lua.bindClassMemberFunction("free", &DBResult::free); g_lua.bindClassMemberFunction("next", &DBResult::next); @@ -872,5 +875,15 @@ void Application::registerLuaFunctions() g_lua.bindClassMemberFunction("commit", &DatabaseMySQL::commit); g_lua.bindClassMemberFunction("executeQuery", &DatabaseMySQL::executeQuery); g_lua.bindClassMemberFunction("storeQuery", &DatabaseMySQL::storeQuery); + + // MySQLResult + g_lua.registerClass(); + g_lua.bindClassMemberFunction("getDataInt", &MySQLResult::getDataInt); + g_lua.bindClassMemberFunction("getDataLong", &MySQLResult::getDataLong); + g_lua.bindClassMemberFunction("getDataString", &MySQLResult::getDataString); + //g_lua.bindClassMemberFunction("getDataStream", &MySQLResult::getDataStream); // need to write a cast for this type to work (if needed) + g_lua.bindClassMemberFunction("getRowCount", &MySQLResult::getRowCount); + g_lua.bindClassMemberFunction("free", &MySQLResult::free); + g_lua.bindClassMemberFunction("next", &MySQLResult::next); #endif } diff --git a/src/framework/sql/database.h b/src/framework/sql/database.h index cc635d82..2f33dede 100644 --- a/src/framework/sql/database.h +++ b/src/framework/sql/database.h @@ -27,7 +27,6 @@ #include #include -#include class Database : public LuaObject { From 61be2103ed923598e24da960e1d1eb8ae8ad507a Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Wed, 6 Mar 2013 12:54:27 -0300 Subject: [PATCH 09/10] Fix linkage --- src/framework/CMakeLists.txt | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/framework/CMakeLists.txt b/src/framework/CMakeLists.txt index 2b5b2aa3..ad4a4d5a 100644 --- a/src/framework/CMakeLists.txt +++ b/src/framework/CMakeLists.txt @@ -219,20 +219,22 @@ message(STATUS "LuaJIT: " ${LUAJIT}) find_package(PhysFS REQUIRED) find_package(OpenSSL REQUIRED) +find_package(ZLIB REQUIRED) set(framework_LIBRARIES ${framework_LIBRARIES} ${Boost_LIBRARIES} ${LUA_LIBRARY} ${PHYSFS_LIBRARY} ${OPENSSL_LIBRARIES} + ${ZLIB_LIBRARY} ) set(framework_INCLUDE_DIRS ${framework_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} - ${OPENGL_INCLUDE_DIR} ${LUA_INCLUDE_DIR} ${PHYSFS_INCLUDE_DIR} ${OpenSSL_INCLUDE_DIR} + ${framework_INCLUDE_DIRS} ) if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") @@ -276,8 +278,8 @@ else() set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -rdynamic -Wl,-rpath,./libs") # rdynamic is needed by backtrace.h used in crash handler set(SYSTEM_LIBRARIES dl rt) endif() - set(framework_LIBRARIES ${framework_LIBRARIES} ${SYSTEM_LIBRARIES}) endif() +set(framework_LIBRARIES ${framework_LIBRARIES} ${SYSTEM_LIBRARIES}) if(FRAMEWORK_GRAPHICS) set(OPENGLES "OFF" CACHE "Use OpenGL ES 1.0 or 2.0 (for mobiles devices)" STRING) @@ -453,7 +455,7 @@ if(FRAMEWORK_SOUND) find_package(Ogg REQUIRED) set(framework_INCLUDE_DIRS ${framework_INCLUDE_DIRS} ${OPENAL_INCLUDE_DIR} ${VORBISFILE_INCLUDE_DIR}) - set(framework_LIBRARIES ${framework_LIBRARIES} ${OPENAL_LIBRARY} ${VORBISFILE_LIBRARY} ${VORBIS_LIBRARY} ${OGG_LIBRARY}) + set(framework_LIBRARIES ${OPENAL_LIBRARY} ${VORBISFILE_LIBRARY} ${VORBIS_LIBRARY} ${OGG_LIBRARY} ${framework_LIBRARIES}) if(WIN32) set(framework_LIBRARIES ${framework_LIBRARIES} winmm) @@ -525,7 +527,7 @@ if(FRAMEWORK_SQL) find_package(MySQL REQUIRED) set(framework_INCLUDE_DIRS ${framework_INCLUDE_DIRS} ${MYSQL_INCLUDE_DIR}) - set(framework_LIBRARIES ${framework_LIBRARIES} ${MYSQL_LIBRARY}) + set(framework_LIBRARIES ${MYSQL_LIBRARY} ${framework_LIBRARIES}) set(framework_SOURCES ${framework_SOURCES} ${CMAKE_CURRENT_LIST_DIR}/sql/declarations.h @@ -537,10 +539,6 @@ if(FRAMEWORK_SQL) set(framework_DEFINITIONS ${framework_DEFINITIONS} -DFW_SQL) endif() -find_package(ZLIB REQUIRED) -set(framework_LIBRARIES ${framework_LIBRARIES} ${ZLIB_LIBRARY}) -set(framework_INCLUDE_DIRS ${framework_INCLUDE_DIRS} ${ZLIB_INCLUDE_DIR}) - include_directories(${framework_INCLUDE_DIRS}) add_definitions(${framework_DEFINITIONS}) From d911b38d6833b7fd2471d257678bd5048d516019 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Wed, 6 Mar 2013 12:59:59 -0300 Subject: [PATCH 10/10] defaults SQL to off --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b1ef98f..3ab151f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ option(FRAMEWORK_SOUND "Use SOUND " ON) option(FRAMEWORK_GRAPHICS "Use GRAPHICS " ON) option(FRAMEWORK_XML "Use XML " ON) option(FRAMEWORK_NET "Use NET " ON) -option(FRAMEWORK_SQL "Use SQL" ON) +option(FRAMEWORK_SQL "Use SQL" OFF) include(src/framework/CMakeLists.txt) include(src/client/CMakeLists.txt)