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-2006, 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/compiler-public.hxx"
00021 #include "pqxx/compiler-internal-pre.hxx"
00022 
00023 /* End-user programs need not include this file, unless they define their own
00024  * transaction classes.  This is not something the typical program should want
00025  * to do.
00026  *
00027  * However, reading this file is worthwhile because it defines the public
00028  * interface for the available transaction classes such as transaction and
00029  * nontransaction.
00030  */
00031 
00032 #include "pqxx/connection_base"
00033 #include "pqxx/isolation"
00034 #include "pqxx/result"
00035 
00036 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
00037  */
00038 
00039 namespace pqxx
00040 {
00041 class connection_base;
00042 class transaction_base;
00043 
00044 
00045 namespace internal
00046 {
00047 class PQXX_LIBEXPORT transactionfocus : public virtual namedclass
00048 {
00049 public:
00050   explicit transactionfocus(transaction_base &t) :
00051     namedclass("transactionfocus"),
00052     m_Trans(t),
00053     m_registered(false)
00054   {
00055   }
00056 
00057 protected:
00058   void register_me();
00059   void unregister_me() throw ();
00060   void reg_pending_error(const PGSTD::string &) throw ();
00061   bool registered() const throw () { return m_registered; }
00062 
00063   transaction_base &m_Trans;
00064 
00065 private:
00066   bool m_registered;
00067 
00069   transactionfocus();
00071   transactionfocus(const transactionfocus &);
00073   transactionfocus &operator=(const transactionfocus &);
00074 };
00075 
00076 } // namespace internal
00077 
00078 
00079 
00081 
00091 class PQXX_LIBEXPORT transaction_base : public virtual internal::namedclass
00092 {
00093 public:
00095   typedef isolation_traits<read_committed> isolation_tag;
00096 
00097   virtual ~transaction_base() =0;                                       //[t1]
00098 
00100 
00112   void commit();                                                        //[t1]
00113 
00115 
00118   void abort();                                                         //[t10]
00119 
00124 
00125   PGSTD::string esc(const char str[]) const;                            //[t90]
00127   PGSTD::string esc(const char str[], size_t maxlen) const              //[t90]
00128         { return m_Conn.esc(str,maxlen); }
00130   PGSTD::string esc(const PGSTD::string &) const;                       //[t90]
00132 
00134 
00139   result exec(const char Query[],
00140               const PGSTD::string &Desc=PGSTD::string());               //[t1]
00141 
00143 
00151   result exec(const PGSTD::string &Query,
00152               const PGSTD::string &Desc=PGSTD::string())                //[t2]
00153         { return exec(Query.c_str(), Desc); }
00154 
00155   result exec(const PGSTD::stringstream &Query,
00156               const PGSTD::string &Desc=PGSTD::string())                //[t9]
00157         { return exec(Query.str(), Desc); }
00158 
00163 
00164 
00198   prepare::invocation prepared(const PGSTD::string &statement);         //[t85]
00199 
00201 
00206 
00207   void process_notice(const char Msg[]) const                           //[t14]
00208         { m_Conn.process_notice(Msg); }
00210   void process_notice(const PGSTD::string &Msg) const                   //[t14]
00211         { m_Conn.process_notice(Msg); }
00213 
00215   connection_base &conn() const { return m_Conn; }                      //[t4]
00216 
00218 
00226   void set_variable(const PGSTD::string &Var, const PGSTD::string &Val);//[t61]
00227 
00229 
00238   PGSTD::string get_variable(const PGSTD::string &);                    //[t61]
00239 
00240 #ifdef PQXX_DEPRECATED_HEADERS
00241 
00245 
00246   void Commit() PQXX_DEPRECATED { commit(); }
00248   void Abort() PQXX_DEPRECATED { abort(); }
00250   result Exec(const char Q[], const PGSTD::string &D=PGSTD::string())
00251         PQXX_DEPRECATED { return exec(Q,D); }
00253   result Exec(const PGSTD::string &Q, const PGSTD::string &D=PGSTD::string())
00254         PQXX_DEPRECATED { return exec(Q,D); }
00256   void ProcessNotice(const char M[]) const PQXX_DEPRECATED
00257         { return process_notice(M); }
00259   void ProcessNotice(const PGSTD::string &M) const PQXX_DEPRECATED
00260         { return process_notice(M); }
00262   PGSTD::string Name() const PQXX_DEPRECATED { return name(); }
00264   connection_base &Conn() const PQXX_DEPRECATED { return conn(); }
00266   void SetVariable(const PGSTD::string &Var, const PGSTD::string &Val)
00267         PQXX_DEPRECATED { set_variable(Var,Val); }
00269 #endif
00270 
00271 protected:
00273 
00278   explicit transaction_base(connection_base &, bool direct=true);
00279 
00281 
00283   void Begin();
00284 
00286   void End() throw ();
00287 
00289   virtual void do_begin() =0;
00291   virtual result do_exec(const char Query[]) =0;
00293   virtual void do_commit() =0;
00295   virtual void do_abort() =0;
00296 
00297   // For use by implementing class:
00298 
00300 
00308   result DirectExec(const char C[], int Retries=0);
00309 
00311   void reactivation_avoidance_clear() throw ()
00312         {m_reactivation_avoidance.clear();}
00313 
00314 private:
00315   /* A transaction goes through the following stages in its lifecycle:
00316    * <ul>
00317    * <li> nascent: the transaction hasn't actually begun yet.  If our connection
00318    *    fails at this stage, it may recover and the transaction can attempt to
00319    *    establish itself again.
00320    * <li> active: the transaction has begun.  Since no commit command has been
00321    *    issued, abortion is implicit if the connection fails now.
00322    * <li> aborted: an abort has been issued; the transaction is terminated and
00323    *    its changes to the database rolled back.  It will accept no further
00324    *    commands.
00325    * <li> committed: the transaction has completed successfully, meaning that a
00326    *    commit has been issued.  No further commands are accepted.
00327    * <li> in_doubt: the connection was lost at the exact wrong time, and there
00328    *    is no way of telling whether the transaction was committed or aborted.
00329    * </ul>
00330    *
00331    * Checking and maintaining state machine logic is the responsibility of the
00332    * base class (ie., this one).
00333    */
00334   enum Status
00335   {
00336     st_nascent,
00337     st_active,
00338     st_aborted,
00339     st_committed,
00340     st_in_doubt
00341   };
00342 
00343 
00344   void PQXX_PRIVATE CheckPendingError();
00345 
00346   template<typename T> bool parm_is_null(T *p) const throw () { return !p; }
00347   template<typename T> bool parm_is_null(T) const throw () { return false; }
00348 
00349   friend class Cursor;
00350   friend class cursor_base;
00351   void MakeEmpty(result &R) const { m_Conn.MakeEmpty(R); }
00352 
00353   friend class internal::transactionfocus;
00354   void PQXX_PRIVATE RegisterFocus(internal::transactionfocus *);
00355   void PQXX_PRIVATE UnregisterFocus(internal::transactionfocus *) throw ();
00356   void PQXX_PRIVATE RegisterPendingError(const PGSTD::string &) throw ();
00357   friend class tablereader;
00358   void PQXX_PRIVATE BeginCopyRead(const PGSTD::string &, const PGSTD::string &);
00359   bool ReadCopyLine(PGSTD::string &L) { return m_Conn.ReadCopyLine(L); }
00360   friend class tablewriter;
00361   void PQXX_PRIVATE BeginCopyWrite(const PGSTD::string &Table,
00362         const PGSTD::string &Columns = PGSTD::string());
00363   void WriteCopyLine(const PGSTD::string &L) { m_Conn.WriteCopyLine(L); }
00364   void EndCopyWrite() { m_Conn.EndCopyWrite(); }
00365 
00366   friend class pipeline;
00367   void start_exec(const PGSTD::string &Q) { m_Conn.start_exec(Q); }
00368   internal::pq::PGresult *get_result() { return m_Conn.get_result(); }
00369   void consume_input() throw () { m_Conn.consume_input(); }
00370   bool is_busy() const throw () { return m_Conn.is_busy(); }
00371 
00372   friend class prepare::invocation;
00373   result prepared_exec(const PGSTD::string &, const char *const[], int);
00374 
00375   connection_base &m_Conn;
00376 
00377   internal::unique<internal::transactionfocus> m_Focus;
00378   Status m_Status;
00379   bool m_Registered;
00380   PGSTD::map<PGSTD::string, PGSTD::string> m_Vars;
00381   PGSTD::string m_PendingError;
00382 
00383   friend class subtransaction;
00385 
00387   internal::reactivation_avoidance_counter m_reactivation_avoidance;
00388 
00390   transaction_base();
00392   transaction_base(const transaction_base &);
00394   transaction_base &operator=(const transaction_base &);
00395 };
00396 
00397 } // namespace pqxx
00398 
00399 
00400 #include "pqxx/compiler-internal-post.hxx"

Generated on Sat May 27 17:33:49 2006 for libpqxx by  doxygen 1.4.6