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