transaction.hxx 3.0 KB

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