TOracleRow.cxx

Go to the documentation of this file.
00001 // @(#)root/oracle:$Id: TOracleRow.cxx 35527 2010-09-21 12:27:01Z brun $
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 "TOracleRow.h"
00013 #include "TOracleServer.h"
00014 #include <string.h>
00015 
00016 ClassImp(TOracleRow);
00017 
00018 //______________________________________________________________________________
00019 TOracleRow::TOracleRow(ResultSet *rs, vector<MetaData> *fieldMetaData)
00020 {
00021    // Single row of query result.
00022 
00023    fResult      = rs;
00024    fFieldInfo   = fieldMetaData;
00025    fFieldCount  = fFieldInfo->size();
00026 
00027    fFieldsBuffer = 0;
00028 
00029    GetRowData();
00030 }
00031 
00032 //______________________________________________________________________________
00033 TOracleRow::~TOracleRow()
00034 {
00035    // Destroy row object.
00036 
00037    Close();
00038 }
00039 
00040 //______________________________________________________________________________
00041 void TOracleRow::Close(Option_t *)
00042 {
00043    // Close row.
00044 
00045    if (fFieldsBuffer!=0) {
00046       for (int n=0;n<fFieldCount;n++)
00047         if (fFieldsBuffer[n]) delete[] fFieldsBuffer[n];
00048       delete[] fFieldsBuffer;
00049    }
00050 
00051    fFieldInfo   = 0;
00052    fFieldCount  = 0;
00053    fResult      = 0;
00054 }
00055 
00056 //______________________________________________________________________________
00057 Bool_t TOracleRow::IsValid(Int_t field)
00058 {
00059    // Check if row is open and field index within range.
00060 
00061    if (!fResult) {
00062       Error("IsValid", "row closed");
00063       return kFALSE;
00064    }
00065    if (field < 0 || field >= (Int_t)fFieldInfo->size()) {
00066       Error("IsValid", "field index out of bounds");
00067       return kFALSE;
00068    }
00069    return kTRUE;
00070 }
00071 
00072 //______________________________________________________________________________
00073 ULong_t TOracleRow::GetFieldLength(Int_t field)
00074 {
00075    // Get length in bytes of specified field.
00076 
00077    if (!IsValid(field) || fFieldInfo->size() <= 0)
00078       return 0;
00079 
00080    MetaData fieldMD = (*fFieldInfo)[field];
00081 
00082    return fieldMD.getInt(MetaData::ATTR_DATA_SIZE);
00083 }
00084 
00085 //______________________________________________________________________________
00086 const char* TOracleRow::GetField(Int_t field)
00087 {
00088    if ((field<0) || (field>=fFieldCount)) {
00089       Error("TOracleRow","GetField(): out-of-range or No RowData/ResultSet/MetaData");
00090       return 0;
00091    }
00092 
00093    return fFieldsBuffer ? fFieldsBuffer[field] : 0;
00094 }
00095 
00096 //______________________________________________________________________________
00097 void TOracleRow::GetRowData()
00098 {
00099    if (!fResult || !fFieldInfo || (fFieldCount<=0)) return;
00100 
00101    fFieldsBuffer = new char* [fFieldCount];
00102    for (int n=0;n<fFieldCount;n++)
00103      fFieldsBuffer[n] = 0;
00104 
00105    std::string res;
00106 
00107    char str_number[200];
00108 
00109    int fPrecision, fScale, fDataType;
00110    double double_val;
00111 
00112    try {
00113 
00114    for (int field=0;field<fFieldCount;field++) {
00115       if (fResult->isNull(field+1)) continue;
00116 
00117       fDataType = (*fFieldInfo)[field].getInt(MetaData::ATTR_DATA_TYPE);
00118 
00119       switch (fDataType) {
00120         case SQLT_NUM: //NUMBER
00121            fPrecision = (*fFieldInfo)[field].getInt(MetaData::ATTR_PRECISION);
00122            fScale = (*fFieldInfo)[field].getInt(MetaData::ATTR_SCALE);
00123 
00124            if ((fScale == 0) || (fPrecision == 0)) {
00125               res = fResult->getString(field+1);
00126            } else {
00127               double_val = fResult->getDouble(field+1);
00128               snprintf(str_number, sizeof(str_number), TSQLServer::GetFloatFormat(), double_val);
00129               res = str_number;
00130            }
00131            break;
00132 
00133         case SQLT_CHR:  // character string
00134         case SQLT_VCS:  // variable character string
00135         case SQLT_AFC: // ansi fixed char
00136         case SQLT_AVC: // ansi var char
00137            res = fResult->getString(field+1);
00138            break;
00139         case SQLT_DAT:  // Oracle native DATE type
00140            res = (fResult->getDate(field+1)).toText(TOracleServer::GetDatimeFormat());
00141            break;
00142         case SQLT_TIMESTAMP:     // TIMESTAMP
00143         case SQLT_TIMESTAMP_TZ:  // TIMESTAMP WITH TIMEZONE
00144         case SQLT_TIMESTAMP_LTZ: // TIMESTAMP WITH LOCAL TIMEZONE
00145            res = (fResult->getTimestamp(field+1)).toText(TOracleServer::GetDatimeFormat(), 0);
00146            break;
00147         default:
00148            Error("GetRowData","Oracle type %d not supported.", fDataType);
00149            continue;
00150       }
00151 
00152       int len = res.length();
00153       if (len>0) {
00154          fFieldsBuffer[field] = new char[len+1];
00155          strcpy(fFieldsBuffer[field], res.c_str());
00156       }
00157    }
00158 
00159    } catch (SQLException &oraex) {
00160       Error("GetRowData", "%s", (oraex.getMessage()).c_str());
00161    }
00162 }

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