00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "pqxx/libcompiler.h"
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "pqxx/connection_base"
00032 #include "pqxx/isolation"
00033 #include "pqxx/result"
00034
00035
00036
00037
00038
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 }
00079
00080
00081
00083
00091 class PQXX_LIBEXPORT transaction_base : public internal::namedclass
00092 {
00093
00094 public:
00096 typedef isolation_traits<read_committed> isolation_tag;
00097
00098 virtual ~transaction_base() =0;
00099
00101
00113 void commit();
00114
00116
00119 void abort();
00120
00122
00127 result exec(const char Query[],
00128 const PGSTD::string &Desc=PGSTD::string());
00129
00131
00139 result exec(const PGSTD::string &Query,
00140 const PGSTD::string &Desc=PGSTD::string())
00141 { return exec(Query.c_str(), Desc); }
00142
00143 result exec(const PGSTD::stringstream &Query,
00144 const PGSTD::string &Desc=PGSTD::string())
00145 { return exec(Query.str(), Desc); }
00146
00148 void process_notice(const char Msg[]) const
00149 { m_Conn.process_notice(Msg); }
00151 void process_notice(const PGSTD::string &Msg) const
00152 { m_Conn.process_notice(Msg); }
00153
00155 connection_base &conn() const { return m_Conn; }
00156
00158
00166 void set_variable(const PGSTD::string &Var, const PGSTD::string &Val);
00167
00169
00175 PGSTD::string get_variable(const PGSTD::string &) const;
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
00228
00230
00238 result DirectExec(const char C[], int Retries=0);
00239
00240 private:
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
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 }
00314
00315