Pol  Revision:cb584c9
sqlscrobj.h
Go to the documentation of this file.
1 
7 #ifndef SQLSCROBJ_H
8 #define SQLSCROBJ_H
9 
10 #include "pol_global_config.h"
11 
12 #ifdef HAVE_MYSQL
13 
14 #ifdef _WIN32
15 #include "../clib/Header_Windows.h"
16 #include <mysql.h>
17 #else
18 #include <mysql/mysql.h>
19 #endif
20 
21 #include <functional>
22 #include <memory>
23 #include <string>
24 
25 #include "../bscript/bobject.h"
26 #include "../clib/compilerspecifics.h"
27 #include "../clib/message_queue.h"
28 #include "../clib/rawtypes.h"
29 
30 namespace Pol
31 {
32 namespace Bscript
33 {
34 class Executor;
35 } // namespace Bscript
36 } // namespace Pol
37 namespace Pol
38 {
39 namespace Core
40 {
41 class BSQLResultSet;
42 
44 {
45 public:
46  ResultWrapper();
47  explicit ResultWrapper( MYSQL_RES* res );
48  ~ResultWrapper();
49  void set( MYSQL_RES* result );
50  MYSQL_RES* ptr();
51 
52 private:
53  MYSQL_RES* _result;
54 };
55 typedef std::shared_ptr<ResultWrapper> RES_WRAPPER;
56 
58 {
59 public:
60  BSQLRow( RES_WRAPPER resultset );
61  BSQLRow( BSQLResultSet* resultset );
62  // BSQLRow(MYSQL_RES *_result, MYSQL_ROW _row);
63  BSQLRow( RES_WRAPPER _result, MYSQL_ROW _row, MYSQL_FIELD* _fields );
64  ~BSQLRow();
65 
66  // virtual BObjectRef get_member( const char* membername );
67  // virtual BObjectRef get_member_id( const int id ); //id test
68  // virtual Bscript::BObjectImp* call_method( const char* methodname, Executor& ex );
69  // virtual Bscript::BObjectImp* call_method_id( const int id, Executor& ex, bool
70  // forcebuiltin=false );
71  virtual Bscript::BObjectImp* copy() const POL_OVERRIDE;
72  virtual std::string getStringRep() const POL_OVERRIDE { return "SQLRow"; }
73  virtual size_t sizeEstimate() const POL_OVERRIDE
74  {
75  return sizeof( *this ) + sizeof( MYSQL_FIELD );
76  }
77  virtual const char* typeOf() const POL_OVERRIDE { return "SQLRow"; }
78  virtual u8 typeOfInt() const POL_OVERRIDE { return OTSQLRow; }
79  virtual bool isTrue() const POL_OVERRIDE { return _row != 0; };
80  virtual Bscript::BObjectRef OperSubscript( const Bscript::BObject& obj ) POL_OVERRIDE;
81 
82 private:
83  MYSQL_ROW _row;
84  RES_WRAPPER _result;
85  MYSQL_FIELD* _fields;
86 };
87 
89 {
90 public:
91  BSQLResultSet( RES_WRAPPER result );
92  BSQLResultSet( RES_WRAPPER result, MYSQL_FIELD* fields );
93  BSQLResultSet( int affected_rows );
94  ~BSQLResultSet();
95  int num_fields() const;
96  int affected_rows() const;
97  const char* field_name( unsigned int index ) const;
98  int num_rows() const;
99  virtual Bscript::BObjectImp* copy() const POL_OVERRIDE;
100  virtual std::string getStringRep() const POL_OVERRIDE;
101  virtual size_t sizeEstimate() const POL_OVERRIDE
102  {
103  return sizeof( *this ) + sizeof( MYSQL_FIELD );
104  }
105  virtual const char* typeOf() const POL_OVERRIDE { return "SQLResultSet"; }
106  virtual u8 typeOfInt() const POL_OVERRIDE { return OTSQLResultSet; }
107  virtual bool isTrue() const POL_OVERRIDE;
108  // virtual BObjectRef OperSubscript( const BObject& obj );
109 
110  friend class BSQLRow;
111 
112 private:
113  RES_WRAPPER _result;
114  MYSQL_FIELD* _fields;
116 };
118 
119 typedef std::vector<std::string> QueryParam;
120 typedef std::shared_ptr<QueryParam> QueryParams;
121 
123 {
124  class ConnectionWrapper;
125 
126 public:
127  BSQLConnection();
128  BSQLConnection( std::shared_ptr<ConnectionWrapper> conn );
129  ~BSQLConnection();
130  bool connect( const char* host, const char* user, const char* passwd );
131  bool query( const std::string query );
132  bool query( const std::string query, const QueryParams params );
133  bool select_db( const char* db );
134  bool close();
135  Bscript::BObjectImp* getResultSet() const;
136 
137  std::string getLastError() const;
138  int getLastErrNo() const;
139  std::shared_ptr<ConnectionWrapper> getConnection() const;
140 
141  virtual Bscript::BObjectRef get_member( const char* membername ) POL_OVERRIDE;
142  virtual Bscript::BObjectRef get_member_id( const int id ) POL_OVERRIDE; // id test
143  virtual Bscript::BObjectImp* call_method( const char* methodname,
145  virtual Bscript::BObjectImp* call_method_id( const int id, Bscript::Executor& ex,
146  bool forcebuiltin = false ) POL_OVERRIDE;
147  virtual Bscript::BObjectImp* copy() const POL_OVERRIDE;
148  virtual std::string getStringRep() const POL_OVERRIDE;
149  virtual size_t sizeEstimate() const POL_OVERRIDE { return sizeof( *this ) + _error.capacity(); }
150  virtual const char* typeOf() const POL_OVERRIDE { return "SQLConnection"; }
151  virtual u8 typeOfInt() const POL_OVERRIDE { return OTSQLConnection; }
152  virtual bool isTrue() const POL_OVERRIDE;
153  // virtual BObjectRef OperSubscript( const BObject& obj );
154 
155 private:
156  std::shared_ptr<ConnectionWrapper> _conn;
157  int _errno;
158  std::string _error;
159 
161  {
162  public:
165  void set( MYSQL* conn );
166  MYSQL* ptr();
167 
168  private:
169  MYSQL* _conn;
170  };
171 };
172 
174 {
175 public:
176  typedef std::function<void()> msg;
178  SQLService();
179  ~SQLService();
180  void start();
181  void stop();
182  void push( msg&& msg_ );
183 
184 private:
185  msg_queue _msgs;
186 };
187 void start_sql_service();
188 }
189 }
190 
191 #endif
192 
193 #endif
unsigned char u8
Definition: rawtypes.h:25
#define POL_OVERRIDE
virtual size_t sizeEstimate() const POL_OVERRIDE
Definition: sqlscrobj.h:73
Clib::message_queue< msg > msg_queue
Definition: sqlscrobj.h:177
MYSQL_FIELD * _fields
Definition: sqlscrobj.h:114
virtual size_t sizeEstimate() const POL_OVERRIDE
Definition: sqlscrobj.h:101
virtual u8 typeOfInt() const POL_OVERRIDE
Definition: sqlscrobj.h:78
virtual const char * typeOf() const POL_OVERRIDE
Definition: sqlscrobj.h:150
virtual u8 typeOfInt() const POL_OVERRIDE
Definition: sqlscrobj.h:106
virtual size_t sizeEstimate() const POL_OVERRIDE
Definition: sqlscrobj.h:149
std::shared_ptr< QueryParam > QueryParams
Definition: sqlscrobj.h:120
MYSQL_FIELD * _fields
Definition: sqlscrobj.h:85
void start_sql_service()
Definition: sqlscrobj.cpp:501
std::function< void()> msg
Definition: sqlscrobj.h:176
virtual const char * typeOf() const POL_OVERRIDE
Definition: sqlscrobj.h:105
virtual u8 typeOfInt() const POL_OVERRIDE
Definition: sqlscrobj.h:151
MYSQL_ROW _row
Definition: sqlscrobj.h:83
std::shared_ptr< ResultWrapper > RES_WRAPPER
Definition: sqlscrobj.h:55
virtual const char * typeOf() const POL_OVERRIDE
Definition: sqlscrobj.h:77
virtual std::string getStringRep() const POL_OVERRIDE
Definition: sqlscrobj.h:72
std::vector< std::string > QueryParam
Definition: sqlscrobj.h:117
std::shared_ptr< ConnectionWrapper > _conn
Definition: sqlscrobj.h:156
Definition: berror.cpp:12
RES_WRAPPER _result
Definition: sqlscrobj.h:84
virtual bool isTrue() const POL_OVERRIDE
Definition: sqlscrobj.h:79