Logo ROOT   6.14/05
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  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 = 0;
122  fResult = 0;
123  fFieldInfo = 0;
124  fPool = 0;
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 0;
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 0;
184 }
185 
186 ////////////////////////////////////////////////////////////////////////////////
187 
189 {
190  if (!fResult) return 0;
191 
192  if (fPool==0) ((TOracleResult*) this)->ProducePool();
193 
194  return fRowCount;
195 }
196 
197 ////////////////////////////////////////////////////////////////////////////////
198 
200 {
201  if (fPool!=0) return;
202 
203  TList* pool = new TList;
204  TSQLRow* res = 0;
205  while ((res = Next()) !=0) {
206  pool->Add(res);
207  }
208 
209  fPool = pool;
210 }
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
STL namespace.
void Close(Option_t *opt="")
Close query result.
virtual Int_t GetRowCount() const
void initResultSet(oracle::occi::Statement *stmt)
Oracle query result.
Bool_t IsValid(Int_t field)
Check if result set is open and field index within range.
void Error(const char *location, const char *msgfmt,...)
A doubly linked list.
Definition: TList.h:44
const Bool_t kFALSE
Definition: RtypesCore.h:88
#define ClassImp(name)
Definition: Rtypes.h:359
TSQLRow * Next()
Get next query result row.
virtual void Add(TObject *obj)
Definition: TList.h:87
Int_t GetFieldCount()
Get number of fields in result.
~TOracleResult()
Cleanup Oracle query result.
const Bool_t kTRUE
Definition: RtypesCore.h:87