mirror of
https://github.com/SapphireServer/Sapphire.git
synced 2025-05-24 02:27:46 +00:00
What can i say but... more refactoring of db stuff
This commit is contained in:
parent
63346359c7
commit
224ecadef1
2 changed files with 165 additions and 155 deletions
|
@ -49,7 +49,22 @@ bool QueryResult::nextRow()
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Database::Database()
|
Field *QueryResult::fetch()
|
||||||
|
{
|
||||||
|
return m_currentRow;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t QueryResult::getFieldCount() const
|
||||||
|
{
|
||||||
|
return m_fieldCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t QueryResult::getRowCount() const
|
||||||
|
{
|
||||||
|
return m_rowCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
Database::Database()
|
||||||
{
|
{
|
||||||
m_port = 0;
|
m_port = 0;
|
||||||
m_counter = 0;
|
m_counter = 0;
|
||||||
|
@ -339,5 +354,59 @@ void Database::shutdown()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::string &Database::getHostName()
|
||||||
|
{
|
||||||
|
return m_hostname;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string &Database::getDatabaseName()
|
||||||
|
{
|
||||||
|
return m_databaseName;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Field::setValue( char *value )
|
||||||
|
{
|
||||||
|
m_pValue = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Field::setLength( uint32_t value )
|
||||||
|
{
|
||||||
|
m_size = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Field::getString() const
|
||||||
|
{
|
||||||
|
if( !m_pValue )
|
||||||
|
return "";
|
||||||
|
return std::string( m_pValue );
|
||||||
|
}
|
||||||
|
|
||||||
|
void Field::getBinary( char *dstBuf, uint16_t size ) const
|
||||||
|
{
|
||||||
|
if( m_pValue )
|
||||||
|
{
|
||||||
|
memcpy( dstBuf, m_pValue, size );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dstBuf = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float Field::getFloat() const
|
||||||
|
{
|
||||||
|
return m_pValue ? static_cast< float >( atof( m_pValue ) ) : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Field::getBool() const
|
||||||
|
{
|
||||||
|
return m_pValue ? atoi( m_pValue ) > 0 : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t Field::getLength() const
|
||||||
|
{
|
||||||
|
return m_size;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,191 +10,132 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
namespace Db {
|
namespace Db {
|
||||||
|
|
||||||
// CField is used to access db-query resultsets
|
class Field
|
||||||
class Field
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
// set value
|
||||||
|
void setValue( char* value );
|
||||||
|
void setLength( uint32_t value );
|
||||||
|
|
||||||
|
std::string getString() const;
|
||||||
|
void getBinary( char* dstBuf, uint16_t size ) const;
|
||||||
|
float getFloat() const;
|
||||||
|
bool getBool() const;
|
||||||
|
|
||||||
|
template< class T >
|
||||||
|
__inline T get() const
|
||||||
{
|
{
|
||||||
public:
|
if( !m_pValue )
|
||||||
|
return 0;
|
||||||
|
|
||||||
// set value
|
return static_cast< T >( atol( m_pValue ) );
|
||||||
__inline void setValue( char* value )
|
}
|
||||||
{
|
|
||||||
m_pValue = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// set value
|
uint32_t getLength() const;
|
||||||
__inline void setLength( uint32_t value )
|
|
||||||
{
|
|
||||||
m_size = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// return as string
|
private:
|
||||||
__inline std::string getString() const
|
char *m_pValue;
|
||||||
{
|
uint32_t m_size;
|
||||||
if( !m_pValue )
|
};
|
||||||
return "";
|
|
||||||
return std::string( m_pValue );
|
|
||||||
}
|
|
||||||
|
|
||||||
// return as string
|
|
||||||
__inline void getBinary( char* dstBuf, uint16_t size ) const
|
|
||||||
{
|
|
||||||
if( m_pValue )
|
|
||||||
{
|
|
||||||
memcpy( dstBuf, m_pValue, size );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
dstBuf = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// return as float
|
|
||||||
__inline float getFloat() const
|
|
||||||
{
|
|
||||||
return m_pValue ? static_cast< float >( atof( m_pValue ) ) : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// return as bool
|
|
||||||
__inline bool getBool() const
|
|
||||||
{
|
|
||||||
return m_pValue ? atoi( m_pValue ) > 0 : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
template< class T >
|
|
||||||
__inline T get() const
|
|
||||||
{
|
|
||||||
if( !m_pValue )
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return static_cast< T >( atol( m_pValue ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
__inline uint32_t getLength() const
|
|
||||||
{
|
|
||||||
return m_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
class QueryResult
|
||||||
char *m_pValue;
|
{
|
||||||
uint32_t m_size;
|
public:
|
||||||
};
|
QueryResult( MYSQL_RES *res, uint32_t fields, uint32_t rows );
|
||||||
|
~QueryResult();
|
||||||
|
|
||||||
|
bool nextRow();
|
||||||
|
|
||||||
class QueryResult
|
Field* fetch();
|
||||||
{
|
uint32_t getFieldCount() const;
|
||||||
public:
|
uint32_t getRowCount() const;
|
||||||
QueryResult( MYSQL_RES *res, uint32_t fields, uint32_t rows );
|
|
||||||
~QueryResult();
|
|
||||||
|
|
||||||
bool nextRow();
|
protected:
|
||||||
|
uint32_t m_fieldCount;
|
||||||
|
uint32_t m_rowCount;
|
||||||
|
Field *m_currentRow;
|
||||||
|
MYSQL_RES *m_result;
|
||||||
|
};
|
||||||
|
|
||||||
__inline Field* fetch()
|
struct DatabaseConnection
|
||||||
{
|
{
|
||||||
return m_currentRow;
|
std::mutex lock;
|
||||||
}
|
MYSQL *conn;
|
||||||
__inline uint32_t getFieldCount() const
|
};
|
||||||
{
|
|
||||||
return m_fieldCount;
|
|
||||||
}
|
|
||||||
__inline uint32_t getRowCount() const
|
|
||||||
{
|
|
||||||
return m_rowCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
struct DatabaseParams
|
||||||
uint32_t m_fieldCount;
|
{
|
||||||
uint32_t m_rowCount;
|
std::string hostname;
|
||||||
Field *m_currentRow;
|
std::string username;
|
||||||
MYSQL_RES *m_result;
|
std::string password;
|
||||||
};
|
std::string databaseName;
|
||||||
|
uint16_t port;
|
||||||
|
uint32_t bufferSize;
|
||||||
|
uint32_t connectionCount;
|
||||||
|
};
|
||||||
|
|
||||||
struct DatabaseConnection
|
class Database
|
||||||
{
|
{
|
||||||
std::mutex lock;
|
public:
|
||||||
MYSQL *conn;
|
Database();
|
||||||
};
|
virtual ~Database();
|
||||||
|
|
||||||
struct DatabaseParams
|
bool initialize( const DatabaseParams& params );
|
||||||
{
|
|
||||||
std::string hostname;
|
|
||||||
std::string username;
|
|
||||||
std::string password;
|
|
||||||
std::string databaseName;
|
|
||||||
uint16_t port;
|
|
||||||
uint32_t bufferSize;
|
|
||||||
uint32_t connectionCount;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Database
|
void shutdown();
|
||||||
{
|
|
||||||
public:
|
|
||||||
Database();
|
|
||||||
virtual ~Database();
|
|
||||||
|
|
||||||
/************************************************************************/
|
boost::shared_ptr< QueryResult > query( const std::string& QueryString );
|
||||||
/* Virtual Functions */
|
bool waitExecuteNA( const char* QueryString );//Wait For Request Completion
|
||||||
/************************************************************************/
|
bool execute( const char* QueryString, ... );
|
||||||
bool initialize( const DatabaseParams& params );
|
bool execute( const std::string& QueryString );
|
||||||
|
|
||||||
void shutdown();
|
const std::string& getHostName();
|
||||||
|
|
||||||
boost::shared_ptr<QueryResult> query( const std::string& QueryString );
|
const std::string& getDatabaseName();
|
||||||
bool waitExecuteNA( const char* QueryString );//Wait For Request Completion
|
|
||||||
bool execute( const char* QueryString, ... );
|
|
||||||
bool execute( const std::string& QueryString );
|
|
||||||
|
|
||||||
__inline const std::string& getHostName()
|
std::string escapeString( std::string Escape );
|
||||||
{
|
void escapeLongString( const char * str, uint32_t len, std::stringstream& out );
|
||||||
return m_hostname;
|
std::string escapeString( const char * esc, DatabaseConnection *con );
|
||||||
}
|
|
||||||
|
|
||||||
__inline const std::string& getDatabaseName()
|
void freeQueryResult( QueryResult * p );
|
||||||
{
|
|
||||||
return m_databaseName;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string escapeString( std::string Escape );
|
DatabaseConnection *getFreeConnection();
|
||||||
void escapeLongString( const char * str, uint32_t len, std::stringstream& out );
|
|
||||||
std::string escapeString( const char * esc, DatabaseConnection *con );
|
|
||||||
|
|
||||||
void freeQueryResult( QueryResult * p );
|
void cleanupLibs();
|
||||||
|
|
||||||
DatabaseConnection *getFreeConnection();
|
/* database is killed off manually. */
|
||||||
|
void onShutdown() {}
|
||||||
|
|
||||||
void cleanupLibs();
|
uint64_t getNextUId();
|
||||||
|
|
||||||
/* database is killed off manually. */
|
protected:
|
||||||
void onShutdown() {}
|
|
||||||
|
|
||||||
uint64_t getNextUId();
|
// actual query function
|
||||||
|
bool sendQuery( DatabaseConnection *con, const std::string &sql, bool Self );
|
||||||
|
QueryResult * storeQueryResult( DatabaseConnection * con );
|
||||||
|
bool handleError( DatabaseConnection *conn, uint32_t ErrorNumber );
|
||||||
|
bool reconnect( DatabaseConnection *conn );
|
||||||
|
|
||||||
protected:
|
DatabaseConnection *m_pConnections;
|
||||||
|
|
||||||
// actual query function
|
uint32_t m_counter;
|
||||||
bool sendQuery( DatabaseConnection *con, const std::string &sql, bool Self );
|
///////////////////////////////
|
||||||
QueryResult * storeQueryResult( DatabaseConnection * con );
|
|
||||||
bool handleError( DatabaseConnection *conn, uint32_t ErrorNumber );
|
|
||||||
bool reconnect( DatabaseConnection *conn );
|
|
||||||
|
|
||||||
DatabaseConnection *m_pConnections;
|
int32_t m_connectionCount;
|
||||||
|
|
||||||
uint32_t m_counter;
|
// For reconnecting a broken connection
|
||||||
///////////////////////////////
|
std::string m_hostname;
|
||||||
|
std::string m_username;
|
||||||
|
std::string m_password;
|
||||||
|
std::string m_databaseName;
|
||||||
|
uint32_t m_port;
|
||||||
|
|
||||||
int32_t m_connectionCount;
|
};
|
||||||
|
|
||||||
// For reconnecting a broken connection
|
}
|
||||||
std::string m_hostname;
|
|
||||||
std::string m_username;
|
|
||||||
std::string m_password;
|
|
||||||
std::string m_databaseName;
|
|
||||||
uint32_t m_port;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Reference in a new issue