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
// 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
g_lua.registerClass<DBResult>();
@ -847,15 +860,17 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<DBResult>("getDataLong", &DBResult::getDataLong);
g_lua.bindClassMemberFunction<DBResult>("getDataString", &DBResult::getDataString);
g_lua.bindClassMemberFunction<DBResult>("getRowCount", &DBResult::getRowCount);
g_lua.bindClassMemberFunction<DBResult>("free", &DBResult::free);
g_lua.bindClassMemberFunction<DBResult>("next", &DBResult::next);
// Mysql
// MySQL
g_lua.registerClass<DatabaseMySQL, Database>();
g_lua.bindClassStaticFunction<DatabaseMySQL>("create", []{ return DatabaseMySQLPtr(new DatabaseMySQL); });
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>("storeQuery", &DatabaseMySQL::storeQuery);
g_lua.bindClassMemberFunction<DatabaseMySQL>("escapeString", &DatabaseMySQL::escapeString);
#endif
}

View File

@ -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)
{

View File

@ -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.
*/