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.
* *
@ -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. * 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 beginTransaction() { return false; }
virtual bool rollback() {return false;} virtual bool rollback() { return false; }
virtual bool commit() {return false;} virtual bool commit() { return false; }
/** /**
* Executes command. * Executes command.
@ -79,7 +62,7 @@ class Database : public LuaObject
* @param std::string query command * @param std::string query command
* @return true on success, false on error * @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. * Queries database.
@ -89,7 +72,7 @@ class Database : public LuaObject
* @param std::string query * @param std::string query
* @return results object (null on error) * @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. * Escapes string for query.
@ -99,7 +82,7 @@ class Database : public LuaObject
* @param std::string string to be escaped * @param std::string string to be escaped
* @return quoted string * @return quoted string
*/ */
virtual std::string escapeString(const std::string&) {return "''";} virtual std::string escapeString(const std::string&) { return "''"; }
/** /**
* Escapes binary stream for query. * Escapes binary stream for query.
@ -110,29 +93,29 @@ class Database : public LuaObject
* @param long stream length * @param long stream length
* @return quoted string * @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 * Retrieve id of last inserted row
* *
* @return id on success, 0 if last query did not result on any rows with auto_increment keys * @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 * Get case insensitive string comparison operator
* *
* @return the case insensitive operator * @return the case insensitive operator
*/ */
virtual std::string getStringComparer() {return "= ";} virtual std::string getStringComparer() { return "= "; }
virtual std::string getUpdateLimiter() {return " LIMIT 1;";} virtual std::string getUpdateLimiter() { return " LIMIT 1;"; }
/** /**
* Get database engine * Get database engine
* *
* @return the database engine type * @return the database engine type
*/ */
virtual Fw::DatabaseEngine getDatabaseEngine() {return Fw::DatabaseNone;} virtual Fw::DatabaseEngine getDatabaseEngine() { return Fw::DatabaseNone; }
/** /**
* Database connected. * Database connected.
@ -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,14 +136,13 @@ 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;}
DBResultPtr verifyResult(DBResultPtr result); DBResultPtr verifyResult(DBResultPtr result);
Database(): m_connected(false) {} Database(): m_connected(false) {}
virtual ~Database() {m_connected = false;} virtual ~Database() { m_connected = false; }
ticks_t m_use; ticks_t m_use;
bool m_connected; bool m_connected;
@ -173,25 +158,25 @@ class DBResult : public LuaObject
*\returns The Integer value of the selected field and row *\returns The Integer value of the selected field and row
*\param s The name of the field *\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 /** Get the Long value of a field in database
*\returns The Long value of the selected field and row *\returns The Long value of the selected field and row
*\param s The name of the field *\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 /** Get the String of a field in database
*\returns The String of the selected field and row *\returns The String of the selected field and row
*\param s The name of the field *\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 /** 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. *\returns a PropStream that is initiated with the blob data field, if not exist it returns NULL.
*\param s The name of the field *\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 /** Result freeing
*/ */
@ -200,7 +185,7 @@ class DBResult : public LuaObject
/** Moves to next result in set /** Moves to next result in set
*\returns true if moved, false if there are no more results. *\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 /** Returned the number of rows from result
*\returns integer value of row amount, 0 if result is empty. *\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. * 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.
*/ */