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