ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TODBCRow.cxx
Go to the documentation of this file.
1 // @(#)root/odbc:$Id$
2 // Author: Sergey Linev 6/02/2006
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TODBCRow.h"
13 
14 #include <sqlext.h>
15 
16 
18 
19 ////////////////////////////////////////////////////////////////////////////////
20 /// Single row of query result.
21 
22 TODBCRow::TODBCRow(SQLHSTMT stmt, Int_t fieldcount)
23 {
24  fHstmt = stmt;
25  fFieldCount = fieldcount;
26 
27  fBuffer = 0;
28  fLengths = 0;
29 
30  if (fFieldCount>0) {
31  fBuffer = new char*[fFieldCount];
32  fLengths = new ULong_t[fFieldCount];
33  for (Int_t n = 0; n < fFieldCount; n++) {
34  fBuffer[n] = 0;
35  fLengths[n] = 0;
36  CopyFieldValue(n);
37  }
38  }
39 }
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 /// Destroy row object.
43 
45 {
46  Close();
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Close row.
51 
53 {
54  if (fBuffer!=0) {
55  for (Int_t n = 0; n < fFieldCount; n++)
56  delete[] fBuffer[n];
57  delete[] fBuffer;
58  fBuffer = 0;
59  }
60 
61  if (fLengths!=0) {
62  delete[] fLengths;
63  fLengths = 0;
64  }
65 }
66 
67 ////////////////////////////////////////////////////////////////////////////////
68 /// Extracts field value from statement.
69 /// First allocates 128 bytes for buffer.
70 /// If there is not enouth space, bigger buffer is allocated and
71 /// request is repeated
72 
74 {
75  #define buffer_len 128
76 
77  fBuffer[field] = new char[buffer_len];
78 
79  SQLLEN ressize;
80 
81  SQLRETURN retcode = SQLGetData(fHstmt, field+1, SQL_C_CHAR, fBuffer[field], buffer_len, &ressize);
82 
83  if (ressize==SQL_NULL_DATA) {
84  delete[] fBuffer[field];
85  fBuffer[field] = 0;
86  return;
87  }
88 
89  fLengths[field] = ressize;
90 
91  if (retcode==SQL_SUCCESS_WITH_INFO) {
92  SQLINTEGER code;
93  SQLCHAR state[ 7 ];
94  SQLGetDiagRec(SQL_HANDLE_STMT, fHstmt, 1, state, &code, 0, 0, 0);
95 
96  if (strcmp((char*)state,"01004")==0) {
97 // Info("CopyFieldValue","Before %d %s", ressize, fBuffer[field]);
98 
99  char* newbuf = new char[ressize+10];
100  strlcpy(newbuf, fBuffer[field], buffer_len);
101  delete fBuffer[field];
102  fBuffer[field] = newbuf;
103  newbuf+=(buffer_len-1); // first data will not be read again
104  retcode = SQLGetData(fHstmt, field+1, SQL_C_CHAR, newbuf, ressize+10-buffer_len, &ressize);
105 
106 // Info("CopyFieldValue","After %d %s", ressize, fBuffer[field]);
107  }
108  }
109 }
110 
111 ////////////////////////////////////////////////////////////////////////////////
112 /// Get length in bytes of specified field.
113 
115 {
116  if ((field<0) || (field>=fFieldCount)) return 0;
117 
118  return fLengths[field];
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Get specified field from row (0 <= field < GetFieldCount()).
123 
124 const char *TODBCRow::GetField(Int_t field)
125 {
126  if ((field<0) || (field>=fFieldCount)) return 0;
127 
128  return fBuffer[field];
129 }
ClassImp(TODBCRow) TODBCRow
Single row of query result.
Definition: TODBCRow.cxx:17
#define buffer_len
const char Option_t
Definition: RtypesCore.h:62
SQLHSTMT fHstmt
Definition: TODBCRow.h:35
virtual ~TODBCRow()
Destroy row object.
Definition: TODBCRow.cxx:44
void Close(Option_t *opt="")
Close row.
Definition: TODBCRow.cxx:52
int Int_t
Definition: RtypesCore.h:41
Int_t fFieldCount
Definition: TODBCRow.h:36
ULong_t * fLengths
Definition: TODBCRow.h:38
char ** fBuffer
Definition: TODBCRow.h:37
void CopyFieldValue(Int_t field)
Extracts field value from statement.
Definition: TODBCRow.cxx:73
ULong_t GetFieldLength(Int_t field)
Get length in bytes of specified field.
Definition: TODBCRow.cxx:114
unsigned long ULong_t
Definition: RtypesCore.h:51
const char * GetField(Int_t field)
Get specified field from row (0 <= field < GetFieldCount()).
Definition: TODBCRow.cxx:124
const Int_t n
Definition: legend1.C:16