TSapDBResult.cxx

Go to the documentation of this file.
00001 // @(#)root/sapdb:$Id: TSapDBResult.cxx 20882 2007-11-19 11:31:26Z rdm $
00002 // Author: Mark Hemberger & Fons Rademakers   03/08/2001
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2001, 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 "TString.h"
00013 #include "TSapDBResult.h"
00014 #include "TSapDBRow.h"
00015 
00016 
00017 ClassImp(TSapDBResult)
00018 
00019 //______________________________________________________________________________
00020 TSapDBResult::TSapDBResult(SQLHSTMT result, SDWORD rowCount)
00021 {
00022    // SapDB query result.
00023 
00024    fResult     = result;
00025    fFieldNames = 0;
00026    fFieldCount = -1;
00027    fRowCount   = 0;
00028 
00029    if (fResult) {
00030       SQLLEN rowcount = 0;
00031       if (SQLRowCount(fResult, &rowcount) != SQL_SUCCESS) {
00032          Error("TSapDBResult", "no rows counted");
00033       }
00034       // -1 means: result has been found but the number of columns is
00035       // undetermined (only for SYSTEM tables, a valid number is available)
00036       fRowCount = rowcount < 0 ? rowCount : rowcount;
00037    }
00038 }
00039 
00040 //______________________________________________________________________________
00041 TSapDBResult::~TSapDBResult()
00042 {
00043    // Cleanup SapDB query result.
00044 
00045    if (fResult)
00046       Close();
00047 }
00048 
00049 //______________________________________________________________________________
00050 void TSapDBResult::Close(Option_t *)
00051 {
00052    // Close query result.
00053 
00054    if (!fResult)
00055       return;
00056 
00057    delete [] fFieldNames;
00058    fFieldNames = 0;
00059    fFieldCount = 0;
00060    fResult     = 0;
00061    fRowCount   = 0;
00062 }
00063 
00064 //______________________________________________________________________________
00065 Bool_t TSapDBResult::IsValid(Int_t field)
00066 {
00067    // Check if result set is open and field index within range.
00068 
00069    if (!fResult) {
00070       Error("IsValid", "result set closed");
00071       return kFALSE;
00072    }
00073    if (field < 0 || field >= GetFieldCount()) {
00074       Error("IsValid", "field index out of bounds");
00075       return kFALSE;
00076    }
00077    return kTRUE;
00078 }
00079 
00080 //______________________________________________________________________________
00081 Int_t TSapDBResult::GetFieldCount()
00082 {
00083    // Get number of fields in result.
00084 
00085    if (!fResult) {
00086       Error("GetFieldCount", "result set closed");
00087       return 0;
00088    }
00089 
00090    if (fFieldCount >= 0)
00091       return fFieldCount;
00092 
00093    SQLSMALLINT columnCount;
00094    if (SQLNumResultCols(fResult, &columnCount) == SQL_SUCCESS)
00095       fFieldCount = columnCount;
00096    else
00097       fFieldCount = 0;
00098 
00099    return fFieldCount;
00100 }
00101 
00102 //______________________________________________________________________________
00103 const char *TSapDBResult::GetFieldName(Int_t field)
00104 {
00105    // Get name of specified field.
00106 
00107    if (!IsValid(field))
00108       return 0;
00109 
00110    if (!fFieldNames)
00111       fFieldNames = new TString[GetFieldCount()];
00112 
00113    if (!fFieldNames[field].IsNull())
00114       return fFieldNames[field];
00115 
00116    // Get name of specified field.
00117    SQLUSMALLINT columnNumber;
00118    SQLCHAR      columnName[256];
00119    SQLSMALLINT  bufferLength = 256;
00120    SQLSMALLINT  nameLength;
00121    SQLSMALLINT  dataType;
00122    SQLULEN      columnSize;
00123    SQLSMALLINT  decimalDigits;
00124    SQLSMALLINT  nullable;
00125 
00126    columnNumber = field + 1;
00127    if (SQLDescribeCol(fResult, columnNumber, columnName, bufferLength,
00128                       &nameLength, &dataType, &columnSize, &decimalDigits,
00129                       &nullable) == SQL_SUCCESS) {
00130       //printf ("ColumnNumber: %d\n", columnNumber);
00131       //printf ("ColumnName: %s\n", columnName);
00132       //printf ("DataType: %d\n", dataType);
00133       //printf ("ColumnSize: %ld\n", columnSize);
00134       fFieldNames[field] = (const char *)columnName;
00135    } else {
00136       Error("GetFieldName", "cannot get field info");
00137       return 0;
00138    }
00139 
00140    return fFieldNames[field];
00141 }
00142 
00143 //______________________________________________________________________________
00144 TSQLRow *TSapDBResult::Next()
00145 {
00146    // Get next query result row. The returned object must be
00147    // deleted by the user.
00148 
00149    if (!fResult) {
00150       Error("Next", "result set closed");
00151       return 0;
00152    }
00153 
00154    RETCODE rc = SQLFetchScroll(fResult, SQL_FETCH_NEXT, 0);
00155    if (rc == SQL_SUCCESS)
00156       return new TSapDBRow(fResult, GetFieldCount());
00157    else if (rc == SQL_NO_DATA)
00158       return 0;
00159    else {
00160       Error("Next", "error during fetchscroll");
00161       return 0;
00162    }
00163 
00164    return 0;
00165 }

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