Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files 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 \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 
27 ClassImp(TTreeResult);
28 
29 ////////////////////////////////////////////////////////////////////////////////
30 /// Create a query result object.
31 
32 TTreeResult::TTreeResult()
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 
44 TTreeResult::TTreeResult(Int_t nfields)
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 
56 TTreeResult::~TTreeResult()
57 {
58  if (fResult)
59  Close();
60 
61  delete [] fFields;
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Close query result.
66 
67 void TTreeResult::Close(Option_t *)
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 
81 Bool_t TTreeResult::IsValid(Int_t field)
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 
97 Int_t TTreeResult::GetFieldCount()
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 
109 const char *TTreeResult::GetFieldName(Int_t field)
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 
122 TSQLRow *TTreeResult::Next()
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 
154 void TTreeResult::AddRow(TSQLRow *row)
155 {
156  if (!fResult) {
157  Error("AddRow", "result set closed");
158  return;
159  }
160 
161  fResult->Add(row);
162  fRowCount++;
163 }