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-2002, 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/connection.h"
00030 #include "pqxx/util.h"
00031 
00032 /* Methods tested in eg. self-test program test1 are marked with "//[t1]"
00033  */
00034 
00035 
00036 namespace pqxx
00037 {
00038 class Connection;       // See pqxx/connection.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 
00054 class PQXX_LIBEXPORT TransactionItf
00055 {
00056 public:
00057   virtual ~TransactionItf() =0;                                         //[t1]
00058 
00059   void Commit();                                                        //[t1]
00060   void Abort();                                                         //[t10]
00061 
00063   Result Exec(const char[]);                                            //[t1]
00064 
00065   void ProcessNotice(const char Msg[]) { m_Conn.ProcessNotice(Msg); }   //[t1]
00066   void ProcessNotice(PGSTD::string Msg) { m_Conn.ProcessNotice(Msg); }  //[t1]
00067 
00068   PGSTD::string Name() const { return m_Name; }                         //[t1]
00069 
00070 protected:
00073   explicit TransactionItf(Connection &, 
00074                           PGSTD::string Name=PGSTD::string());
00075 
00078   void Begin();
00079 
00081   void End() throw ();
00082 
00084   virtual void DoBegin() =0;
00085   virtual Result DoExec(const char C[]) =0;
00086   virtual void DoCommit() =0;
00087   virtual void DoAbort() =0;
00088 
00089   // For use by implementing class:
00090 
00092   Result DirectExec(const char C[], int Retries, const char OnReconnect[]);
00093   Connection &Conn() const { return m_Conn; }
00094 
00095  
00096 private:
00097   /* A Transaction goes through the following stages in its lifecycle:
00098    *  - nascent: the transaction hasn't actually begun yet.  If our connection 
00099    *    fails at this stage, the Connection may recover and the Transaction can 
00100    *    attempt to establish itself again.
00101    *  - active: the transaction has begun.  Since no commit command has been 
00102    *    issued, abortion is implicit if the connection fails now.
00103    *  - aborted: an abort has been issued; the transaction is terminated and 
00104    *    its changes to the database rolled back.  It will accept no further 
00105    *    commands.
00106    *  - committed: the transaction has completed successfully, meaning that a 
00107    *    commit has been issued.  No further commands are accepted.
00108    *  - in_doubt: the connection was lost at the exact wrong time, and there is
00109    *    no way of telling whether the transaction was committed or aborted.
00110    *
00111    * Checking and maintaining state machine logic is the responsibility of the 
00112    * base class (ie., this one).
00113    */
00114   enum Status 
00115   { 
00116     st_nascent, 
00117     st_active, 
00118     st_aborted, 
00119     st_committed,
00120     st_in_doubt
00121   };
00122 
00123 
00124   friend class Cursor;
00125   int GetUniqueCursorNum() { return m_UniqueCursorNum++; }
00126   void MakeEmpty(Result &R) const { m_Conn.MakeEmpty(R); }
00127 
00128   friend class TableStream;
00129   void RegisterStream(const TableStream *);
00130   void UnregisterStream(const TableStream *) throw ();
00131   void EndCopy() { m_Conn.EndCopy(); }
00132   friend class TableReader;
00133   void BeginCopyRead(PGSTD::string Table) { m_Conn.BeginCopyRead(Table); }
00134   bool ReadCopyLine(PGSTD::string &L) { return m_Conn.ReadCopyLine(L); }
00135   friend class TableWriter;
00136   void BeginCopyWrite(PGSTD::string Table) {m_Conn.BeginCopyWrite(Table);}
00137   void WriteCopyLine(PGSTD::string L) { m_Conn.WriteCopyLine(L); }
00138 
00139   Connection &m_Conn;
00140 
00141   PGSTD::string m_Name;
00142   int m_UniqueCursorNum;
00143   Unique<TableStream> m_Stream;
00144   Status m_Status;
00145   bool m_Registered;
00146 
00147   // Not allowed:
00148   TransactionItf();
00149   TransactionItf(const TransactionItf &);
00150   TransactionItf &operator=(const TransactionItf &);
00151 };
00152 
00153 
00155 
00161 class PQXX_LIBEXPORT in_doubt_error : public PGSTD::runtime_error
00162 {
00163 public:
00164   explicit in_doubt_error(const PGSTD::string &whatarg) : 
00165     PGSTD::runtime_error(whatarg) {}
00166 };
00167 
00168 }
00169 
00170 #endif
00171 

Generated on Sun Dec 1 20:39:57 2002 for libpqxx by doxygen1.3-rc1