00001 00002 #ifndef DBICONNECTION 00003 #define DBICONNECTION 00004 00005 00006 ////////////////////////////////////////////////////////////////////////// 00007 //////////////////////////// ROOT API //////////////////////////// 00008 ////////////////////////////////////////////////////////////////////////// 00009 00010 /** 00011 * 00012 * 00013 * \class TDbiConnection 00014 * 00015 * 00016 * \brief 00017 * <b>Concept</b> A managed TSQLServer connection- dropped when idle. 00018 * 00019 * \brief 00020 * <b>Purpose</b> To minimise connections. 00021 * 00022 * Contact: A.Finch@lancaster.ac.uk 00023 * 00024 * 00025 */ 00026 00027 00028 #include <string> 00029 00030 #ifndef ROOT_Rtypes 00031 #if !defined(__CINT__) || defined(__MAKECINT__) 00032 #include "Rtypes.h" 00033 #endif 00034 #endif 00035 #include "TSQLServer.h" 00036 #include "TSQLStatement.h" 00037 #include "TUrl.h" 00038 00039 #include "TDbi.hxx" 00040 #include "TDbiExceptionLog.hxx" 00041 00042 class TDbiConnection 00043 { 00044 00045 public: 00046 00047 // Constructors and destructors. 00048 /// Passed in 00049 /// url - address of sql server 00050 /// user - username to use 00051 /// password - password to use 00052 /// macConnects = maximum number of connections to attempt before giving up, default to 20 00053 /// throws EBadConnection() if can not make connection 00054 /// 00055 TDbiConnection(const std::string& url = "", 00056 const std::string& user = "", 00057 const std::string& password = "", 00058 int maxConnects=20); 00059 virtual ~TDbiConnection(); 00060 00061 // State testing member functions 00062 00063 // Standard getters. 00064 00065 const std::string& GetDbName() const { return fDbName; } 00066 const std::string& GetPassword() const { return fPassword; } 00067 const std::string& GetUrl() const; 00068 const std::string& GetUser() const { return fUser; } 00069 Bool_t IsClosed() const { return ! fServer; } 00070 Bool_t IsTemporary() const { return fIsTemporary; } 00071 Bool_t TableExists(const std::string& tableName) const; 00072 00073 // Exception log handling 00074 00075 const TDbiExceptionLog& GetExceptionLog() const { return fExceptionLog; } 00076 void ClearExceptionLog() { fExceptionLog.Clear(); } 00077 00078 /// Print exceptions at level of above and return true if any 00079 Bool_t PrintExceptionLog(Int_t level = 3) const; 00080 00081 void RecordException(); 00082 00083 // State changing member functions 00084 00085 /// Add name to list of existing tables (necessary when creating tables) 00086 /// Default name = "", reread all tables from database. 00087 void SetTableExists(const std::string& tableName = ""); 00088 00089 // Idle connnection management 00090 00091 /// Methods used when "borrowing" the server (use same as for Statement). 00092 void Connect() { this->ConnectStatement(); } 00093 void DisConnect() { this->DisConnectStatement(); } 00094 00095 /// Increment number of statements relying on this connection 00096 void ConnectStatement() { ++fNumConnectedStatements; } 00097 /// Decrement number of statements relying on this connection and close if idle 00098 void DisConnectStatement() { 00099 --fNumConnectedStatements; 00100 if ( ! fNumConnectedStatements ) this->CloseIdleConnection(); } 00101 /// Connection is permanent, don't close even when idle. 00102 void SetPermanent(Bool_t permanent = true) { fIsTemporary = ! permanent; } 00103 00104 00105 Bool_t Close(Bool_t force = false); 00106 Bool_t Open(); 00107 00108 /// Get server, opening if necessary 00109 /// TDbiConnection retains ownership 00110 TSQLServer* GetServer(); 00111 00112 /// Get statement, opening if necessary. 00113 /// Caller must take ownership. 00114 TSQLStatement* CreatePreparedStatement(const std::string& sql); 00115 00116 private: 00117 00118 void CloseIdleConnection(); 00119 00120 00121 // Data members 00122 00123 /// Database Name. 00124 std::string fDbName; 00125 00126 /// A comma separate list of existing tables each in single quotes : 'table1','table2',... 00127 std::string fExistingTableList; 00128 00129 /// TSQLServer URL 00130 TUrl fUrl; 00131 00132 /// Username 00133 std::string fUser; 00134 00135 /// Password 00136 std::string fPassword; 00137 00138 /// True if URL works 00139 Bool_t fUrlValidated; 00140 00141 /// Maximum number of times to try making a connection. 00142 int fMaxConnectionAttempts; 00143 00144 /// Number of connected statements 00145 Int_t fNumConnectedStatements; 00146 00147 /// Connection closes after each I/O (no connections left) 00148 Bool_t fIsTemporary; 00149 00150 /// TSQLServer or 0 if closed 00151 TSQLServer* fServer; 00152 00153 /// Log of exceptions generated. 00154 /// Cleared by Open Close and (implicitly) by CreatePreparedStatement, GetServer 00155 TDbiExceptionLog fExceptionLog; 00156 00157 ClassDef(TDbiConnection,0) // Managed TSQLServer 00158 00159 }; 00160 00161 00162 #endif // DBICONNECTION 00163 00164