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 "snprintf.h"
15#include <cstring>
16#include <ctime>
17
18#include <occi.h>
19
21
22////////////////////////////////////////////////////////////////////////////////
23/// Single row of query result.
24
25TOracleRow::TOracleRow(oracle::occi::ResultSet *rs, std::vector<oracle::occi::MetaData> *fieldMetaData)
26{
27 fResult = rs;
28 fFieldInfo = fieldMetaData;
29 fFieldCount = fFieldInfo->size();
30
31 fFieldsBuffer = nullptr;
32
33 GetRowData();
34}
35
36////////////////////////////////////////////////////////////////////////////////
37/// Destroy row object.
38
40{
41 Close();
42}
43
44////////////////////////////////////////////////////////////////////////////////
45/// Close row.
46
48{
49 if (fFieldsBuffer) {
50 for (int n=0;n<fFieldCount;n++)
51 if (fFieldsBuffer[n])
52 delete[] fFieldsBuffer[n];
53 delete [] fFieldsBuffer;
54 }
55
56 fFieldsBuffer = nullptr;
57 fFieldInfo = nullptr;
58 fFieldCount = 0;
59 fResult = nullptr;
60}
61
62////////////////////////////////////////////////////////////////////////////////
63/// Check if row is open and field index within range.
64
66{
67 if (!fResult) {
68 Error("IsValid", "row closed");
69 return kFALSE;
70 }
71 if (field < 0 || field >= (Int_t)fFieldInfo->size()) {
72 Error("IsValid", "field index out of bounds");
73 return kFALSE;
74 }
75 return kTRUE;
76}
77
78////////////////////////////////////////////////////////////////////////////////
79/// Get length in bytes of specified field.
80
82{
83 if (!IsValid(field) || fFieldInfo->size() <= 0)
84 return 0;
85
86 oracle::occi::MetaData fieldMD = (*fFieldInfo)[field];
87
88 return fieldMD.getInt(oracle::occi::MetaData::ATTR_DATA_SIZE);
89}
90
91////////////////////////////////////////////////////////////////////////////////
92
93const char* TOracleRow::GetField(Int_t field)
94{
95 if ((field<0) || (field>=fFieldCount)) {
96 Error("TOracleRow","GetField(): out-of-range or No RowData/ResultSet/MetaData");
97 return nullptr;
98 }
99
100 return fFieldsBuffer ? fFieldsBuffer[field] : nullptr;
101}
102
103////////////////////////////////////////////////////////////////////////////////
104
106{
107 if (!fResult || !fFieldInfo || (fFieldCount<=0)) return;
108
109 fFieldsBuffer = new char* [fFieldCount];
110 for (int n = 0; n < fFieldCount; n++)
111 fFieldsBuffer[n] = nullptr;
112
113 std::string res;
114
115 char str_number[200];
116
117 int fPrecision, fScale, fDataType;
118 double double_val;
119
120 try {
121
122 for (int field=0;field<fFieldCount;field++) {
123 if (fResult->isNull(field+1)) continue;
124
125 fDataType = (*fFieldInfo)[field].getInt(oracle::occi::MetaData::ATTR_DATA_TYPE);
126
127 switch (fDataType) {
128 case SQLT_NUM: //NUMBER
129 fPrecision = (*fFieldInfo)[field].getInt(oracle::occi::MetaData::ATTR_PRECISION);
130 fScale = (*fFieldInfo)[field].getInt(oracle::occi::MetaData::ATTR_SCALE);
131
132 if ((fScale == 0) || (fPrecision == 0)) {
133 res = fResult->getString(field+1);
134 } else {
135 double_val = fResult->getDouble(field+1);
136 snprintf(str_number, sizeof(str_number), TSQLServer::GetFloatFormat(), double_val);
137 res = str_number;
138 }
139 break;
140
141 case SQLT_CHR: // character string
142 case SQLT_VCS: // variable character string
143 case SQLT_AFC: // ansi fixed char
144 case SQLT_AVC: // ansi var char
145 res = fResult->getString(field+1);
146 break;
147 case SQLT_DAT: // Oracle native DATE type
148 res = (fResult->getDate(field+1)).toText(TOracleServer::GetDatimeFormat());
149 break;
150 case SQLT_TIMESTAMP: // TIMESTAMP
151 case SQLT_TIMESTAMP_TZ: // TIMESTAMP WITH TIMEZONE
152 case SQLT_TIMESTAMP_LTZ: // TIMESTAMP WITH LOCAL TIMEZONE
153 res = (fResult->getTimestamp(field+1)).toText(TOracleServer::GetDatimeFormat(), 0);
154 break;
155 case SQLT_IBFLOAT:
156 case SQLT_IBDOUBLE:
157 res = fResult->getString(field+1);
158 break;
159 default:
160 Error("GetRowData","Oracle type %d was not yet tested - please inform ROOT developers", fDataType);
161 continue;
162 }
163
164 int len = res.length();
165 if (len>0) {
166 fFieldsBuffer[field] = new char[len+1];
167 strcpy(fFieldsBuffer[field], res.c_str());
168 }
169 }
170
171 } catch (oracle::occi::SQLException &oraex) {
172 Error("GetRowData", "%s", (oraex.getMessage()).c_str());
173 }
174}
const Bool_t kFALSE
Definition: RtypesCore.h:101
const Bool_t kTRUE
Definition: RtypesCore.h:100
unsigned long ULong_t
Definition: RtypesCore.h:55
const char Option_t
Definition: RtypesCore.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
ClassImp(TOracleRow)
#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.
Definition: TOracleRow.cxx:39
ULong_t GetFieldLength(Int_t field) final
Get length in bytes of specified field.
Definition: TOracleRow.cxx:81
Bool_t IsValid(Int_t field)
Check if row is open and field index within range.
Definition: TOracleRow.cxx:65
Int_t fFieldCount
Definition: TOracleRow.h:31
TOracleRow(const TOracleRow &)=delete
std::vector< oracle::occi::MetaData > * fFieldInfo
Definition: TOracleRow.h:30
void GetRowData()
Definition: TOracleRow.cxx:105
void Close(Option_t *opt="") final
Close row.
Definition: TOracleRow.cxx:47
oracle::occi::ResultSet * fResult
Definition: TOracleRow.h:29
const char * GetField(Int_t field) final
Definition: TOracleRow.cxx:93
char ** fFieldsBuffer
Definition: TOracleRow.h:32
static const char * GetDatimeFormat()
return value of actual conversion format from timestamps or date to string
static const char * GetFloatFormat()
return current printf format for float/double members, default "%e"
Definition: TSQLServer.cxx:279
const Int_t n
Definition: legend1.C:16