FEDRA emulsion software from the OPERA Collaboration
TOracleServer Class Reference

#include <TOracleServer.h>

Inheritance diagram for TOracleServer:
Collaboration diagram for TOracleServer:

Public Member Functions

void Close (Option_t *opt="")
 
Int_t CreateDataBase (const char *dbname)
 
Int_t DropDataBase (const char *dbname)
 
TSQLResult * GetColumns (const char *dbname, const char *table, const char *wild=0)
 
TSQLResult * GetDataBases (const char *wild=0)
 
TSQLResult * GetTables (const char *dbname, const char *wild=0)
 
Int_t PrintResult ()
 
Int_t PrintResultStr (TString &result)
 
TSQLResult * Query (const char *sql)
 
Int_t QueryTree (char *query, TTree *tree, char *leafs=0)
 
Int_t Reload ()
 
Int_t SelectDataBase (const char *dbname)
 
const char * ServerInfo ()
 
Int_t Shutdown ()
 
 TOracleServer (const char *db, const char *uid, const char *pw)
 
 ~TOracleServer ()
 

Private Attributes

Connection * fConn
 
Environment * fEnv
 
oracle::occi::Statement * fStmt
 

Friends

class TOracleServerE
 
class TOracleServerE2
 
class TOracleServerE2W
 
class TOracleServerE2WFB
 
class TOracleServerE2WX
 

Constructor & Destructor Documentation

◆ TOracleServer()

TOracleServer::TOracleServer ( const char *  db,
const char *  uid,
const char *  pw 
)

◆ ~TOracleServer()

TOracleServer::~TOracleServer ( )
69 {
70  // Close connection to Oracle DB server.
71 
72  if (IsConnected())
73  Close();
74 }
void Close(Option_t *opt="")
Definition: TOracleServer.cxx:77

Member Function Documentation

◆ Close()

void TOracleServer::Close ( Option_t *  opt = "")
78 {
79  // Close connection to Oracle DB server.
80 
81  try {
82  if (fStmt)
83  fConn->terminateStatement(fStmt);
84  if (fConn)
85  fEnv->terminateConnection(fConn);
86  if (fEnv)
87  Environment::terminateEnvironment(fEnv);
88  } catch (SQLException &oraex) {
89  Error("TOracleServer", "close connection failed: (error: %s)", (oraex.getMessage()).c_str());
90  //MakeZombie();
91  }
92 
93  fPort = -1;
94 }
Connection * fConn
Definition: TOracleServer.h:48
Environment * fEnv
Definition: TOracleServer.h:47
oracle::occi::Statement * fStmt
Definition: TOracleServer.h:52

◆ CreateDataBase()

Int_t TOracleServer::CreateDataBase ( const char *  dbname)
250 {
251  // Create a database. Returns 0 if successful, non-zero otherwise.
252  // NOT IMPLEMENTED.
253 
254  if (!IsConnected()) {
255  Error("CreateDataBase", "not connected");
256  return -1;
257  }
258  return -1;
259 }

◆ DropDataBase()

Int_t TOracleServer::DropDataBase ( const char *  dbname)
263 {
264  // Drop (i.e. delete) a database. Returns 0 if successful, non-zero
265  // otherwise.
266  // NOT IMPLEMENTED.
267 
268  if (!IsConnected()) {
269  Error("DropDataBase", "not connected");
270  return -1;
271  }
272 
273  return -1;
274 }

◆ GetColumns()

TSQLResult * TOracleServer::GetColumns ( const char *  dbname,
const char *  table,
const char *  wild = 0 
)
198 {
199  // List all columns in specified table in the specified database.
200  // Wild is for wildcarding "t%" list all columns starting with "t".
201  // Returns a pointer to a TSQLResult object if successful, 0 otherwise.
202  // The result object must be deleted by the user.
203 
204  if (!IsConnected()) {
205  Error("GetColumns", "not connected");
206  return 0;
207  }
208 
209  if (SelectDataBase(dbname) != 0) {
210  Error("GetColumns", "no such database %s", dbname);
211  return 0;
212  }
213  return new TOracleResult(fConn, table);
214 }
Definition: TOracleResult.h:35
Int_t SelectDataBase(const char *dbname)
Definition: TOracleServer.cxx:217

◆ GetDataBases()

TSQLResult * TOracleServer::GetDataBases ( const char *  wild = 0)
233 {
234  // List all available databases. Wild is for wildcarding "t%" list all
235  // databases starting with "t".
236  // Returns a pointer to a TSQLResult object if successful, 0 otherwise.
237  // The result object must be deleted by the user.
238  // NOT IMPLEMENTED.
239 
240  if (!IsConnected()) {
241  Error("GetDataBases", "not connected");
242  return 0;
243  }
244 
245  return 0;
246 }

◆ GetTables()

TSQLResult * TOracleServer::GetTables ( const char *  dbname,
const char *  wild = 0 
)
169 {
170  // List all tables in the specified database. Wild is for wildcarding
171  // "t%" list all tables starting with "t".
172  // Returns a pointer to a TSQLResult object if successful, 0 otherwise.
173  // The result object must be deleted by the user.
174 
175  // In Oracle 9 and above, table is accessed in schema.table format.
176  // GetTables returns tables in all schemas accessible for the user.
177  // Assumption: table ALL_OBJECTS is accessible for the user, which is true in Oracle 10g
178  // The returned TSQLResult has two columns: schema_name, table_name
179  // "dbname": if specified, return table list of this schema, or return all tables
180  // "wild" is not used in this implementation
181 
182  if (!IsConnected()) {
183  Error("GetTables", "not connected");
184  return 0;
185  }
186 
187  TString sqlstr("SELECT owner, object_name FROM ALL_OBJECTS WHERE object_type='TABLE'");
188  if (dbname)
189  sqlstr = sqlstr + " AND owner='" + dbname + "'";
190  TSQLResult *tabRs;
191  tabRs = Query(sqlstr.Data());
192  return tabRs;
193 }
TSQLResult * Query(const char *sql)
Definition: TOracleServer.cxx:97

◆ PrintResult()

Int_t TOracleServer::PrintResult ( )
394 {
395  if (!fStmt) printf("statement is not defined!\n");
396  else {
397  ResultSet *rset = fStmt->getResultSet();
398  vector<MetaData> cmd = rset->getColumnListMetaData();
399  const int nlmax=cmd.size();
400  printf("TOracleServer::PrintResult: %d columns\n", nlmax);
401  if(nlmax<1) return 0;
402 
403  TString leaflist(nlmax*64);
404 
405  for (int i = 0; i < nlmax; i++) {
406  string s = cmd[i].getString(MetaData::ATTR_NAME);
407  if (i > 0) leaflist+=" : ";
408  leaflist+= s.c_str();
409  }
410 
411  printf("\n #: %s\n", leaflist.Data());
412 
413  int line=0;
414  while (rset->next())
415  {
416  printf("%6d: ",++line);
417  for (int i = 1; i <= nlmax; i++)
418  {
419  string fruit = rset->getString(i);
420  printf("%s ", rset->getString(i).c_str() );
421  }
422  printf("\n");
423  }
424 
425  delete rset;
426  }
427  return 0;
428 }
EdbSegP * s
Definition: tlg2pattern.C:32

◆ PrintResultStr()

Int_t TOracleServer::PrintResultStr ( TString &  result)
432 {
433  int nlines=0;
434  if (!fStmt) Log(1,"TOracleServer::PrintResultStr","ERROR: statement is not defined!");
435  else {
436  ResultSet *rset = fStmt->getResultSet();
437  vector<MetaData> cmd = rset->getColumnListMetaData();
438  const int nlmax=cmd.size();
439  Log(2,"TOracleServer::PrintResult","%d columns", nlmax);
440  if(nlmax<1) return nlines;
441 
442  TString leaflist(nlmax*64);
443 
444  for (int i = 0; i < nlmax; i++) {
445  string s = cmd[i].getString(MetaData::ATTR_NAME);
446  if (i > 0) leaflist+=" : ";
447  leaflist+= s.c_str();
448  }
449 
450  result += Form(" #: %s\n", leaflist.Data());
451 
452  int line=0;
453  while (rset->next())
454  {
455  result += Form("%6d: ",++line);
456  for (int i = 1; i <= nlmax; i++)
457  {
458  result += Form("%s ", rset->getString(i).c_str() );
459  }
460  result += Form("\n");
461  nlines++;
462  }
463 
464  delete rset;
465  }
466  return nlines;
467 }
bool Log(int level, const char *location, const char *fmt,...)
Definition: EdbLog.cxx:75

◆ Query()

TSQLResult * TOracleServer::Query ( const char *  sql)
98 {
99  // Execute SQL command. Result object must be deleted by the user.
100  // Returns a pointer to a TSQLResult object if successful, 0 otherwise.
101  // The result object must be deleted by the user.
102 
103  if (!IsConnected()) {
104  Error("Query", "not connected");
105  return 0;
106  }
107 
108  try {
109  if (!fStmt)
110  fStmt = fConn->createStatement();
111 
112  // count the number of rows of the resultset of this select
113  // NOTE: Oracle doesn't provide a way through OCI or OCCI to count
114  // the number of rows. The reason is the memory concern of client
115  // application. Consider a select statment with 1 million rows
116  // returned. In Oracle, user can set prefetch size to repeatedly
117  // retrieve all rows by calling next(#rows_to_fetch). By default,
118  // prefetch size is set to 1, meaning client only fetch 1 row each
119  // time it contacts db server.
120  // The best-so-far way to count the number of rows is to traverse the
121  // resultset (count(query)). This method is neither efficient, fast
122  // nor 100% accurate. Please see OCI/OCCI discussion forum for details
123  // So the only purpose to count rows is follow TSQL specification.
124 
125  // TODO: We should change TSQL spec on GetRowCount(). Not every db server
126  // provides natural way to do so like mysql. User can loop over resultset
127  // and get the count after the last next() unless he must know the count
128  // before next()
129 
130  // NOTE: sql should not end with ";" !!!
131  int row_count = -1;
132  //char sql_chars[strlen(sql)+1],*str;
133  char* sql_chars = new char[strlen(sql)+1];
134  char* str;
135  strcpy(sql_chars, sql);
136  str = sql_chars;
137  // skip space and newline chars
138  while ( *str == '\n' || *str == '\t' || *str == ' ' || *str == '\r' )
139  str ++;
140  string sql_string = sql;
141  if (strncasecmp(str, "SELECT",6)==0) {
142  string count_sql = "select COUNT(*) from ( " + sql_string + " )";
143  fStmt->setSQL(count_sql.c_str());
144  fStmt->execute();
145 
146  if (fStmt->status() == Statement::RESULT_SET_AVAILABLE) {
147  ResultSet *count_rs = fStmt->getResultSet();
148  if (count_rs->next())
149  row_count = count_rs->getInt(1);
150  fStmt->closeResultSet(count_rs);
151  }
152  }
153  // NOTE: between above and below execute(), if there is any DDL operated
154  // on target tables, the row_count may not be accurate
155  fStmt->setSQL(sql);
156  fStmt->execute();
157 
158  TOracleResult *res = new TOracleResult(fStmt, row_count);
159  return res;
160  } catch (SQLException &oraex) {
161  Error("TOracleServer", "query failed: (error: %s)", (oraex.getMessage()).c_str());
162  }
163 
164  return 0;
165 }
strcpy(cmd,"cp Shower.root Shower2.root")

◆ QueryTree()

Int_t TOracleServer::QueryTree ( char *  query,
TTree *  tree,
char *  leafs = 0 
)
319 {
320  // read from database the given query and fill the tree with the results
321  // example: db->QueryTree("select * from tb_plates",t);
322  // VT(2004)
323 
324  if (!IsConnected()) {
325  Error("QueryTree", "not connected");
326  return 0;
327  }
328  if (!tree) {
329  Error("QueryTree", "tree is not valid");
330  return 0;
331  }
332 
333  try {
334  if (!fStmt)
335  fStmt = fConn->createStatement();
336  fStmt->setSQL(query);
337  fStmt->setPrefetchRowCount(2000);
338  printf("\nexecute sql query: %s ...\n",query);
339  fStmt->execute();
340 
341  ResultSet *rset = fStmt->getResultSet();
342  vector<MetaData> cmd = rset->getColumnListMetaData();
343  const int nlmax=cmd.size();
344  printf("Number of metadata fields: %d\n", nlmax);
345  if(nlmax<1) return 0;
346 
347  TString leaflist(nlmax*64);
348  int ind=0;
349  int *ifields = new int[nlmax];
350  int dtype=0;
351 
352  for (int i = 0; i < nlmax; i++) {
353  dtype = cmd[i].getInt(MetaData::ATTR_DATA_TYPE);
354  printf("\nocci field type: %d \t",dtype);
355  string s = cmd[i].getString(MetaData::ATTR_NAME);
356  if (dtype == OCCI_SQLT_NUM )
357  {
358  ifields[ind++] = i + 1;
359  if (ind > 1) leaflist+=":";
360  leaflist+= s.c_str();
361  leaflist+="/F";
362  printf("%s added", s.c_str());
363  } else printf("%s skipped", s.c_str());
364  }
365 
366  leaflist.ToLower();
367  printf("\n\nleaflist=%s\n", leaflist.Data());
368  if(leafs) {
369  leaflist=leafs;
370  printf("\n\nleaflist=%s\n", leaflist.Data());
371  }
372  Float_t *data = new Float_t[ind];
373  tree->Branch("query", data, leaflist.Data() );
374 
375  while (rset->next() == 1)
376  {
377  for (int i = 0; i < ind; i++)
378  data[i] = rset->getFloat(ifields[i]);
379  tree->Fill();
380  }
381 
382  delete rset;
383  delete[] ifields;
384  delete[] data;
385  return tree->GetEntries();
386  } catch (SQLException &oraex) {
387  Error("TOracleServer", "QueryTree failed: (error: %s)", (oraex.getMessage()).c_str());
388  }
389  return 0;
390 }

◆ Reload()

Int_t TOracleServer::Reload ( )
278 {
279  // Reload permission tables. Returns 0 if successful, non-zero
280  // otherwise. User must have reload permissions.
281  // NOT IMPLEMENTED.
282 
283  if (!IsConnected()) {
284  Error("Reload", "not connected");
285  return -1;
286  }
287  return -1;
288 }

◆ SelectDataBase()

Int_t TOracleServer::SelectDataBase ( const char *  dbname)
218 {
219  // Select a database. Returns 0 if successful, non-zero otherwise.
220  // NOT IMPLEMENTED.
221 
222  if (!IsConnected()) {
223  Error("SelectDataBase", "not connected");
224  return -1;
225  }
226 
227  // do nothing and return success code
228  return 0;
229 }

◆ ServerInfo()

const char * TOracleServer::ServerInfo ( )
306 {
307  // Return server info.
308  // NOT IMPLEMENTED.
309 
310  if (!IsConnected()) {
311  Error("ServerInfo", "not connected");
312  return 0;
313  }
314  return "Oracle";
315 }

◆ Shutdown()

Int_t TOracleServer::Shutdown ( )
292 {
293  // Shutdown the database server. Returns 0 if successful, non-zero
294  // otherwise. User must have shutdown permissions.
295  // NOT IMPLEMENTED.
296 
297  if (!IsConnected()) {
298  Error("Shutdown", "not connected");
299  return -1;
300  }
301  return -1;
302 }

Friends And Related Function Documentation

◆ TOracleServerE

friend class TOracleServerE
friend

◆ TOracleServerE2

friend class TOracleServerE2
friend

◆ TOracleServerE2W

friend class TOracleServerE2W
friend

◆ TOracleServerE2WFB

friend class TOracleServerE2WFB
friend

◆ TOracleServerE2WX

friend class TOracleServerE2WX
friend

Member Data Documentation

◆ fConn

Connection* TOracleServer::fConn
private

◆ fEnv

Environment* TOracleServer::fEnv
private

◆ fStmt

oracle::occi::Statement* TOracleServer::fStmt
private

The documentation for this class was generated from the following files: