00001 // @(#)root/tree:$Id: TTreeResult.cxx 20882 2007-11-19 11:31:26Z rdm $ 00002 // Author: Fons Rademakers 30/11/99 00003 00004 /************************************************************************* 00005 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. * 00006 * All rights reserved. * 00007 * * 00008 * For the licensing terms see $ROOTSYS/LICENSE. * 00009 * For the list of contributors see $ROOTSYS/README/CREDITS. * 00010 *************************************************************************/ 00011 00012 ////////////////////////////////////////////////////////////////////////// 00013 // // 00014 // TTreeResult // 00015 // // 00016 // Class defining interface to a TTree query result with the same // 00017 // interface as for SQL databases. A TTreeResult is returned by // 00018 // TTree::Query() (actually TTreePlayer::Query()). // 00019 // // 00020 // Related classes are TTreeRow. // 00021 // // 00022 ////////////////////////////////////////////////////////////////////////// 00023 00024 #include "TTreeResult.h" 00025 #include "TTreeRow.h" 00026 #include "TString.h" 00027 #include "TObjArray.h" 00028 00029 00030 ClassImp(TTreeResult) 00031 00032 //______________________________________________________________________________ 00033 TTreeResult::TTreeResult() 00034 { 00035 // Create a query result object. 00036 00037 fColumnCount = 0; 00038 fRowCount = 0; 00039 fFields = 0; 00040 fResult = 0; 00041 fNextRow = 0; 00042 } 00043 00044 //______________________________________________________________________________ 00045 TTreeResult::TTreeResult(Int_t nfields) 00046 { 00047 // Create a query result object. 00048 00049 fColumnCount = nfields; 00050 fRowCount = 0; 00051 fFields = new TString [nfields]; 00052 fResult = new TObjArray; 00053 fNextRow = 0; 00054 } 00055 00056 //______________________________________________________________________________ 00057 TTreeResult::~TTreeResult() 00058 { 00059 // Cleanup result object. 00060 00061 if (fResult) 00062 Close(); 00063 00064 delete [] fFields; 00065 } 00066 00067 //______________________________________________________________________________ 00068 void TTreeResult::Close(Option_t *) 00069 { 00070 // Close query result. 00071 00072 if (!fResult) 00073 return; 00074 00075 fResult->Delete(); 00076 delete fResult; 00077 fResult = 0; 00078 fRowCount = 0; 00079 } 00080 00081 //______________________________________________________________________________ 00082 Bool_t TTreeResult::IsValid(Int_t field) 00083 { 00084 // Check if result set is open and field index within range. 00085 00086 if (!fResult) { 00087 Error("IsValid", "result set closed"); 00088 return kFALSE; 00089 } 00090 if (field < 0 || field >= GetFieldCount()) { 00091 Error("IsValid", "field index out of bounds"); 00092 return kFALSE; 00093 } 00094 return kTRUE; 00095 } 00096 00097 //______________________________________________________________________________ 00098 Int_t TTreeResult::GetFieldCount() 00099 { 00100 // Get number of fields in result. 00101 00102 if (!fResult) { 00103 Error("GetFieldCount", "result set closed"); 00104 return 0; 00105 } 00106 return fColumnCount; 00107 } 00108 00109 //______________________________________________________________________________ 00110 const char *TTreeResult::GetFieldName(Int_t field) 00111 { 00112 // Get name of specified field. 00113 00114 if (!IsValid(field)) 00115 return 0; 00116 00117 return fFields[field].Data(); 00118 } 00119 00120 //______________________________________________________________________________ 00121 TSQLRow *TTreeResult::Next() 00122 { 00123 // Get next query result row. The returned object must be 00124 // deleted by the user and becomes invalid when the result set is 00125 // closed or deleted. 00126 00127 if (!fResult) { 00128 Error("Next", "result set closed"); 00129 return 0; 00130 } 00131 00132 if (fNextRow >= fRowCount) 00133 return 0; 00134 else { 00135 TTreeRow *row = new TTreeRow((TTreeRow*)fResult->At(fNextRow)); 00136 fNextRow++; 00137 return row; 00138 } 00139 } 00140 00141 //______________________________________________________________________________ 00142 void TTreeResult::AddField(Int_t field, const char *fieldname) 00143 { 00144 // Add field name to result set. This is an internal method that is not 00145 // exported via the abstract interface and that should not be user called. 00146 00147 if (!IsValid(field)) 00148 return; 00149 00150 fFields[field] = fieldname; 00151 } 00152 00153 //______________________________________________________________________________ 00154 void TTreeResult::AddRow(TSQLRow *row) 00155 { 00156 // Adopt a row to result set. This is an internal method that is not 00157 // exported via the abstract interface and that should not be user called. 00158 00159 if (!fResult) { 00160 Error("AddRow", "result set closed"); 00161 return; 00162 } 00163 00164 fResult->Add(row); 00165 fRowCount++; 00166 }