BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Calibration/rdbModel/include/rdbModel/Db/MysqlConnection.h
Go to the documentation of this file.
1// $Header: /bes/bes/BossCvs/Calibration/rdbModel/rdbModel/Db/MysqlConnection.h,v 1.3
2// 2009/03/18 02:01:02 huangb Exp $
3#ifndef RDBMODEL_MYSQLCONNECTION_H
4#define RDBMODEL_MYSQLCONNECTION_H
5
6#include "rdbModel/Db/Connection.h"
7#include "rdbModel/Management/Visitor.h"
8#include "rdbModel/Tables/Assertion.h"
9#include <iostream>
10#include <map>
11
12typedef struct st_mysql MYSQL;
13typedef struct st_mysql_res MYSQL_RES;
14
15namespace rdbModel {
16
17 class MysqlResults;
18 class Datatype;
19 /**
20 Class to handle connection to a MySQL database
21
22 - initiate connection
23 - make queries, including making returned data from queries available
24 - issue SQL statements such as INSERT and UPDATE which have no
25 returned data other than status
26 - close down connection.
27 Someday it might also have a method to create a database
28
29 Initial design will just use host, password, userid passed in.
30 Will be up to caller to insure that the userid has the right
31 privilages for the operations caller intends to do.
32 */
33 class MysqlConnection : public virtual Connection, public Visitor {
34 public:
35 /** Open a connection
36 Allowed operations will depend on userid, etc., specified
37 return true if successful
38 */
39 MysqlConnection( std::ostream* out = 0, std::ostream* errOut = 0 );
40 virtual ~MysqlConnection();
41 virtual bool open( const std::string& host, const std::string& userid,
42 const std::string& password, const std::string& dbName );
43 // ,unsigned int port=0);
44
45 /** Parameter is normally path for an xml file descrbing the
46 connection parameters */
47 virtual bool open( const std::string& parms );
48
49 /** Close the current open connection , if any. Return true if there
50 was a connection to close and it was closed successfully */
51 virtual bool close();
52
53 /// Return true iff open has been done with no matching close
54 virtual bool isConnected() { return m_connected; }
55
56 std::ostream* getOut() const { return m_out; }
57 std::ostream* getErrOut() const { return m_err; }
58 /**
59 Check to what degree local schema definition is compatible with
60 remote db accessed via this connection. By default check db names
61 match, but this may be disabled.
62
63 If match is successful, may also want to cache information about
64 some things; for example, rows for which agent = "service"
65 */
66 virtual MATCH matchSchema( Rdb* rdb, bool matchDbName = true );
67
68 /** Typical derived class will form a syntactically correct
69 INSERT statement from the input arguments and issue it to
70 the dbms. Return true if row was inserted successfully
71 If @a auto_value is non-zero and the table has an auto-increment
72 column, its value will be returned.
73 If @a nullCols is non-zero, insertRow will treat each string
74 in the vector as a column name whose value should be set to
75 NULL
76
77 Might also want to add a routine for INSERT ... SELECT
78 */
79
80 virtual bool insertRow( const std::string& tableName, const StringVector& colNames,
81 const StringVector& values, int* auto_value = 0,
82 const StringVector* nullCols = 0 );
83
84 /**
85 Generic UPDATE. Return value is number of rows changed.
86 */
87 virtual unsigned int update( const std::string& tableName, const StringVector& colNames,
88 const StringVector& values, const Assertion* where = 0,
89 const StringVector* nullCols = 0 );
90
91 /**
92 Support only for relatively simple SELECT, involving just
93 one table.
94 @param tableName
95 @param getCols vector of columns to be retrieved
96 @param where ptr to an Assertion object
97 @param rowLimit max number of rows to return
98 @param rowOffset offset for first row returned among those satisfying
99 conditions; ignored if 0.
100 @return If the SELECT succeeds, a pointer to an object which
101 manages the returned data; else 0. Caller is responsible for
102 deleting the ResultHandle object.
103 */
104 virtual ResultHandle* select( const std::string& tableName, const StringVector& getCols,
105 const StringVector& orderCols, const Assertion* where = 0,
106 int rowLimit = 0, int rowOffset = 0 );
107
108 /**
109 Transmit raw request of any form to our other end. If it is a
110 request that returns results, those results will be stored in a
111 newly-allocated ResultHandle and dbRequest will return a pointer
112 to it. Otherwise dbRequest will return a null pointer.
113 Throw an exception if request fails for any reason.
114 */
115 virtual ResultHandle* dbRequest( const std::string& request );
116
117 /**
118 compile method for assertions. Use it internally, but also make
119 it publicly available so assertions belonging to a table
120 can save the compiled version.
121 */
122 virtual bool compileAssertion( const Assertion* a, std::string& sqlString ) const;
123
124 /**
125 Turn select and update into no-ops: output SQL string for
126 debugging but don't change db
127 */
128 virtual void disableModify( bool disable ) { m_writeDisabled = disable; }
129
130 // Needed to satisfy Visitor interface:
136
139 virtual VisitorState visitQuery( Query* );
140 virtual VisitorState visitSet( Set* );
142
143 private:
144 /// Someday we may want more than one kind of visitor; for example,
145 /// might want one to create dbs
146 enum VISITOR { VISITORundefined, VISITORmatch };
147
148 MYSQL* m_mysql;
149 bool m_connected;
150
151 std::string m_dbName;
152 std::ostream* m_out;
153 std::ostream* m_err;
154
155 // Following collection of data members is only of interest while
156 // visit is in progress.
157
158 VISITOR m_visitorType;
159 bool m_matchDbName;
160
161 /// Keep track of status during matching process
162 MATCH m_matchReturn;
163
164 /// If successful match, save pointer to schema for use with smart insert
165 Rdb* m_rdb;
166
167 /// Also save list of columns we ("service") are responsible for
168 /// Could organize this by table, or just use Column::getTableName()
169 std::vector<Column*> m_ourCols;
170
171 /// For query results while visiting.
172 MYSQL_RES* m_tempRes;
173
174 /// Index by colname; data is row number with "SHOW COLUMNS.." result set
175 std::map<std::string, unsigned int> m_colIx;
176
177 std::string m_primColName;
178
179 static bool m_compileInit;
180 bool m_writeDisabled;
181
182 static void compileInit();
183 static bool compileComparison( Assertion::Operator* op, std::string& sqlString );
184 static bool compileOperator( Assertion::Operator* op, std::string& sqlString );
185
186 bool checkDType( Datatype* dtype, const std::string& sqlType );
187
188 // huangb add to store the database information
189 std::string m_host;
190 std::string m_user;
191 std::string m_password;
192 // std::string m_dbName;
193 };
194
195} // end namespace rdbModel
196#endif
*******DOUBLE PRECISION m_EGridB INTEGER m_out
Definition BStra.h:10
virtual VisitorState visitInterRow(InterRow *)
virtual Visitor::VisitorState visitRdb(Rdb *)
This method says if the visitor is recursive or not.
virtual bool isConnected()
Return true iff open has been done with no matching close.
virtual Visitor::VisitorState visitAssertion(Assertion *)
virtual VisitorState visitQuery(Query *)
virtual VisitorState visitSet(Set *)
virtual bool open(const std::string &host, const std::string &userid, const std::string &password, const std::string &dbName)
virtual ResultHandle * select(const std::string &tableName, const StringVector &getCols, const StringVector &orderCols, const Assertion *where=0, int rowLimit=0, int rowOffset=0)
virtual Visitor::VisitorState visitTable(Table *)
virtual Visitor::VisitorState visitIndex(Index *)
virtual bool compileAssertion(const Assertion *a, std::string &sqlString) const
virtual VisitorState visitSupersede(Supersede *)
virtual ResultHandle * dbRequest(const std::string &request)
virtual VisitorState visitInsertNew(InsertNew *)
virtual MATCH matchSchema(Rdb *rdb, bool matchDbName=true)
virtual bool insertRow(const std::string &tableName, const StringVector &colNames, const StringVector &values, int *auto_value=0, const StringVector *nullCols=0)
virtual Visitor::VisitorState visitColumn(Column *)
virtual unsigned int update(const std::string &tableName, const StringVector &colNames, const StringVector &values, const Assertion *where=0, const StringVector *nullCols=0)
MysqlConnection(std::ostream *out=0, std::ostream *errOut=0)