# 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-2006, 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_OLD_CURSOR_H
    00019 #define PQXX_OLD_CURSOR_H
    00020
    00021 #include "pqxx/compiler-public.hxx"
    00022 #include "pqxx/compiler-internal-pre.hxx"
    00023
    00024 #include "pqxx/result"
    00025 #include "pqxx/transaction_base"
    00026 #include "pqxx/util"
    00027
    00028 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
    00029  */
    00030
    00031 namespace pqxx
    00032 {
    00033 class result;
    00034
    00035
    00036 // Work around bug in CodeWarrior 8.3
    00037 #ifdef __MWERKS__
    00038 #pragma defer_defarg_parsing on
    00039 #endif
    00040
    00041
    00043
    00073 class PQXX_LIBEXPORT Cursor
    00074 {
    00075   enum { dist_next=1 };
    00076
    00077 public:
    00078   typedef result::size_type size_type;
    00079   typedef result::difference_type difference_type;
    00080
    00081   enum pos { pos_unknown = -1, pos_start = 0 };
    00082
    00084   struct PQXX_LIBEXPORT unknown_position : PGSTD::runtime_error
    00085   {
    00086     unknown_position(const PGSTD::string &cursorname) :
    00087       PGSTD::runtime_error("Position for cursor '" + cursorname + "' "
    00088                            "is unknown")
    00089     {
    00090     }
    00091   };
    00092
    00094
    00102   template<typename TRANSACTION>
    00103     Cursor(TRANSACTION &T,
    00104            const char Query[],
    00105            const PGSTD::string &BaseName="cur",
    00106            difference_type Count=dist_next) :                           //[t3]
    00107       m_Trans(T),
    00108       m_Name(T.conn().adorn_name(BaseName)),
    00109       m_Count(Count),
    00110       m_Done(false),
    00111       m_Pos(pos_start),
    00112       m_Size(pos_unknown)
    00113   {
    00114     // Trigger build error if T has insufficient isolation level
    00115     error_permitted_isolation_level(PQXX_TYPENAME TRANSACTION::isolation_tag());
    00116     init(Query);
    00117   }
    00118
    00120
    00150   template<typename TRANSACTION>
    00151     Cursor(TRANSACTION &T,
    00152            const result::field &Name,
    00153            difference_type Count=dist_next) :                           //[t45]
    00154       m_Trans(T),
    00155       m_Name(Name.c_str()),
    00156       m_Count(Count),
    00157       m_Done(false),
    00158       m_Pos(size_type(pos_unknown)),
    00159       m_Size(size_type(pos_unknown))
    00160   {
    00161     // Trigger build error if T has insufficient isolation level
    00162     error_permitted_isolation_level(PQXX_TYPENAME TRANSACTION::isolation_tag());
    00163   }
    00164
    00166   difference_type SetCount(difference_type);                            //[t19]
    00167
    00169
    00178   result Fetch(difference_type Count);                                  //[t19]
    00179
    00181
    00189   difference_type Move(difference_type Count);                          //[t42]
    00190
    00191   void MoveTo(size_type);                                               //[t44]
    00192
    00194
    00198   static difference_type ALL() throw ();                                //[t3]
    00199
    00201   static difference_type NEXT() throw () { return dist_next; }          //[t19]
    00202
    00204   static difference_type PRIOR() throw () { return -1; }                //[t19]
    00205
    00208
    00212   static difference_type BACKWARD_ALL() throw ();                       //[t19]
    00213
    00215
    00222   Cursor &operator>>(result &);                                         //[t3]
    00223
    00225   operator bool() const throw () { return !m_Done; }                    //[t3]
    00227   bool operator!() const throw () { return m_Done; }                    //[t3]
    00228
    00230   Cursor &operator+=(difference_type N) { Move(N); return *this;}       //[t19]
    00232   Cursor &operator-=(difference_type N) { Move(-N); return *this;}      //[t19]
    00233
    00235
    00246   size_type size() const throw () { return m_Size; }                    //[t44]
    00247
    00249
    00256   size_type Pos() const throw (unknown_position)                        //[t43]
    00257   {
    00258     if (m_Pos==size_type(pos_unknown)) throw unknown_position(m_Name);
    00259     return m_Pos;
    00260   }
    00261
    00262
    00263 private:
    00264   static PGSTD::string OffsetString(difference_type);
    00265   void init(const char Query[]);
    00266   PGSTD::string MakeFetchCmd(difference_type) const;
    00267   difference_type NormalizedMove(difference_type Intended,
    00268       difference_type Actual);
    00269
    00271
    00275   static void
    00276     error_permitted_isolation_level(isolation_traits<serializable>) throw () {}
    00277
    00278   transaction_base &m_Trans;
    00279   PGSTD::string m_Name;
    00280   difference_type m_Count;
    00281   bool m_Done;
    00282   size_type m_Pos;
    00283   difference_type m_Size;
    00284
    00285   // Not allowed:
    00286   Cursor(const Cursor &);
    00287   Cursor &operator=(const Cursor &);
    00288 };
    00289
    00290
    00291 } // namespace pqxx
    00292
    00293 #include "pqxx/compiler-internal-post.hxx"
    00294
    00295 #endif
    00296