Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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#include "strlcpy.h"
16
18
19////////////////////////////////////////////////////////////////////////////////
20/// Single row of query result.
21
22TODBCRow::TODBCRow(SQLHSTMT stmt, Int_t fieldcount)
23{
24 fHstmt = stmt;
25 fFieldCount = fieldcount;
26
27 fBuffer = nullptr;
28 fLengths = nullptr;
29
30 if (fFieldCount>0) {
31 fBuffer = new char*[fFieldCount];
33 for (Int_t n = 0; n < fFieldCount; n++) {
34 fBuffer[n] = nullptr;
35 fLengths[n] = 0;
37 }
38 }
39}
40
41////////////////////////////////////////////////////////////////////////////////
42/// Destroy row object.
43
45{
46 Close();
47}
48
49////////////////////////////////////////////////////////////////////////////////
50/// Close row.
51
53{
54 if (fBuffer) {
55 for (Int_t n = 0; n < fFieldCount; n++)
56 delete[] fBuffer[n];
57 delete[] fBuffer;
58 fBuffer = nullptr;
59 }
60
61 if (fLengths) {
62 delete[] fLengths;
63 fLengths = nullptr;
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] = nullptr;
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, nullptr, 0, nullptr);
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))
117 return 0;
118
119 return fLengths[field];
120}
121
122////////////////////////////////////////////////////////////////////////////////
123/// Get specified field from row (0 <= field < GetFieldCount()).
124
125const char *TODBCRow::GetField(Int_t field)
126{
127 if ((field < 0) || (field >= fFieldCount))
128 return nullptr;
129
130 return fBuffer[field];
131}
unsigned long ULong_t
Definition RtypesCore.h:55
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:382
#define buffer_len
TODBCRow(const TODBCRow &)=delete
char ** fBuffer
Definition TODBCRow.h:33
void Close(Option_t *opt="") final
Close row.
Definition TODBCRow.cxx:52
const char * GetField(Int_t field) final
Get specified field from row (0 <= field < GetFieldCount()).
Definition TODBCRow.cxx:125
ULong_t GetFieldLength(Int_t field) final
Get length in bytes of specified field.
Definition TODBCRow.cxx:114
Int_t fFieldCount
Definition TODBCRow.h:32
SQLHSTMT fHstmt
Definition TODBCRow.h:31
ULong_t * fLengths
Definition TODBCRow.h:34
virtual ~TODBCRow()
Destroy row object.
Definition TODBCRow.cxx:44
void CopyFieldValue(Int_t field)
Extracts field value from statement.
Definition TODBCRow.cxx:73
const Int_t n
Definition legend1.C:16