Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ODBCConnection.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ODBCConnection.cxx
1 // $Id: ODBCConnection.cxx,v 1.2 2010/09/17 14:55:49 phnxbld Exp $
2 //*-- Author : Valeriy Onuchin 14/02/2000
3 //
4 
5 /**************************************************************************
6 
7  ROOT wrappers of libodbc++ library
8 
9  Copyright (C) 1999-2000 Manush Dodunekov <manush@stendahls.net>
10 
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Library General Public
13  License as published by the Free Software Foundation; either
14  version 2 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Library General Public License for more details.
20 
21  You should have received a copy of the GNU Library General Public License
22  along with this library; see the file COPYING. If not, write to
23  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  Boston, MA 02111-1307, USA.
25 
26 **************************************************************************/
27 
29 //
30 //
31 // A connection (session) with a specific database. Within the context
32 // of a TSQLConnection, SQL statements are executed and results are
33 // returned. A TSQLConnection's database is able to provide information
34 // describing its tables, its supported SQL grammar, its stored procedures,
35 // the capabilities of this connection, and so on. This information is
36 // obtained with the GetMetaData() method.
37 //
38 // See also:
39 // TSQLDriverManager::GetConnection(TString)
40 // TSQLStatement TSQLPreparedStatement TSQLCallableStatement
41 // TSQLResultSet TSQLDatabaseMetaData
42 //
44 //
45 // A transaction is a recoverable sequence of SQL operations grouped
46 // as a single unit. The initiation and termination of transaction
47 // define the following points of data consistency within an
48 // application process; either all SQL operations within a transaction
49 // are applied to the data source (committed), or the effects of all
50 // SQL operations within a transaction are completely "undone"
51 // (rolled back).
52 // RDBC provides the following modes of transaction processing
53 // that determine haw and when transactions are to be committed (or,
54 // if possible rolled back):
55 //
56 // - Auto-commit mode
57 // - Manual-commit mode
58 //
59 // defined by TSQLConnection::SetAutoCommit( Bool_t autoCommit )
60 //
62 //
63 // In multi-user database system, transactions can occur
64 // simultaneously, and each transaction has potential to interfere
65 // with another one. When transactions are not isolated from each
66 // other in multi-user enviroments, the following three types of
67 // events can occur:
68 //___________________________________________________________________
69 //
70 // Dirty Read:
71 //
72 // Transaction 1 changes a row. Transaction 2 reads
73 // the changed row before Transacion 1 commits the change. If
74 // Transaction 1 rolls back the change, Transacion 2 will have
75 // read a row that is considered to have never existed
76 //
77 //___________________________________________________________________
78 //
79 // Nonrepeatable Read:
80 //
81 // Transaction 1 reads a row. Transaction 2 updates or deletes
82 // that row and commits the change. If Transaction 1 attempts
83 // to reread the row, it will receive different row values or
84 // discover that the row has been deleted.
85 //
86 //___________________________________________________________________
87 //
88 // Phantom:
89 //
90 // Transaction 1 reads a set of rows that satisfy some search
91 // criteria. Transaction 2 generates one or more rows ( either
92 // through inserts or updates ) that match the search criteria
93 // If transacion 1 re-executes the statement that reads the rows,
94 // it receives a different set of rows.
95 //
97 //
98 // ODBC defines four levels of transaction isolation that can,
99 // prevent all, some, or none of these events from occurring.
100 // They are:
101 //___________________________________________________________________
102 //
103 // kTRANSACTION_NONE
104 //
105 // Indicates that transactions are not supported.
106 //
107 //___________________________________________________________________
108 //
109 // kTRANSACTION_READ_UNCOMMITTED
110 //
111 // Dirty reads, non-repeatable reads and phantom reads can occur.
112 // This level allows a row changed by one transaction to be read
113 // by another transaction before any changes in that row have been
114 // committed (a "dirty read"). If any of the changes are rolled back,
115 // the second transaction will have retrieved an invalid row.
116 //
117 //___________________________________________________________________
118 //
119 // kTRANSACTION_READ_COMMITTED
120 //
121 //
122 // Dirty reads are prevented; non-repeatable reads and phantom reads
123 // can occur. This level only prohibits a transaction from reading a
124 // row with uncommitted changes in it.
125 //
126 //___________________________________________________________________
127 //
128 // kTRANSACTION_REPEATABLE_READ
129 //
130 // Dirty reads and non-repeatable reads are prevented; phantom reads
131 // can occur. This level prohibits a transaction from reading a row
132 // with uncommitted changes in it, and it also prohibits the situation
133 // where one transaction reads a row, a second transaction alters the
134 // row, and the first transaction rereads the row, getting different
135 // values the second time (a "non-repeatable read").
136 //
137 //___________________________________________________________________
138 //
139 // kTRANSACTION_SERIALIZABLE
140 //
141 // Dirty reads, non-repeatable reads and phantom reads are prevented.
142 // This level includes the prohibitions in kTRANSACTION_REPEATABLE_READ
143 // and further prohibits the situation where one transaction reads
144 // all rows that satisfy a WHERE condition, a second transaction
145 // inserts a row that satisfies that WHERE condition, and the first
146 // transaction rereads for the same condition, retrieving the
147 // additional "phantom" row in the second read.
148 //
149 //
151 
152 #include "ODBCConnection.h"
153 #include "ODBCStatement.h"
154 #include "ODBCPreparedStatement.h"
155 #include "ODBCCallableStatement.h"
156 #include "ODBCDatabaseMetaData.h"
157 #include "ODBCResultSet.h"
158 #include <RDBC/TSQLDriverManager.h>
159 #include <RDBC/TSQLUrl.h>
160 #include <RDBC/TSQLDriverInfo.h>
161 #include <TList.h>
162 #include <TNamed.h>
163 #include <RDBC/odbc++/connection.h>
164 #include <RDBC/odbc++/statement.h>
165 #include <RDBC/odbc++/resultset.h>
169 
170 using namespace odbc;
171 using namespace std;
172 
174 
175 
176 //___________________________________________________________________
177 ODBCConnection::ODBCConnection( const TString& connectString ):
178  TSQLConnection(connectString)
179 {
180  // Attempts to establish a connection to the given database
181  // by specified connection string. This string is simply
182  // a series of keyword/value pairs, searated by semicolons,
183  // that contains information used to establish the connection.
184  // The TSQLDriverManager attempts to select an appropriate driver
185  // from the set of registered drivers.
186  //
187  // Parameters:
188  // connectString usually something like:
189  // "dsn=minos;uid=scott;pwd=tiger"
190  //
191  // Throws:
192  // TSQLException - if a database access error occurs
193 
194  odbc::Connection* imp = 0;
195  odbc::DatabaseMetaData* md = 0;
196 
197  try {
199  ODBCXX_STRING_C(connectString) );
200  fImp = imp;
201  if(imp) md = imp->getMetaData();
202  } catch(odbc::SQLException& e) {
206  e.getErrorCode()) );
207  // failed to connect => clean all
208  if(imp) delete imp;
209  fImp = 0;
210  return;
211  }
212 
213  if(!fMetaData) fMetaData = new ODBCDatabaseMetaData(this,md);
214 }
215 
216 //___________________________________________________________________
217 ODBCConnection::ODBCConnection( const TString& dsn,
218  const TString& username,
219  const TString& password ):
220  TSQLConnection(dsn,username,password)
221 {
222  // Attempts to establish a connection to the given Data Source Name (DSN).
223  // The TSQLDriverManager attempts to select an appropriate driver from
224  // the set of registered drivers.
225  //
226  // Parameters:
227  // dsn - DataSourceName string
228  // username - the database user on whose behalf the TSQLConnection
229  // is being made
230  // password - the user's password
231  //
232  // Throws:
233  // TSQLException - if a database access error occurs
234  //
235 
236  odbc::Connection* imp = 0;
237  odbc::DatabaseMetaData* md = 0;
238 
239  try {
241  ODBCXX_STRING_C(dsn),
242  ODBCXX_STRING_C(username),
243  ODBCXX_STRING_C(password) );
244  fImp = imp;
245  if(imp) md = imp->getMetaData();
246  } catch(odbc::SQLException& e) {
250  e.getErrorCode()) );
251  // failed to connect => clean all
252  if(imp) delete imp;
253  fImp = 0;
254  return;
255  }
256  if(!fMetaData) fMetaData = new ODBCDatabaseMetaData(this,md);
257 }
258 
259 //___________________________________________________________________
261 {
262  // Destructor:
263  //
264  // - deallocate all statements produced by this connection
265  // - disconnect this connection
266 
267  if(IsClosed()) return;
268 
270 
271  if(fListOfStatements) { // deallocate all statements
272  fListOfStatements->Delete();
273  delete fListOfStatements;
274  }
275 
276  fListOfStatements = 0;
277 
278  try {
279  if(con) delete con;
280  } catch(odbc::SQLException& e) {
283  e.getErrorCode()) );
284  }
285 
287 
288  fImp = 0;
289  fMetaData = 0;
290 }
291 
292 //___________________________________________________________________
294 {
295  // Creates a TSQLStatement object for sending SQL statements
296  // to the database. SQL statements without parameters are
297  // normally executed using TSQLStatement objects. If the
298  // same SQL statement is executed many times, it is more
299  // efficient to use a TSQLPreparedStatement. TSQLResultSet s
300  // created using the returned TSQLStatement will have
301  // forward-only type, and read-only concurrency, by default.
302  //
303  // Returns:
304  // a new ODBCStatement object
305  // zero - in case of error
306  // Throws:
307  // TSQLException - if a database access error occurs
308 
309  if(IsClosed()) {
310  Throw( new TSQLException( "Connection is closed","",0) );
311  return 0;
312  }
313 
314  ClearWarnings();
315 
316  TSQLStatement* stmt = 0;
318  odbc::Statement* imp = 0;
319 
320  try {
321  imp = con->createStatement();
322  } catch(odbc::SQLException& e) {
325  e.getErrorCode()) );
326  if(imp) delete imp;
327  return 0;
328  }
329  stmt = new ODBCStatement(this,imp);
330  fListOfStatements->Add(stmt);
331  return stmt;
332 }
333 
334 //___________________________________________________________________
336  Int_t resultSetConcurrency )
337 {
338  // Creates a TSQLStatement object that will generate TSQLResultSet
339  // objects with the given type and concurrency. This method is the
340  // same as the CreateStatement() method above, but it allows the
341  // default result set type and result set concurrency type to be
342  // overridden.
343  //
344  // Parameters:
345  // resultSetType - a result set type;
346  // see TSQLResultSet::kTYPE_XXX
347  // resultSetConcurrency - a concurrency type;
348  // see TSQLResultSet::kCONCUR_XXX
349  // Returns:
350  // a new ODBCStatement object
351  // Throws:
352  // TSQLException - if a database access error occurs
353 
354  if(IsClosed()) {
355  Throw( new TSQLException( "Connection is closed","",0) );
356  return 0;
357  }
358  ClearWarnings();
359 
360  TSQLStatement* stmt = 0;
362  odbc::Statement* imp = 0;
363 
364  try {
365  imp = con->createStatement( resultSetType,resultSetConcurrency );
366  } catch(odbc::SQLException& e) {
369  e.getErrorCode()) );
370  if(imp) delete imp;
371  return 0;
372  }
373  stmt = new ODBCStatement(this,imp);
374  fListOfStatements->Add(stmt);
375  return stmt;
376 }
377 
378 //___________________________________________________________________
380 {
381  // Creates a TSQLPreparedStatement object for sending
382  // parameterized SQL statements to the database. A SQL statement
383  // with or without IN parameters can be pre-compiled and stored
384  // in a TSQLPreparedStatement object. This object can then be
385  // used to efficiently execute this statement multiple times.
386  //
387  // Note: This method is optimized for handling parametric SQL
388  // statements that benefit from precompilation.
389  // If the driver supports precompilation, the method
390  // PrepareStatement() will send the statement to the database
391  // for precompilation. Some drivers may not support precompilation.
392  // In this case, the statement may not be sent to the database
393  // until the TSQLPreparedStatement is executed. This has no direct
394  // effect on users; however, it does affect which method throws
395  // certain TSQLException s. Result sets created using the returned
396  // TSQLPreparedStatement will have forward-only type and read-only
397  // concurrency, by default.
398  //
399  // Parameters:
400  // sql - a SQL statement that may contain one or more '?'
401  // IN parameter placeholders
402  // Returns:
403  // a new ODBCPreparedStatement object containing the
404  // pre-compiled statement
405  // Throws:
406  // TSQLException - if a database access error occurs
407 
408  if(IsClosed()) {
409  Throw( new TSQLException( "Connection is closed","",0) );
410  return 0;
411  }
412 
413  ClearWarnings();
414 
415  ODBCPreparedStatement* stmt = 0;
417  odbc::PreparedStatement* imp = 0;
418 
419  try {
420  imp = con->prepareStatement( ODBCXX_STRING_C(sql.Data()) );
421  } catch(odbc::SQLException& e) {
424  e.getErrorCode()) );
425  if(imp) delete imp;
426  return 0;
427  }
428  stmt = new ODBCPreparedStatement(this,imp);
429  fListOfStatements->Add(stmt);
430  return stmt;
431 }
432 
433 //___________________________________________________________________
435 {
436  // Creates a TSQLCallableStatement object for calling database
437  // stored procedures. The TSQLCallableStatement provides methods
438  // for setting up its IN and OUT parameters, and methods for
439  // executing the call to a stored procedure.
440  //
441  // Note: This method is optimized for handling stored procedure
442  // call statements. Some drivers may send the call statement
443  // to the database when the method PrepareCall() is done;
444  // others may wait until the TSQLCallableStatement is
445  // executed. This has no direct effect on users; however,
446  // it does affect which method throws certain SQLExceptions.
447  // Result sets created using the returned
448  // TSQLCallableStatement will have forward-only type and
449  // read-only concurrency, by default.
450  // Parameters:
451  // sql - a SQL statement that may contain one or more '?'
452  // parameter placeholders. Typically this statement is
453  // a function call escape string.
454  // Returns:
455  // a new ODBCCallableStatement object containing the
456  // pre-compiled SQL statement
457  // Throws:
458  // TSQLException - if a database access error occurs
459 
460  if(IsClosed()) {
461  Throw( new TSQLException( "Connection is closed","",0) );
462  return 0;
463  }
464 
465  ClearWarnings();
466 
467  ODBCCallableStatement* stmt = 0;
469  odbc::CallableStatement* imp = 0;
470 
471  try {
472  imp = con->prepareCall( ODBCXX_STRING_C(sql.Data()) );
473  } catch(odbc::SQLException& e) {
476  e.getErrorCode()) );
477  if(imp) delete imp;
478  return 0;
479  }
480  stmt = new ODBCCallableStatement(this,imp);
481  fListOfStatements->Add(stmt);
482  return stmt;
483 }
484 
485 //___________________________________________________________________
487  Int_t resultSetType,
488  Int_t resultSetConcurrency )
489 {
490  // Creates a TSQLPreparedStatement object that will generate
491  // TSQLResultSet objects with the given type and concurrency.
492  // This method is the same as the PrepareStatement() method above,
493  // but it allows the default result set type and result set
494  // concurrency type to be overridden.
495  //
496  // Parameters:
497  // resultSetType - a result set type;
498  // see TSQLResultSet::kTYPE_XXX
499  // resultSetConcurrency - a concurrency type;
500  // see TSQLResultSet::kCONCUR_XXX
501  // Returns:
502  // a new ODBCPreparedStatement object containing the
503  // pre-compiled SQL statement
504  // Throws:
505  // TSQLException - if a database access error occurs
506 
507  if(IsClosed()) {
508  Throw( new TSQLException( "Connection is closed","",0) );
509  return 0;
510  }
511 
512  ClearWarnings();
513 
514  ODBCPreparedStatement* stmt = 0;
516  odbc::PreparedStatement* imp =0;
517 
518  try {
519  imp = con->prepareStatement( ODBCXX_STRING_C(sql.Data()),
520  resultSetType,
521  resultSetConcurrency );
522  } catch(odbc::SQLException& e) {
525  e.getErrorCode()) );
526  if(imp) delete imp;
527  return 0;
528  }
529  stmt = new ODBCPreparedStatement(this,imp);
530  fListOfStatements->Add(stmt);
531  return stmt;
532 }
533 
534 //___________________________________________________________________
536  Int_t resultSetType,
537  Int_t resultSetConcurrency )
538 {
539  // Creates a TSQLCallableStatement object that will generate
540  // TSQLResultSet objects with the given type and concurrency.
541  // This method is the same as the PrepareCall() method above,
542  // but it allows the default result set type and result set
543  // concurrency type to be overridden.
544  //
545  // Parameters:
546  // resultSetType - a result set type;
547  // see TSQLResultSet::kTYPE_XXX
548  // resultSetConcurrency - a concurrency type;
549  // see TSQLResultSet::kCONCUR_XXX
550  //
551  // Returns:
552  // a new ODBCCallableStatement object containing the
553  // pre-compiled SQL statement
554  // Throws:
555  // TSQLException - if a database access error occurs
556 
557  if(IsClosed()) {
558  Throw( new TSQLException( "Connection is closed","",0) );
559  return 0;
560  }
561 
562  ClearWarnings();
563 
564  ODBCCallableStatement* stmt = 0;
566  odbc::CallableStatement* imp = 0;
567 
568  try {
569  imp= con->prepareCall( ODBCXX_STRING_C(sql.Data()),
570  resultSetType,
571  resultSetConcurrency );
572  } catch(odbc::SQLException& e) {
575  e.getErrorCode()) );
576  if(imp) delete imp;
577  return 0;
578  }
579  stmt = new ODBCCallableStatement(this,imp);
580  fListOfStatements->Add(stmt);
581  return stmt;
582 }
583 
584 //___________________________________________________________________
585 TString ODBCConnection::NativeSQL( const TString& sql )
586 {
587  // Converts the given SQL statement into the system's native SQL
588  // grammar. A driver may convert the sql grammar into its system's
589  // native SQL grammar prior to sending it; this method returns
590  // the native form of the statement that the driver would have
591  // sent.
592  //
593  // Parameters:
594  // sql - a SQL statement that may contain one or more '?'
595  // parameter placeholders
596  // Returns:
597  // the native form of this statement
598  // Throws:
599  // TSQLException - if a database access error occurs
600 
601  if(IsClosed()) {
602  Throw( new TSQLException( "Connection is closed","",0) );
603  return "0";
604  }
605 
606  ClearWarnings();
607 
608  TString str;
610 
611  try {
612  ODBCXX_STRING s = con->nativeSQL( ODBCXX_STRING_C(sql.Data()) );
613  str = ODBCXX_STRING_CSTR(s);
614  } catch(odbc::SQLException& e) {
617  e.getErrorCode()) );
618  return "";
619  }
620  return str;
621 }
622 
623 //___________________________________________________________________
624 void ODBCConnection::SetAutoCommit( Bool_t autoCommit )
625 {
626  // Sets this connection's auto-commit mode. If a connection is in
627  // auto-commit mode, then all its SQL statements will be executed
628  // and committed as individual transactions. Otherwise, its SQL
629  // statements are grouped into transactions that are terminated
630  // by a call to either the method commit or the method rollback.
631  // By default, new connections are in auto-commit mode. The commit
632  // occurs when the statement completes or the next execute occurs,
633  // whichever comes first. In the case of statements returning a
634  // TSQLResultSet, the statement completes when the last row
635  // of the TSQLResultSet has been retrieved or the TSQLResultSet
636  // has been closed. In advanced cases, a single statement may
637  // return multiple results as well as output parameter values.
638  // In these cases the commit occurs when all results and output
639  // parameter values have been retrieved.
640  //
641  // Parameters:
642  // autoCommit - kTRUE enables auto-commit;
643  // kFALSE disables auto-commit.
644  // Throws:
645  // TSQLException - if a database access error occurs
646 
647  if(IsClosed()) {
648  Throw( new TSQLException( "Connection is closed","",0) );
649  return;
650  }
652 
653  try {
654  con->setAutoCommit(autoCommit);
655  } catch(odbc::SQLException& e) {
658  e.getErrorCode()) );
659  }
660 }
661 
662 //___________________________________________________________________
664 {
665  // Gets the current auto-commit state.
666  //
667  // Returns:
668  // the current state of auto-commit mode
669  // Throws:
670  // TSQLException - if a database access error occurs
671  // See Also:
672  // SetAutoCommit(Bool_t)
673 
674  if(IsClosed()) {
675  Throw( new TSQLException( "Connection is closed","",0) );
676  return 0;
677  }
678 
679  Bool_t return_value = kFALSE;
681 
682  try {
683  return_value = con->getAutoCommit();
684  } catch(odbc::SQLException& e) {
687  e.getErrorCode()) );
688  return kFALSE;
689  }
690  return return_value;
691 }
692 
693 //___________________________________________________________________
695 {
696  // Makes all changes made since the previous commit/rollback
697  // permanent and releases any database locks currently held by
698  // the TSQLConnection. This method should be used only when
699  // auto-commit mode has been disabled.
700  //
701  // Throws:
702  // TSQLException - if a database access error occurs
703  // See Also:
704  // SetAutoCommit(Bool_t)
705 
706  if(IsClosed()) {
707  Throw( new TSQLException( "Connection is closed","",0) );
708  return;
709  }
710 
712 
713  try {
714  con->commit();
715  } catch(odbc::SQLException& e) {
718  e.getErrorCode()) );
719  }
720 }
721 
722 //___________________________________________________________________
724 {
725  // Drops all changes made since the previous commit/rollback and
726  // releases any database locks currently held by this TSQLConnection.
727  // This method should be used only when auto-commit has been disabled.
728  //
729  // Throws:
730  // TSQLException - if a database access error occurs
731  // See Also:
732  // SetAutoCommit(Bool_t)
733 
734  if(IsClosed()) {
735  Throw( new TSQLException( "Connection is closed","",0) );
736  return;
737  }
738 
740 
741  try {
742  con->rollback();
743  } catch(odbc::SQLException& e) {
746  e.getErrorCode()) );
747  }
748 }
749 
750 //___________________________________________________________________
752 {
753  // Releases a TSQLConnection's database and resources immediately
754  // instead of waiting for them to be automatically released.
755  //
756  // Throws:
757  // TSQLException - if a database access error occurs
758 
760  if(!IsClosed()) return; // connection is in use
761 
763 
764  try {
765  if(con) delete con; // !!!!
766  } catch(odbc::SQLException& e) {
769  e.getErrorCode()) );
770  }
771 
773  fMetaData = 0;
774  fImp = 0;
775 }
776 
777 //___________________________________________________________________
779 {
780  // Gets the metadata regarding this connection's database.
781  // A TSQLConnection's database is able to provide information
782  // describing its tables, its supported SQL grammar, its
783  // stored procedures, the capabilities of this connection,
784  // and so on. This information is made available through a
785  // TSQLDatabaseMetaData object.
786  //
787  // Returns:
788  // a TSQLDatabaseMetaData object for this TSQLConnection
789  // Throws:
790  // TSQLException - if a database access error occurs
791 
792  if(IsClosed()) {
793  Throw( new TSQLException( "Connection is closed","",0) );
794  return 0;
795  }
796 
797  odbc::DatabaseMetaData* md = 0;
799 
800  try {
801  md = con->getMetaData();
802  } catch(odbc::SQLException& e) {
805  e.getErrorCode()) );
806  return 0;
807  }
808  if(fMetaData) ((ODBCDatabaseMetaData*)fMetaData)->Set(this,md);
809  return fMetaData;
810 }
811 
812 //___________________________________________________________________
813 void ODBCConnection::SetReadOnly( Bool_t readOnly )
814 {
815  // Puts this connection in read-only mode as a hint to enable
816  // database optimizations.
817  //
818  // Note: This method cannot be called while in the middle of a
819  // transaction.
820  //
821  // Parameters:
822  // readOnly - kTRUE enables read-only mode;
823  // kFALSE disables read-only mode.
824  // Throws:
825  // TSQLException - if a database access error occurs
826 
827  if(IsClosed()) {
828  Throw( new TSQLException( "Connection is closed","",0) );
829  return;
830  }
832 
833  try {
834  con->setReadOnly(readOnly);
835  } catch(odbc::SQLException& e) {
838  e.getErrorCode()) );
839  }
840 }
841 
842 //___________________________________________________________________
844 {
845  // Tests to see if the connection is in read-only mode.
846  //
847  // Returns:
848  // kTRUE if connection is read-only and kFALSE otherwise
849  // Throws:
850  // TSQLException - if a database access error occurs
851 
852  if(IsClosed()) {
853  Throw( new TSQLException( "Connection is closed","",0) );
854  return kTRUE;
855  }
856 
857  Bool_t return_value = kTRUE;
859 
860  try {
861  return_value = con->isReadOnly();
862  } catch(odbc::SQLException& e) {
865  e.getErrorCode()) );
866  return kTRUE;
867  }
868  return return_value;
869 }
870 
871 //___________________________________________________________________
872 void ODBCConnection::SetCatalog( const TString& catalog )
873 {
874  // Sets a catalog name in order to select a subspace of this
875  // TSQLConnection's database in which to work. If the driver
876  // does not support catalogs, it will silently ignore this
877  // request.
878  //
879  // Throws:
880  // TSQLException - if a database access error occurs
881 
882  if(IsClosed()) {
883  Throw( new TSQLException( "Connection is closed","",0) );
884  return;
885  }
886 
888 
889  try {
890  con->setCatalog( ODBCXX_STRING_C(catalog.Data()) );
891  } catch(odbc::SQLException& e) {
894  e.getErrorCode()) );
895  }
896 }
897 
898 //___________________________________________________________________
900 {
901  // Returns the TSQLConnection's current catalog name.
902  //
903  // Returns:
904  // the current catalog name or null string
905  // Throws:
906  // TSQLException - if a database access error occurs
907 
908  if(IsClosed()) {
909  Throw( new TSQLException( "Connection is closed","",0) );
910  return "0";
911  }
912 
913  TString str;
915 
916  try {
917  str = ODBCXX_STRING_CSTR( con->getCatalog() );
918  } catch(odbc::SQLException& e) {
921  e.getErrorCode()) );
922  return "";
923  }
924  return str;
925 }
926 
927 //___________________________________________________________________
929 {
930  // Attempts to change the transaction isolation level to the one
931  // given. The constants defined in the interface TSQLConnection
932  // are the possible transaction isolation levels.
933  //
934  // Note: This method cannot be called while in the middle of a
935  // transaction.
936  //
937  // Parameters:
938  // level - one of the kTRANSACTION_XXX isolation values with
939  // the exception of kTRANSACTION_NONE;
940  // some databases may not support other values
941  // Throws:
942  // TSQLException - if a database access error occurs
943  //
944  // See Also:
945  // TSQLDatabaseMetaData::SupportsTransactionIsolationLevel(Int_t)
946 
947  if(IsClosed()) {
948  Throw( new TSQLException( "Connection is closed","",0) );
949  return;
950  }
951 
952  try {
953  // con->setTransactionIsolation(level);
954  } catch(odbc::SQLException& e) {
957  e.getErrorCode()) );
958  }
959 }
960 
961 //___________________________________________________________________
963 {
964  // Gets this TSQLConnection's current transaction isolation level.
965  //
966  // Returns:
967  // the current kTRANSACTION_XXX mode value
968  // Throws:
969  // TSQLException - if a database access error occurs
970 
971  if(IsClosed()) {
972  Throw( new TSQLException( "Connection is closed","",0) );
973  return 0;
974  }
975 
976  Int_t return_value = 0;
978 
979  try {
980  return_value = con->getTransactionIsolation();
981 
982  } catch(odbc::SQLException& e) {
985  e.getErrorCode()) );
986  return 0;
987  }
988  return return_value;
989 }
990 
991 //___________________________________________________________________
993 {
994  // Returns kTRUE if tracing is enabled on this connection
995 
996  if(IsClosed()) {
997  Throw( new TSQLException( "Connection is closed","",0) );
998  return kFALSE;
999  }
1000 
1001  Bool_t return_value = kFALSE;
1003 
1004  try {
1005  return_value = con->getTrace();
1006  } catch(odbc::SQLException& e) {
1009  e.getErrorCode()) );
1010  return kFALSE;
1011  }
1012  return return_value;
1013 }
1014 
1015 //___________________________________________________________________
1017 {
1018  // Sets tracing on or off
1019 
1020  if(IsClosed()) {
1021  Throw( new TSQLException( "Connection is closed","",0) );
1022  return;
1023  }
1024 
1026 
1027  try {
1028  con->setTrace(on);
1029  } catch(odbc::SQLException& e) {
1032  e.getErrorCode()) );
1033  }
1034 }
1035 
1036 //___________________________________________________________________
1038 {
1039  // Returns the file tracing is currently written to
1040 
1041  TString str;
1042 
1043  if(IsClosed()) {
1044  Throw( new TSQLException( "Connection is closed","",0) );
1045  return str;
1046  }
1047 
1048  if(!fImp) { Destroyed(); return str; }
1050 
1051  try {
1052  str = ODBCXX_STRING_CSTR( con->getTraceFile() );
1053  } catch(odbc::SQLException& e) {
1056  e.getErrorCode()) );
1057  return "";
1058  }
1059  return str;
1060 }
1061 
1062 //___________________________________________________________________
1063 void ODBCConnection::SetTraceFile( const TString& fn )
1064 {
1065  // Sets the file racing is written to
1066 
1067  if(IsClosed()) {
1068  Throw( new TSQLException( "Connection is closed","",0) );
1069  return;
1070  }
1071 
1073 
1074  try {
1075  con->setTraceFile( ODBCXX_STRING_C(fn.Data()) );
1076  } catch(odbc::SQLException& e) {
1079  e.getErrorCode()) );
1080  }
1081 }
1082 
1083 //___________________________________________________________________
1085 {
1086  // Returns kTRUE if batch are supported
1087  //
1088  // Throws:
1089  // TSQLException - if a database access error occurs
1090 
1091  return kFALSE;
1092 }
1093 
1095 
1096 //___________________________________________________________________
1097 void ODBCConnection::SetLoginTimeout( Int_t seconds )
1098 {
1099  // Sets the maximum time in seconds that a driver will wait while
1100  // attempting to connect to a database. Set to 0 to disable.
1101  //
1102  // Parameters:
1103  // seconds - the login time limit in seconds
1104 
1106 }
1107 
1108 //___________________________________________________________________
1110 {
1111  // Gets the maximum time in seconds that a driver can wait when
1112  // attempting to log in to a database.
1113  //
1114  // Returns:
1115  // the driver login time limit in seconds, or 0 if disabled.
1116 
1118 }
1119 
1120 //___________________________________________________________________
1122 {
1123  // Should be called before an application is to exit
1124 
1125  try {
1127  } catch(odbc::SQLException& e) {
1131  e.getErrorCode()) );
1132  }
1133 }
1134 
1135 //___________________________________________________________________
1137 {
1138  // Fetch a list of all of currently loaded drivers
1139  // to which the current caller has access.
1140 
1141  if(!gDrivers) return 0;
1142 
1143  gDrivers->Delete();
1144 
1145  TSQLDriverInfo* driver;
1146  TNamed* attribute;
1147  TList* attributeList;
1148 
1149  try {
1151 
1152  for( odbc::DriverList::iterator i=list->begin();
1153  i != list->end(); i++) {
1154 
1155  TString description = ODBCXX_STRING_CSTR((*i)->getDescription());
1156  const vector<ODBCXX_STRING>& attrs=(*i)->getAttributes();
1157 
1158  attributeList = new TList();
1159 
1160  for( vector<ODBCXX_STRING>::const_iterator x=attrs.begin();
1161  x!=attrs.end(); x++) {
1162 
1163  attribute = new TNamed( ODBCXX_STRING_CSTR((*x)), "attribute" );
1164  attributeList->Add(attribute);
1165  }
1166  driver = new TSQLDriverInfo(description,attributeList);
1167  gDrivers->Add(driver);
1168  }
1169  } catch(odbc::SQLException& e) {
1173  e.getErrorCode()) );
1174  }
1175  return gDrivers;
1176 }
1177 
1178 //___________________________________________________________________
1179 TList* ODBCConnection::RefreshDataSources(TList* gDataSources)
1180 {
1181  // Fetch a list of of all available data sources ( TSQLUrl objects)
1182 
1183  if(!gDataSources) return 0;
1184 
1185  gDataSources->Delete(); // remove all
1186  TSQLUrl* url;
1187 
1188  try {
1189  odbc::DataSourceList* list =
1191 
1192  for( odbc::DataSourceList::iterator i=list->begin();
1193  i != list->end(); i++) {
1194 
1195  odbc::DataSource* ds = (*i);
1196  url = new TSQLUrl(ODBCXX_STRING_CSTR(ds->getName()),
1198  gDataSources->Add(url);
1199  }
1200  } catch(odbc::SQLException& e) {
1204  e.getErrorCode()) );
1205  }
1206  return gDataSources;
1207 }