Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

transactionitf.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  *   FILE
00004  *      pqxx/transactionitf.h
00005  *
00006  *   DESCRIPTION
00007  *      common code and definitions for the transaction classes.
00008  *   pqxx::TransactionItf defines the interface for any abstract class that
00009  *   represents a database transaction
00010  *
00011  * Copyright (c) 2001-2003, Jeroen T. Vermeulen <jtv@xs4all.nl>
00012  *
00013  *-------------------------------------------------------------------------
00014  */
00015 #ifndef PQXX_TRANSACTIONITF_H
00016 #define PQXX_TRANSACTIONITF_H
00017 
00018 
00019 /* End-user programs need not include this file, unless they define their own
00020  * transaction classes.  This is not something the typical program should want
00021  * to do.
00022  *
00023  * However, reading this file is worthwhile because it defines the public
00024  * interface for the available transaction classes such as Transaction and 
00025  * NonTransaction.
00026  */
00027 
00028 
00029 #include "pqxx/connectionitf.h"
00030 #include "pqxx/result.h"
00031 
00032 /* Methods tested in eg. self-test program test1 are marked with "//[t1]"
00033  */
00034 
00035 
00036 namespace pqxx
00037 {
00038 class ConnectionItf;    // See pqxx/connectionitf.h
00039 class Result;           // See pqxx/result.h
00040 class TableStream;      // See pqxx/tablestream.h
00041 
00042 
00043 template<> inline PGSTD::string Classname(const TableStream *) 
00044 { 
00045   return "TableStream"; 
00046 }
00047 
00048 
00050 
00058 class PQXX_LIBEXPORT TransactionItf
00059 {
00060 public:
00061   virtual ~TransactionItf() =0;                                         //[t1]
00062 
00064 
00076   void Commit();                                                        //[t1]
00077 
00079 
00082   void Abort();                                                         //[t10]
00083 
00085 
00089   Result Exec(const char Query[], 
00090               const PGSTD::string &Desc=PGSTD::string());               //[t1]
00091 
00093 
00100   Result Exec(const PGSTD::string &Query,
00101               const PGSTD::string &Desc=PGSTD::string())                //[t2]
00102         { return Exec(Query.c_str(), Desc); }
00103 
00105   void ProcessNotice(const char Msg[]) { m_Conn.ProcessNotice(Msg); }   //[t14]
00107   void ProcessNotice(const PGSTD::string &Msg)                          //[t14]
00108         { m_Conn.ProcessNotice(Msg); }
00109 
00110   PGSTD::string Name() const { return m_Name; }                         //[t1]
00111 
00113   ConnectionItf &Conn() const { return m_Conn; }                        //[t4]
00114 
00115 protected:
00118   explicit TransactionItf(ConnectionItf &, 
00119                           const PGSTD::string &TName=PGSTD::string());
00120 
00123   void Begin();
00124 
00126   void End() throw ();
00127 
00129   virtual void DoBegin() =0;
00130   virtual Result DoExec(const char Query[]) =0;
00131   virtual void DoCommit() =0;
00132   virtual void DoAbort() =0;
00133 
00134   // For use by implementing class:
00135 
00137   Result DirectExec(const char C[], int Retries, const char OnReconnect[]);
00138  
00139 private:
00140   /* A Transaction goes through the following stages in its lifecycle:
00141    *  - nascent: the transaction hasn't actually begun yet.  If our connection 
00142    *    fails at this stage, it may recover and the Transaction can attempt to
00143    *    establish itself again.
00144    *  - active: the transaction has begun.  Since no commit command has been 
00145    *    issued, abortion is implicit if the connection fails now.
00146    *  - aborted: an abort has been issued; the transaction is terminated and 
00147    *    its changes to the database rolled back.  It will accept no further 
00148    *    commands.
00149    *  - committed: the transaction has completed successfully, meaning that a 
00150    *    commit has been issued.  No further commands are accepted.
00151    *  - in_doubt: the connection was lost at the exact wrong time, and there is
00152    *    no way of telling whether the transaction was committed or aborted.
00153    *
00154    * Checking and maintaining state machine logic is the responsibility of the 
00155    * base class (ie., this one).
00156    */
00157   enum Status 
00158   { 
00159     st_nascent, 
00160     st_active, 
00161     st_aborted, 
00162     st_committed,
00163     st_in_doubt
00164   };
00165 
00166 
00167   friend class Cursor;
00168   int GetUniqueCursorNum() { return m_UniqueCursorNum++; }
00169   void MakeEmpty(Result &R) const { m_Conn.MakeEmpty(R); }
00170 
00171   friend class TableStream;
00172   void RegisterStream(const TableStream *);
00173   void UnregisterStream(const TableStream *) throw ();
00174   void EndCopy() { m_Conn.EndCopy(); }
00175   friend class TableReader;
00176   void BeginCopyRead(const PGSTD::string &Table) 
00177         { m_Conn.BeginCopyRead(Table); }
00178   bool ReadCopyLine(PGSTD::string &L) { return m_Conn.ReadCopyLine(L); }
00179   friend class TableWriter;
00180   void BeginCopyWrite(const PGSTD::string &Table) 
00181         { m_Conn.BeginCopyWrite(Table); }
00182   void WriteCopyLine(const PGSTD::string &L) { m_Conn.WriteCopyLine(L); }
00183 
00184   ConnectionItf &m_Conn;
00185 
00186   PGSTD::string m_Name;
00187   int m_UniqueCursorNum;
00188   Unique<TableStream> m_Stream;
00189   Status m_Status;
00190   bool m_Registered;
00191 
00192   // Not allowed:
00193   TransactionItf();
00194   TransactionItf(const TransactionItf &);
00195   TransactionItf &operator=(const TransactionItf &);
00196 };
00197 
00198 
00199 }
00200 
00201 #endif
00202 

Generated on Mon Mar 31 11:29:45 2003 for libpqxx by doxygen1.3-rc3