Logo ROOT   6.08/07
Reference Guide
TTreeResult.cxx
Go to the documentation of this file.
1 // @(#)root/tree:$Id$
2 // Author: Fons Rademakers 30/11/99
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, 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 /** \class TTreeResult
13 \ingroup tree
14 
15 Class defining interface to a TTree query result with the same
16 interface as for SQL databases. A TTreeResult is returned by
17 TTree::Query() (actually TTreePlayer::Query()).
18 
19 Related classes are TTreeRow.
20 */
21 
22 #include "TTreeResult.h"
23 #include "TTreeRow.h"
24 #include "TString.h"
25 #include "TObjArray.h"
26 
28 
29 ////////////////////////////////////////////////////////////////////////////////
30 /// Create a query result object.
31 
33 {
34  fColumnCount = 0;
35  fRowCount = 0;
36  fFields = 0;
37  fResult = 0;
38  fNextRow = 0;
39 }
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 /// Create a query result object.
43 
45 {
46  fColumnCount = nfields;
47  fRowCount = 0;
48  fFields = new TString [nfields];
49  fResult = new TObjArray;
50  fNextRow = 0;
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// Cleanup result object.
55 
57 {
58  if (fResult)
59  Close();
60 
61  delete [] fFields;
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Close query result.
66 
68 {
69  if (!fResult)
70  return;
71 
72  fResult->Delete();
73  delete fResult;
74  fResult = 0;
75  fRowCount = 0;
76 }
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 /// Check if result set is open and field index within range.
80 
82 {
83  if (!fResult) {
84  Error("IsValid", "result set closed");
85  return kFALSE;
86  }
87  if (field < 0 || field >= GetFieldCount()) {
88  Error("IsValid", "field index out of bounds");
89  return kFALSE;
90  }
91  return kTRUE;
92 }
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Get number of fields in result.
96 
98 {
99  if (!fResult) {
100  Error("GetFieldCount", "result set closed");
101  return 0;
102  }
103  return fColumnCount;
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// Get name of specified field.
108 
110 {
111  if (!IsValid(field))
112  return 0;
113 
114  return fFields[field].Data();
115 }
116 
117 ////////////////////////////////////////////////////////////////////////////////
118 /// Get next query result row. The returned object must be
119 /// deleted by the user and becomes invalid when the result set is
120 /// closed or deleted.
121 
123 {
124  if (!fResult) {
125  Error("Next", "result set closed");
126  return 0;
127  }
128 
129  if (fNextRow >= fRowCount)
130  return 0;
131  else {
132  TTreeRow *row = new TTreeRow((TTreeRow*)fResult->At(fNextRow));
133  fNextRow++;
134  return row;
135  }
136 }
137 
138 ////////////////////////////////////////////////////////////////////////////////
139 /// Add field name to result set. This is an internal method that is not
140 /// exported via the abstract interface and that should not be user called.
141 
142 void TTreeResult::AddField(Int_t field, const char *fieldname)
143 {
144  if (!IsValid(field))
145  return;
146 
147  fFields[field] = fieldname;
148 }
149 
150 ////////////////////////////////////////////////////////////////////////////////
151 /// Adopt a row to result set. This is an internal method that is not
152 /// exported via the abstract interface and that should not be user called.
153 
155 {
156  if (!fResult) {
157  Error("AddRow", "result set closed");
158  return;
159  }
160 
161  fResult->Add(row);
162  fRowCount++;
163 }
An array of TObjects.
Definition: TObjArray.h:39
void AddRow(TSQLRow *row)
Adopt a row to result set.
const char Option_t
Definition: RtypesCore.h:62
virtual void Delete(Option_t *option="")
Remove all objects from the array AND delete all heap based objects.
Definition: TObjArray.cxx:329
Int_t fColumnCount
number of columns in result
Definition: TTreeResult.h:41
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Bool_t IsValid(Int_t field)
Check if result set is open and field index within range.
Definition: TTreeResult.cxx:81
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
Class defining interface to a row of a TTree query result.
Definition: TTreeRow.h:31
TObjArray * fResult
query result (TTreeRow objects)
Definition: TTreeResult.h:43
Int_t GetFieldCount()
Get number of fields in result.
Definition: TTreeResult.cxx:97
TString * fFields
[fColumnCount] array containing field strings
Definition: TTreeResult.h:42
TTreeResult()
Create a query result object.
Definition: TTreeResult.cxx:32
TSQLRow * Next()
Get next query result row.
virtual ~TTreeResult()
Cleanup result object.
Definition: TTreeResult.cxx:56
Int_t fNextRow
row iterator
Definition: TTreeResult.h:44
Class defining interface to a TTree query result with the same interface as for SQL databases...
Definition: TTreeResult.h:36
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:925
const char * GetFieldName(Int_t field)
Get name of specified field.
#define ClassImp(name)
Definition: Rtypes.h:279
Int_t fRowCount
Definition: TSQLResult.h:37
void Close(Option_t *option="")
Close query result.
Definition: TTreeResult.cxx:67
void Add(TObject *obj)
Definition: TObjArray.h:75
const Bool_t kTRUE
Definition: Rtypes.h:91
void AddField(Int_t field, const char *fieldname)
Add field name to result set.
const char * Data() const
Definition: TString.h:349