00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef PQXX_CACHEDRESULT_H
00019 #define PQXX_CACHEDRESULT_H
00020
00021 #include <map>
00022
00023 #include "pqxx/cursor.h"
00024 #include "pqxx/result.h"
00025
00026 namespace pqxx
00027 {
00028
00050 class PQXX_LIBEXPORT cachedresult
00051 {
00052 public:
00053 typedef result::size_type size_type;
00054 typedef size_type blocknum;
00055
00057 typedef result::tuple tuple;
00058
00060 typedef tuple Tuple;
00061
00073 template<typename TRANSACTION> explicit
00074 cachedresult(TRANSACTION &T,
00075 const char Query[],
00076 const PGSTD::string &BaseName="query",
00077 size_type Granularity=100) :
00078 m_Granularity(Granularity),
00079 m_Cache(),
00080 m_Cursor(T, Query, BaseName, Granularity),
00081 m_EmptyResult(),
00082 m_HaveEmpty(false)
00083 {
00084
00085 error_permitted_isolation_level(PQXX_TYPENAME TRANSACTION::isolation_tag());
00086 init();
00087 }
00088
00089
00091
00099 const tuple operator[](size_type i) const
00100 { return GetBlock(BlockFor(i))[Offset(i)]; }
00101
00103
00114 const tuple at(size_type i) const
00115 { return GetBlock(BlockFor(i)).at(Offset(i)); }
00116
00118 size_type size() const;
00119
00121 bool empty() const;
00122
00123 private:
00124 typedef Cursor::pos pos;
00125
00126 #ifndef PQXX_WORKAROUND_VC7
00127
00128
00133 template<typename ISOLATIONTAG>
00134 static inline void error_permitted_isolation_level(ISOLATIONTAG) throw();
00135 #else
00136
00137 template<> static inline void
00138 error_permitted_isolation_level(isolation_traits<serializable>) throw ();
00139 #endif
00140
00141 void init();
00142
00143 blocknum BlockFor(size_type Row) const throw ()
00144 { return Row / m_Granularity; }
00145 size_type Offset(size_type Row) const throw ()
00146 { return Row % m_Granularity; }
00147 Cursor::size_type FirstRowOf(blocknum Block) const throw ()
00148 { return Block*m_Granularity; }
00149
00150 void MoveTo(blocknum) const;
00151
00153 const result &Fetch() const;
00154
00155 const result &GetBlock(blocknum b) const
00156 {
00157 CacheMap::const_iterator i = m_Cache.find(b);
00158 if (i != m_Cache.end()) return i->second;
00159
00160 MoveTo(b);
00161 return Fetch();
00162 }
00163
00165 size_type m_Granularity;
00166
00167 typedef PGSTD::map<blocknum, result> CacheMap;
00168 mutable CacheMap m_Cache;
00169
00170 mutable Cursor m_Cursor;
00171 mutable result m_EmptyResult;
00172 mutable bool m_HaveEmpty;
00173
00174
00175 cachedresult();
00176 cachedresult(const cachedresult &);
00177 cachedresult &operator=(const cachedresult &);
00178 };
00179
00181 typedef cachedresult CachedResult;
00182
00183 template<> inline void
00184 cachedresult::error_permitted_isolation_level(isolation_traits<serializable>)
00185 throw () {}
00186
00187 }
00188
00189 #endif
00190