1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-05-24 02:27:46 +00:00
sapphire/src/servers/Server_Common/Database/Database.h

201 lines
4.7 KiB
C
Raw Normal View History

2017-08-08 13:53:47 +02:00
#ifndef _DATABASE_H
#define _DATABASE_H
#include <mutex>
#include <stdio.h>
#include <mysql.h>
#include <boost/shared_ptr.hpp>
#include <string.h>
namespace Core {
namespace Db {
// CField is used to access db-query resultsets
class Field
{
public:
// set value
__inline void setValue( char* value )
{
m_pValue = value;
}
// set value
__inline void setLength( uint32_t value )
{
m_size = value;
}
2017-08-08 13:53:47 +02:00
// return as string
2017-09-01 17:00:09 +02:00
__inline std::string getString() const
2017-08-08 13:53:47 +02:00
{
if( !m_pValue )
return "";
return std::string( m_pValue );
2017-08-08 13:53:47 +02:00
}
// return as string
2017-09-01 17:00:09 +02:00
__inline void getBinary( char* dstBuf, uint16_t size ) const
2017-08-08 13:53:47 +02:00
{
if( m_pValue )
{
memcpy( dstBuf, m_pValue, size );
}
else
{
dstBuf = NULL;
}
}
// return as float
2017-09-01 17:00:09 +02:00
__inline float getFloat() const
2017-08-08 13:53:47 +02:00
{
return m_pValue ? static_cast< float >( atof( m_pValue ) ) : 0;
}
// return as bool
2017-09-01 17:00:09 +02:00
__inline bool getBool() const
2017-08-08 13:53:47 +02:00
{
return m_pValue ? atoi( m_pValue ) > 0 : false;
}
2017-08-31 23:29:45 +02:00
template< class T >
2017-09-01 17:00:09 +02:00
__inline T get() const
2017-08-08 13:53:47 +02:00
{
2017-08-31 23:29:45 +02:00
if( !m_pValue )
return 0;
2017-08-08 13:53:47 +02:00
2017-08-31 23:29:45 +02:00
return static_cast< T >( atol( m_pValue ) );
2017-08-08 13:53:47 +02:00
}
2017-09-01 17:00:09 +02:00
__inline uint32_t getLength() const
{
return m_size;
}
2017-08-08 13:53:47 +02:00
private:
char *m_pValue;
uint32_t m_size;
2017-08-08 13:53:47 +02:00
};
class QueryResult
{
public:
QueryResult( MYSQL_RES *res, uint32_t fields, uint32_t rows );
~QueryResult();
bool nextRow();
__inline Field* fetch()
{
return m_currentRow;
}
__inline uint32_t getFieldCount() const
{
return m_fieldCount;
}
__inline uint32_t getRowCount() const
{
return m_rowCount;
}
protected:
uint32_t m_fieldCount;
uint32_t m_rowCount;
Field *m_currentRow;
MYSQL_RES *m_result;
};
struct DatabaseConnection
{
std::mutex lock;
MYSQL *conn;
};
struct DatabaseParams
{
std::string hostname;
std::string username;
std::string password;
std::string databaseName;
uint16_t port;
uint32_t bufferSize;
uint32_t connectionCount;
};
class Database
{
public:
Database();
virtual ~Database();
/************************************************************************/
/* Virtual Functions */
/************************************************************************/
bool initialize( const DatabaseParams& params );
void shutdown();
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 );
__inline const std::string& getHostName()
{
return m_hostname;
}
__inline const std::string& getDatabaseName()
{
return m_databaseName;
}
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 );
void freeQueryResult( QueryResult * p );
DatabaseConnection *getFreeConnection();
void cleanupLibs();
/* database is killed off manually. */
void onShutdown() {}
uint64_t getNextUId();
protected:
// actual query function
bool sendQuery( DatabaseConnection *con, const std::string &sql, bool Self );
2017-09-01 17:00:09 +02:00
QueryResult * storeQueryResult( DatabaseConnection * con );
bool handleError( DatabaseConnection *conn, uint32_t ErrorNumber );
2017-09-01 17:00:09 +02:00
bool reconnect( DatabaseConnection *conn );
2017-08-08 13:53:47 +02:00
DatabaseConnection *m_pConnections;
uint32_t m_counter;
2017-08-08 13:53:47 +02:00
///////////////////////////////
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