BOSS 7.0.9
BESIII Offline Software System
Loading...
Searching...
No Matches
Connection.h
Go to the documentation of this file.
1// $Header: /bes/bes/BossCvs/Calibration/rdbModel/rdbModel/Db/Connection.h,v 1.1.1.1 2005/10/17 06:10:53 maqm Exp $
2#ifndef RDBMODEL_CONNECTION_H
3#define RDBMODEL_CONNECTION_H
4#include <vector>
5#include <string>
6
7
8namespace rdbModel{
9
10 /** The following are used as return codes from the function schemaMatch.
11 which checks for compatibility between the schema definition embodied
12 in an Rdb object and the one at the other end of a Connection.
13 <ul>
14 <li>MATCHequivalent means that every comparison attempted has succeeded
15 </li>
16 <li>MATCHcompatible means that everything required by the local
17 schema exists in the remote db, but not necessarily the other way around.
18 selects, inserts and updates attempted via the Connection should work.
19 </li>
20 <li>MATCHfail means the remote db is missing something described locally,
21 or types are incompatible for corresponding objects.
22 </li>
23 </ul>
24 */
25 enum MATCH {
30 };
31
32 class ResultHandle;
33 class Assertion;
34 class Rdb;
35 /**
36 Class to handle connection to an SQL database, or something very like
37 it. It should be able to
38 - initiate connection
39 - make queries, including making returned data from queries available
40 - issue SQL statements such as INSERT and UPDATE which have no
41 returned data other than status
42 - close down connection.
43 Someday it might also have a method to create a database
44
45 Maybe make it pure virtual? And make a MySQL implementation
46 derived from it.
47
48 Initial design will just use host, password, userid passed in.
49 Will be up to caller to insure that the userid has the right
50 privilages for the operations caller intends to do.
51 */
52 typedef std::vector<std::string> StringVector;
53
54 class Connection {
55 /** Open a connection
56 Allowed operations will depend on userid, etc., specified
57 return true if successful
58 */
59 public:
61 virtual ~Connection() {};
62 virtual bool open(const std::string& host, const std::string& userid,
63 const std::string& password,
64 const std::string& dbName) = 0;
65 //, unsigned int port) = 0;
66 /** Close the current open connection , if any. Return true if there
67 was a connection to close and it was closed successfully */
68 virtual bool close() = 0;
69
70 /** Parameter is normally path for an xml file descrbing the
71 connection parameters */
72 virtual bool open(const std::string& parms) = 0;
73
74 /// Return true iff open has been done with no matching close
75 virtual bool isConnected() = 0;
76
77 virtual std::ostream* getOut() const = 0;
78 virtual std::ostream* getErrOut() const = 0;
79
80 /**
81 Check to what degree local schema definition is compatible with
82 remote db accessed via this connection. By default check db names
83 match, but this may be disabled.
84 */
85 virtual MATCH matchSchema(Rdb *rdb, bool matchDbName=true) = 0;
86
87 /** Typical derived class will form a syntactically correct
88 INSERT statement from the input arguments and issue it to
89 the dbms. Return true if row was inserted successfully
90
91 Might also want to add a routine for INSERT ... SELECT
92 */
93 virtual bool insertRow(const std::string& tableName,
94 const StringVector& colNames,
95 const StringVector& values,
96 int* auto_value=0,
97 const StringVector* nullCols = 0) = 0;
98
99 /*
100 So far anticipated uses of UPDATE would just modify a single row
101 identified by ser_no (or, more generally, primary key), so
102 make a method for this case. Can call the more general
103 version below.
104
105 @return true if no errors were encountered; else false. If the
106 update requested was error-free but entailed no actual change,
107 returns ??
108 */
109 /*
110 At this level, don't have access to column name for primary
111 key, if any. This has to be done by caller
112 virtual bool updateUnique(const std::string& tableName,
113 const StringVector& colNames,
114 const StringVector& values,
115 const std::string& keyValue) = 0;
116 */
117
118 /**
119 Generic UPDATE. Return value is number of rows changed.
120 */
121 virtual unsigned int update(const std::string& tableName,
122 const StringVector& colNames,
123 const StringVector& values,
124 const Assertion* where=0,
125 const StringVector* nullCols = 0) = 0;
126
127 /**
128 Support only for relatively simple SELECT, involving just
129 one table.
130 @param tableName
131 @param getCols vector of columns to be retrieved
132 @param where ptr to an Assertion object
133 @param rowLimit max number of rows to return
134 @param rowOffset offset for first row returned among those satisfying
135 conditions; ignored if 0.
136 @return If the SELECT succeeds, a pointer to an object which
137 manages the returned data; else 0. Caller is responsible for
138 deleting the ResultHandle object.
139 */
140 virtual ResultHandle* select(const std::string& tableName,
141 const StringVector& getCols,
142 const StringVector& orderCols,
143 const Assertion* where=0,
144 int rowLimit=0,
145 int rowOffset=0)=0;
146
147 /**
148 Turn select and update into no-ops: output SQL string for
149 debugging but don't change db
150 */
151 virtual void disableModify(bool disable)=0;
152
153 /**
154 Transmit raw request of any form to database. If it is a request
155 that returns results, those results will be stored in a newly-
156 allocated ResultHandle and dbRequest will return a pointer to
157 it. Otherwise dbRequest will return a null pointer.
158 Throw an exception if request fails for any reason.
159 */
160 virtual ResultHandle* dbRequest(const std::string& request)=0;
161
162 /**
163 compile method for assertions. Use it internally, but also make
164 it publicly available so assertions belonging to a table
165 can save the compiled version.
166 */
167 virtual bool compileAssertion(const Assertion* a, std::string& sqlString)
168 const = 0;
169
170 };
171
172} // end namespace rdbModel
173#endif
virtual ResultHandle * select(const std::string &tableName, const StringVector &getCols, const StringVector &orderCols, const Assertion *where=0, int rowLimit=0, int rowOffset=0)=0
virtual MATCH matchSchema(Rdb *rdb, bool matchDbName=true)=0
virtual unsigned int update(const std::string &tableName, const StringVector &colNames, const StringVector &values, const Assertion *where=0, const StringVector *nullCols=0)=0
virtual bool isConnected()=0
Return true iff open has been done with no matching close.
virtual bool compileAssertion(const Assertion *a, std::string &sqlString) const =0
virtual std::ostream * getOut() const =0
virtual bool open(const std::string &host, const std::string &userid, const std::string &password, const std::string &dbName)=0
virtual void disableModify(bool disable)=0
virtual bool insertRow(const std::string &tableName, const StringVector &colNames, const StringVector &values, int *auto_value=0, const StringVector *nullCols=0)=0
virtual ~Connection()
Definition: Connection.h:61
virtual ResultHandle * dbRequest(const std::string &request)=0
virtual bool close()=0
virtual std::ostream * getErrOut() const =0
virtual bool open(const std::string &parms)=0
@ MATCHequivalent
Definition: Connection.h:26
@ MATCHnoConnection
Definition: Connection.h:29
@ MATCHfail
Definition: Connection.h:28
@ MATCHcompatible
Definition: Connection.h:27
std::vector< std::string > StringVector
Definition: Connection.h:52