00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef PQXX_CURSOR_H
00019 #define PQXX_CURSOR_H
00020
00021 #include "pqxx/result.h"
00022 #include "pqxx/transaction_base.h"
00023 #include "pqxx/util.h"
00024
00025
00026
00027
00028 namespace pqxx
00029 {
00030 class result;
00031
00032
00033
00034 #ifdef __MWERKS__
00035 #pragma defer_defarg_parsing on
00036 #endif
00037
00038
00040
00066 class PQXX_LIBEXPORT Cursor
00067 {
00068 public:
00069
00070
00071
00072 typedef result::size_type size_type;
00073
00074 enum pos { pos_unknown = -1, pos_start = 0 };
00075
00077 struct PQXX_LIBEXPORT unknown_position : PGSTD::runtime_error
00078 {
00079 unknown_position(const PGSTD::string &cursorname) :
00080 PGSTD::runtime_error("Position for cursor '" + cursorname + "' "
00081 "is unknown")
00082 {
00083 }
00084 };
00085
00086
00088
00096 template<typename TRANSACTION>
00097 Cursor(TRANSACTION &T,
00098 const char Query[],
00099 const PGSTD::string &BaseName="cur",
00100 size_type Count=NEXT()) :
00101 m_Trans(T),
00102 m_Name(),
00103 m_Count(Count),
00104 m_Done(false),
00105 m_Pos(pos_start),
00106 m_Size(pos_unknown)
00107 {
00108
00109 error_permitted_isolation_level(typename TRANSACTION::isolation_tag());
00110 init(BaseName, Query);
00111 }
00112
00114
00144 template<typename TRANSACTION>
00145 Cursor(TRANSACTION &T,
00146 const result::field &Name,
00147 size_type Count=NEXT()) :
00148 m_Trans(T),
00149 m_Name(Name.c_str()),
00150 m_Count(Count),
00151 m_Done(false),
00152 m_Pos(pos_unknown),
00153 m_Size(pos_unknown)
00154 {
00155
00156 error_permitted_isolation_level(typename TRANSACTION::isolation_tag());
00157 }
00158
00160 size_type SetCount(size_type);
00161
00163
00172 result Fetch(size_type Count);
00173
00175
00183 size_type Move(size_type Count);
00184
00185 void MoveTo(size_type);
00186
00188
00192 static size_type ALL() throw ()
00193 { return PGSTD::numeric_limits<result::size_type>::max(); }
00194
00196 static size_type NEXT() throw () { return 1; }
00197
00199 static size_type PRIOR() throw () { return -1; }
00200
00203
00207 static size_type BACKWARD_ALL() throw ()
00208 { return PGSTD::numeric_limits<result::size_type>::min() + 1; }
00209
00211
00218 Cursor &operator>>(result &);
00219
00221 operator bool() const throw () { return !m_Done; }
00223 bool operator!() const throw () { return m_Done; }
00224
00226 Cursor &operator+=(size_type N) { Move(N); return *this;}
00228 Cursor &operator-=(size_type N) { Move(-N); return *this;}
00229
00231
00242 size_type size() const throw () { return m_Size; }
00243
00245
00252 size_type Pos() const throw (unknown_position)
00253 { if (m_Pos==pos_unknown) throw unknown_position(m_Name); return m_Pos; }
00254
00255
00256 private:
00257 static PGSTD::string OffsetString(size_type);
00258 void init(const PGSTD::string &BaseName, const char Query[]);
00259 PGSTD::string MakeFetchCmd(size_type) const;
00260 size_type NormalizedMove(size_type Intended, size_type Actual);
00261
00263
00267 template<typename ISOLATIONTAG>
00268 static inline void error_permitted_isolation_level(ISOLATIONTAG) throw();
00269
00270 transaction_base &m_Trans;
00271 PGSTD::string m_Name;
00272 size_type m_Count;
00273 bool m_Done;
00274 size_type m_Pos;
00275 size_type m_Size;
00276
00277
00278 Cursor(const Cursor &);
00279 Cursor &operator=(const Cursor &);
00280 };
00281
00282 template<> inline void
00283 Cursor::error_permitted_isolation_level(isolation_traits<serializable>) throw ()
00284 {}
00285
00286 }
00287
00288 #endif
00289