//*-- AUTHOR : Ilse Koenig //*-- Created : 26/11/2004 by I.Koenig ///////////////////////////////////////////////////////////// // FairOraConn // // Connection class to database Oracle // (uses the Oracle C/C++ precompiler) // ///////////////////////////////////////////////////////////// using namespace std; #include "FairOraConn.h" #include "TRandom.h" #include #include #include #include #include #include #include #include #include #include #include // Oracle communication area #include // SQL Communications Area #include ClassImp(FairOraConn) FairOraConn::FairOraConn() { // default constructor // defines default values for user ("FAIR_ANA_PUBLIC") and the database // ("db-cbm.oracle.gsi.de", the FAIR Oracle database on Linux at GSI) // User FAIR_ANA_PUBLIC has Readonly access to the Oracle tables. // The connection to Oracle is not opened! strcpy(dbName,"db-hades-test"); strcpy(userName,"fairana"); isConnected=kFALSE; actRunId=-1; runStart=-1; historyDate[0]='\0'; needsServerCheck=kTRUE; } FairOraConn::~FairOraConn() { // default destructor (closes connection) close(); } Bool_t FairOraConn::open() { // opens default connection with readonly access char* password = new char[9]; strcpy(password,"fair"); Bool_t rc=openConnection(password); if (!rc) close(); return rc; } Bool_t FairOraConn::open(char *uName) { // opens connection to database db-cbm for user given by name // asks for password strncpy(userName,uName,30); char* password=getPassword(); Bool_t rc=openConnection(password); if (!rc) close(); return rc; } Bool_t FairOraConn::open(char *dbN, char *uN) { // opens connection to database with name dbName for user given by name // asks for password strncpy(dbName,dbN,30); strncpy(userName,uN,30); char* password=getPassword(); Bool_t rc=openConnection(password); if (!rc) close(); return rc; } Bool_t FairOraConn::reconnect() { // opens connection (contains the SQL-statements) if (isConnected) return kTRUE; if (strcmp(userName,"fairana")!=0) return kFALSE; char connId[80]; char password[]={"fair"}; strcpy(connId,userName); strcat(connId,"@"); strcat(connId,dbName); EXEC SQL BEGIN DECLARE SECTION; char* uName; char* pWord; EXEC SQL END DECLARE SECTION; uName=connId; pWord=password; EXEC SQL WHENEVER SQLERROR DO showSqlError("openConnection(char*)","Wrong user/password"); EXEC SQL CONNECT :uName IDENTIFIED BY :pWord; if (sqlca.sqlcode==0) { isConnected=kTRUE; setHistoryDate(historyDate); } else Error("reconnect()","*** Failed to connect to Oracle ***"); return isConnected; } void FairOraConn::close() { // disconnects from ORACLE // A transaction will be automatically rolled back, // that means changes in the database are not stored // without an explicit COMMIT actRunId=-1; runStart=-1; historyDate[0]='\0'; EXEC SQL WHENEVER SQLERROR DO showSqlError("close()"); if (isConnected) { EXEC SQL ROLLBACK RELEASE; isConnected=kFALSE; cout<<"connection to Oracle closed"< "; scanf("%[^\n]%*c",passwd); return passwd; } itio = tio; tio.c_lflag &= ~(ECHO|ICANON); tcsetattr (ttyfd, TCSAFLUSH, &tio); cout<<"enter password for user "< "; fgets(buf, 20, stdin); tcsetattr (ttyfd, TCSAFLUSH, &itio); //close(ttyfd); sscanf(buf, "%s", passwd); cout<Integer(20000); nTotWait+=nWait; cout<<"Oracle server busy, retry in "<0) needsServerCheck=kFALSE; else return -1; } if (strlen(historyDate)==0) setParamRelease(id); actRunId=id; runStart=-1; EXEC SQL BEGIN DECLARE SECTION; int ri; int rs; EXEC SQL END DECLARE SECTION; EXEC SQL WHENEVER SQLERROR GOTO notfound; ri = actRunId; EXEC SQL EXECUTE BEGIN hades_oper.run_query.get_run_start(:ri,:rs); END; END-EXEC; if (ri==(int)actRunId) { runStart=rs; /* cout<<"actRunId: "<