Few issues with compilation and layout

This commit is contained in:
BeniS 2013-03-05 17:47:43 +13:00
parent 15ee7962f1
commit 9305053e34
2 changed files with 245 additions and 244 deletions

View File

@ -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 <ctime>
* 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 <ctime>
* 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

View File

@ -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<const std::string, uint32> 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<const std::string, uint32> RowNames_t;
RowNames_t m_names;
MYSQL_RES* m_result;
MYSQL_ROW m_row;
};
#endif