Removed singleton instance & added more lua bindings

* Some tidying up
This commit is contained in:
BeniS 2013-03-04 04:11:29 +13:00
parent 48ac91d173
commit f50c63e9e5
3 changed files with 48 additions and 55 deletions

View File

@ -840,6 +840,19 @@ void Application::registerLuaFunctions()
#ifdef FW_SQL #ifdef FW_SQL
// Database // Database
g_lua.registerClass<Database>(); g_lua.registerClass<Database>();
g_lua.bindClassMemberFunction<Database>("getDatabaseEngine", &Database::getDatabaseEngine);
g_lua.bindClassMemberFunction<Database>("isConnected", &Database::isConnected);
g_lua.bindClassMemberFunction<Database>("getStringComparer", &Database::getStringComparer);
g_lua.bindClassMemberFunction<Database>("getUpdateLimiter", &Database::getUpdateLimiter);
g_lua.bindClassMemberFunction<Database>("getLastInsertedRowID", &Database::getLastInsertedRowID);
g_lua.bindClassMemberFunction<Database>("escapeString", &Database::escapeString);
//g_lua.bindClassMemberFunction<Database>("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>();
g_lua.bindClassStaticFunction<DBQuery>("create", []{ return DBQuery(); });
g_lua.bindClassMemberFunction<DBQuery>("append", &DBQuery::append);
g_lua.bindClassMemberFunction<DBQuery>("set", &DBQuery::set);*/
// DBResult // DBResult
g_lua.registerClass<DBResult>(); g_lua.registerClass<DBResult>();
@ -847,15 +860,17 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<DBResult>("getDataLong", &DBResult::getDataLong); g_lua.bindClassMemberFunction<DBResult>("getDataLong", &DBResult::getDataLong);
g_lua.bindClassMemberFunction<DBResult>("getDataString", &DBResult::getDataString); g_lua.bindClassMemberFunction<DBResult>("getDataString", &DBResult::getDataString);
g_lua.bindClassMemberFunction<DBResult>("getRowCount", &DBResult::getRowCount); g_lua.bindClassMemberFunction<DBResult>("getRowCount", &DBResult::getRowCount);
g_lua.bindClassMemberFunction<DBResult>("free", &DBResult::free);
g_lua.bindClassMemberFunction<DBResult>("next", &DBResult::next); g_lua.bindClassMemberFunction<DBResult>("next", &DBResult::next);
// MySQL
// Mysql
g_lua.registerClass<DatabaseMySQL, Database>(); g_lua.registerClass<DatabaseMySQL, Database>();
g_lua.bindClassStaticFunction<DatabaseMySQL>("create", []{ return DatabaseMySQLPtr(new DatabaseMySQL); }); g_lua.bindClassStaticFunction<DatabaseMySQL>("create", []{ return DatabaseMySQLPtr(new DatabaseMySQL); });
g_lua.bindClassMemberFunction<DatabaseMySQL>("connect", &DatabaseMySQL::connect); g_lua.bindClassMemberFunction<DatabaseMySQL>("connect", &DatabaseMySQL::connect);
g_lua.bindClassMemberFunction<DatabaseMySQL>("beginTransaction", &DatabaseMySQL::beginTransaction);
g_lua.bindClassMemberFunction<DatabaseMySQL>("rollback", &DatabaseMySQL::rollback);
g_lua.bindClassMemberFunction<DatabaseMySQL>("commit", &DatabaseMySQL::commit);
g_lua.bindClassMemberFunction<DatabaseMySQL>("executeQuery", &DatabaseMySQL::executeQuery); g_lua.bindClassMemberFunction<DatabaseMySQL>("executeQuery", &DatabaseMySQL::executeQuery);
g_lua.bindClassMemberFunction<DatabaseMySQL>("storeQuery", &DatabaseMySQL::storeQuery); g_lua.bindClassMemberFunction<DatabaseMySQL>("storeQuery", &DatabaseMySQL::storeQuery);
g_lua.bindClassMemberFunction<DatabaseMySQL>("escapeString", &DatabaseMySQL::escapeString);
#endif #endif
} }

View File

@ -21,19 +21,8 @@
*/ */
#include "database.h" #include "database.h"
#include "mysql.h"
boost::recursive_mutex DBQuery::databaseLock; 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) DBResultPtr Database::verifyResult(DBResultPtr result)
{ {

View File

@ -32,23 +32,6 @@
class Database : public LuaObject class Database : public LuaObject
{ {
public: 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. * Database connector.
* *
@ -141,8 +124,11 @@ class Database : public LuaObject
* *
* @return whether or not the database is connected. * @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. * Database set connected.
* *
@ -150,7 +136,6 @@ class Database : public LuaObject
*/ */
void setConnected(bool connected) { m_connected = connected; } void setConnected(bool connected) { m_connected = connected; }
protected:
virtual bool handleError() { return false; } virtual bool handleError() { return false; }
virtual bool internalExecuteQuery(const std::string &query) { return false; } virtual bool internalExecuteQuery(const std::string &query) { return false; }
@ -217,13 +202,16 @@ class DBResult : public LuaObject
* *
* By using this class for your queries you lock and unlock database for threads. * 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; friend class Database;
public: public:
DBQuery() { databaseLock.lock(); } DBQuery() { databaseLock.lock(); }
~DBQuery() { databaseLock.unlock(); } ~DBQuery() { databaseLock.unlock(); }
void set(std::string& query) { str(query); }
void append(char query) { putback(query); }
protected: protected:
static boost::recursive_mutex databaseLock; static boost::recursive_mutex databaseLock;
}; };
@ -259,6 +247,7 @@ class DBInsert
* @param std::string& row data * @param std::string& row data
*/ */
bool addRow(const std::string& row); bool addRow(const std::string& row);
/** /**
* Allows to use addRow() with stringstream as parameter. * Allows to use addRow() with stringstream as parameter.
*/ */