Logo ROOT  
Reference Guide
TOracleResult.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 "TOracleResult.h"
13#include "TOracleRow.h"
14#include "TList.h"
15
16using namespace std;
17using namespace oracle::occi;
18
20
21////////////////////////////////////////////////////////////////////////////////
22/// Oracle query result.
23
24void TOracleResult::initResultSet(Statement *stmt)
25{
26 if (!stmt) {
27 Error("initResultSet", "construction: empty statement");
28 } else {
29 try {
30 fStmt = stmt;
31 if (stmt->status() == Statement::RESULT_SET_AVAILABLE) {
32 fResultType = 1;
33 fResult = stmt->getResultSet();
34 fFieldInfo = (fResult==0) ? 0 : new vector<MetaData>(fResult->getColumnListMetaData());
35 fFieldCount = (fFieldInfo==0) ? 0 : fFieldInfo->size();
36 } else if (stmt->status() == Statement::UPDATE_COUNT_AVAILABLE) {
37 fResultType = 3; // this is update_count_available
38 fResult = 0;
39 fFieldInfo = 0;
40 fFieldCount = 0;
41 fUpdateCount = stmt->getUpdateCount();
42 }
43 } catch (SQLException &oraex) {
44 Error("initResultSet", "%s", (oraex.getMessage()).c_str());
45 MakeZombie();
46 }
47 }
48}
49
50////////////////////////////////////////////////////////////////////////////////
51
52TOracleResult::TOracleResult(Connection *conn, Statement *stmt)
53{
54 fConn = conn;
55 fResult = nullptr;
56 fStmt = nullptr;
57 fPool = nullptr;
58 fRowCount = 0;
59 fFieldInfo = nullptr;
60 fResultType = 0;
61 fUpdateCount = 0;
62
63 initResultSet(stmt);
64
65 if (fResult) ProducePool();
66}
67
68////////////////////////////////////////////////////////////////////////////////
69/// This construction func is only used to get table metainfo.
70
71TOracleResult::TOracleResult(Connection *conn, const char *tableName)
72{
73 fResult = nullptr;
74 fStmt = nullptr;
75 fConn = nullptr;
76 fPool = nullptr;
77 fRowCount = 0;
78 fFieldInfo = nullptr;
79 fResultType = 0;
80 fUpdateCount = 0;
81 fFieldCount = 0;
82
83 if (!tableName || !conn) {
84 Error("TOracleResult", "construction: empty input parameter");
85 } else {
86 MetaData connMD = conn->getMetaData(tableName, MetaData::PTYPE_TABLE);
87 fFieldInfo = new vector<MetaData>(connMD.getVector(MetaData::ATTR_LIST_COLUMNS));
88 fFieldCount = fFieldInfo->size();
89 fResultType = 2; // indicates that this is just an table metainfo
90 }
91}
92
93////////////////////////////////////////////////////////////////////////////////
94/// Cleanup Oracle query result.
95
97{
98 Close();
99}
100
101////////////////////////////////////////////////////////////////////////////////
102/// Close query result.
103
105{
106 if (fConn && fStmt) {
107 if (fResult) fStmt->closeResultSet(fResult);
108 fConn->terminateStatement(fStmt);
109 }
110
111 if (fPool) {
112 fPool->Delete();
113 delete fPool;
114 }
115
116 if (fFieldInfo)
117 delete fFieldInfo;
118
119 fResultType = 0;
120
121 fStmt = nullptr;
122 fResult = nullptr;
123 fFieldInfo = nullptr;
124 fPool = nullptr;
125}
126
127////////////////////////////////////////////////////////////////////////////////
128/// Check if result set is open and field index within range.
129
131{
132 if (field < 0 || field >= fFieldCount) {
133 Error("IsValid", "field index out of bounds");
134 return kFALSE;
135 }
136 return kTRUE;
137}
138
139////////////////////////////////////////////////////////////////////////////////
140/// Get number of fields in result.
141
143{
144 return fFieldCount;
145}
146
147////////////////////////////////////////////////////////////////////////////////
148/// Get name of specified field.
149
151{
152 if (!IsValid(field))
153 return nullptr;
154 fNameBuffer = (*fFieldInfo)[field].getString(MetaData::ATTR_NAME);
155 return fNameBuffer.c_str();
156}
157
158////////////////////////////////////////////////////////////////////////////////
159/// Get next query result row. The returned object must be
160/// deleted by the user.
161
163{
164 if (!fResult || (fResultType!=1)) return 0;
165
166 if (fPool!=0) {
167 TSQLRow* row = (TSQLRow*) fPool->First();
168 if (row!=0) fPool->Remove(row);
169 return row;
170 }
171
172 // if select query,
173 try {
174 if (fResult->next() != oracle::occi::ResultSet::END_OF_FETCH) {
175 fRowCount++;
176 return new TOracleRow(fResult, fFieldInfo);
177 } else
178 return 0;
179 } catch (SQLException &oraex) {
180 Error("Next", "%s", (oraex.getMessage()).c_str());
181 MakeZombie();
182 }
183 return nullptr;
184}
185
186////////////////////////////////////////////////////////////////////////////////
187
189{
190 if (!fResult) return 0;
191
192 if (!fPool) ((TOracleResult*) this)->ProducePool();
193
194 return fRowCount;
195}
196
197////////////////////////////////////////////////////////////////////////////////
198
200{
201 if (fPool) return;
202
203 TList* pool = new TList;
204 TSQLRow* res = nullptr;
205 while ((res = Next()) != nullptr) {
206 pool->Add(res);
207 }
208
209 fPool = pool;
210}
const Bool_t kFALSE
Definition: RtypesCore.h:90
const Bool_t kTRUE
Definition: RtypesCore.h:89
const char Option_t
Definition: RtypesCore.h:64
#define ClassImp(name)
Definition: Rtypes.h:361
A doubly linked list.
Definition: TList.h:44
virtual void Add(TObject *obj)
Definition: TList.h:87
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:821
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:469
virtual TObject * First() const
Return the first object in the list. Returns 0 when list is empty.
Definition: TList.cxx:658
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:891
void MakeZombie()
Definition: TObject.h:49
UInt_t fUpdateCount
Definition: TOracleResult.h:39
Bool_t IsValid(Int_t field)
Check if result set is open and field index within range.
Int_t GetRowCount() const final
TSQLRow * Next() final
Get next query result row.
oracle::occi::Statement * fStmt
Definition: TOracleResult.h:35
std::vector< oracle::occi::MetaData > * fFieldInfo
Definition: TOracleResult.h:37
const char * GetFieldName(Int_t field) final
Get name of specified field.
void initResultSet(oracle::occi::Statement *stmt)
Oracle query result.
void Close(Option_t *opt="") final
Close query result.
~TOracleResult()
Cleanup Oracle query result.
Int_t GetFieldCount() final
Get number of fields in result.
oracle::occi::Connection * fConn
Definition: TOracleResult.h:34
std::string fNameBuffer
Definition: TOracleResult.h:42
oracle::occi::ResultSet * fResult
Definition: TOracleResult.h:36
TOracleResult(const TOracleResult &)
Int_t fRowCount
Definition: TSQLResult.h:35