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

transaction_base.hxx

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  *   FILE
00004  *      pqxx/transaction_base.hxx
00005  *
00006  *   DESCRIPTION
00007  *      common code and definitions for the transaction classes.
00008  *   pqxx::transaction_base defines the interface for any abstract class that
00009  *   represents a database transaction
00010  *   DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/transaction_base instead.
00011  *
00012  * Copyright (c) 2001-2003, Jeroen T. Vermeulen <jtv@xs4all.nl>
00013  *
00014  * See COPYING for copyright license.  If you did not receive a file called
00015  * COPYING with this source code, please notify the distributor of this mistake,
00016  * or contact the author.
00017  *
00018  *-------------------------------------------------------------------------
00019  */
00020 
00021 /* End-user programs need not include this file, unless they define their own
00022  * transaction classes.  This is not something the typical program should want
00023  * to do.
00024  *
00025  * However, reading this file is worthwhile because it defines the public
00026  * interface for the available transaction classes such as transaction and 
00027  * nontransaction.
00028  */
00029 
00030 #include "pqxx/connection_base"
00031 #include "pqxx/isolation"
00032 #include "pqxx/result"
00033 
00034 /* Methods tested in eg. self-test program test1 are marked with "//[t1]"
00035  */
00036 
00037 
00038 namespace pqxx
00039 {
00040 class connection_base;  // See pqxx/connection_base.h
00041 class result;           // See pqxx/result.h
00042 class tablestream;      // See pqxx/tablestream.h
00043 
00044 
00046 template<> inline PGSTD::string Classname(const tablestream *) 
00047 { 
00048   return "tablestream"; 
00049 }
00050 
00051 
00053 
00061 class PQXX_LIBEXPORT transaction_base
00062 {
00063   // TODO: Retry non-serializable transaction w/update only on broken_connection
00064 public:
00066   typedef isolation_traits<read_committed> isolation_tag;
00067 
00068   virtual ~transaction_base() =0;                                       //[t1]
00069 
00071 
00083   void commit();                                                        //[t1]
00084 
00086 
00089   void abort();                                                         //[t10]
00090 
00092 
00096   result exec(const char Query[], 
00097               const PGSTD::string &Desc=PGSTD::string());               //[t1]
00098 
00100 
00107   result exec(const PGSTD::string &Query,
00108               const PGSTD::string &Desc=PGSTD::string())                //[t2]
00109         { return exec(Query.c_str(), Desc); }
00110 
00112   void process_notice(const char Msg[]) const                           //[t14]
00113         { m_Conn.process_notice(Msg); }
00115   void process_notice(const PGSTD::string &Msg) const                   //[t14]
00116         { m_Conn.process_notice(Msg); }
00117 
00118   PGSTD::string name() const { return m_Name; }                         //[t1]
00119 
00121   connection_base &conn() const { return m_Conn; }                      //[t4]
00122 
00124 
00132   void set_variable(const PGSTD::string &Var, const PGSTD::string &Val);//[t61]
00133 
00134 
00135 #ifdef PQXX_DEPRECATED_HEADERS
00136 
00137   void Commit() { commit(); }
00139   void Abort() { abort(); }
00141   result Exec(const char Q[], const PGSTD::string &D=PGSTD::string())
00142         { return exec(Q,D); }
00144   result Exec(const PGSTD::string &Q, const PGSTD::string &D=PGSTD::string())
00145         { return exec(Q,D); }
00147   void ProcessNotice(const char M[]) const { return process_notice(M); }
00149   void ProcessNotice(const PGSTD::string &M) const { return process_notice(M); }
00151   PGSTD::string Name() const { return name(); }
00153   connection_base &Conn() const { return conn(); }
00155   void SetVariable(const PGSTD::string &Var, const PGSTD::string &Val)
00156         { set_variable(Var,Val); }
00157 #endif
00158 
00159 protected:
00162   explicit transaction_base(connection_base &, 
00163                           const PGSTD::string &TName=PGSTD::string());
00164 
00167   void Begin();
00168 
00170   void End() throw ();
00171 
00173   virtual void do_begin() =0;
00175   virtual result do_exec(const char Query[]) =0;
00177   virtual void do_commit() =0;
00179   virtual void do_abort() =0;
00180 
00181   // For use by implementing class:
00182 
00184   result DirectExec(const char C[], int Retries, const char OnReconnect[]);
00185  
00186 private:
00187   /* A transaction goes through the following stages in its lifecycle:
00188    *  - nascent: the transaction hasn't actually begun yet.  If our connection 
00189    *    fails at this stage, it may recover and the transaction can attempt to
00190    *    establish itself again.
00191    *  - active: the transaction has begun.  Since no commit command has been 
00192    *    issued, abortion is implicit if the connection fails now.
00193    *  - aborted: an abort has been issued; the transaction is terminated and 
00194    *    its changes to the database rolled back.  It will accept no further 
00195    *    commands.
00196    *  - committed: the transaction has completed successfully, meaning that a 
00197    *    commit has been issued.  No further commands are accepted.
00198    *  - in_doubt: the connection was lost at the exact wrong time, and there is
00199    *    no way of telling whether the transaction was committed or aborted.
00200    *
00201    * Checking and maintaining state machine logic is the responsibility of the 
00202    * base class (ie., this one).
00203    */
00204   enum Status 
00205   { 
00206     st_nascent, 
00207     st_active, 
00208     st_aborted, 
00209     st_committed,
00210     st_in_doubt
00211   };
00212 
00213 
00214   friend class Cursor;
00215   int GetUniqueCursorNum() { return m_UniqueCursorNum++; }
00216   void MakeEmpty(result &R) const { m_Conn.MakeEmpty(R); }
00217 
00218   friend class tablestream;
00219   void RegisterStream(tablestream *);
00220   void UnregisterStream(tablestream *) throw ();
00221   void EndCopy() throw () { m_Conn.EndCopy(); }
00222   friend class tablereader;
00223   void BeginCopyRead(const PGSTD::string &Table) 
00224         { m_Conn.BeginCopyRead(Table); }
00225   bool ReadCopyLine(PGSTD::string &L) { return m_Conn.ReadCopyLine(L); }
00226   friend class tablewriter;
00227   void BeginCopyWrite(const PGSTD::string &Table) 
00228         { m_Conn.BeginCopyWrite(Table); }
00229   void WriteCopyLine(const PGSTD::string &L) { m_Conn.WriteCopyLine(L); }
00230   void EndCopyWrite() throw () { m_Conn.EndCopyWrite(); }
00231 
00232   connection_base &m_Conn;
00233 
00234   PGSTD::string m_Name;
00235   int m_UniqueCursorNum;
00236   unique<tablestream> m_Stream;
00237   Status m_Status;
00238   bool m_Registered;
00239   mutable PGSTD::map<PGSTD::string, PGSTD::string> m_Vars;
00240 
00241   // Not allowed:
00242   transaction_base();
00243   transaction_base(const transaction_base &);
00244   transaction_base &operator=(const transaction_base &);
00245 };
00246 
00247 
00248 }
00249 
00250 

Generated on Fri Nov 21 19:50:08 2003 for libpqxx by doxygen 1.3.4