BOSS 6.6.4.p01
BESIII Offline Software System
Loading...
Searching...
No Matches
MysqlConnection.h
Go to the documentation of this file.
1// $Header: /bes/bes/BossCvs/Calibration/rdbModel/rdbModel/Db/MysqlConnection.h,v 1.3 2009/03/18 02:01:02 huangb Exp $
2#ifndef RDBMODEL_MYSQLCONNECTION_H
3#define RDBMODEL_MYSQLCONNECTION_H
4
8#include <map>
9#include <iostream>
10
11typedef struct st_mysql MYSQL;
12typedef struct st_mysql_res MYSQL_RES;
13
14namespace rdbModel{
15
16 class MysqlResults;
17 class Datatype;
18 /**
19 Class to handle connection to a MySQL database
20
21 - initiate connection
22 - make queries, including making returned data from queries available
23 - issue SQL statements such as INSERT and UPDATE which have no
24 returned data other than status
25 - close down connection.
26 Someday it might also have a method to create a database
27
28 Initial design will just use host, password, userid passed in.
29 Will be up to caller to insure that the userid has the right
30 privilages for the operations caller intends to do.
31 */
32 class MysqlConnection : public virtual Connection, public Visitor {
33 public:
34 /** Open a connection
35 Allowed operations will depend on userid, etc., specified
36 return true if successful
37 */
38 MysqlConnection(std::ostream* out=0, std::ostream* errOut=0);
39 virtual ~MysqlConnection();
40 virtual bool open(const std::string& host, const std::string& userid,
41 const std::string& password,
42 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,
81 const StringVector& colNames,
82 const StringVector& values,
83 int* auto_value=0,
84 const StringVector* nullCols = 0);
85
86 /**
87 Generic UPDATE. Return value is number of rows changed.
88 */
89 virtual unsigned int update(const std::string& tableName,
90 const StringVector& colNames,
91 const StringVector& values,
92 const Assertion* where=0,
93 const StringVector* nullCols = 0);
94
95 /**
96 Support only for relatively simple SELECT, involving just
97 one table.
98 @param tableName
99 @param getCols vector of columns to be retrieved
100 @param where ptr to an Assertion object
101 @param rowLimit max number of rows to return
102 @param rowOffset offset for first row returned among those satisfying
103 conditions; ignored if 0.
104 @return If the SELECT succeeds, a pointer to an object which
105 manages the returned data; else 0. Caller is responsible for
106 deleting the ResultHandle object.
107 */
108 virtual ResultHandle* select(const std::string& tableName,
109 const StringVector& getCols,
110 const StringVector& orderCols,
111 const Assertion* where=0,
112 int rowLimit=0,
113 int rowOffset=0);
114
115 /**
116 Transmit raw request of any form to our other end. If it is a
117 request that returns results, those results will be stored in a
118 newly-allocated ResultHandle and dbRequest will return a pointer
119 to it. Otherwise dbRequest will return a null pointer.
120 Throw an exception if request fails for any reason.
121 */
122 virtual ResultHandle* dbRequest(const std::string& request);
123
124
125 /**
126 compile method for assertions. Use it internally, but also make
127 it publicly available so assertions belonging to a table
128 can save the compiled version.
129 */
130 virtual bool
131 compileAssertion(const Assertion* a, std::string& sqlString) const;
132
133 /**
134 Turn select and update into no-ops: output SQL string for
135 debugging but don't change db
136 */
137 virtual void disableModify(bool disable) {m_writeDisabled=disable;}
138
139 // Needed to satisfy Visitor interface:
145
148 virtual VisitorState visitQuery(Query*);
149 virtual VisitorState visitSet(Set*);
151
152
153
154
155 private:
156 /// Someday we may want more than one kind of visitor; for example,
157 /// might want one to create dbs
158 enum VISITOR {
159 VISITORundefined,
160 VISITORmatch
161 };
162
163 MYSQL* m_mysql;
164 bool m_connected;
165
166 std::string m_dbName;
167 std::ostream* m_out;
168 std::ostream* m_err;
169
170 // Following collection of data members is only of interest while
171 // visit is in progress.
172
173 VISITOR m_visitorType;
174 bool m_matchDbName;
175
176 /// Keep track of status during matching process
177 MATCH m_matchReturn;
178
179 /// If successful match, save pointer to schema for use with smart insert
180 Rdb* m_rdb;
181
182 /// Also save list of columns we ("service") are responsible for
183 /// Could organize this by table, or just use Column::getTableName()
184 std::vector<Column* > m_ourCols;
185
186 /// For query results while visiting.
187 MYSQL_RES* m_tempRes;
188
189 /// Index by colname; data is row number with "SHOW COLUMNS.." result set
190 std::map<std::string, unsigned int> m_colIx;
191
192 std::string m_primColName;
193
194 static bool m_compileInit;
195 bool m_writeDisabled;
196
197 static void compileInit();
198 static bool compileComparison(Assertion::Operator* op,
199 std::string& sqlString);
200 static bool compileOperator(Assertion::Operator* op,
201 std::string &sqlString);
202
203 bool checkDType(Datatype* dtype, const std::string& sqlType);
204
205 //huangb add to store the database information
206 std::string m_host;
207 std::string m_user;
208 std::string m_password;
209// std::string m_dbName;
210 };
211
212} // end namespace rdbModel
213#endif
struct st_mysql_res MYSQL_RES
struct st_mysql MYSQL
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.
std::ostream * getOut() const
virtual Visitor::VisitorState visitAssertion(Assertion *)
virtual VisitorState visitQuery(Query *)
std::ostream * getErrOut() const
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 void disableModify(bool disable)
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 *)
std::vector< std::string > StringVector
Definition: Connection.h:52