Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

cursor.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  *   FILE
00004  *      pqxx/cursor.h
00005  *
00006  *   DESCRIPTION
00007  *      definition of the pqxx::Cursor class.
00008  *   pqxx::Cursor represents a database cursor.
00009  *
00010  * Copyright (c) 2001-2003, Jeroen T. Vermeulen <jtv@xs4all.nl>
00011  *
00012  * See COPYING for copyright license.  If you did not receive a file called
00013  * COPYING with this source code, please notify the distributor of this mistake,
00014  * or contact the author.
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 /* Methods tested in eg. self-test program test1 are marked with "//[t1]"
00026  */
00027 
00028 namespace pqxx
00029 {
00030 class result;
00031 
00032 
00033 // Work around bug in CodeWarrior 8.3
00034 #ifdef __MWERKS__
00035 #pragma defer_defarg_parsing on
00036 #endif
00037 
00038 
00040 
00067 class PQXX_LIBEXPORT Cursor
00068 {
00069 public:
00070   // TODO: Split Cursor into different levels of intelligence
00071   // TODO: Forward-only cursors
00072   // TODO: Iterator interface (random-access==absolute)
00073   typedef result::size_type size_type;
00074 
00075   enum pos { pos_unknown = -1, pos_start = 0 };
00076 
00078   struct PQXX_LIBEXPORT unknown_position : PGSTD::runtime_error
00079   {
00080     unknown_position(const PGSTD::string &cursorname) :
00081       PGSTD::runtime_error("Position for cursor '" + cursorname + "' "
00082                            "is unknown") 
00083     {
00084     }
00085   };
00086 
00087   // TODO: Allow usage of SCROLL ("DECLARE foo SCROLL CURSOR FOR ...")
00089 
00097   template<typename TRANSACTION> 
00098     Cursor(TRANSACTION &T,
00099            const char Query[], 
00100            const PGSTD::string &BaseName="cur",
00101            size_type Count=NEXT()) :                                    //[t3]
00102       m_Trans(T),
00103       m_Name(),
00104       m_Count(Count),
00105       m_Done(false),
00106       m_Pos(pos_start),
00107       m_Size(pos_unknown)
00108   {
00109     // Trigger build error if T has insufficient isolation level
00110     error_permitted_isolation_level(PQXX_TYPENAME TRANSACTION::isolation_tag());
00111     init(BaseName, Query);
00112   }
00113 
00115 
00145   template<typename TRANSACTION>
00146     Cursor(TRANSACTION &T,
00147            const result::field &Name,
00148            size_type Count=NEXT()) :                                    //[t45]
00149       m_Trans(T),
00150       m_Name(Name.c_str()),
00151       m_Count(Count),
00152       m_Done(false),
00153       m_Pos(pos_unknown),
00154       m_Size(pos_unknown)
00155   {
00156     // Trigger build error if T has insufficient isolation level
00157     error_permitted_isolation_level(PQXX_TYPENAME TRANSACTION::isolation_tag());
00158   }
00159 
00161   size_type SetCount(size_type);                                        //[t19]
00162 
00164 
00173   result Fetch(size_type Count);                                        //[t19]
00174 
00176 
00184   size_type Move(size_type Count);                                      //[t42]
00185 
00186   void MoveTo(size_type);                                               //[t44]
00187 
00189 
00193   static size_type ALL() throw ();                                      //[t3]
00194 
00196   static size_type NEXT() throw () { return 1; }                        //[t19]
00197 
00199   static size_type PRIOR() throw () { return -1; }                      //[t19]
00200 
00203 
00207   static size_type BACKWARD_ALL() throw ();                             //[t19]
00208 
00210 
00217   Cursor &operator>>(result &);                                         //[t3]
00218 
00220   operator bool() const throw () { return !m_Done; }                    //[t3]
00222   bool operator!() const throw () { return m_Done; }                    //[t3]
00223 
00225   Cursor &operator+=(size_type N) { Move(N); return *this;}             //[t19]
00227   Cursor &operator-=(size_type N) { Move(-N); return *this;}            //[t19]
00228 
00230 
00241   size_type size() const throw () { return m_Size; }                    //[t44]
00242 
00244 
00251   size_type Pos() const throw (unknown_position)                        //[t43]
00252   { if (m_Pos==pos_unknown) throw unknown_position(m_Name); return m_Pos; }
00253 
00254 
00255 private:
00256   static PGSTD::string OffsetString(size_type);
00257   void init(const PGSTD::string &BaseName, const char Query[]);
00258   PGSTD::string MakeFetchCmd(size_type) const;
00259   size_type NormalizedMove(size_type Intended, size_type Actual);
00260 
00261 #ifndef PQXX_WORKAROUND_VC7
00262 
00263 
00267   template<typename ISOLATIONTAG> 
00268     static inline void error_permitted_isolation_level(ISOLATIONTAG) throw();
00269 #else
00270   // Incorrect, but needed to compile with Visual C++ 7
00271   template<> static inline void 
00272     error_permitted_isolation_level(isolation_traits<serializable>) throw ();
00273 #endif
00274 
00275   transaction_base &m_Trans;
00276   PGSTD::string m_Name;
00277   size_type m_Count;
00278   bool m_Done;
00279   size_type m_Pos;
00280   size_type m_Size;
00281 
00282   // Not allowed:
00283   Cursor(const Cursor &);
00284   Cursor &operator=(const Cursor &);
00285 };
00286 
00287 template<> inline void 
00288 Cursor::error_permitted_isolation_level(isolation_traits<serializable>) throw ()
00289         {}
00290 
00291 }
00292 
00293 #endif
00294 

Generated on Fri Nov 14 19:37:18 2003 for libpqxx by doxygen 1.3.4