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
27
28////////////////////////////////////////////////////////////////////////////////
29/// Create a query result object.
30
32{
33 fColumnCount = 0;
34 fRowCount = 0;
35 fFields = nullptr;
36 fResult = nullptr;
37 fNextRow = 0;
38}
39
40////////////////////////////////////////////////////////////////////////////////
41/// Create a query result object.
42
44{
45 fColumnCount = nfields;
46 fRowCount = 0;
47 fFields = new TString [nfields];
48 fResult = new TObjArray;
49 fNextRow = 0;
50}
51
52////////////////////////////////////////////////////////////////////////////////
53/// Cleanup result object.
54
56{
57 if (fResult)
58 Close();
59
60 delete [] fFields;
61}
62
63////////////////////////////////////////////////////////////////////////////////
64/// Close query result.
65
67{
68 if (!fResult)
69 return;
70
71 fResult->Delete();
72 delete fResult;
73 fResult = nullptr;
74 fRowCount = 0;
75}
76
77////////////////////////////////////////////////////////////////////////////////
78/// Check if result set is open and field index within range.
79
81{
82 if (!fResult) {
83 Error("IsValid", "result set closed");
84 return false;
85 }
86 if (field < 0 || field >= GetFieldCount()) {
87 Error("IsValid", "field index out of bounds");
88 return false;
89 }
90 return true;
91}
92
93////////////////////////////////////////////////////////////////////////////////
94/// Get number of fields in result.
95
97{
98 if (!fResult) {
99 Error("GetFieldCount", "result set closed");
100 return 0;
101 }
102 return fColumnCount;
103}
104
105////////////////////////////////////////////////////////////////////////////////
106/// Get name of specified field.
107
109{
110 if (!IsValid(field))
111 return nullptr;
112
113 return fFields[field].Data();
114}
115
116////////////////////////////////////////////////////////////////////////////////
117/// Get next query result row. The returned object must be
118/// deleted by the user and becomes invalid when the result set is
119/// closed or deleted.
120
122{
123 if (!fResult) {
124 Error("Next", "result set closed");
125 return nullptr;
126 }
127
128 if (fNextRow >= fRowCount)
129 return nullptr;
130 else {
131 TTreeRow *row = new TTreeRow((TTreeRow*)fResult->At(fNextRow));
132 fNextRow++;
133 return row;
134 }
135}
136
137////////////////////////////////////////////////////////////////////////////////
138/// Add field name to result set. This is an internal method that is not
139/// exported via the abstract interface and that should not be user called.
140
141void TTreeResult::AddField(Int_t field, const char *fieldname)
142{
143 if (!IsValid(field))
144 return;
145
146 fFields[field] = fieldname;
147}
148
149////////////////////////////////////////////////////////////////////////////////
150/// Adopt a row to result set. This is an internal method that is not
151/// exported via the abstract interface and that should not be user called.
152
154{
155 if (!fResult) {
156 Error("AddRow", "result set closed");
157 return;
158 }
159
160 fResult->Add(row);
161 fRowCount++;
162}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
const char Option_t
Option string (const char).
Definition RtypesCore.h:80
An array of TObjects.
Definition TObjArray.h:31
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1098
Int_t fRowCount
Definition TSQLResult.h:34
Basic string class.
Definition TString.h:138
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