//*-- AUTHOR : Ilse Koenig //*-- Created : 25/05/2010 by Ilse Koenig //_HADES_CLASS_DESCRIPTION ///////////////////////////////////////////////////////////// // HOra2Conn // // Connection class to database Oracle in Hydra2 // (uses the Oracle C/C++ precompiler) // ///////////////////////////////////////////////////////////// using namespace std; #include "hora2conn.h" #include "TRandom.h" #include #include #include #include #include #include #include #include #include #include #include // Oracle communication area #include // SQL Communications Area #include // SQL prototype routines #include ClassImp(HOra2Conn) HOra2Conn::HOra2Conn() { // default constructor // defines default values for user ("hades_ana") and the database // ("db-hades", the HADES Oracle 8 database on Linux at GSI) // User "hades" has Readonly access to the Oracle tables. // The connection to Oracle is not opened! strcpy(dbName,"db-hades"); strcpy(userName,"hades_ana"); isConnected=kFALSE; actRunId=-1; runStart=-1; expLocation[0]='\0'; historyDate[0]='\0'; needsServerCheck=kTRUE; } HOra2Conn::~HOra2Conn() { // default destructor (closes connection) close(); } Bool_t HOra2Conn::open() { // opens default connection with readonly access Char_t* password = new Char_t[9]; strcpy(password,"hades"); Bool_t rc=openConnection(password); if (!rc) close(); return rc; } Bool_t HOra2Conn::open(Char_t *uName) { // opens connection to database Hades for user given by name // asks for password strncpy(userName,uName,20); Char_t* password=getPassword(); Bool_t rc=openConnection(password); if (!rc) close(); return rc; } Bool_t HOra2Conn::open(Char_t *dbN, Char_t *uN) { // opens connection to database with name dbName for user given by name // asks for password strncpy(dbName,dbN,30); strncpy(userName,uN,20); Char_t* password=getPassword(); Bool_t rc=openConnection(password); if (!rc) close(); return rc; } Bool_t HOra2Conn::reconnect() { // opens connection (contains the SQL-statements) if (isConnected) return kTRUE; if (strcmp(userName,"hades_ana")!=0) return kFALSE; Char_t connId[80]; Char_t password[]={"hades"}; strcpy(connId,userName); strcat(connId,"@"); strcat(connId,dbName); // e.g."hades_ana@db-hades" 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_t*)","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 HOra2Conn::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; expLocation[0]='\0'; 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; alignmentOutVers=-1; runStart=-1; EXEC SQL BEGIN DECLARE SECTION; int ri; int rs; varchar exp_loc[11]; EXEC SQL END DECLARE SECTION; EXEC SQL WHENEVER SQLERROR GOTO notfound; ri = actRunId; exp_loc.len=11; // run start converted to number of seconds since 01-JAN-1970 local time EXEC SQL EXECUTE BEGIN hades_oper.run_query_2.get_run_start(:ri,:rs,:exp_loc); END; END-EXEC; if (ri==(Int_t)actRunId) { runStart=rs; exp_loc.arr[exp_loc.len]='\0'; if (strlen(expLocation)==0) strcpy(expLocation,(Char_t*)exp_loc.arr); else { if (strcmp(expLocation,(Char_t*)exp_loc.arr)!=0) { Error("getRunStart(Int_t)", "\nA switch from beamtime runs to simulations runs and vice versa is not " "possible\nwithout the close and reopen of the Oracle connection!\n\n"); runStart=-1; } } /* cout<<"actRunId: "<