From f50c63e9e53c312eb7ef29bbf19171c4f8e299f4 Mon Sep 17 00:00:00 2001 From: BeniS Date: Mon, 4 Mar 2013 04:11:29 +1300 Subject: [PATCH] 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. */