Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
15Class defining interface to a TTree query result with the same
16interface as for SQL databases. A TTreeResult is returned by
17TTree::Query() (actually TTreePlayer::Query()).
18
19Related 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 = nullptr;
37 fResult = nullptr;
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 = nullptr;
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 false;
86 }
87 if (field < 0 || field >= GetFieldCount()) {
88 Error("IsValid", "field index out of bounds");
89 return false;
90 }
91 return true;
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 nullptr;
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 nullptr;
127 }
128
129 if (fNextRow >= fRowCount)
130 return nullptr;
131 else {
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
142void 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}
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:377
An array of TObjects.
Definition TObjArray.h:31
void Delete(Option_t *option="") override
Remove all objects from the array AND delete all heap based objects.
TObject * At(Int_t idx) const override
Definition TObjArray.h:164
void Add(TObject *obj) override
Definition TObjArray.h:68
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:976
Int_t fRowCount
Definition TSQLResult.h:34
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
Class defining interface to a TTree query result with the same interface as for SQL databases.
Definition TTreeResult.h:34
void Close(Option_t *option="") override
Close query result.
Int_t fColumnCount
number of columns in result
Definition TTreeResult.h:39
~TTreeResult() override
Cleanup result object.
TTreeResult()
Create a query result object.
TSQLRow * Next() override
Get next query result row.
bool IsValid(Int_t field)
Check if result set is open and field index within range.
Int_t fNextRow
row iterator
Definition TTreeResult.h:42
const char * GetFieldName(Int_t field) override
Get name of specified field.
Int_t GetFieldCount() override
Get number of fields in result.
TString * fFields
[fColumnCount] array containing field strings
Definition TTreeResult.h:40
void AddRow(TSQLRow *row)
Adopt a row to result set.
TObjArray * fResult
query result (TTreeRow objects)
Definition TTreeResult.h:41
void AddField(Int_t field, const char *fieldname)
Add field name to result set.
Class defining interface to a row of a TTree query result.
Definition TTreeRow.h:29