Logo ROOT   6.12/07
Reference Guide
TOracleRow.cxx
Go to the documentation of this file.
1 // @(#)root/oracle:$Id$
2 // Author: Yan Liu and Shaowen Wang 23/11/04
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, 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 "TOracleRow.h"
13 #include "TOracleServer.h"
14 #include <string.h>
15 #include <ctime>
16 
18 
19 using namespace std;
20 using namespace oracle::occi;
21 
22 
23 ////////////////////////////////////////////////////////////////////////////////
24 /// Single row of query result.
25 
26 TOracleRow::TOracleRow(ResultSet *rs, vector<MetaData> *fieldMetaData)
27 {
28  fResult = rs;
29  fFieldInfo = fieldMetaData;
30  fFieldCount = fFieldInfo->size();
31 
32  fFieldsBuffer = 0;
33 
34  GetRowData();
35 }
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// Destroy row object.
39 
41 {
42  Close();
43 }
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 /// Close row.
47 
49 {
50  if (fFieldsBuffer!=0) {
51  for (int n=0;n<fFieldCount;n++)
52  if (fFieldsBuffer[n]) delete[] fFieldsBuffer[n];
53  delete[] fFieldsBuffer;
54  }
55 
56  fFieldInfo = 0;
57  fFieldCount = 0;
58  fResult = 0;
59 }
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// Check if row is open and field index within range.
63 
65 {
66  if (!fResult) {
67  Error("IsValid", "row closed");
68  return kFALSE;
69  }
70  if (field < 0 || field >= (Int_t)fFieldInfo->size()) {
71  Error("IsValid", "field index out of bounds");
72  return kFALSE;
73  }
74  return kTRUE;
75 }
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// Get length in bytes of specified field.
79 
81 {
82  if (!IsValid(field) || fFieldInfo->size() <= 0)
83  return 0;
84 
85  MetaData fieldMD = (*fFieldInfo)[field];
86 
87  return fieldMD.getInt(MetaData::ATTR_DATA_SIZE);
88 }
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 
92 const char* TOracleRow::GetField(Int_t field)
93 {
94  if ((field<0) || (field>=fFieldCount)) {
95  Error("TOracleRow","GetField(): out-of-range or No RowData/ResultSet/MetaData");
96  return 0;
97  }
98 
99  return fFieldsBuffer ? fFieldsBuffer[field] : 0;
100 }
101 
102 ////////////////////////////////////////////////////////////////////////////////
103 
105 {
106  if (!fResult || !fFieldInfo || (fFieldCount<=0)) return;
107 
108  fFieldsBuffer = new char* [fFieldCount];
109  for (int n=0;n<fFieldCount;n++)
110  fFieldsBuffer[n] = 0;
111 
112  std::string res;
113 
114  char str_number[200];
115 
116  int fPrecision, fScale, fDataType;
117  double double_val;
118 
119  try {
120 
121  for (int field=0;field<fFieldCount;field++) {
122  if (fResult->isNull(field+1)) continue;
123 
124  fDataType = (*fFieldInfo)[field].getInt(MetaData::ATTR_DATA_TYPE);
125 
126  switch (fDataType) {
127  case SQLT_NUM: //NUMBER
128  fPrecision = (*fFieldInfo)[field].getInt(MetaData::ATTR_PRECISION);
129  fScale = (*fFieldInfo)[field].getInt(MetaData::ATTR_SCALE);
130 
131  if ((fScale == 0) || (fPrecision == 0)) {
132  res = fResult->getString(field+1);
133  } else {
134  double_val = fResult->getDouble(field+1);
135  snprintf(str_number, sizeof(str_number), TSQLServer::GetFloatFormat(), double_val);
136  res = str_number;
137  }
138  break;
139 
140  case SQLT_CHR: // character string
141  case SQLT_VCS: // variable character string
142  case SQLT_AFC: // ansi fixed char
143  case SQLT_AVC: // ansi var char
144  res = fResult->getString(field+1);
145  break;
146  case SQLT_DAT: // Oracle native DATE type
147  res = (fResult->getDate(field+1)).toText(TOracleServer::GetDatimeFormat());
148  break;
149  case SQLT_TIMESTAMP: // TIMESTAMP
150  case SQLT_TIMESTAMP_TZ: // TIMESTAMP WITH TIMEZONE
151  case SQLT_TIMESTAMP_LTZ: // TIMESTAMP WITH LOCAL TIMEZONE
152  res = (fResult->getTimestamp(field+1)).toText(TOracleServer::GetDatimeFormat(), 0);
153  break;
154  case SQLT_IBFLOAT:
155  case SQLT_IBDOUBLE:
156  res = fResult->getString(field+1);
157  break;
158  default:
159  Error("GetRowData","Oracle type %d was not yet tested - please inform ROOT developers", fDataType);
160  continue;
161  }
162 
163  int len = res.length();
164  if (len>0) {
165  fFieldsBuffer[field] = new char[len+1];
166  strcpy(fFieldsBuffer[field], res.c_str());
167  }
168  }
169 
170  } catch (SQLException &oraex) {
171  Error("GetRowData", "%s", (oraex.getMessage()).c_str());
172  }
173 }
const char Option_t
Definition: RtypesCore.h:62
~TOracleRow()
Destroy row object.
Definition: TOracleRow.cxx:40
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
STL namespace.
static const char * GetFloatFormat()
return current printf format for float/double members, default "%e"
Definition: TSQLServer.cxx:269
void Error(const char *location, const char *msgfmt,...)
TOracleRow(const TOracleRow &)
void GetRowData()
Definition: TOracleRow.cxx:104
ULong_t GetFieldLength(Int_t field)
Get length in bytes of specified field.
Definition: TOracleRow.cxx:80
Bool_t IsValid(Int_t field)
Check if row is open and field index within range.
Definition: TOracleRow.cxx:64
const Bool_t kFALSE
Definition: RtypesCore.h:88
const char * GetField(Int_t field)
Definition: TOracleRow.cxx:92
#define ClassImp(name)
Definition: Rtypes.h:359
unsigned long ULong_t
Definition: RtypesCore.h:51
void Close(Option_t *opt="")
Close row.
Definition: TOracleRow.cxx:48
#define snprintf
Definition: civetweb.c:822
static const char * GetDatimeFormat()
return value of actul convertion format from timestamps or date to string
const Bool_t kTRUE
Definition: RtypesCore.h:87
const Int_t n
Definition: legend1.C:16