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 
00135 
00141   PGSTD::string get_variable(const PGSTD::string &) const;              //[t61]
00142 
00143 #ifdef PQXX_DEPRECATED_HEADERS
00144 
00145   void Commit() { commit(); }
00147   void Abort() { abort(); }
00149   result Exec(const char Q[], const PGSTD::string &D=PGSTD::string())
00150         { return exec(Q,D); }
00152   result Exec(const PGSTD::string &Q, const PGSTD::string &D=PGSTD::string())
00153         { return exec(Q,D); }
00155   void ProcessNotice(const char M[]) const { return process_notice(M); }
00157   void ProcessNotice(const PGSTD::string &M) const { return process_notice(M); }
00159   PGSTD::string Name() const { return name(); }
00161   connection_base &Conn() const { return conn(); }
00163   void SetVariable(const PGSTD::string &Var, const PGSTD::string &Val)
00164         { set_variable(Var,Val); }
00165 #endif
00166 
00167 protected:
00170   explicit transaction_base(connection_base &, 
00171                           const PGSTD::string &TName=PGSTD::string());
00172 
00175   void Begin();
00176 
00178   void End() throw ();
00179 
00181   virtual void do_begin() =0;
00183   virtual result do_exec(const char Query[]) =0;
00185   virtual void do_commit() =0;
00187   virtual void do_abort() =0;
00188 
00189   // For use by implementing class:
00190 
00192 
00200   result DirectExec(const char C[], int Retries=0);
00201  
00202 private:
00203   /* A transaction goes through the following stages in its lifecycle:
00204    *  - nascent: the transaction hasn't actually begun yet.  If our connection 
00205    *    fails at this stage, it may recover and the transaction can attempt to
00206    *    establish itself again.
00207    *  - active: the transaction has begun.  Since no commit command has been 
00208    *    issued, abortion is implicit if the connection fails now.
00209    *  - aborted: an abort has been issued; the transaction is terminated and 
00210    *    its changes to the database rolled back.  It will accept no further 
00211    *    commands.
00212    *  - committed: the transaction has completed successfully, meaning that a 
00213    *    commit has been issued.  No further commands are accepted.
00214    *  - in_doubt: the connection was lost at the exact wrong time, and there is
00215    *    no way of telling whether the transaction was committed or aborted.
00216    *
00217    * Checking and maintaining state machine logic is the responsibility of the 
00218    * base class (ie., this one).
00219    */
00220   enum Status 
00221   { 
00222     st_nascent, 
00223     st_active, 
00224     st_aborted, 
00225     st_committed,
00226     st_in_doubt
00227   };
00228 
00229 
00230   void CheckPendingError();
00231 
00232   friend class Cursor;
00233   int GetUniqueCursorNum() { return m_UniqueCursorNum++; }
00234   void MakeEmpty(result &R) const { m_Conn.MakeEmpty(R); }
00235 
00236   friend class tablestream;
00237   void RegisterStream(tablestream *);
00238   void UnregisterStream(tablestream *) throw ();
00239   void RegisterPendingError(const PGSTD::string &) throw ();
00240   friend class tablereader;
00241   void BeginCopyRead(const PGSTD::string &Table);
00242   bool ReadCopyLine(PGSTD::string &L) { return m_Conn.ReadCopyLine(L); }
00243   friend class tablewriter;
00244   void BeginCopyWrite(const PGSTD::string &Table);
00245   bool WriteCopyLine(const PGSTD::string &L, bool async=false) 
00246         { return m_Conn.WriteCopyLine(L, async); }
00247   void EndCopyWrite() throw () { m_Conn.EndCopyWrite(); }
00248 
00249   connection_base &m_Conn;
00250 
00251   PGSTD::string m_Name;
00252   int m_UniqueCursorNum;
00253   unique<tablestream> m_Stream;
00254   Status m_Status;
00255   bool m_Registered;
00256   mutable PGSTD::map<PGSTD::string, PGSTD::string> m_Vars;
00257   PGSTD::string m_PendingError;
00258 
00259   // Not allowed:
00260   transaction_base();
00261   transaction_base(const transaction_base &);
00262   transaction_base &operator=(const transaction_base &);
00263 };
00264 
00265 
00266 }
00267 
00268 

Generated on Thu Dec 25 07:22:38 2003 for libpqxx by doxygen 1.3.4