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