ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 Class defining interface to a TTree query result with the same
14 interface as for SQL databases. A TTreeResult is returned by
15 TTree::Query() (actually TTreePlayer::Query()).
16 
17 Related classes are TTreeRow.
18 */
19 
20 #include "TTreeResult.h"
21 #include "TTreeRow.h"
22 #include "TString.h"
23 #include "TObjArray.h"
24 
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// Create a query result object.
29 
31 {
32  fColumnCount = 0;
33  fRowCount = 0;
34  fFields = 0;
35  fResult = 0;
36  fNextRow = 0;
37 }
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 /// Create a query result object.
41 
43 {
44  fColumnCount = nfields;
45  fRowCount = 0;
46  fFields = new TString [nfields];
47  fResult = new TObjArray;
48  fNextRow = 0;
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Cleanup result object.
53 
55 {
56  if (fResult)
57  Close();
58 
59  delete [] fFields;
60 }
61 
62 ////////////////////////////////////////////////////////////////////////////////
63 /// Close query result.
64 
66 {
67  if (!fResult)
68  return;
69 
70  fResult->Delete();
71  delete fResult;
72  fResult = 0;
73  fRowCount = 0;
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// Check if result set is open and field index within range.
78 
80 {
81  if (!fResult) {
82  Error("IsValid", "result set closed");
83  return kFALSE;
84  }
85  if (field < 0 || field >= GetFieldCount()) {
86  Error("IsValid", "field index out of bounds");
87  return kFALSE;
88  }
89  return kTRUE;
90 }
91 
92 ////////////////////////////////////////////////////////////////////////////////
93 /// Get number of fields in result.
94 
96 {
97  if (!fResult) {
98  Error("GetFieldCount", "result set closed");
99  return 0;
100  }
101  return fColumnCount;
102 }
103 
104 ////////////////////////////////////////////////////////////////////////////////
105 /// Get name of specified field.
106 
108 {
109  if (!IsValid(field))
110  return 0;
111 
112  return fFields[field].Data();
113 }
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 /// Get next query result row. The returned object must be
117 /// deleted by the user and becomes invalid when the result set is
118 /// closed or deleted.
119 
121 {
122  if (!fResult) {
123  Error("Next", "result set closed");
124  return 0;
125  }
126 
127  if (fNextRow >= fRowCount)
128  return 0;
129  else {
131  fNextRow++;
132  return row;
133  }
134 }
135 
136 ////////////////////////////////////////////////////////////////////////////////
137 /// Add field name to result set. This is an internal method that is not
138 /// exported via the abstract interface and that should not be user called.
139 
140 void TTreeResult::AddField(Int_t field, const char *fieldname)
141 {
142  if (!IsValid(field))
143  return;
144 
145  fFields[field] = fieldname;
146 }
147 
148 ////////////////////////////////////////////////////////////////////////////////
149 /// Adopt a row to result set. This is an internal method that is not
150 /// exported via the abstract interface and that should not be user called.
151 
153 {
154  if (!fResult) {
155  Error("AddRow", "result set closed");
156  return;
157  }
158 
159  fResult->Add(row);
160  fRowCount++;
161 }
tuple row
Definition: mrt.py:26
An array of TObjects.
Definition: TObjArray.h:39
void AddRow(TSQLRow *row)
Adopt a row to result set.
ClassImp(TTreeResult) TTreeResult
Create a query result object.
Definition: TTreeResult.cxx:25
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
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:79
const char * Data() const
Definition: TString.h:349
Class defining interface to a row of a TTree query result.
Definition: TTreeRow.h:31
TObjArray * fResult
Definition: TTreeResult.h:43
Int_t GetFieldCount()
Get number of fields in result.
Definition: TTreeResult.cxx:95
TString * fFields
Definition: TTreeResult.h:42
TSQLRow * Next()
Get next query result row.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
virtual ~TTreeResult()
Cleanup result object.
Definition: TTreeResult.cxx:54
Int_t fNextRow
Definition: TTreeResult.h:44
Class defining interface to a TTree query result with the same interface as for SQL databases...
Definition: TTreeResult.h:36
const char * GetFieldName(Int_t field)
Get name of specified field.
Int_t fRowCount
Definition: TSQLResult.h:37
void Close(Option_t *option="")
Close query result.
Definition: TTreeResult.cxx:65
void Add(TObject *obj)
Definition: TObjArray.h:75
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
const Bool_t kTRUE
Definition: Rtypes.h:91
void AddField(Int_t field, const char *fieldname)
Add field name to result set.