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 class Database : public LuaObject
{ {
public: public:
/** friend class DBTransaction;
* 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 = "") {}
/** Database(): m_connected(false) {}
* Transaction related methods. virtual ~Database() { m_connected = false; }
*
* 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.
*/
virtual bool beginTransaction() { return false; } /**
virtual bool rollback() { return false; } * Database connector.
virtual bool commit() { return false; } *
* 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. * Transaction related methods.
* *
* Executes query which doesn't generates results (eg. INSERT, UPDATE, DELETE...). * Methods for starting, commiting and rolling back transaction. Each of the returns boolean value.
* *
* @param std::string query command * @return true on success, false on error
* @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.
virtual bool executeQuery(const std::string& query) { return false; } */
/** virtual bool beginTransaction() { return false; }
* Queries database. virtual bool rollback() { return false; }
* virtual bool commit() { return false; }
* 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; }
/** /**
* Escapes string for query. * Executes command.
* *
* Prepares string to fit SQL queries including quoting it. * Executes query which doesn't generates results (eg. INSERT, UPDATE, DELETE...).
* *
* @param std::string string to be escaped * @param std::string query command
* @return quoted string * @return true on success, false on error
*/ */
virtual std::string escapeString(const std::string&) { return "''"; } virtual bool executeQuery(const std::string& query) { return false; }
/** /**
* Escapes binary stream for query. * Queries database.
* *
* Prepares binary stream to fit SQL queries. * Executes query which generates results (mostly SELECT).
* *
* @param char* binary stream * @param std::string query
* @param long stream length * @return results object (null on error)
* @return quoted string */
*/ virtual DBResultPtr storeQuery(const std::string& query) { return nullptr; }
virtual std::string escapeBlob(const char*, uint32) { return "''"; }
/** /**
* Retrieve id of last inserted row * Escapes string for query.
* *
* @return id on success, 0 if last query did not result on any rows with auto_increment keys * Prepares string to fit SQL queries including quoting it.
*/ *
virtual uint64 getLastInsertedRowID() { return 0; } * @param std::string string to be escaped
* @return quoted string
*/
virtual std::string escapeString(const std::string&) { return "''"; }
/** /**
* Get case insensitive string comparison operator * Escapes binary stream for query.
* *
* @return the case insensitive operator * Prepares binary stream to fit SQL queries.
*/ *
virtual std::string getStringComparer() { return "= "; } * @param char* binary stream
virtual std::string getUpdateLimiter() { return " LIMIT 1;"; } * @param long stream length
* @return quoted string
*/
virtual std::string escapeBlob(const char*, uint32) { return "''"; }
/** /**
* Get database engine * Retrieve id of last inserted row
* *
* @return the database engine type * @return id on success, 0 if last query did not result on any rows with auto_increment keys
*/ */
virtual Fw::DatabaseEngine getDatabaseEngine() { return Fw::DatabaseNone; } virtual uint64 getLastInsertedRowID() { return 0; }
/** /**
* Database connected. * Get case insensitive string comparison operator
* *
* Returns whether or not the database is connected. * @return the case insensitive operator
* */
* @return whether or not the database is connected. virtual std::string getStringComparer() { return "= "; }
*/ virtual std::string getUpdateLimiter() { return " LIMIT 1;"; }
bool isConnected() { return m_connected; }
friend class DBTransaction; /**
* Get database engine
*
* @return the database engine type
*/
virtual Fw::DatabaseEngine getDatabaseEngine() { return Fw::DatabaseNone; }
protected: /**
/** * Database connected.
* Database set connected. *
* * Returns whether or not the database is connected.
* Sets the database to know that it is connected. *
*/ * @return whether or not the database is connected.
void setConnected(bool connected) { m_connected = connected; } */
bool isConnected() { return m_connected; }
virtual bool handleError() { return false; } protected:
virtual bool internalExecuteQuery(const std::string &query) { return false; } /**
* 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) {} DBResultPtr verifyResult(DBResultPtr result);
virtual ~Database() { m_connected = false; }
ticks_t m_use; ticks_t m_use;
bool m_connected; bool m_connected;
private: private:
static DatabasePtr m_instance; static DatabasePtr m_instance;
}; };
class DBResult : public LuaObject class DBResult : public LuaObject
{ {
public: public:
/** Get the Integer value of a field in database DBResult() {}
*\returns The Integer value of the selected field and row virtual ~DBResult() {}
*\param s The name of the field
*/
virtual int32 getDataInt(const std::string&) { return 0; }
/** Get the Long value of a field in database /** Get the Integer value of a field in database
*\returns The Long 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 int64 getDataLong(const std::string&) { return 0; } virtual int32 getDataInt(const std::string&) { return 0; }
/** Get the String of a field in database /** Get the Long value of a field in database
*\returns The String 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 std::string getDataString(const std::string&) { return ""; } virtual int64 getDataLong(const std::string&) { return 0; }
/** Get the blob of a field in database /** Get the String of a field in database
*\returns a PropStream that is initiated with the blob data field, if not exist it returns NULL. *\returns The String of the selected field and row
*\param s The name of the field *\param s The name of the field
*/ */
virtual const char* getDataStream(const std::string&, uint64&) { return ""; } virtual std::string getDataString(const std::string&) { return ""; }
/** Result freeing /** 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.
virtual void free() {} *\param s The name of the field
*/
virtual const char* getDataStream(const std::string&, uint64&) { return ""; }
/** Moves to next result in set /** Result freeing
*\returns true if moved, false if there are no more results. */
*/ virtual void free() {}
virtual bool next() { return false; }
/** Returned the number of rows from result /** Moves to next result in set
*\returns integer value of row amount, 0 if result is empty. *\returns true if moved, false if there are no more results.
*/ */
virtual int getRowCount() { return 0; } virtual bool next() { return false; }
protected: /** Returned the number of rows from result
DBResult() {} *\returns integer value of row amount, 0 if result is empty.
virtual ~DBResult() {} */
virtual int getRowCount() { return 0; }
}; };
/** /**
@ -204,16 +203,16 @@ class DBResult : public LuaObject
*/ */
class DBQuery : public std::stringstream, public LuaObject 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 set(std::string& query) { str(query); }
void append(char query) { putback(query); } void append(char query) { putback(query); }
protected: protected:
static boost::recursive_mutex databaseLock; static boost::recursive_mutex databaseLock;
}; };
/** /**
@ -223,87 +222,87 @@ class DBQuery : public std::stringstream, public LuaObject
*/ */
class DBInsert class DBInsert
{ {
public: public:
/** /**
* Associates with given database handler. * Associates with given database handler.
* *
* @param DatabasePtr database wrapper * @param DatabasePtr database wrapper
*/ */
DBInsert(DatabasePtr db): m_db(db), m_rows(0) {} DBInsert(const DatabasePtr& db): m_db(db), m_rows(0) {}
~DBInsert() {} ~DBInsert() {}
/** /**
* Sets query prototype. * Sets query prototype.
* *
* @param std::string& INSERT query * @param std::string& INSERT query
*/ */
void setQuery(const std::string& query); void setQuery(const std::string& query);
/** /**
* Adds new row to INSERT statement. * Adds new row to INSERT statement.
* *
* On databases that doesn't support multiline INSERTs it simply execute INSERT for each row. * On databases that doesn't support multiline INSERTs it simply execute INSERT for each row.
* *
* @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.
*/ */
bool addRow(std::stringstream& row); bool addRow(std::stringstream& row);
/** /**
* Executes current buffer. * Executes current buffer.
*/ */
bool execute(); bool execute();
protected: protected:
DatabasePtr m_db; DatabasePtr m_db;
uint32 m_rows; uint32 m_rows;
std::string m_query; std::string m_query;
std::string m_buf; std::string m_buf;
}; };
class DBTransaction class DBTransaction
{ {
public: public:
DBTransaction(DatabasePtr database) DBTransaction(DatabasePtr database)
{ {
m_db = database; m_db = database;
m_state = STATE_FRESH; m_state = STATE_FRESH;
} }
~DBTransaction() ~DBTransaction()
{ {
if(m_state == STATE_READY) if(m_state == STATE_READY)
m_db->rollback(); m_db->rollback();
} }
bool begin() bool begin()
{ {
m_state = STATE_READY; m_state = STATE_READY;
return m_db->beginTransaction(); return m_db->beginTransaction();
} }
bool commit() bool commit()
{ {
if(m_state != STATE_READY) if(m_state != STATE_READY)
return false; return false;
m_state = STATE_DONE; m_state = STATE_DONE;
return m_db->commit(); return m_db->commit();
} }
private: private:
DatabasePtr m_db; DatabasePtr m_db;
enum TransactionStates_t enum TransactionStates_t
{ {
STATE_FRESH, STATE_FRESH,
STATE_READY, STATE_READY,
STATE_DONE STATE_DONE
} m_state; } m_state;
}; };
#endif #endif

View File

@ -35,55 +35,57 @@
class DatabaseMySQL : public Database class DatabaseMySQL : public Database
{ {
public: public:
DatabaseMySQL(); DatabaseMySQL();
virtual ~DatabaseMySQL(); virtual ~DatabaseMySQL();
virtual void connect(const std::string& host, const std::string& user, const std::string& pass, 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 = ""); const std::string& db, uint16 port, const std::string& unix_socket = "");
virtual bool beginTransaction(); virtual bool beginTransaction();
virtual bool rollback(); virtual bool rollback();
virtual bool commit(); virtual bool commit();
virtual bool executeQuery(const std::string& query); virtual bool executeQuery(const std::string& query);
virtual DBResultPtr storeQuery(const std::string& query); virtual DBResultPtr storeQuery(const std::string& query);
virtual std::string escapeString(const std::string &s); virtual std::string escapeString(const std::string &s);
virtual std::string escapeBlob(const char* s, uint32 length); virtual std::string escapeBlob(const char* s, uint32 length);
virtual uint64 getLastInsertedRowID(); virtual uint64 getLastInsertedRowID();
virtual Fw::DatabaseEngine getDatabaseEngine() {return Fw::DatabaseMySQL;} virtual Fw::DatabaseEngine getDatabaseEngine() {return Fw::DatabaseMySQL;}
protected: protected:
bool handleError(); bool handleError();
bool internalExecuteQuery(const std::string &query); bool internalExecuteQuery(const std::string &query);
MYSQL* m_handle; MYSQL* m_handle;
}; };
class MySQLResult : public DBResult 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(); friend class DatabaseMySQL;
virtual bool next();
virtual int getRowCount() { return mysql_num_rows(m_result); }
protected: public:
MySQLResult(MYSQL_RES* result); MySQLResult(MYSQL_RES* result);
virtual ~MySQLResult(); virtual ~MySQLResult();
typedef std::map<const std::string, uint32> RowNames_t; virtual int32 getDataInt(const std::string& s);
RowNames_t m_names; 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; virtual void free();
MYSQL_ROW m_row; 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 #endif