TODBCRow.cxx

Go to the documentation of this file.
00001 // @(#)root/odbc:$Id: TODBCRow.cxx 35505 2010-09-21 08:18:20Z brun $
00002 // Author: Sergey Linev   6/02/2006
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2006, 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 "TODBCRow.h"
00013 
00014 #include <sqlext.h>
00015 
00016 
00017 ClassImp(TODBCRow)
00018 
00019 //______________________________________________________________________________
00020 TODBCRow::TODBCRow(SQLHSTMT stmt, Int_t fieldcount)
00021 {
00022    // Single row of query result.
00023    fHstmt = stmt;
00024    fFieldCount = fieldcount;
00025 
00026    fBuffer = 0;
00027    fLengths = 0;      
00028 
00029    if (fFieldCount>0) {
00030       fBuffer = new char*[fFieldCount];
00031       fLengths = new ULong_t[fFieldCount];
00032       for (Int_t n = 0; n < fFieldCount; n++) {
00033          fBuffer[n] = 0;
00034          fLengths[n] = 0;
00035          CopyFieldValue(n);
00036       }
00037    }
00038 }
00039 
00040 //______________________________________________________________________________
00041 TODBCRow::~TODBCRow()
00042 {
00043    // Destroy row object.
00044 
00045    Close();
00046 }
00047 
00048 //______________________________________________________________________________
00049 void TODBCRow::Close(Option_t *)
00050 {
00051    // Close row.
00052 
00053    if (fBuffer!=0) {
00054       for (Int_t n = 0; n < fFieldCount; n++)
00055          delete[] fBuffer[n];
00056       delete[] fBuffer;
00057       fBuffer = 0;
00058    }
00059    
00060    if (fLengths!=0) {
00061       delete[] fLengths;
00062       fLengths = 0;
00063    } 
00064 }
00065 
00066 //______________________________________________________________________________
00067 void TODBCRow::CopyFieldValue(Int_t field)
00068 {
00069    // Extracts field value from statement.
00070    // First allocates 128 bytes for buffer.
00071    // If there is not enouth space, bigger buffer is allocated and
00072    // request is repeated 
00073     
00074    #define buffer_len 128
00075 
00076    fBuffer[field] = new char[buffer_len];
00077 
00078    SQLLEN ressize;
00079 
00080    SQLRETURN retcode = SQLGetData(fHstmt, field+1, SQL_C_CHAR, fBuffer[field], buffer_len, &ressize);
00081    
00082    if (ressize==SQL_NULL_DATA) {
00083       delete[] fBuffer[field];
00084       fBuffer[field] = 0;
00085       return;   
00086    }
00087    
00088    fLengths[field] = ressize;
00089    
00090    if (retcode==SQL_SUCCESS_WITH_INFO) {
00091       SQLINTEGER code;
00092       SQLCHAR state[ 7 ];
00093       SQLGetDiagRec(SQL_HANDLE_STMT, fHstmt, 1, state, &code, 0, 0, 0);
00094       
00095       if (strcmp((char*)state,"01004")==0) {
00096 //         Info("CopyFieldValue","Before %d %s", ressize, fBuffer[field]);
00097          
00098          char* newbuf = new char[ressize+10];
00099          strlcpy(newbuf, fBuffer[field], buffer_len);
00100          delete fBuffer[field];
00101          fBuffer[field] = newbuf;
00102          newbuf+=(buffer_len-1); // first data will not be read again
00103          retcode = SQLGetData(fHstmt, field+1, SQL_C_CHAR, newbuf, ressize+10-buffer_len, &ressize);
00104          
00105 //         Info("CopyFieldValue","After %d %s", ressize, fBuffer[field]);
00106       }
00107    }
00108 }
00109 
00110 //______________________________________________________________________________
00111 ULong_t TODBCRow::GetFieldLength(Int_t field)
00112 {
00113    // Get length in bytes of specified field.
00114 
00115    if ((field<0) || (field>=fFieldCount)) return 0;
00116    
00117    return fLengths[field];
00118 }
00119 
00120 //______________________________________________________________________________
00121 const char *TODBCRow::GetField(Int_t field)
00122 {
00123    // Get specified field from row (0 <= field < GetFieldCount()).
00124 
00125    if ((field<0) || (field>=fFieldCount)) return 0;
00126 
00127    return fBuffer[field];
00128 }

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