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