1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-05-23 18:17:46 +00:00

What can i say but... more refactoring of db stuff

This commit is contained in:
Mordred 2017-09-01 23:21:35 +02:00
parent 63346359c7
commit 224ecadef1
2 changed files with 165 additions and 155 deletions

View file

@ -49,7 +49,22 @@ bool QueryResult::nextRow()
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_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;
}
}
}

View file

@ -10,191 +10,132 @@
#include <string.h>
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
__inline void setValue( char* value )
{
m_pValue = value;
}
return static_cast< T >( atol( m_pValue ) );
}
// set value
__inline void setLength( uint32_t value )
{
m_size = value;
}
uint32_t getLength() const;
// return as string
__inline std::string getString() const
{
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:
char *m_pValue;
uint32_t m_size;
};
private:
char *m_pValue;
uint32_t m_size;
};
class QueryResult
{
public:
QueryResult( MYSQL_RES *res, uint32_t fields, uint32_t rows );
~QueryResult();
bool nextRow();
class QueryResult
{
public:
QueryResult( MYSQL_RES *res, uint32_t fields, uint32_t rows );
~QueryResult();
Field* fetch();
uint32_t getFieldCount() const;
uint32_t getRowCount() const;
bool nextRow();
protected:
uint32_t m_fieldCount;
uint32_t m_rowCount;
Field *m_currentRow;
MYSQL_RES *m_result;
};
__inline Field* fetch()
{
return m_currentRow;
}
__inline uint32_t getFieldCount() const
{
return m_fieldCount;
}
__inline uint32_t getRowCount() const
{
return m_rowCount;
}
struct DatabaseConnection
{
std::mutex lock;
MYSQL *conn;
};
protected:
uint32_t m_fieldCount;
uint32_t m_rowCount;
Field *m_currentRow;
MYSQL_RES *m_result;
};
struct DatabaseParams
{
std::string hostname;
std::string username;
std::string password;
std::string databaseName;
uint16_t port;
uint32_t bufferSize;
uint32_t connectionCount;
};
struct DatabaseConnection
{
std::mutex lock;
MYSQL *conn;
};
class Database
{
public:
Database();
virtual ~Database();
struct DatabaseParams
{
std::string hostname;
std::string username;
std::string password;
std::string databaseName;
uint16_t port;
uint32_t bufferSize;
uint32_t connectionCount;
};
bool initialize( const DatabaseParams& params );
class Database
{
public:
Database();
virtual ~Database();
void shutdown();
/************************************************************************/
/* Virtual Functions */
/************************************************************************/
bool initialize( const DatabaseParams& params );
boost::shared_ptr< QueryResult > query( const std::string& QueryString );
bool waitExecuteNA( const char* QueryString );//Wait For Request Completion
bool execute( const char* QueryString, ... );
bool execute( const std::string& QueryString );
void shutdown();
const std::string& getHostName();
boost::shared_ptr<QueryResult> query( const std::string& QueryString );
bool waitExecuteNA( const char* QueryString );//Wait For Request Completion
bool execute( const char* QueryString, ... );
bool execute( const std::string& QueryString );
const std::string& getDatabaseName();
__inline const std::string& getHostName()
{
return m_hostname;
}
std::string escapeString( std::string Escape );
void escapeLongString( const char * str, uint32_t len, std::stringstream& out );
std::string escapeString( const char * esc, DatabaseConnection *con );
__inline const std::string& getDatabaseName()
{
return m_databaseName;
}
void freeQueryResult( QueryResult * p );
std::string escapeString( std::string Escape );
void escapeLongString( const char * str, uint32_t len, std::stringstream& out );
std::string escapeString( const char * esc, DatabaseConnection *con );
DatabaseConnection *getFreeConnection();
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. */
void onShutdown() {}
protected:
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
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 );
uint32_t m_counter;
///////////////////////////////
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