ROOT  6.06/09
Reference Guide
TSapDBRow.cxx
Go to the documentation of this file.
1 // @(#)root/sapdb:$Id$
2 // Author: Mark Hemberger & Fons Rademakers 03/08/2001
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2001, 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 "TString.h"
13 #include "TSapDBRow.h"
14 
15 
17 
18 ////////////////////////////////////////////////////////////////////////////////
19 /// Single row of query result.
20 
21 TSapDBRow::TSapDBRow(SQLHSTMT result, Int_t nfields)
22 {
23  fResult = result;
24  fFieldCount = nfields;
25  fFieldLength = 0;
26  fFieldValue = 0;
27 }
28 
29 ////////////////////////////////////////////////////////////////////////////////
30 /// Destroy row object.
31 
33 {
34  if (fResult)
35  Close();
36 }
37 
38 ////////////////////////////////////////////////////////////////////////////////
39 /// Close row.
40 
42 {
43  delete [] fFieldLength;
44  delete [] fFieldValue;
45  fResult = 0;
46  fFieldCount = 0;
47  fFieldLength = 0;
48  fFieldValue = 0;
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Check if row is open and field index within range.
53 
55 {
56  if (field < 0 || field >= fFieldCount) {
57  Error("IsValid", "field index out of bounds");
58  return kFALSE;
59  }
60 
61  return kTRUE;
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Get length in bytes of specified field.
66 
68 {
69  if (!IsValid(field))
70  return 0;
71 
72  if (!fFieldLength) {
74  for (int i = 0; i < fFieldCount; i++)
75  fFieldLength[i] = 0;
76  }
77 
78  if (fFieldLength[field])
79  return fFieldLength[field];
80 
81  SQLUSMALLINT columnNumber;
82  SQLCHAR columnName[256];
83  SQLSMALLINT bufferLength = 256;
84  SQLSMALLINT nameLength;
85  SQLSMALLINT dataType;
86  SQLULEN columnSize;
87  SQLSMALLINT decimalDigits;
88  SQLSMALLINT nullable;
89 
90  columnNumber = field + 1;
91  if (SQLDescribeCol(fResult, columnNumber, columnName, bufferLength,
92  &nameLength, &dataType, &columnSize, &decimalDigits,
93  &nullable) == SQL_SUCCESS) {
94  fFieldLength[field] = columnSize;
95  return columnSize;
96  } else {
97  Error("GetFieldLength", "cannot get field length");
98  return 0;
99  }
100 }
101 
102 ////////////////////////////////////////////////////////////////////////////////
103 /// Get specified field from row (0 <= field < GetFieldCount()).
104 
105 const char *TSapDBRow::GetField(Int_t field)
106 {
107  if (!IsValid(field))
108  return 0;
109 
110  if (!fFieldValue)
112 
113  if (!fFieldValue[field].IsNull())
114  return fFieldValue[field];
115 
116  SQLUSMALLINT columnNumber;
117  SQLCHAR columnName[256];
118  SQLSMALLINT bufferLength = 256;
119  SQLSMALLINT nameLength;
120  SQLSMALLINT dataType;
121  SQLULEN columnSize;
122  SQLSMALLINT decimalDigits;
123  SQLSMALLINT nullable;
124 
125  columnNumber = field + 1;
126  RETCODE rc;
127  rc = SQLDescribeCol(fResult, columnNumber, columnName, bufferLength,
128  &nameLength, &dataType, &columnSize, &decimalDigits,
129  &nullable);
130  if (rc != SQL_SUCCESS) {
131  Error("TSapDBRow", "error in getting description");
132  return 0;
133  }
134 
135  if (columnSize > 4000) {
136  Error("TSapDBRow", "column size too large for current implementation.");
137  return 0;
138  }
139 
140  SQLLEN strLenOrIndPtr;
141  SQLPOINTER targetValuePtr[4000];
142  bufferLength = sizeof(targetValuePtr);
143 
144  if (SQLGetData(fResult, columnNumber, SQL_C_DEFAULT, targetValuePtr,
145  bufferLength, &strLenOrIndPtr) != SQL_SUCCESS) {
146  Error("TSapDBRow", "error in getting data");
147  return 0;
148  }
149 
150  char fieldstr[4001];
151 
152  switch (dataType) {
153  case SQL_CHAR:
154  case SQL_VARCHAR:
155  case SQL_LONGVARCHAR:
156  // not yet supported...
157  //case SQL_WCHAR:
158  //case SQL_WVARCHAR:
159  //case SQL_WLONGVARCHAR:
160  snprintf(fieldstr,4001, "%-*.*s", (int)columnSize, (int)columnSize,
161  (char*) targetValuePtr);
162  break;
163  case SQL_TINYINT:
164  case SQL_SMALLINT:
165  case SQL_INTEGER:
166  case SQL_BIGINT:
167  snprintf(fieldstr,4001, "%-*ld", (int)columnSize, *(long int*)(targetValuePtr));
168  break;
169  case SQL_DECIMAL:
170  case SQL_NUMERIC:
171  case SQL_REAL:
172  case SQL_FLOAT:
173  snprintf(fieldstr,4001, "%-*.2f", (int)columnSize, *(float*)(targetValuePtr));
174  break;
175  case SQL_DOUBLE:
176  snprintf(fieldstr,4001, "%-*.2f", (int)columnSize, *(double*)(targetValuePtr));
177  break;
178  case SQL_BIT:
179  case SQL_BINARY:
180  case SQL_VARBINARY:
181  case SQL_LONGVARBINARY:
182  case SQL_TYPE_DATE:
183  case SQL_TYPE_TIME:
184  case SQL_TYPE_TIMESTAMP:
185  default:
186  snprintf(fieldstr,4001, "%-*.*s", (int)columnSize, (int)columnSize,
187  (char*)targetValuePtr);
188  break;
189  }
190 
191  fFieldValue[field] = fieldstr;
192 
193  return fFieldValue[field];
194 }
TString * fFieldValue
Definition: TSapDBRow.h:43
ClassImp(TSapDBRow) TSapDBRow
Single row of query result.
Definition: TSapDBRow.cxx:16
Int_t fFieldCount
Definition: TSapDBRow.h:41
const char Option_t
Definition: RtypesCore.h:62
void Close(Option_t *opt="")
Close row.
Definition: TSapDBRow.cxx:41
const char * GetField(Int_t field)
Get specified field from row (0 <= field < GetFieldCount()).
Definition: TSapDBRow.cxx:105
Basic string class.
Definition: TString.h:137
ULong_t GetFieldLength(Int_t field)
Get length in bytes of specified field.
Definition: TSapDBRow.cxx:67
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Bool_t IsValid(Int_t field)
Check if row is open and field index within range.
Definition: TSapDBRow.cxx:54
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
ULong_t * fFieldLength
Definition: TSapDBRow.h:42
SQLHSTMT fResult
Definition: TSapDBRow.h:40
unsigned long ULong_t
Definition: RtypesCore.h:51
double result[121]
const Bool_t kTRUE
Definition: Rtypes.h:91
~TSapDBRow()
Destroy row object.
Definition: TSapDBRow.cxx:32