TOracleStatement.cxx

Go to the documentation of this file.
00001 // @(#)root/oracle:$Id: TOracleStatement.cxx 35983 2010-10-01 10:12:54Z pcanal $
00002 // Author: Sergey Linev   6/02/2006
00003 
00004 
00005 /*************************************************************************
00006  * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers.               *
00007  * All rights reserved.                                                  *
00008  *                                                                       *
00009  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00010  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00011  *************************************************************************/
00012 
00013 //////////////////////////////////////////////////////////////////////////
00014 //                                                                      //
00015 //  SQL statement class for Oracle                                      //
00016 //                                                                      //
00017 //  See TSQLStatement class documentation for more details.             //
00018 //                                                                      //
00019 //////////////////////////////////////////////////////////////////////////
00020 
00021 #include "TOracleStatement.h"
00022 #include "TOracleServer.h"
00023 #include "TDataType.h"
00024 #include <stdlib.h>
00025 
00026 ClassImp(TOracleStatement)
00027 
00028 //______________________________________________________________________________
00029 TOracleStatement::TOracleStatement(Environment* env, Connection* conn, Statement* stmt, Int_t niter, Bool_t errout) :
00030    TSQLStatement(errout),
00031    fEnv(env),
00032    fConn(conn),
00033    fStmt(stmt),
00034    fResult(0),
00035    fFieldInfo(0),
00036    fBuffer(0),
00037    fBufferSize(0),
00038    fNumIterations(niter),
00039    fIterCounter(0),
00040    fWorkingMode(0),
00041    fTimeFmt(TOracleServer::GetDatimeFormat())
00042 {
00043    // Normal constructor of TOracleStatement class
00044    // On creation time specifies buffer length, which should be
00045    // used in data fetching or data inserting
00046 
00047    if (fStmt) {
00048       fStmt->setPrefetchMemorySize(1000000);
00049       fStmt->setPrefetchRowCount(niter);
00050       fStmt->setMaxIterations(niter);
00051    }
00052 }
00053 
00054 //______________________________________________________________________________
00055 TOracleStatement::~TOracleStatement()
00056 {
00057    // Destructor of TOracleStatement clas
00058 
00059    Close();
00060 }
00061 
00062 //______________________________________________________________________________
00063 void TOracleStatement::Close(Option_t *)
00064 {
00065    // Close Oracle statement
00066    // Removes and destroys all buffers and metainfo
00067 
00068 
00069    if (fFieldInfo)
00070       delete fFieldInfo;
00071 
00072    if (fResult && fStmt)
00073       fStmt->closeResultSet(fResult);
00074 
00075    if (fConn && fStmt)
00076       fConn->terminateStatement(fStmt);
00077       
00078    CloseBuffer();
00079 
00080    fConn = 0;
00081    fStmt = 0;
00082    fResult = 0;
00083    fFieldInfo = 0;
00084    fIterCounter = 0;
00085 }
00086 
00087 // Check that statement is ready for use
00088 #define CheckStatement(method, res)                     \
00089    {                                                    \
00090       ClearError();                                     \
00091       if (fStmt==0) {                                   \
00092          SetError(-1,"Statement is not correctly initialized",method); \
00093          return res;                                    \
00094       }                                                 \
00095    }
00096 
00097 // Check that parameter can be set for statement
00098 #define CheckSetPar(method)                             \
00099    {                                                    \
00100       CheckStatement(method, kFALSE);                   \
00101       if (!IsParSettMode()) {                           \
00102          SetError(-1,"Parameters cannot be set for this statement", method); \
00103          return kFALSE;                                 \
00104       }                                                 \
00105       if (npar<0) {                                     \
00106          TString errmsg("Invalid parameter number ");   \
00107          errmsg+= npar;                                 \
00108          SetError(-1,errmsg.Data(),method);             \
00109          return kFALSE;                                 \
00110       }                                                 \
00111    }
00112 
00113 #define CheckGetField(method, defres)                   \
00114    {                                                    \
00115       ClearError();                                     \
00116       if (!IsResultSet()) {                             \
00117          SetError(-1,"There is no result set for statement", method); \
00118          return defres;                                 \
00119       }                                                 \
00120       if ((npar<0) || (npar>=fBufferSize)) {                                     \
00121          TString errmsg("Invalid parameter number ");   \
00122          errmsg+= npar;                                 \
00123          SetError(-1,errmsg.Data(),method);             \
00124          return defres;                                 \
00125       }                                                 \
00126    }
00127 
00128 //______________________________________________________________________________
00129 void TOracleStatement::SetBufferSize(Int_t size)
00130 {
00131     // Set buffer size, which is used to keep string values of
00132     // currently fetched column.
00133 
00134     CloseBuffer();
00135     if (size<=0) return;
00136     fBufferSize = size;
00137     fBuffer = new TBufferRec[size];
00138     for (Int_t n=0;n<fBufferSize;n++) {
00139        fBuffer[n].strbuf = 0;
00140        fBuffer[n].strbufsize = -1;
00141        fBuffer[n].namebuf = 0;
00142     }
00143 }
00144 
00145 //______________________________________________________________________________
00146 void TOracleStatement::CloseBuffer()
00147 {
00148    // Destroy buffers, used in data fetching
00149 
00150    if (fBuffer) {
00151       for (Int_t n=0;n<fBufferSize;n++) {
00152          delete[] fBuffer[n].strbuf;
00153          delete[] fBuffer[n].namebuf;
00154       }
00155 
00156       delete[] fBuffer;
00157    }
00158    fBuffer = 0;
00159    fBufferSize = 0;
00160 }
00161 
00162 //______________________________________________________________________________
00163 Bool_t TOracleStatement::Process()
00164 {
00165    // Process SQL statement
00166    
00167    CheckStatement("Process", kFALSE);
00168 
00169    try {
00170 
00171       if (IsParSettMode()) {
00172          fStmt->executeUpdate();
00173          fWorkingMode = 0;
00174       } else {
00175          fStmt->execute();
00176       }
00177 
00178       return kTRUE;
00179    } catch (SQLException &oraex)  {
00180       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "Process");
00181    }
00182 
00183    return kFALSE;
00184 }
00185 
00186 //______________________________________________________________________________
00187 Int_t TOracleStatement::GetNumAffectedRows()
00188 {
00189    // Return number of affected rows after statement Process() was called
00190    // Make sense for queries like SELECT, INSERT, UPDATE
00191     
00192    CheckStatement("GetNumAffectedRows", -1);
00193 
00194    try {
00195       return fStmt->getUpdateCount();
00196    } catch (SQLException &oraex)  {
00197       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetNumAffectedRows");
00198    }
00199    return -1;
00200 }
00201 
00202 
00203 //______________________________________________________________________________
00204 Int_t TOracleStatement::GetNumParameters()
00205 {
00206    // Return number of parameters in statement
00207    // Not yet implemented for Oracle 
00208     
00209    CheckStatement("GetNumParameters", -1);
00210 
00211    Info("GetParametersNumber","Not implemented");
00212 
00213    return 0;
00214 }
00215 
00216 //______________________________________________________________________________
00217 Bool_t TOracleStatement::SetNull(Int_t npar)
00218 {
00219    // Set NULL as value of parameter npar
00220    
00221    CheckSetPar("SetNull");
00222 
00223    try {
00224       fStmt->setNull(npar+1, OCCIINT);
00225 
00226       return kTRUE;
00227    } catch (SQLException &oraex)  {
00228       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetNull");
00229    }
00230 
00231    return kFALSE;
00232 }
00233 
00234 
00235 //______________________________________________________________________________
00236 Bool_t TOracleStatement::SetInt(Int_t npar, Int_t value)
00237 {
00238    // Set integer value for parameter npar
00239     
00240    CheckSetPar("SetInt");
00241 
00242    try {
00243       fStmt->setInt(npar+1, value);
00244 
00245       return kTRUE;
00246    } catch (SQLException &oraex)  {
00247       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetInt");
00248    }
00249 
00250    return kFALSE;
00251 }
00252 
00253 //______________________________________________________________________________
00254 Bool_t TOracleStatement::SetUInt(Int_t npar, UInt_t value)
00255 {
00256    // Set unsigned integer value for parameter npar
00257 
00258    CheckSetPar("SetUInt");
00259 
00260    try {
00261       fStmt->setUInt(npar+1, value);
00262       return kTRUE;
00263    } catch (SQLException &oraex)  {
00264       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetUInt");
00265    }
00266 
00267    return kFALSE;
00268 }
00269 
00270 //______________________________________________________________________________
00271 Bool_t TOracleStatement::SetLong(Int_t npar, Long_t value)
00272 {
00273    // Set long integer value for parameter npar
00274 
00275    CheckSetPar("SetLong");
00276 
00277    try {
00278       fStmt->setNumber(npar+1, Number(value));
00279       return kTRUE;
00280    } catch (SQLException &oraex)  {
00281       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetLong");
00282    }
00283    return kFALSE;
00284 }
00285 
00286 //______________________________________________________________________________
00287 Bool_t TOracleStatement::SetLong64(Int_t npar, Long64_t value)
00288 {
00289    // Set 64-bit integer value for parameter npar
00290 
00291    CheckSetPar("SetLong64");
00292    
00293    try {
00294       fStmt->setNumber(npar+1, Number((long double)value));
00295       return kTRUE;
00296    } catch (SQLException &oraex)  {
00297       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetLong64");
00298    }
00299    return kFALSE;
00300 }
00301 
00302 //______________________________________________________________________________
00303 Bool_t TOracleStatement::SetULong64(Int_t npar, ULong64_t value)
00304 {
00305    // Set unsigned 64-bit integer value for parameter npar
00306 
00307    CheckSetPar("SetULong64");
00308 
00309    try {
00310       fStmt->setNumber(npar+1, Number((long double)value));
00311       return kTRUE;
00312    } catch (SQLException &oraex)  {
00313       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetULong64");
00314    }
00315    return kFALSE;
00316 }
00317 
00318 //______________________________________________________________________________
00319 Bool_t TOracleStatement::SetDouble(Int_t npar, Double_t value)
00320 {
00321    // Set double value for parameter npar
00322 
00323    CheckSetPar("SetDouble");
00324    
00325    try {
00326       fStmt->setDouble(npar+1, value);
00327       return kTRUE;
00328    } catch (SQLException &oraex)  {
00329       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetDouble");
00330    }
00331    return kFALSE;
00332 }
00333 
00334 //______________________________________________________________________________
00335 Bool_t TOracleStatement::SetString(Int_t npar, const char* value, Int_t maxsize)
00336 {
00337    // Set string value for parameter npar
00338 
00339    CheckSetPar("SetString");
00340 
00341    try {
00342 
00343    // this is when NextIteration is called first time
00344       if (fIterCounter==1) {
00345          fStmt->setDatabaseNCHARParam(npar+1, true);
00346          fStmt->setMaxParamSize(npar+1, maxsize);
00347       }
00348 
00349       fStmt->setString(npar+1, value);
00350       return kTRUE;
00351    } catch (SQLException &oraex)  {
00352       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetString");
00353    }
00354    return kFALSE;
00355 }
00356 
00357 //______________________________________________________________________________
00358 Bool_t TOracleStatement::SetBinary(Int_t npar, void* mem, Long_t size, Long_t maxsize)
00359 {
00360    // set parameter value as binary data
00361    
00362    CheckSetPar("SetBinary");
00363 
00364    try {
00365 
00366       // this is when NextIteration is called first time
00367       if (fIterCounter==1) 
00368          fStmt->setMaxParamSize(npar+1, maxsize);
00369          
00370       Bytes buf((unsigned char*) mem, size);
00371 
00372       fStmt->setBytes(npar+1, buf);
00373       
00374       return kTRUE;
00375 
00376    } catch (SQLException &oraex)  {
00377       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetBinary");
00378    }
00379    return kFALSE;
00380 }   
00381 
00382 //______________________________________________________________________________
00383 Bool_t TOracleStatement::SetDate(Int_t npar, Int_t year, Int_t month, Int_t day)
00384 {
00385    // Set date value for parameter npar
00386 
00387    CheckSetPar("SetDate");
00388 
00389    try {
00390       Date tm = fStmt->getDate(npar+1);
00391       int o_year;
00392       unsigned int o_month, o_day, o_hour, o_minute, o_second; 
00393       tm.getDate(o_year, o_month, o_day, o_hour, o_minute, o_second);
00394       tm.setDate(year, month, day, o_hour, o_minute, o_second);
00395       fStmt->setDate(npar+1, tm);
00396       return kTRUE;
00397    } catch (SQLException &oraex)  {
00398       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetDate");
00399    }
00400 
00401    return kFALSE;
00402 }
00403 
00404 //______________________________________________________________________________
00405 Bool_t TOracleStatement::SetTime(Int_t npar, Int_t hour, Int_t min, Int_t sec)
00406 {
00407    // Set time value for parameter npar
00408 
00409    CheckSetPar("SetTime");
00410    
00411    try {
00412       Date tm = fStmt->getDate(npar+1);
00413       int o_year;
00414       unsigned int o_month, o_day, o_hour, o_minute, o_second; 
00415       tm.getDate(o_year, o_month, o_day, o_hour, o_minute, o_second);
00416       tm.setDate(o_year, o_month, o_day, hour, min, sec);
00417       fStmt->setDate(npar+1, tm);
00418       return kTRUE;
00419    } catch (SQLException &oraex)  {
00420       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetTime");
00421    }
00422 
00423    return kFALSE;
00424 }
00425 
00426 //______________________________________________________________________________
00427 Bool_t TOracleStatement::SetDatime(Int_t npar, Int_t year, Int_t month, Int_t day, Int_t hour, Int_t min, Int_t sec)
00428 {
00429    // Set date & time value for parameter npar
00430 
00431    CheckSetPar("SetDatime");
00432 
00433    try {
00434       Date tm(fEnv, year, month, day, hour, min, sec);
00435       fStmt->setDate(npar+1, tm);
00436       return kTRUE;
00437    } catch (SQLException &oraex)  {
00438       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetDatime");
00439    }
00440 
00441    return kFALSE;
00442 }
00443 
00444 //______________________________________________________________________________
00445 Bool_t TOracleStatement::SetTimestamp(Int_t npar, Int_t year, Int_t month, Int_t day, Int_t hour, Int_t min, Int_t sec, Int_t frac)
00446 {
00447    // Set date & time value for parameter npar
00448 
00449    CheckSetPar("SetTimestamp");
00450 
00451    try {
00452       Timestamp tm(fEnv, year, month, day, hour, min, sec, frac);
00453       fStmt->setTimestamp(npar+1, tm);
00454       return kTRUE;
00455    } catch (SQLException &oraex)  {
00456       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetTimestamp");
00457    }
00458 
00459    return kFALSE;
00460 }
00461 
00462 //______________________________________________________________________________
00463 Bool_t TOracleStatement::SetVInt(Int_t npar, const std::vector<Int_t> value, const char* schemaName, const char* typeName)
00464 {
00465    // Set vector of integer values for parameter npar
00466     
00467    CheckSetPar("SetVInt");
00468 
00469    try {
00470       setVector(fStmt, npar+1, value, schemaName, typeName);
00471       return kTRUE;
00472    } catch (SQLException &oraex)  {
00473       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetVInt");
00474    }
00475 
00476    return kFALSE;
00477 }
00478 
00479 //______________________________________________________________________________
00480 Bool_t TOracleStatement::SetVUInt(Int_t npar, const std::vector<UInt_t> value, const char* schemaName, const char* typeName)
00481 {
00482    // Set vector of unsigned integer values for parameter npar
00483 
00484    CheckSetPar("SetVUInt");
00485 
00486    try {
00487       setVector(fStmt, npar+1, value, schemaName, typeName);
00488       return kTRUE;
00489    } catch (SQLException &oraex)  {
00490       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetVUInt");
00491    }
00492 
00493    return kFALSE;
00494 }
00495 
00496 //______________________________________________________________________________
00497 Bool_t TOracleStatement::SetVLong(Int_t npar, const std::vector<Long_t> value, const char* schemaName, const char* typeName)
00498 {
00499    // Set vector of long integer values for parameter npar
00500 
00501    CheckSetPar("SetVLong");
00502 
00503    try {
00504       std::vector<Number> nvec;
00505       for (std::vector<Long_t>::const_iterator it = value.begin();
00506            it != value.end();
00507            it++) {
00508          nvec.push_back(Number(*it));
00509       }
00510       setVector(fStmt, npar+1, nvec, schemaName, typeName);
00511       return kTRUE;
00512    } catch (SQLException &oraex)  {
00513       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetVLong");
00514    }
00515    return kFALSE;
00516 }
00517 
00518 //______________________________________________________________________________
00519 Bool_t TOracleStatement::SetVLong64(Int_t npar, const std::vector<Long64_t> value, const char* schemaName, const char* typeName)
00520 {
00521    // Set vector of 64-bit integer values for parameter npar
00522 
00523    CheckSetPar("SetVLong64");
00524    
00525    try {
00526       std::vector<Number> nvec;
00527       for (std::vector<Long64_t>::const_iterator it = value.begin();
00528            it != value.end();
00529            it++) {
00530         nvec.push_back(Number((long double)*it));
00531       }
00532       setVector(fStmt, npar+1, nvec, schemaName, typeName);
00533       return kTRUE;
00534    } catch (SQLException &oraex)  {
00535       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetVLong64");
00536    }
00537    return kFALSE;
00538 }
00539 
00540 //______________________________________________________________________________
00541 Bool_t TOracleStatement::SetVULong64(Int_t npar, std::vector<ULong64_t> value, const char* schemaName, const char* typeName)
00542 {
00543    // Set vector of unsigned 64-bit integer values for parameter npar
00544 
00545    CheckSetPar("SetVULong64");
00546 
00547    try {
00548       std::vector<Number> nvec;
00549       for (std::vector<ULong64_t>::const_iterator it = value.begin();
00550            it != value.end();
00551            it++) {
00552         nvec.push_back(Number((long double)*it));
00553       }
00554       setVector(fStmt, npar+1, nvec, schemaName, typeName);
00555       return kTRUE;
00556    } catch (SQLException &oraex)  {
00557       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetVULong64");
00558    }
00559    return kFALSE;
00560 }
00561 
00562 //______________________________________________________________________________
00563 Bool_t TOracleStatement::SetVDouble(Int_t npar, const std::vector<Double_t> value, const char* schemaName, const char* typeName)
00564 {
00565    // Set vector of double values for parameter npar
00566 
00567    CheckSetPar("SetVDouble");
00568    
00569    try {
00570       setVector(fStmt, npar+1, value, schemaName, typeName);
00571       return kTRUE;
00572    } catch (SQLException &oraex)  {
00573       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetVDouble");
00574    }
00575    return kFALSE;
00576 }
00577 
00578 //______________________________________________________________________________
00579 Bool_t TOracleStatement::NextIteration()
00580 {
00581    // Add next iteration for statement with parameters
00582 
00583    CheckStatement("NextIteration", kFALSE);
00584 
00585    try {
00586       fWorkingMode=1;
00587       // if number of iterations achievs limit, execute it and continue to fill
00588       if ((fIterCounter % fNumIterations == 0) && (fIterCounter>0)) {
00589          fStmt->executeUpdate();
00590       }
00591 
00592       if (fIterCounter % fNumIterations != 0) {
00593          fStmt->addIteration();
00594       }
00595 
00596       fIterCounter++;
00597 
00598       return kTRUE;
00599    } catch (SQLException &oraex)  {
00600       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "NextIteration");
00601    }
00602    return kFALSE;
00603 }
00604 
00605 //______________________________________________________________________________
00606 Bool_t TOracleStatement::StoreResult()
00607 {
00608    // Store result of statement processing.
00609    // Required to access results of SELECT queries 
00610 
00611    CheckStatement("StoreResult", kFALSE);
00612 
00613    try {
00614       if (fStmt->status() == Statement::RESULT_SET_AVAILABLE) {
00615          fResult      = fStmt->getResultSet();
00616          fFieldInfo   = (fResult==0) ? 0 : new std::vector<MetaData>(fResult->getColumnListMetaData());
00617          Int_t count  = (fFieldInfo==0) ? 0 : fFieldInfo->size();
00618          SetBufferSize(count);
00619          if ((fResult!=0) && (count>0)) fWorkingMode = 2;
00620 
00621          return IsResultSet();
00622       }
00623    } catch (SQLException &oraex) {
00624       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "StoreResult");
00625    }
00626    return kFALSE;
00627 }
00628 
00629 //______________________________________________________________________________
00630 Bool_t TOracleStatement::SetMaxFieldSize(Int_t nfield, Long_t maxsize)
00631 {
00632    // Defines maximum size for field which must be used for read or write operation
00633    // Some Oracle types as LONG (long binary continer) requires this call
00634    // before any data can be read from database. Call it once before first call to NextResultRow()
00635    
00636    CheckStatement("SetMaxFieldSize", kFALSE);
00637 
00638    try {
00639       if (fResult)
00640          fResult->setMaxColumnSize(nfield+1, maxsize);
00641       else
00642          fStmt->setMaxParamSize(nfield+1, maxsize);
00643       return kTRUE;
00644    } catch (SQLException &oraex) {
00645       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetMaxFieldSize");
00646    }
00647 
00648    return kFALSE;
00649 }
00650 
00651 //______________________________________________________________________________
00652 Int_t TOracleStatement::GetNumFields()
00653 {
00654    // Returns number of fields in result set 
00655     
00656    return IsResultSet() ?  fBufferSize : -1;
00657 }
00658 
00659 //______________________________________________________________________________
00660 const char* TOracleStatement::GetFieldName(Int_t npar)
00661 {
00662    // Return field name in result set 
00663     
00664    CheckGetField("GetFieldName", 0);
00665 
00666    if (!IsResultSet() || (npar<0) || (npar>=fBufferSize)) return 0;
00667 
00668    if (fBuffer[npar].namebuf!=0) return fBuffer[npar].namebuf;
00669 
00670    std::string buff = (*fFieldInfo)[npar].getString(MetaData::ATTR_NAME);
00671 
00672    if (buff.length()==0) return 0;
00673 
00674    fBuffer[npar].namebuf = new char[buff.length()+1];
00675 
00676    strcpy(fBuffer[npar].namebuf, buff.c_str());
00677 
00678    return fBuffer[npar].namebuf;
00679 }
00680 
00681 //______________________________________________________________________________
00682 Bool_t TOracleStatement::NextResultRow()
00683 {
00684    // Move cursor to next row in result set.
00685    // For Oracle it may lead to additional request to database 
00686     
00687    ClearError();
00688    
00689    if (fResult==0) {
00690       SetError(-1,"There is no result set for statement", "NextResultRow");
00691       return kFALSE;
00692    }
00693 
00694    if (fResult==0) return kFALSE;
00695 
00696    try {
00697       for (int n=0;n<fBufferSize;n++) {
00698         if (fBuffer[n].strbuf) 
00699            delete[] fBuffer[n].strbuf;
00700         fBuffer[n].strbuf = 0;
00701         fBuffer[n].strbufsize = -1;
00702       }
00703       if (fResult->next() == oracle::occi::ResultSet::END_OF_FETCH) {
00704          fWorkingMode = 0;
00705          CloseBuffer();
00706          return kFALSE;
00707       }
00708       return kTRUE;
00709    } catch (SQLException &oraex) {
00710       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "NextResultRow");
00711       
00712       if (oraex.getErrorCode()==32108) 
00713          Info("NextResultRow", "Use TSQLStatement::SetMaxFieldSize() to solve a problem");
00714       
00715    }
00716 
00717    return kFALSE;
00718 }
00719 
00720 //______________________________________________________________________________
00721 Bool_t TOracleStatement::IsNull(Int_t npar)
00722 {
00723    // Checks if fieled value in result set is NULL  
00724     
00725    CheckGetField("IsNull", kFALSE);
00726 
00727    try {
00728       return fResult->isNull(npar+1);
00729    } catch (SQLException &oraex) {
00730       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "IsNull");
00731    }
00732 
00733    return kTRUE;
00734 }
00735 
00736 //______________________________________________________________________________
00737 Int_t TOracleStatement::GetInt(Int_t npar)
00738 {
00739    // return field value as integer
00740     
00741    CheckGetField("GetInt", 0);
00742 
00743    Int_t res = 0;
00744 
00745    try {
00746       if (!fResult->isNull(npar+1))
00747         res = fResult->getInt(npar+1);
00748    } catch (SQLException &oraex) {
00749       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetInt");
00750    }
00751 
00752    return res;
00753 }
00754 
00755 //______________________________________________________________________________
00756 UInt_t TOracleStatement::GetUInt(Int_t npar)
00757 {
00758    // return field value as unsigned integer
00759 
00760    CheckGetField("GetUInt", 0);
00761 
00762    UInt_t res = 0;
00763 
00764    try {
00765       if (!fResult->isNull(npar+1))
00766         res = fResult->getUInt(npar+1);
00767    } catch (SQLException &oraex) {
00768       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetUInt");
00769    }
00770 
00771    return res;
00772 }
00773 
00774 
00775 //______________________________________________________________________________
00776 Long_t TOracleStatement::GetLong(Int_t npar)
00777 {
00778    // return field value as long integer
00779 
00780    CheckGetField("GetLong", 0);
00781 
00782    Long_t res = 0;
00783 
00784    try {
00785       if (!fResult->isNull(npar+1))
00786         res = (Long_t) fResult->getNumber(npar+1);
00787    } catch (SQLException &oraex) {
00788       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetLong");
00789    }
00790 
00791    return res;
00792 }
00793 
00794 //______________________________________________________________________________
00795 Long64_t TOracleStatement::GetLong64(Int_t npar)
00796 {
00797    // return field value as 64-bit integer
00798 
00799    CheckGetField("GetLong64", 0);
00800 
00801    Long64_t res = 0;
00802 
00803    try {
00804       if (!fResult->isNull(npar+1))
00805         res = (Long64_t) (long double) fResult->getNumber(npar+1);
00806    } catch (SQLException &oraex) {
00807       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetLong64");
00808    }
00809 
00810    return res;
00811 }
00812 
00813 //______________________________________________________________________________
00814 ULong64_t TOracleStatement::GetULong64(Int_t npar)
00815 {
00816    // return field value as unsigned 64-bit integer
00817 
00818    CheckGetField("GetULong64", 0);
00819 
00820    ULong64_t res = 0;
00821 
00822    try {
00823       if (!fResult->isNull(npar+1))
00824         res = (ULong64_t) (long double) fResult->getNumber(npar+1);
00825    } catch (SQLException &oraex) {
00826       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetULong64");
00827    }
00828 
00829    return res;
00830 }
00831 
00832 //______________________________________________________________________________
00833 Double_t TOracleStatement::GetDouble(Int_t npar)
00834 {
00835    // return field value as double
00836 
00837    CheckGetField("GetDouble", 0.);
00838 
00839    Double_t res = 0;
00840 
00841    try {
00842       if (!fResult->isNull(npar+1))
00843         res = fResult->getDouble(npar+1);
00844    } catch (SQLException &oraex) {
00845       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetDouble");
00846    }
00847 
00848    return res;
00849 }
00850 
00851 //______________________________________________________________________________
00852 const char* TOracleStatement::GetString(Int_t npar)
00853 {
00854    // return field value as string
00855 
00856    CheckGetField("GetString", 0);
00857 
00858    if (fBuffer[npar].strbuf!=0) return fBuffer[npar].strbuf;
00859 
00860    try {
00861       if (fResult->isNull(npar+1)) return 0;
00862 
00863       int datatype = (*fFieldInfo)[npar].getInt(MetaData::ATTR_DATA_TYPE);
00864 
00865       std::string res;
00866 
00867       switch (datatype) {
00868         case SQLT_NUM: { // oracle numeric NUMBER
00869            int prec = (*fFieldInfo)[npar].getInt(MetaData::ATTR_PRECISION);
00870            int scale = (*fFieldInfo)[npar].getInt(MetaData::ATTR_SCALE);
00871 
00872            if ((scale == 0) || (prec == 0)) {
00873               res = fResult->getString(npar+1);
00874            } else {
00875               double double_val = fResult->getDouble(npar+1);
00876               char str_number[50];
00877               snprintf(str_number, sizeof(str_number), TSQLServer::GetFloatFormat(), double_val);
00878               res = str_number;
00879            }
00880            break;
00881         }
00882         case SQLT_CHR:  // character string
00883         case SQLT_VCS:  // variable character string
00884         case SQLT_AFC: // ansi fixed char
00885         case SQLT_AVC: // ansi var char
00886            res = fResult->getString(npar+1);
00887            break;
00888         case SQLT_DAT:  // Oracle native DATE type
00889            res = (fResult->getDate(npar+1)).toText(fTimeFmt.Data());
00890            break;
00891         case SQLT_TIMESTAMP:     // TIMESTAMP
00892         case SQLT_TIMESTAMP_TZ:  // TIMESTAMP WITH TIMEZONE
00893         case SQLT_TIMESTAMP_LTZ: // TIMESTAMP WITH LOCAL TIMEZONE
00894            res = (fResult->getTimestamp(npar+1)).toText(fTimeFmt.Data(), 0);
00895            break;
00896         default:
00897            res = fResult->getString(npar+1);
00898            Info("getString","Type %d may not be supported", datatype);
00899       }
00900 
00901       int len = res.length();
00902 
00903       if (len>0) {
00904           fBuffer[npar].strbuf = new char[len+1];
00905           fBuffer[npar].strbufsize = len+1;
00906           strcpy(fBuffer[npar].strbuf, res.c_str());
00907       }
00908 
00909       return fBuffer[npar].strbuf;
00910 
00911    } catch (SQLException &oraex) {
00912       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetString");
00913    }
00914 
00915    return 0;
00916 }
00917 
00918 //______________________________________________________________________________
00919 Bool_t TOracleStatement::GetBinary(Int_t npar, void* &mem, Long_t& size)
00920 {
00921    // Return field value as binary array 
00922    // Supports LONG, BLOB, CLOB, BFILE, CFILE types of columns
00923    // Reads complete content of the column, therefore not suitable for
00924    // big structures
00925 
00926    mem = 0;
00927    size = 0;
00928 
00929    CheckGetField("GetBinary", kFALSE);
00930 
00931    if (fBuffer[npar].strbufsize>=0) {
00932       mem = fBuffer[npar].strbuf; 
00933       size = fBuffer[npar].strbufsize;
00934       return kTRUE;
00935    }
00936 
00937    try {
00938       if (fResult->isNull(npar+1)) return kTRUE;
00939 
00940       int datatype = (*fFieldInfo)[npar].getInt(MetaData::ATTR_DATA_TYPE);
00941       
00942       switch (datatype) {
00943          case SQLT_LNG: {
00944             Bytes parbytes = fResult->getBytes(npar+1);
00945             
00946             size = parbytes.length();
00947       
00948             fBuffer[npar].strbufsize = size;
00949             
00950             if (size>0) {
00951                mem = malloc(size); 
00952                
00953                fBuffer[npar].strbuf = (char*) mem;
00954                
00955                parbytes.getBytes((unsigned char*) mem, size);
00956             }
00957             
00958             break;
00959          }
00960 
00961          case SQLT_BLOB: {
00962             Blob parblob = fResult->getBlob(npar+1);
00963             
00964             size = parblob.length();
00965             
00966             fBuffer[npar].strbufsize = size;
00967             
00968             if (size>0) {
00969                mem = malloc(size); 
00970                
00971                fBuffer[npar].strbuf = (char*) mem;
00972                
00973                parblob.read(size, (unsigned char*) mem, size);
00974             }
00975             
00976             break;
00977          }
00978          
00979          case SQLT_CLOB: {
00980             Clob parclob = fResult->getClob(npar+1);
00981             
00982             size = parclob.length();
00983             
00984             fBuffer[npar].strbufsize = size;
00985             
00986             if (size>0) {
00987                mem = malloc(size); 
00988                
00989                fBuffer[npar].strbuf = (char*) mem;
00990                
00991                parclob.read(size, (unsigned char*) mem, size);
00992             }
00993 
00994             break;
00995          }
00996 
00997          case SQLT_BFILEE: 
00998          case SQLT_CFILEE: {
00999 
01000             Bfile parbfile = fResult->getBfile(npar+1);
01001             
01002             size = parbfile.length();
01003             
01004             fBuffer[npar].strbufsize = size;
01005             
01006             if (size>0) {
01007                mem = malloc(size); 
01008                
01009                fBuffer[npar].strbuf = (char*) mem;
01010                
01011                parbfile.read(size, (unsigned char*) mem, size);
01012             }
01013             
01014             break;
01015          }
01016          
01017          default: 
01018            Error("GetBinary", "Oracle data type %d not supported", datatype);
01019            SetError(-1, "Unsupported type for binary convertion", "GetBinary");
01020            return false;
01021       }
01022       
01023       return kTRUE;
01024          
01025    } catch (SQLException &oraex) {
01026       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetBinary");
01027    }
01028    
01029    return kFALSE;
01030 }
01031 
01032 
01033 //______________________________________________________________________________
01034 Bool_t TOracleStatement::GetDate(Int_t npar, Int_t& year, Int_t& month, Int_t& day)
01035 {
01036    // return field value as date
01037    
01038    Int_t hour, min, sec;
01039    
01040    return GetDatime(npar, year, month, day, hour, min, sec);
01041 }
01042 
01043 //______________________________________________________________________________
01044 Bool_t TOracleStatement::GetTime(Int_t npar, Int_t& hour, Int_t& min, Int_t& sec)
01045 {
01046    // return field value as time
01047     
01048    Int_t year, month, day;
01049 
01050    return GetDatime(npar, year, month, day, hour, min, sec);
01051 }
01052 
01053 //______________________________________________________________________________
01054 Bool_t TOracleStatement::GetDatime(Int_t npar, Int_t& year, Int_t& month, Int_t& day, Int_t& hour, Int_t& min, Int_t& sec)
01055 {
01056    // return field value as date & time
01057     
01058    CheckGetField("GetDatime", kFALSE);
01059 
01060    try {
01061       if (!fResult->isNull(npar+1)) {
01062          int datatype = (*fFieldInfo)[npar].getInt(MetaData::ATTR_DATA_TYPE);
01063          
01064          if (datatype!=SQLT_DAT) return kFALSE;
01065           
01066          Date tm = fResult->getDate(npar+1);
01067          int o_year;
01068          unsigned int o_month, o_day, o_hour, o_minute, o_second; 
01069          tm.getDate(o_year, o_month, o_day, o_hour, o_minute, o_second);
01070          year = (Int_t) o_year;
01071          month = (Int_t) o_month;
01072          day = (Int_t) o_day;
01073          hour = (Int_t) o_hour;
01074          min = (Int_t) o_minute;
01075          sec = (Int_t) o_second;
01076          return kTRUE;
01077       }
01078    } catch (SQLException &oraex) {
01079       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetDatime");
01080    }
01081 
01082    return kFALSE;
01083 }
01084 
01085 //______________________________________________________________________________
01086 Bool_t TOracleStatement::GetTimestamp(Int_t npar, Int_t& year, Int_t& month, Int_t& day, Int_t& hour, Int_t& min, Int_t& sec, Int_t& frac)
01087 {
01088    // return field value as date & time
01089     
01090    CheckGetField("GetTimestamp", kFALSE);
01091 
01092    try {
01093       if (!fResult->isNull(npar+1)) {
01094          int datatype = (*fFieldInfo)[npar].getInt(MetaData::ATTR_DATA_TYPE);
01095 
01096          if ((datatype!=SQLT_TIMESTAMP) && 
01097              (datatype!=SQLT_TIMESTAMP_TZ) && 
01098              (datatype!=SQLT_TIMESTAMP_LTZ)) return kFALSE;
01099 
01100          Timestamp tm = fResult->getTimestamp(npar+1);
01101          int o_year;
01102          unsigned int o_month, o_day, o_hour, o_minute, o_second, o_frac; 
01103          tm.getDate(o_year, o_month, o_day);
01104          tm.getTime(o_hour, o_minute, o_second, o_frac);
01105          year = (Int_t) o_year;
01106          month = (Int_t) o_month;
01107          day = (Int_t) o_day;
01108          hour = (Int_t) o_hour;
01109          min = (Int_t) o_minute;
01110          sec = (Int_t) o_second;
01111          frac = (Int_t) o_frac;
01112          return kTRUE;  
01113       }
01114    } catch (SQLException &oraex) {
01115       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetTimestamp");
01116    }
01117 
01118    return kFALSE;
01119 }
01120 
01121 //______________________________________________________________________________
01122 Bool_t TOracleStatement::GetVInt(Int_t npar, std::vector<Int_t> &value)
01123 {
01124    // return field value as vector of integers
01125    CheckGetField("GetVInt", kFALSE);
01126    try {
01127       if (!fResult->isNull(npar+1))
01128          getVector(fResult, npar+1, value);
01129       return kTRUE;
01130    } catch (SQLException &oraex) {
01131       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetVInt");
01132    }
01133    return kFALSE;
01134 }
01135 
01136 //______________________________________________________________________________
01137 Bool_t TOracleStatement::GetVUInt(Int_t npar, std::vector<UInt_t> &value)
01138 {
01139    // return field value as vector of unsigned integers
01140    CheckGetField("GetVUInt", kFALSE);
01141    try {
01142       if (!fResult->isNull(npar+1))
01143          getVector(fResult, npar+1, value);
01144       return kTRUE;
01145    } catch (SQLException &oraex) {
01146       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetVUInt");
01147    }
01148    return kFALSE;
01149 }
01150 
01151 
01152 //______________________________________________________________________________
01153 Bool_t TOracleStatement::GetVLong(Int_t npar, std::vector<Long_t> &value)
01154 {
01155    // return field value as vector of long integers
01156    CheckGetField("GetVLong", kFALSE);
01157    try {
01158       std::vector<Number> res;
01159       if (!fResult->isNull(npar+1))
01160          getVector(fResult, npar+1, res);
01161       for (std::vector<Number>::const_iterator it = res.begin();
01162            it != res.end();
01163            it++ ) {
01164          value.push_back((Long_t)*it);
01165       }
01166       return kTRUE;
01167    } catch (SQLException &oraex) {
01168       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetVLong");
01169    }
01170    return kFALSE;
01171 }
01172 
01173 //______________________________________________________________________________
01174 Bool_t TOracleStatement::GetVLong64(Int_t npar, std::vector<Long64_t> &value)
01175 {
01176    // return field value as vector of 64-bit integers
01177    CheckGetField("GetVLong64", kFALSE);
01178    try {
01179       std::vector<Number> res;
01180       if (!fResult->isNull(npar+1))
01181          getVector(fResult, npar+1, res);
01182       for (std::vector<Number>::const_iterator it = res.begin();
01183            it != res.end();
01184            it++ ) {
01185          value.push_back((Long_t)*it);
01186       }
01187       return kTRUE;
01188    } catch (SQLException &oraex) {
01189       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetVLong64");
01190    }
01191    return kFALSE;
01192 }
01193 
01194 //______________________________________________________________________________
01195 Bool_t TOracleStatement::GetVULong64(Int_t npar, std::vector<ULong64_t> &value)
01196 {
01197    // return field value as vector of unsigned 64-bit integers
01198    CheckGetField("GetVULong64", kFALSE);
01199    try {
01200       std::vector<Number> res;
01201       if (!fResult->isNull(npar+1))
01202          getVector(fResult, npar+1, res);
01203       for (std::vector<Number>::const_iterator it = res.begin();
01204            it != res.end();
01205            it++ ) {
01206         value.push_back((Long_t)(long double)*it);
01207       }
01208       return kTRUE;
01209    } catch (SQLException &oraex) {
01210       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetVULong64");
01211    }
01212    return kFALSE;
01213 }
01214 
01215 //______________________________________________________________________________
01216 Bool_t TOracleStatement::GetVDouble(Int_t npar, std::vector<Double_t> &value)
01217 {
01218    // return field value as vector of doubles
01219    CheckGetField("GetVDouble", kFALSE);
01220    try {
01221       if (!fResult->isNull(npar+1))
01222          getVector(fResult, npar+1, value);
01223       return kTRUE;
01224    } catch (SQLException &oraex) {
01225       SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetVDouble");
01226    }
01227    return kFALSE;
01228 }
01229 

Generated on Tue Jul 5 15:14:39 2011 for ROOT_528-00b_version by  doxygen 1.5.1