Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TRDBCServer.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TRDBCServer.cxx
1 // $Id: TRDBCServer.cxx,v 1.1.1.1 2004/02/18 20:58:02 dave Exp $
2 //*-- Author : Valeriy Onuchin 14/02/2001
3 //
4 
6 //
7 // TRDBCServer is implementation of TSQLServer with RDBC
8 //
9 // This class is experimental and not completed yet,
10 // as much as possible use TSQLDriverManager instead.
11 //
12 
13 #include <RDBC/TRDBCServer.h>
14 #include <RDBC/TSQLConnection.h>
15 #include <RDBC/TSQLDriverManager.h>
16 #include <RDBC/TSQLResultSet.h>
18 #include <RDBC/TSQLStatement.h>
19 #include <iostream>
20 
23 //___________________________________________________________________
24 TRDBCServer::TRDBCServer(const char *db, const char *uid, const char *pw)
25 {
26  // Open a connection to a DB server. The db arguments should be
27  // of the form
28  // "protocol:[subprotocol]:[driver]://<host>[:<port>][/<database>]", e.g.:
29  // "mysql:odbc:myodbc//pcroot.cern.ch:3456/test".
30  // The uid is the username and pw
31  // the password that should be used for the connection.
32  //
33 
34  fConnection = TSQLDriverManager::GetConnection(db,uid,pw);
35 
36  if(!fConnection) {
37  Error("TRDBCServer", "connection to %s failed", db);
38  return;
39  }
40  TSQLDatabaseMetaData* md = fConnection->GetMetaData();
41  fDbName = md->GetDatabaseProductName();
42 }
43 
44 //___________________________________________________________________
46 {
47  // Close connection to DB server
48 
49  if (IsConnected()) Close();
50 }
51 
52 //______________________________________________________________________________
53 void TRDBCServer::Close(Option_t *)
54 {
55  // Close connection to DB server.
56 
57  if (!fConnection) return;
58  fConnection->Close();
59  if(fConnection->IsClosed()) fConnection = 0;
60 }
61 
62 //______________________________________________________________________________
64 {
65  // Execute SQL command.
66  //
67  // Returns a pointer to a TSQLResultSet object if successful, 0 otherwise.
68 
69  if (!IsConnected()) {
70  Error("Query", "not connected");
71  return 0;
72  }
74 }
75 
76 //___________________________________________________________________
77 Int_t TRDBCServer::SelectDataBase(const char* dbname)
78 {
79  // Select/Change a database. Returns kTRUE if successful,
80  // kFALSE otherwise.
81  //
82  // Corresponds to MySQL statement "USE database"
83 
84  if(!IsConnected()) return 0;
85 
86 
88  TString query = "USE ";
89  query += dbname;
90 
91  if(!stmt->Execute(query)) return 0;
92  delete stmt;
93  return 1;
94 }
95 
96 //______________________________________________________________________________
98 {
99  // List all available databases. Wild is for wildcarding "t%" list all
100  // databases starting with "t".
101  // Returns a pointer to a TSQLResultSet object if successful, 0 otherwise.
102 
103  if (!IsConnected()) {
104  Error("GetDataBases", "not connected");
105  return 0;
106  }
107 
108  TString query;
109 
110  if( fDbName == "MySQL" ) {
111  query= "show databases";
112  if(wild) { query += " like "; query += wild; }
113  return Query(query);
114  } else {
115  return 0;
116  }
117 }
118 
119 //______________________________________________________________________________
120 TSQLResultSet* TRDBCServer::GetTables(const char *dbname, const char *wild)
121 {
122  // List all tables in the specified database. Wild is for wildcarding
123  // "t%" list all tables starting with "t".
124  // Returns a pointer to a TSQLResultSet object if successful, 0 otherwise.
125 
126  if (!IsConnected()) {
127  Error("GetTables", "not connected");
128  return 0;
129  }
130 
131  TString query;
132 
133  if( fDbName == "MySQL") {
134  query = "SHOW TABLES";
135 
136  if(dbname) {
137  if(SelectDataBase(dbname) != 0) {
138  Error("GetTables", "no such database %s", dbname);
139  return 0;
140  }
141  query += " from "; query += dbname;
142  }
143 
144  if(wild) { query += " like "; query += wild; }
145  return Query(query);
146  } else {
147  return 0;
148  }
149 }
150 
151 //______________________________________________________________________________
152 TSQLResultSet* TRDBCServer::GetColumns( const char *dbname, const char *table,
153  const char *wild )
154 {
155  // List all columns in specified table in the specified database.
156  // Wild is for wildcarding "t%" list all columns starting with "t".
157  // Returns a pointer to a TSQLResultSet object if successful, 0 otherwise.
158  // The result object must be deleted by the user.
159 
160  if (!IsConnected()) {
161  Error("GetColumns", "not connected");
162  return 0;
163  }
164 
165  TString query;
166 
167  if ( dbname && (SelectDataBase(dbname) != 0) ) {
168  Error("GetColumns", "no such database %s", dbname);
169  return 0;
170  }
171 
172  if(fDbName == "MySQL") {
173  query = "SHOW COLUMNS FROM ";
174  query += table;
175 
176  if (wild) { query += " LIKE "; query += wild; }
177  return Query(query);
178  } else {
179  return 0;
180  }
181 }
182 
183 //______________________________________________________________________________
184 Int_t TRDBCServer::CreateDataBase(const char *dbname)
185 {
186  // Create a database. Returns kTRUE if successful, kFALSE otherwise.
187 
188  if (!IsConnected()) {
189  Error("CreateDataBase", "not connected");
190  return kFALSE;
191  }
192 
193  Bool_t res;
194  TString query;
195  TSQLStatement* stmt;
196 
197  query = "CREATE DATABASE "; // standart SQL
198  query += dbname;
199 
200  stmt = fConnection->CreateStatement();
201  res = stmt->Execute(query);
202  delete stmt;
203  return res;
204 }
205 
206 //______________________________________________________________________________
207 Int_t TRDBCServer::DropDataBase(const char *dbname)
208 {
209  // Drop (i.e. delete) a database. Returns kTRUE if successful, kFALSE otherwise.
210 
211  if (!IsConnected()) {
212  Error("DropDataBase", "not connected");
213  return 0;
214  }
215 
216  Bool_t res;
217  TString query;
218  TSQLStatement* stmt;
219 
220  query = "DROP DATABASE "; // standart SQL
221  query += dbname;
222 
223  stmt = fConnection->CreateStatement();
224  res = stmt->Execute(query);
225  delete stmt;
226  return res;
227 }
228 
229 //______________________________________________________________________________
231 {
232  // Reload permission tables. Returns kTRUE if successful, kFALSE
233  // otherwise. User must have reload permissions.
234 
235  if (!IsConnected()) {
236  Error("Reload", "not connected");
237  return kFALSE;
238  }
239 
240  Bool_t res;
241  TSQLStatement* stmt;
242 
243  if(fDbName == "MySQL") {
244  stmt = fConnection->CreateStatement();
245  res = stmt->Execute("FLUSH PRIVILEGES");
246  delete stmt;
247  return res;
248  } else {
249  return kFALSE;
250  }
251 }
252 
253 //______________________________________________________________________________
255 {
256  // Shutdown the database server. Returns kTRUE if successful, kFALSE
257  // otherwise. User must have shutdown permissions.
258 
259  if (!IsConnected()) {
260  Error("Shutdown", "not connected");
261  return kFALSE;
262  }
263  return kFALSE; // not implemented
264 }
265 
266 //______________________________________________________________________________
268 {
269  // Returns a string that represents the server version number.
270  //
271  // Corresponds to:
272  //
273  // mysql> SELECT VERSION();
274 
275  if (!IsConnected()) {
276  Error("ServerInfo", "not connected");
277  return 0;
278  }
279 
280  static TString str;
281  TSQLResultSet* rs;
282 
283  if(fDbName == "MySQL") {
284 
285  rs = Query("SELECT VERSION()");
286  if(!rs) return str.Data();
287 
288  if(!rs->Next()) return str.Data();
289  str = rs->GetString(1);
290  delete rs;
291  return str.Data();
292  } else {
293  return str.Data();
294  }
295 }
296 
297