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 
00054 class PQXX_LIBEXPORT TransactionItf
00055 {
00056 public:
00057   virtual ~TransactionItf() =0;                                         //[t1]
00058 
00059   void Commit();                                                        //[t1]
00060   void Abort();                                                         //[t10]
00061 
00063 
00067   Result Exec(const char Query[], 
00068               const PGSTD::string &Desc=PGSTD::string());               //[t1]
00069 
00071 
00078   Result Exec(const PGSTD::string &Query,
00079               const PGSTD::string &Desc=PGSTD::string())                //[t2]
00080         { return Exec(Query.c_str(), Desc); }
00081 
00083   void ProcessNotice(const char Msg[]) { m_Conn.ProcessNotice(Msg); }   //[t14]
00085   void ProcessNotice(const PGSTD::string &Msg)                          //[t14]
00086         { m_Conn.ProcessNotice(Msg); }
00087 
00088   PGSTD::string Name() const { return m_Name; }                         //[t1]
00089 
00091   ConnectionItf &Conn() const { return m_Conn; }                        //[]
00092 
00093 protected:
00096   explicit TransactionItf(ConnectionItf &, 
00097                           const PGSTD::string &TName=PGSTD::string());
00098 
00101   void Begin();
00102 
00104   void End() throw ();
00105 
00107   virtual void DoBegin() =0;
00108   virtual Result DoExec(const char Query[]) =0;
00109   virtual void DoCommit() =0;
00110   virtual void DoAbort() =0;
00111 
00112   // For use by implementing class:
00113 
00115   Result DirectExec(const char C[], int Retries, const char OnReconnect[]);
00116  
00117 private:
00118   /* A Transaction goes through the following stages in its lifecycle:
00119    *  - nascent: the transaction hasn't actually begun yet.  If our connection 
00120    *    fails at this stage, it may recover and the Transaction can attempt to
00121    *    establish itself again.
00122    *  - active: the transaction has begun.  Since no commit command has been 
00123    *    issued, abortion is implicit if the connection fails now.
00124    *  - aborted: an abort has been issued; the transaction is terminated and 
00125    *    its changes to the database rolled back.  It will accept no further 
00126    *    commands.
00127    *  - committed: the transaction has completed successfully, meaning that a 
00128    *    commit has been issued.  No further commands are accepted.
00129    *  - in_doubt: the connection was lost at the exact wrong time, and there is
00130    *    no way of telling whether the transaction was committed or aborted.
00131    *
00132    * Checking and maintaining state machine logic is the responsibility of the 
00133    * base class (ie., this one).
00134    */
00135   enum Status 
00136   { 
00137     st_nascent, 
00138     st_active, 
00139     st_aborted, 
00140     st_committed,
00141     st_in_doubt
00142   };
00143 
00144 
00145   friend class Cursor;
00146   int GetUniqueCursorNum() { return m_UniqueCursorNum++; }
00147   void MakeEmpty(Result &R) const { m_Conn.MakeEmpty(R); }
00148 
00149   friend class TableStream;
00150   void RegisterStream(const TableStream *);
00151   void UnregisterStream(const TableStream *) throw ();
00152   void EndCopy() { m_Conn.EndCopy(); }
00153   friend class TableReader;
00154   void BeginCopyRead(const PGSTD::string &Table) 
00155         { m_Conn.BeginCopyRead(Table); }
00156   bool ReadCopyLine(PGSTD::string &L) { return m_Conn.ReadCopyLine(L); }
00157   friend class TableWriter;
00158   void BeginCopyWrite(const PGSTD::string &Table) 
00159         { m_Conn.BeginCopyWrite(Table); }
00160   void WriteCopyLine(const PGSTD::string &L) { m_Conn.WriteCopyLine(L); }
00161 
00162   ConnectionItf &m_Conn;
00163 
00164   PGSTD::string m_Name;
00165   int m_UniqueCursorNum;
00166   Unique<TableStream> m_Stream;
00167   Status m_Status;
00168   bool m_Registered;
00169 
00170   // Not allowed:
00171   TransactionItf();
00172   TransactionItf(const TransactionItf &);
00173   TransactionItf &operator=(const TransactionItf &);
00174 };
00175 
00176 
00178 
00184 class PQXX_LIBEXPORT in_doubt_error : public PGSTD::runtime_error
00185 {
00186 public:
00187   explicit in_doubt_error(const PGSTD::string &whatarg) : 
00188     PGSTD::runtime_error(whatarg) {}
00189 };
00190 
00191 }
00192 
00193 #endif
00194 

Generated on Thu Mar 27 19:34:40 2003 for libpqxx by doxygen1.3-rc3