| 1 | /*------------------------------------------------------------------------- |
|---|
| 2 | * |
|---|
| 3 | * FILE |
|---|
| 4 | * pqxx/transaction.hxx |
|---|
| 5 | * |
|---|
| 6 | * DESCRIPTION |
|---|
| 7 | * definition of the pqxx::transaction class. |
|---|
| 8 | * pqxx::transaction represents a standard database transaction |
|---|
| 9 | * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/transaction instead. |
|---|
| 10 | * |
|---|
| 11 | * Copyright (c) 2001-2006, Jeroen T. Vermeulen <jtv@xs4all.nl> |
|---|
| 12 | * |
|---|
| 13 | * See COPYING for copyright license. If you did not receive a file called |
|---|
| 14 | * COPYING with this source code, please notify the distributor of this mistake, |
|---|
| 15 | * or contact the author. |
|---|
| 16 | * |
|---|
| 17 | *------------------------------------------------------------------------- |
|---|
| 18 | */ |
|---|
| 19 | #include "pqxx/compiler-public.hxx" |
|---|
| 20 | #include "pqxx/compiler-internal-pre.hxx" |
|---|
| 21 | |
|---|
| 22 | #include "pqxx/dbtransaction" |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | /* Methods tested in eg. self-test program test1 are marked with "//[t1]" |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | namespace pqxx |
|---|
| 31 | { |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * @addtogroup transaction Transaction classes |
|---|
| 35 | */ |
|---|
| 36 | //@{ |
|---|
| 37 | |
|---|
| 38 | class PQXX_LIBEXPORT basic_transaction : public dbtransaction |
|---|
| 39 | { |
|---|
| 40 | protected: |
|---|
| 41 | basic_transaction(connection_base &C, |
|---|
| 42 | const PGSTD::string &IsolationLevel); //[t1] |
|---|
| 43 | |
|---|
| 44 | private: |
|---|
| 45 | virtual void do_commit(); //[t1] |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | /// Standard back-end transaction, templatized on isolation level |
|---|
| 50 | /** This is the type you'll normally want to use to represent a transaction on |
|---|
| 51 | * the database. |
|---|
| 52 | * |
|---|
| 53 | * While you may choose to create your own transaction object to interface to |
|---|
| 54 | * the database backend, it is recommended that you wrap your transaction code |
|---|
| 55 | * into a transactor code instead and let the transaction be created for you. |
|---|
| 56 | * @see pqxx/transactor.hxx |
|---|
| 57 | * |
|---|
| 58 | * If you should find that using a transactor makes your code less portable or |
|---|
| 59 | * too complex, go ahead, create your own transaction anyway. |
|---|
| 60 | * |
|---|
| 61 | * Usage example: double all wages |
|---|
| 62 | * |
|---|
| 63 | * @code |
|---|
| 64 | * extern connection C; |
|---|
| 65 | * work T(C); |
|---|
| 66 | * try |
|---|
| 67 | * { |
|---|
| 68 | * T.exec("UPDATE employees SET wage=wage*2"); |
|---|
| 69 | * T.commit(); // NOTE: do this inside try block |
|---|
| 70 | * } |
|---|
| 71 | * catch (const exception &e) |
|---|
| 72 | * { |
|---|
| 73 | * cerr << e.what() << endl; |
|---|
| 74 | * T.abort(); // Usually not needed; same happens when T's life ends. |
|---|
| 75 | * } |
|---|
| 76 | * @endcode |
|---|
| 77 | */ |
|---|
| 78 | template<isolation_level ISOLATIONLEVEL=read_committed> |
|---|
| 79 | class transaction : public basic_transaction |
|---|
| 80 | { |
|---|
| 81 | public: |
|---|
| 82 | typedef isolation_traits<ISOLATIONLEVEL> isolation_tag; |
|---|
| 83 | |
|---|
| 84 | /// Create a transaction |
|---|
| 85 | /** |
|---|
| 86 | * @param C Connection for this transaction to operate on |
|---|
| 87 | * @param TName Optional name for transaction; must begin with a letter and |
|---|
| 88 | * may contain letters and digits only |
|---|
| 89 | */ |
|---|
| 90 | explicit transaction(connection_base &C, const PGSTD::string &TName): //[t1] |
|---|
| 91 | namedclass(fullname("transaction",isolation_tag::name()), TName), |
|---|
| 92 | basic_transaction(C, isolation_tag::name()) |
|---|
| 93 | { Begin(); } |
|---|
| 94 | |
|---|
| 95 | explicit transaction(connection_base &C) : //[t1] |
|---|
| 96 | namedclass(fullname("transaction",isolation_tag::name())), |
|---|
| 97 | basic_transaction(C, isolation_tag::name()) |
|---|
| 98 | { Begin(); } |
|---|
| 99 | |
|---|
| 100 | virtual ~transaction() throw () |
|---|
| 101 | { |
|---|
| 102 | #ifdef PQXX_QUIET_DESTRUCTORS |
|---|
| 103 | internal::disable_noticer Quiet(conn()); |
|---|
| 104 | #endif |
|---|
| 105 | End(); |
|---|
| 106 | } |
|---|
| 107 | }; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | /// Bog-standard, default transaction type |
|---|
| 111 | typedef transaction<> work; |
|---|
| 112 | |
|---|
| 113 | //@} |
|---|
| 114 | |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | #include "pqxx/compiler-internal-post.hxx" |
|---|