TOracleResult.cxx

Go to the documentation of this file.
00001 // @(#)root/oracle:$Id: TOracleResult.cxx 35211 2010-09-08 16:06:13Z pcanal $
00002 // Author: Yan Liu and Shaowen Wang   23/11/04
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "TOracleResult.h"
00013 #include "TOracleRow.h"
00014 #include "TList.h"
00015 
00016 using namespace std;
00017 
00018 
00019 ClassImp(TOracleResult)
00020 
00021 //______________________________________________________________________________
00022 void TOracleResult::initResultSet(Statement *stmt)
00023 {
00024    // Oracle query result.
00025 
00026    if (!stmt) {
00027       Error("initResultSet", "construction: empty statement");
00028    } else {
00029       try {
00030          fStmt = stmt;
00031          if (stmt->status() == Statement::RESULT_SET_AVAILABLE) {
00032             fResultType  = 1;
00033             fResult      = stmt->getResultSet();
00034             fFieldInfo   = (fResult==0) ? 0 : new vector<MetaData>(fResult->getColumnListMetaData());
00035             fFieldCount  = (fFieldInfo==0) ? 0 : fFieldInfo->size();
00036          } else if (stmt->status() == Statement::UPDATE_COUNT_AVAILABLE) {
00037             fResultType  = 3; // this is update_count_available
00038             fResult      = 0;
00039             fFieldInfo   = 0;
00040             fFieldCount  = 0;
00041             fUpdateCount = stmt->getUpdateCount();
00042          }
00043       } catch (SQLException &oraex) {
00044          Error("initResultSet", "%s", (oraex.getMessage()).c_str());
00045          MakeZombie();
00046       }
00047    }
00048 }
00049 
00050 //______________________________________________________________________________
00051 TOracleResult::TOracleResult(Connection *conn, Statement *stmt)
00052 {
00053    fConn        = conn;
00054    fResult      = 0;
00055    fStmt        = 0;
00056    fPool        = 0;
00057    fRowCount    = 0;
00058    fFieldInfo   = 0;
00059    fResultType  = 0;
00060    fUpdateCount = 0;
00061 
00062    initResultSet(stmt);
00063 
00064    if (fResult) ProducePool();
00065 }
00066 
00067 //______________________________________________________________________________
00068 TOracleResult::TOracleResult(Connection *conn, const char *tableName)
00069 {
00070    // This construction func is only used to get table metainfo.
00071 
00072    fResult      = 0;
00073    fStmt        = 0;
00074    fConn        = 0;
00075    fPool        = 0;
00076    fRowCount    = 0;
00077    fFieldInfo   = 0;
00078    fResultType  = 0;
00079    fUpdateCount = 0;
00080 
00081    if (!tableName || !conn) {
00082       Error("TOracleResult", "construction: empty input parameter");
00083    } else {
00084       MetaData connMD = conn->getMetaData(tableName, MetaData::PTYPE_TABLE);
00085       fFieldInfo   = new vector<MetaData>(connMD.getVector(MetaData::ATTR_LIST_COLUMNS));
00086       fFieldCount  = fFieldInfo->size();
00087       fResultType  = 2; // indicates that this is just an table metainfo
00088    }
00089 }
00090 
00091 //______________________________________________________________________________
00092 TOracleResult::~TOracleResult()
00093 {
00094    // Cleanup Oracle query result.
00095 
00096    Close();
00097 }
00098 
00099 //______________________________________________________________________________
00100 void TOracleResult::Close(Option_t *)
00101 {
00102    // Close query result.
00103 
00104    if (fConn && fStmt) {
00105       if (fResult) fStmt->closeResultSet(fResult);
00106       fConn->terminateStatement(fStmt);
00107    }
00108 
00109    if (fPool) {
00110       fPool->Delete();
00111       delete fPool;
00112    }
00113 
00114    if (fFieldInfo)
00115       delete fFieldInfo;
00116 
00117    fResultType = 0;
00118 
00119    fStmt = 0;
00120    fResult = 0;
00121    fFieldInfo = 0;
00122    fPool = 0;
00123 }
00124 
00125 //______________________________________________________________________________
00126 Bool_t TOracleResult::IsValid(Int_t field)
00127 {
00128    // Check if result set is open and field index within range.
00129 
00130    if (field < 0 || field >= fFieldCount) {
00131       Error("IsValid", "field index out of bounds");
00132       return kFALSE;
00133    }
00134    return kTRUE;
00135 }
00136 
00137 //______________________________________________________________________________
00138 Int_t TOracleResult::GetFieldCount()
00139 {
00140    // Get number of fields in result.
00141 
00142    return fFieldCount;
00143 }
00144 
00145 //______________________________________________________________________________
00146 const char *TOracleResult::GetFieldName(Int_t field)
00147 {
00148    // Get name of specified field.
00149 
00150    if (!IsValid(field))
00151       return 0;
00152    fNameBuffer = (*fFieldInfo)[field].getString(MetaData::ATTR_NAME);
00153    return fNameBuffer.c_str();
00154 }
00155 
00156 //______________________________________________________________________________
00157 TSQLRow *TOracleResult::Next()
00158 {
00159    // Get next query result row. The returned object must be
00160    // deleted by the user.
00161 
00162    if (!fResult || (fResultType!=1)) return 0;
00163 
00164    if (fPool!=0) {
00165       TSQLRow* row = (TSQLRow*) fPool->First();
00166       if (row!=0) fPool->Remove(row);
00167       return row;
00168    }
00169 
00170    // if select query,
00171    try {
00172       if (fResult->next() != oracle::occi::ResultSet::END_OF_FETCH) {
00173          fRowCount++;
00174          return new TOracleRow(fResult, fFieldInfo);
00175       } else
00176          return 0;
00177    } catch (SQLException &oraex) {
00178       Error("Next", "%s", (oraex.getMessage()).c_str());
00179       MakeZombie();
00180    }
00181    return 0;
00182 }
00183 
00184 //______________________________________________________________________________
00185 Int_t TOracleResult::GetRowCount() const
00186 {
00187    if (!fResult) return 0;
00188 
00189    if (fPool==0) ((TOracleResult*) this)->ProducePool();
00190 
00191    return fRowCount;
00192 }
00193 
00194 //______________________________________________________________________________
00195 void TOracleResult::ProducePool()
00196 {
00197    if (fPool!=0) return;
00198 
00199    TList* pool = new TList;
00200    TSQLRow* res = 0;
00201    while ((res = Next()) !=0) {
00202       pool->Add(res);
00203    }
00204 
00205    fPool = pool;
00206 }

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