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