Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | 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-2005, 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 #include "pqxx/libcompiler.h"
00021 
00022 /* End-user programs need not include this file, unless they define their own
00023  * transaction classes.  This is not something the typical program should want
00024  * to do.
00025  *
00026  * However, reading this file is worthwhile because it defines the public
00027  * interface for the available transaction classes such as transaction and
00028  * nontransaction.
00029  */
00030 
00031 #include "pqxx/connection_base"
00032 #include "pqxx/isolation"
00033 #include "pqxx/result"
00034 
00035 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
00036  */
00037 
00038 // TODO: Any way the BEGIN and the first query can be concatenated/pipelined?
00039 
00040 namespace pqxx
00041 {
00042 class connection_base;
00043 class transaction_base;
00044 
00045 
00046 namespace internal
00047 {
00048 class PQXX_LIBEXPORT transactionfocus : public namedclass
00049 {
00050 public:
00051   transactionfocus(transaction_base &t,
00052       const PGSTD::string &Name,
00053       const PGSTD::string &Classname) :
00054     namedclass(Name, Classname),
00055     m_Trans(t),
00056     m_registered(false)
00057   {
00058   }
00059 
00060 protected:
00061   void register_me();
00062   void unregister_me() throw ();
00063   void reg_pending_error(const PGSTD::string &) throw ();
00064   bool registered() const throw () { return m_registered; }
00065 
00066   transaction_base &m_Trans;
00067 
00068 private:
00069   bool m_registered;
00070 
00072   transactionfocus();
00074   transactionfocus(const transactionfocus &);
00076   transactionfocus &operator=(const transactionfocus &);
00077 };
00078 } // namespace internal
00079 
00080 
00081 
00083 
00091 class PQXX_LIBEXPORT transaction_base : public internal::namedclass
00092 {
00093   // TODO: Retry non-serializable transaction w/update only on broken_connection
00094 public:
00096   typedef isolation_traits<read_committed> isolation_tag;
00097 
00098   virtual ~transaction_base() =0;                                       //[t1]
00099 
00101 
00113   void commit();                                                        //[t1]
00114 
00116 
00119   void abort();                                                         //[t10]
00120 
00122 
00127   result exec(const char Query[],
00128               const PGSTD::string &Desc=PGSTD::string());               //[t1]
00129 
00131 
00139   result exec(const PGSTD::string &Query,
00140               const PGSTD::string &Desc=PGSTD::string())                //[t2]
00141         { return exec(Query.c_str(), Desc); }
00142 
00143   result exec(const PGSTD::stringstream &Query,
00144               const PGSTD::string &Desc=PGSTD::string())                //[t9]
00145         { return exec(Query.str(), Desc); }
00146 
00148   void process_notice(const char Msg[]) const                           //[t14]
00149         { m_Conn.process_notice(Msg); }
00151   void process_notice(const PGSTD::string &Msg) const                   //[t14]
00152         { m_Conn.process_notice(Msg); }
00153 
00155   connection_base &conn() const { return m_Conn; }                      //[t4]
00156 
00158 
00166   void set_variable(const PGSTD::string &Var, const PGSTD::string &Val);//[t61]
00167 
00169 
00175   PGSTD::string get_variable(const PGSTD::string &) const;              //[t61]
00176 
00177 #ifdef PQXX_DEPRECATED_HEADERS
00178 
00179   void Commit() { commit(); }
00181   void Abort() { abort(); }
00183   result Exec(const char Q[], const PGSTD::string &D=PGSTD::string())
00184         { return exec(Q,D); }
00186   result Exec(const PGSTD::string &Q, const PGSTD::string &D=PGSTD::string())
00187         { return exec(Q,D); }
00189   void ProcessNotice(const char M[]) const { return process_notice(M); }
00191   void ProcessNotice(const PGSTD::string &M) const { return process_notice(M); }
00193   PGSTD::string Name() const { return name(); }
00195   connection_base &Conn() const { return conn(); }
00197   void SetVariable(const PGSTD::string &Var, const PGSTD::string &Val)
00198         { set_variable(Var,Val); }
00199 #endif
00200 
00201 protected:
00203 
00206   explicit transaction_base(connection_base &,
00207                           const PGSTD::string &TName,
00208                           const PGSTD::string &CName);
00209 
00211 
00213   void Begin();
00214 
00216   void End() throw ();
00217 
00219   virtual void do_begin() =0;
00221   virtual result do_exec(const char Query[]) =0;
00223   virtual void do_commit() =0;
00225   virtual void do_abort() =0;
00226 
00227   // For use by implementing class:
00228 
00230 
00238   result DirectExec(const char C[], int Retries=0);
00239 
00240 private:
00241   /* A transaction goes through the following stages in its lifecycle:
00242    * <ul>
00243    * <li> nascent: the transaction hasn't actually begun yet.  If our connection
00244    *    fails at this stage, it may recover and the transaction can attempt to
00245    *    establish itself again.
00246    * <li> active: the transaction has begun.  Since no commit command has been
00247    *    issued, abortion is implicit if the connection fails now.
00248    * <li> aborted: an abort has been issued; the transaction is terminated and
00249    *    its changes to the database rolled back.  It will accept no further
00250    *    commands.
00251    * <li> committed: the transaction has completed successfully, meaning that a
00252    *    commit has been issued.  No further commands are accepted.
00253    * <li> in_doubt: the connection was lost at the exact wrong time, and there
00254    *    is no way of telling whether the transaction was committed or aborted.
00255    * </ul>
00256    *
00257    * Checking and maintaining state machine logic is the responsibility of the
00258    * base class (ie., this one).
00259    */
00260   enum Status
00261   {
00262     st_nascent,
00263     st_active,
00264     st_aborted,
00265     st_committed,
00266     st_in_doubt
00267   };
00268 
00269 
00270   void CheckPendingError();
00271 
00272   friend class Cursor;
00273   friend class cursor_base;
00274   int GetUniqueCursorNum() { return m_UniqueCursorNum++; }
00275   void MakeEmpty(result &R) const { m_Conn.MakeEmpty(R); }
00276 
00277   friend class internal::transactionfocus;
00278   void RegisterFocus(internal::transactionfocus *);
00279   void UnregisterFocus(internal::transactionfocus *) throw ();
00280   void RegisterPendingError(const PGSTD::string &) throw ();
00281   friend class tablereader;
00282   void BeginCopyRead(const PGSTD::string &Table, const PGSTD::string &Columns);
00283   bool ReadCopyLine(PGSTD::string &L) { return m_Conn.ReadCopyLine(L); }
00284   friend class tablewriter;
00285   void BeginCopyWrite(const PGSTD::string &Table,
00286         const PGSTD::string &Columns = PGSTD::string());
00287   void WriteCopyLine(const PGSTD::string &L) { m_Conn.WriteCopyLine(L); }
00288   void EndCopyWrite() { m_Conn.EndCopyWrite(); }
00289 
00290   friend class pipeline;
00291   void start_exec(const PGSTD::string &Q) { m_Conn.start_exec(Q); }
00292   internal::pq::PGresult *get_result() { return m_Conn.get_result(); }
00293   void consume_input() throw () { m_Conn.consume_input(); }
00294   bool is_busy() const throw () { return m_Conn.is_busy(); }
00295 
00296   connection_base &m_Conn;
00297 
00298   int m_UniqueCursorNum;
00299   internal::unique<internal::transactionfocus> m_Focus;
00300   Status m_Status;
00301   bool m_Registered;
00302   mutable PGSTD::map<PGSTD::string, PGSTD::string> m_Vars;
00303   PGSTD::string m_PendingError;
00304 
00306   transaction_base();
00308   transaction_base(const transaction_base &);
00310   transaction_base &operator=(const transaction_base &);
00311 };
00312 
00313 } // namespace pqxx
00314 
00315 

Generated on Mon Feb 28 10:24:56 2005 for libpqxx by  doxygen 1.4.1