ROOT  6.06/09
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 using namespace std;
17 using namespace oracle::occi;
18 
20 
21 ////////////////////////////////////////////////////////////////////////////////
22 /// Oracle query result.
23 
24 void 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 
52 TOracleResult::TOracleResult(Connection *conn, Statement *stmt)
53 {
54  fConn = conn;
55  fResult = 0;
56  fStmt = 0;
57  fPool = 0;
58  fRowCount = 0;
59  fFieldInfo = 0;
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 
71 TOracleResult::TOracleResult(Connection *conn, const char *tableName)
72 {
73  fResult = 0;
74  fStmt = 0;
75  fConn = 0;
76  fPool = 0;
77  fRowCount = 0;
78  fFieldInfo = 0;
79  fResultType = 0;
80  fUpdateCount = 0;
81 
82  if (!tableName || !conn) {
83  Error("TOracleResult", "construction: empty input parameter");
84  } else {
85  MetaData connMD = conn->getMetaData(tableName, MetaData::PTYPE_TABLE);
86  fFieldInfo = new vector<MetaData>(connMD.getVector(MetaData::ATTR_LIST_COLUMNS));
87  fFieldCount = fFieldInfo->size();
88  fResultType = 2; // indicates that this is just an table metainfo
89  }
90 }
91 
92 ////////////////////////////////////////////////////////////////////////////////
93 /// Cleanup Oracle query result.
94 
96 {
97  Close();
98 }
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// Close query result.
102 
104 {
105  if (fConn && fStmt) {
106  if (fResult) fStmt->closeResultSet(fResult);
107  fConn->terminateStatement(fStmt);
108  }
109 
110  if (fPool) {
111  fPool->Delete();
112  delete fPool;
113  }
114 
115  if (fFieldInfo)
116  delete fFieldInfo;
117 
118  fResultType = 0;
119 
120  fStmt = 0;
121  fResult = 0;
122  fFieldInfo = 0;
123  fPool = 0;
124 }
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 /// Check if result set is open and field index within range.
128 
130 {
131  if (field < 0 || field >= fFieldCount) {
132  Error("IsValid", "field index out of bounds");
133  return kFALSE;
134  }
135  return kTRUE;
136 }
137 
138 ////////////////////////////////////////////////////////////////////////////////
139 /// Get number of fields in result.
140 
142 {
143  return fFieldCount;
144 }
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 /// Get name of specified field.
148 
150 {
151  if (!IsValid(field))
152  return 0;
153  fNameBuffer = (*fFieldInfo)[field].getString(MetaData::ATTR_NAME);
154  return fNameBuffer.c_str();
155 }
156 
157 ////////////////////////////////////////////////////////////////////////////////
158 /// Get next query result row. The returned object must be
159 /// deleted by the user.
160 
162 {
163  if (!fResult || (fResultType!=1)) return 0;
164 
165  if (fPool!=0) {
166  TSQLRow* row = (TSQLRow*) fPool->First();
167  if (row!=0) fPool->Remove(row);
168  return row;
169  }
170 
171  // if select query,
172  try {
173  if (fResult->next() != oracle::occi::ResultSet::END_OF_FETCH) {
174  fRowCount++;
175  return new TOracleRow(fResult, fFieldInfo);
176  } else
177  return 0;
178  } catch (SQLException &oraex) {
179  Error("Next", "%s", (oraex.getMessage()).c_str());
180  MakeZombie();
181  }
182  return 0;
183 }
184 
185 ////////////////////////////////////////////////////////////////////////////////
186 
188 {
189  if (!fResult) return 0;
190 
191  if (fPool==0) ((TOracleResult*) this)->ProducePool();
192 
193  return fRowCount;
194 }
195 
196 ////////////////////////////////////////////////////////////////////////////////
197 
199 {
200  if (fPool!=0) return;
201 
202  TList* pool = new TList;
203  TSQLRow* res = 0;
204  while ((res = Next()) !=0) {
205  pool->Add(res);
206  }
207 
208  fPool = pool;
209 }
TOracleResult(const TOracleResult &)
const char Option_t
Definition: RtypesCore.h:62
const char * GetFieldName(Int_t field)
Get name of specified field.
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
void Close(Option_t *opt="")
Close query result.
Bool_t IsValid(Int_t field)
Check if result set is open and field index within range.
ClassImp(TOracleResult) void TOracleResult
Oracle query result.
void Error(const char *location, const char *msgfmt,...)
A doubly linked list.
Definition: TList.h:47
TSQLRow * Next()
Get next query result row.
typedef void((*Func_t)())
virtual void Add(TObject *obj)
Definition: TList.h:81
virtual Int_t GetRowCount() const
Int_t GetFieldCount()
Get number of fields in result.
~TOracleResult()
Cleanup Oracle query result.
const Bool_t kTRUE
Definition: Rtypes.h:91