ROOT logo
// @(#)root/odbc:$Id$
// Author: Sergey Linev   6/02/2006

/*************************************************************************
 * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "TODBCRow.h"

#include <sqlext.h>


ClassImp(TODBCRow)

//______________________________________________________________________________
TODBCRow::TODBCRow(SQLHSTMT stmt, Int_t fieldcount)
{
   // Single row of query result.
   fHstmt = stmt;
   fFieldCount = fieldcount;

   fBuffer = 0;
   fLengths = 0;      

   if (fFieldCount>0) {
      fBuffer = new char*[fFieldCount];
      fLengths = new ULong_t[fFieldCount];
      for (Int_t n = 0; n < fFieldCount; n++) {
         fBuffer[n] = 0;
         fLengths[n] = 0;
         CopyFieldValue(n);
      }
   }
}

//______________________________________________________________________________
TODBCRow::~TODBCRow()
{
   // Destroy row object.

   Close();
}

//______________________________________________________________________________
void TODBCRow::Close(Option_t *)
{
   // Close row.

   if (fBuffer!=0) {
      for (Int_t n = 0; n < fFieldCount; n++)
         delete[] fBuffer[n];
      delete[] fBuffer;
      fBuffer = 0;
   }
   
   if (fLengths!=0) {
      delete[] fLengths;
      fLengths = 0;
   } 
}

//______________________________________________________________________________
void TODBCRow::CopyFieldValue(Int_t field)
{
   // Extracts field value from statement.
   // First allocates 128 bytes for buffer.
   // If there is not enouth space, bigger buffer is allocated and
   // request is repeated 
    
   #define buffer_len 128

   fBuffer[field] = new char[buffer_len];

   SQLLEN ressize;

   SQLRETURN retcode = SQLGetData(fHstmt, field+1, SQL_C_CHAR, fBuffer[field], buffer_len, &ressize);
   
   if (ressize==SQL_NULL_DATA) {
      delete[] fBuffer[field];
      fBuffer[field] = 0;
      return;   
   }
   
   fLengths[field] = ressize;
   
   if (retcode==SQL_SUCCESS_WITH_INFO) {
      SQLINTEGER code;
      SQLCHAR state[ 7 ];
      SQLGetDiagRec(SQL_HANDLE_STMT, fHstmt, 1, state, &code, 0, 0, 0);
      
      if (strcmp((char*)state,"01004")==0) {
//         Info("CopyFieldValue","Before %d %s", ressize, fBuffer[field]);
         
         char* newbuf = new char[ressize+10];
         strlcpy(newbuf, fBuffer[field], buffer_len);
         delete fBuffer[field];
         fBuffer[field] = newbuf;
         newbuf+=(buffer_len-1); // first data will not be read again
         retcode = SQLGetData(fHstmt, field+1, SQL_C_CHAR, newbuf, ressize+10-buffer_len, &ressize);
         
//         Info("CopyFieldValue","After %d %s", ressize, fBuffer[field]);
      }
   }
}

//______________________________________________________________________________
ULong_t TODBCRow::GetFieldLength(Int_t field)
{
   // Get length in bytes of specified field.

   if ((field<0) || (field>=fFieldCount)) return 0;
   
   return fLengths[field];
}

//______________________________________________________________________________
const char *TODBCRow::GetField(Int_t field)
{
   // Get specified field from row (0 <= field < GetFieldCount()).

   if ((field<0) || (field>=fFieldCount)) return 0;

   return fBuffer[field];
}
 TODBCRow.cxx:1
 TODBCRow.cxx:2
 TODBCRow.cxx:3
 TODBCRow.cxx:4
 TODBCRow.cxx:5
 TODBCRow.cxx:6
 TODBCRow.cxx:7
 TODBCRow.cxx:8
 TODBCRow.cxx:9
 TODBCRow.cxx:10
 TODBCRow.cxx:11
 TODBCRow.cxx:12
 TODBCRow.cxx:13
 TODBCRow.cxx:14
 TODBCRow.cxx:15
 TODBCRow.cxx:16
 TODBCRow.cxx:17
 TODBCRow.cxx:18
 TODBCRow.cxx:19
 TODBCRow.cxx:20
 TODBCRow.cxx:21
 TODBCRow.cxx:22
 TODBCRow.cxx:23
 TODBCRow.cxx:24
 TODBCRow.cxx:25
 TODBCRow.cxx:26
 TODBCRow.cxx:27
 TODBCRow.cxx:28
 TODBCRow.cxx:29
 TODBCRow.cxx:30
 TODBCRow.cxx:31
 TODBCRow.cxx:32
 TODBCRow.cxx:33
 TODBCRow.cxx:34
 TODBCRow.cxx:35
 TODBCRow.cxx:36
 TODBCRow.cxx:37
 TODBCRow.cxx:38
 TODBCRow.cxx:39
 TODBCRow.cxx:40
 TODBCRow.cxx:41
 TODBCRow.cxx:42
 TODBCRow.cxx:43
 TODBCRow.cxx:44
 TODBCRow.cxx:45
 TODBCRow.cxx:46
 TODBCRow.cxx:47
 TODBCRow.cxx:48
 TODBCRow.cxx:49
 TODBCRow.cxx:50
 TODBCRow.cxx:51
 TODBCRow.cxx:52
 TODBCRow.cxx:53
 TODBCRow.cxx:54
 TODBCRow.cxx:55
 TODBCRow.cxx:56
 TODBCRow.cxx:57
 TODBCRow.cxx:58
 TODBCRow.cxx:59
 TODBCRow.cxx:60
 TODBCRow.cxx:61
 TODBCRow.cxx:62
 TODBCRow.cxx:63
 TODBCRow.cxx:64
 TODBCRow.cxx:65
 TODBCRow.cxx:66
 TODBCRow.cxx:67
 TODBCRow.cxx:68
 TODBCRow.cxx:69
 TODBCRow.cxx:70
 TODBCRow.cxx:71
 TODBCRow.cxx:72
 TODBCRow.cxx:73
 TODBCRow.cxx:74
 TODBCRow.cxx:75
 TODBCRow.cxx:76
 TODBCRow.cxx:77
 TODBCRow.cxx:78
 TODBCRow.cxx:79
 TODBCRow.cxx:80
 TODBCRow.cxx:81
 TODBCRow.cxx:82
 TODBCRow.cxx:83
 TODBCRow.cxx:84
 TODBCRow.cxx:85
 TODBCRow.cxx:86
 TODBCRow.cxx:87
 TODBCRow.cxx:88
 TODBCRow.cxx:89
 TODBCRow.cxx:90
 TODBCRow.cxx:91
 TODBCRow.cxx:92
 TODBCRow.cxx:93
 TODBCRow.cxx:94
 TODBCRow.cxx:95
 TODBCRow.cxx:96
 TODBCRow.cxx:97
 TODBCRow.cxx:98
 TODBCRow.cxx:99
 TODBCRow.cxx:100
 TODBCRow.cxx:101
 TODBCRow.cxx:102
 TODBCRow.cxx:103
 TODBCRow.cxx:104
 TODBCRow.cxx:105
 TODBCRow.cxx:106
 TODBCRow.cxx:107
 TODBCRow.cxx:108
 TODBCRow.cxx:109
 TODBCRow.cxx:110
 TODBCRow.cxx:111
 TODBCRow.cxx:112
 TODBCRow.cxx:113
 TODBCRow.cxx:114
 TODBCRow.cxx:115
 TODBCRow.cxx:116
 TODBCRow.cxx:117
 TODBCRow.cxx:118
 TODBCRow.cxx:119
 TODBCRow.cxx:120
 TODBCRow.cxx:121
 TODBCRow.cxx:122
 TODBCRow.cxx:123
 TODBCRow.cxx:124
 TODBCRow.cxx:125
 TODBCRow.cxx:126
 TODBCRow.cxx:127
 TODBCRow.cxx:128