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

155 lines
3.3 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 <cinttypes>
2017-08-08 13:53:47 +02:00
#include <mysql.h>
#include <boost/shared_ptr.hpp>
#include <string.h>
namespace Core {
namespace Db {
2017-08-08 13:53:47 +02:00
class Field
{
public:
2017-08-08 13:53:47 +02:00
// 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
2017-08-08 13:53:47 +02:00
{
if( !m_pValue )
return 0;
return static_cast< T >( atol( m_pValue ) );
}
2017-09-15 11:04:42 +02:00
uint64_t getUInt64()
{
if( m_pValue )
{
2017-09-15 11:04:42 +02:00
uint64_t value;
sscanf( m_pValue, "%" SCNu64, &value );
2017-09-15 11:04:42 +02:00
return value;
}
else
2017-09-15 11:04:42 +02:00
return 0;
}
uint32_t getLength() const;
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();
2017-08-08 13:53:47 +02:00
bool nextRow();
2017-08-08 13:53:47 +02:00
Field* fetch();
uint32_t getFieldCount() const;
uint32_t getRowCount() const;
2017-08-08 13:53:47 +02:00
protected:
uint32_t m_fieldCount;
uint32_t m_rowCount;
Field *m_currentRow;
MYSQL_RES *m_result;
};
2017-08-08 13:53:47 +02:00
struct DatabaseConnection
{
std::mutex lock;
MYSQL *conn;
};
2017-08-08 13:53:47 +02:00
struct DatabaseParams
{
std::string hostname;
std::string username;
std::string password;
std::string databaseName;
uint16_t port;
uint32_t bufferSize;
uint32_t connectionCount;
};
2017-08-08 13:53:47 +02:00
class Database
{
public:
Database();
virtual ~Database();
2017-08-08 13:53:47 +02:00
bool initialize( const DatabaseParams& params );
2017-08-08 13:53:47 +02:00
void shutdown();
2017-08-08 13:53:47 +02:00
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 );
2017-08-08 13:53:47 +02:00
const std::string& getHostName();
2017-08-08 13:53:47 +02:00
const std::string& getDatabaseName();
2017-08-08 13:53:47 +02:00
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 );
2017-08-08 13:53:47 +02:00
void freeQueryResult( QueryResult * p );
2017-08-08 13:53:47 +02:00
DatabaseConnection *getFreeConnection();
2017-08-08 13:53:47 +02:00
void cleanupLibs();
2017-08-08 13:53:47 +02:00
/* database is killed off manually. */
void onShutdown() {}
2017-08-08 13:53:47 +02:00
uint64_t getNextUId();
protected:
// 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 );
DatabaseConnection *m_pConnections;
uint32_t m_counter;
///////////////////////////////
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;
};
}
2017-08-08 13:53:47 +02:00
}
#endif