polserver/pol-core/pol/sqlscrobj.h
2024-07-20 03:20:28 +02:00

238 lines
6.8 KiB
C++

/** @file
*
* @par History
*/
#ifndef SQLSCROBJ_H
#define SQLSCROBJ_H
#include "pol_global_config.h"
#ifdef HAVE_MYSQL
#ifdef _WIN32
#include "../clib/Header_Windows.h"
#include <mysql.h>
#else
#include <mysql/mysql.h>
#endif
#include <functional>
#include <memory>
#include <string>
#include "../bscript/bobject.h"
#include "../clib/message_queue.h"
#include "../clib/rawtypes.h"
#include "polobject.h"
namespace Pol
{
namespace Bscript
{
class Executor;
} // namespace Bscript
} // namespace Pol
namespace Pol
{
namespace Core
{
class BSQLResultSet;
class ResultWrapper
{
public:
ResultWrapper();
explicit ResultWrapper( MYSQL_RES* res );
~ResultWrapper();
void set( MYSQL_RES* result );
MYSQL_RES* ptr();
private:
MYSQL_RES* _result;
};
typedef std::shared_ptr<ResultWrapper> RES_WRAPPER;
class BSQLRow final : public Core::PolObjectImp
{
public:
BSQLRow( RES_WRAPPER resultset );
BSQLRow( BSQLResultSet* resultset );
// BSQLRow(MYSQL_RES *_result, MYSQL_ROW _row);
BSQLRow( RES_WRAPPER _result, MYSQL_ROW _row, MYSQL_FIELD* _fields );
~BSQLRow();
// virtual BObjectRef get_member( const char* membername );
// virtual BObjectRef get_member_id( const int id ); //id test
// virtual Bscript::BObjectImp* call_method( const char* methodname, Executor& ex );
// virtual Bscript::BObjectImp* call_method_id( const int id, Executor& ex, bool
// forcebuiltin=false );
virtual Bscript::BObjectImp* copy() const override;
virtual std::string getStringRep() const override { return "SQLRow"; }
virtual size_t sizeEstimate() const override { return sizeof( *this ) + sizeof( MYSQL_FIELD ); }
virtual const char* typeOf() const override { return "SQLRow"; }
virtual u8 typeOfInt() const override { return OTSQLRow; }
virtual bool isTrue() const override { return _row != 0; };
virtual Bscript::BObjectRef OperSubscript( const Bscript::BObject& obj ) override;
private:
MYSQL_ROW _row;
RES_WRAPPER _result;
MYSQL_FIELD* _fields;
};
class BSQLResultSet final : public Bscript::BObjectImp
{
public:
BSQLResultSet( RES_WRAPPER result );
BSQLResultSet( RES_WRAPPER result, MYSQL_FIELD* fields );
BSQLResultSet( int affected_rows );
~BSQLResultSet();
int num_fields() const;
int affected_rows() const;
const char* field_name( unsigned int index ) const;
int num_rows() const;
bool has_result() const;
virtual Bscript::BObjectImp* copy() const override;
virtual std::string getStringRep() const override;
virtual size_t sizeEstimate() const override { return sizeof( *this ) + sizeof( MYSQL_FIELD ); }
virtual const char* typeOf() const override { return "SQLResultSet"; }
virtual u8 typeOfInt() const override { return OTSQLResultSet; }
virtual bool isTrue() const override;
// virtual BObjectRef OperSubscript( const BObject& obj );
friend class BSQLRow;
private:
RES_WRAPPER _result;
MYSQL_FIELD* _fields;
int _affected_rows;
};
class SQLService;
typedef std::vector<std::string> QueryParam;
typedef std::shared_ptr<QueryParam> QueryParams;
class BSQLConnectionBase : public Core::PolObjectImp
{
public:
BSQLConnectionBase();
virtual ~BSQLConnectionBase() = default;
virtual bool query( const std::string query ) = 0;
virtual bool query( const std::string query, const QueryParams params ) = 0;
virtual bool select_db( const char* db ) = 0;
virtual bool close() = 0;
virtual std::string getLastError() const = 0;
virtual int getLastErrNo() const = 0;
virtual Bscript::BObjectImp* copy() const override = 0;
virtual std::string getStringRep() const override { return typeOf(); }
virtual size_t sizeEstimate() const override = 0;
virtual const char* typeOf() const override = 0;
virtual u8 typeOfInt() const override = 0;
};
class BSQLConnection final : public Core::BSQLConnectionBase
{
class ConnectionWrapper;
public:
BSQLConnection();
BSQLConnection( std::shared_ptr<ConnectionWrapper> conn );
virtual ~BSQLConnection() override;
bool connect( const char* host, const char* user, const char* passwd, int port = 0 );
virtual bool query( const std::string query ) override;
virtual bool query( const std::string query, const QueryParams params ) override;
virtual bool select_db( const char* db ) override;
virtual bool close() override;
Bscript::BObjectImp* getResultSet() const;
bool escape_string( const std::string& text, std::string* escaped ) const;
virtual std::string getLastError() const override;
virtual int getLastErrNo() const override;
std::shared_ptr<ConnectionWrapper> getConnection() const;
virtual Bscript::BObjectRef get_member( const char* membername ) override;
virtual Bscript::BObjectRef get_member_id( const int id ) override; // id test
virtual Bscript::BObjectImp* call_polmethod( const char* methodname, UOExecutor& ex ) override;
virtual Bscript::BObjectImp* call_polmethod_id( const int id, UOExecutor& ex,
bool forcebuiltin = false ) override;
virtual Bscript::BObjectImp* copy() const override;
virtual std::string getStringRep() const override;
virtual size_t sizeEstimate() const override { return sizeof( *this ) + _error.capacity(); }
virtual const char* typeOf() const override { return "SQLConnection"; }
virtual u8 typeOfInt() const override { return OTSQLConnection; }
virtual bool isTrue() const override;
// virtual BObjectRef OperSubscript( const BObject& obj );
private:
std::shared_ptr<ConnectionWrapper> _conn;
int _errno;
std::string _error;
class ConnectionWrapper
{
public:
ConnectionWrapper();
~ConnectionWrapper();
void set( MYSQL* conn );
MYSQL* ptr();
private:
MYSQL* _conn;
};
};
class BSQLiteDatabase final : public Core::BSQLConnectionBase
{
public:
BSQLiteDatabase();
virtual ~BSQLiteDatabase() override;
bool open( const std::string& filename );
virtual bool query( const std::string query ) override;
virtual bool query( const std::string query, const QueryParams params ) override;
virtual bool select_db( const char* db ) override;
virtual bool close() override;
virtual std::string getLastError() const override;
virtual int getLastErrNo() const override;
virtual Bscript::BObjectImp* copy() const override;
virtual size_t sizeEstimate() const override { return sizeof( *this ); }
virtual const char* typeOf() const override { return "SQLiteDatabase"; }
virtual u8 typeOfInt() const override { return OTSQLiteDatabase; }
private:
template <typename Callback>
bool withErrorHandler( Callback callback );
int _errno;
std::string _error;
};
class SQLService
{
public:
typedef std::function<void()> msg;
typedef Clib::message_queue<msg> msg_queue;
SQLService();
~SQLService();
void start();
void stop();
void push( msg&& msg_ );
private:
msg_queue _msgs;
};
void start_sql_service();
} // namespace Core
} // namespace Pol
#endif
#endif