00001 #ifndef TDBIRESULT_H 00002 #define TDBIRESULT_H 00003 00004 /** 00005 * 00006 * 00007 * \class TDbiResultSet 00008 * 00009 * 00010 * \brief 00011 * <b>Concept</b> Abstract base class representing the Result of a single 00012 * database query. If query suceeded the Result will hold (own) a vector 00013 * of table row objects that correspond to result of the query. It will 00014 * also own a TDbiValidityRec that gives the range over which the result 00015 * is valid. 00016 * 00017 * \brief 00018 * <b>Purpose</b> To provide suitable objects to cache. TDbiResultSets can 00019 * be checked to see if they satisfy new queries. 00020 * 00021 * Contact: A.Finch@lancaster.ac.uk 00022 * 00023 * 00024 */ 00025 00026 #include <map> 00027 #include <string> 00028 using std::string; 00029 00030 #include "TDbi.hxx" 00031 #include "TDbiExceptionLog.hxx" 00032 #include "TDbiValidityRec.hxx" 00033 00034 typedef std::map<UInt_t,const TDbiTableRow*> IndexToRow_t; 00035 00036 00037 class TDbiBinaryFile; 00038 class TDbiResultKey; 00039 class TDbiResultSet; 00040 class TDbiInRowStream; 00041 class TDbiTableRow; 00042 00043 class TVldContext; 00044 00045 00046 TDbiBinaryFile& operator<<(TDbiBinaryFile& bf, const TDbiResultSet& res); 00047 TDbiBinaryFile& operator>>(TDbiBinaryFile& bf, TDbiResultSet& res); 00048 00049 class TDbiResultSet 00050 { 00051 00052 public: 00053 00054 // Constructors and destructors. 00055 TDbiResultSet(TDbiInRowStream* resultSet = 0, 00056 const TDbiValidityRec* vrec = 0, 00057 const string& sqlQualifiers = ""); 00058 virtual ~TDbiResultSet(); 00059 00060 // State testing member functions 00061 00062 virtual Bool_t CanReuse() const { return fCanReuse; } 00063 virtual Bool_t CanSave() const { return kTRUE; } 00064 virtual void Connect() const { ++fNumClients; } 00065 virtual TDbiResultKey* CreateKey() const = 0; 00066 virtual void Disconnect() const { --fNumClients; } 00067 const TDbiExceptionLog& GetExceptionLog() const { return fExceptionLog; } 00068 Int_t GetID() const { return fID; } 00069 virtual const TDbiResultKey* GetKey() const; 00070 virtual UInt_t GetNumAggregates() const =0; 00071 virtual UInt_t GetNumClients() const { 00072 return fNumClients; } 00073 virtual UInt_t GetNumRows() const =0; 00074 const string& GetSqlQualifiers() const { return fSqlQualifiers; } 00075 virtual const TDbiTableRow* GetTableRow(UInt_t rowNum) const =0; 00076 virtual const TDbiTableRow* GetTableRowByIndex(UInt_t index) const; 00077 virtual const TDbiValidityRec& GetValidityRec( 00078 const TDbiTableRow* /* row */ = 0) const { 00079 return GetValidityRecGlobal(); } 00080 virtual const TDbiValidityRec& GetValidityRecGlobal() const { 00081 return fEffVRec; } 00082 Bool_t IsExtendedContext() const { 00083 return this->GetSqlQualifiers() != ""; } 00084 virtual Bool_t Owns(const TDbiTableRow* /* row */) const { return kFALSE; } 00085 Bool_t ResultsFromDb() const { return fResultsFromDb; } 00086 virtual const string& TableName() const { return fTableName; } 00087 00088 // State changing member functions 00089 void CaptureExceptionLog(UInt_t startFrom); 00090 00091 /// Return true if no clients and unlikely to be reused. 00092 virtual Bool_t CanDelete(const TDbiResultSet* res = 0); 00093 00094 /// All TDbiResultSet classes can satisfy this type of primary 00095 /// query so impliment here. 00096 virtual Bool_t Satisfies(const TVldContext& vc, 00097 const TDbi::Task& task); 00098 /// Not all TDbiResultSet classes can satisfy these types of 00099 /// query so those that do must override. 00100 virtual Bool_t Satisfies(const string&) {return kFALSE;} 00101 virtual Bool_t Satisfies(const TDbiValidityRec&, 00102 const string& = "") {return kFALSE;} 00103 00104 /// Key handling 00105 virtual void GenerateKey(); 00106 00107 virtual void Streamer(TDbiBinaryFile& file); 00108 virtual void SetCanReuse(Bool_t reuse) { fCanReuse = reuse ; } 00109 00110 protected: 00111 void SetResultsFromDb() { fResultsFromDb = kTRUE; } 00112 00113 // State testing member functions 00114 00115 void BuildLookUpTable() const; 00116 Bool_t LookUpBuilt() const { return fIndexKeys.size() > 0; } 00117 00118 // State changing member functions. 00119 00120 virtual void SetTableName(const string& tableName) { 00121 fTableName = tableName; } 00122 virtual void SetValidityRec(const TDbiValidityRec& vRec) { 00123 fEffVRec = vRec; } 00124 00125 00126 private: 00127 00128 // Data members 00129 00130 /// Unique ID within the current job 00131 Int_t fID; 00132 00133 //// Set kTRUE if can be reused 00134 Bool_t fCanReuse; 00135 00136 //// Effective validity record 00137 TDbiValidityRec fEffVRec; 00138 00139 //// Look-up: Index -> TableRow 00140 mutable IndexToRow_t fIndexKeys; 00141 00142 //// Only non-zero for top-level result 00143 const TDbiResultKey* fKey; 00144 00145 /// True is at least part didn't come from cache. 00146 Bool_t fResultsFromDb; 00147 //// Number of clients 00148 mutable Int_t fNumClients; 00149 00150 //// Table name 00151 string fTableName; 00152 00153 /// Null unless Extended Context query in which case it contains:- 00154 /// context-sql;data-sql;fill-options 00155 string fSqlQualifiers; 00156 00157 /// Exception log produced when query was executed. 00158 TDbiExceptionLog fExceptionLog; 00159 00160 00161 /// Used to allocate unique ID within the current job 00162 static Int_t fgLastID; 00163 00164 00165 ClassDef(TDbiResultSet,0) //Abstract base representing query result 00166 00167 }; 00168 00169 00170 #endif // TDBIRESULT_H 00171