libpqxx  7.0.5
pqxx::subtransaction Class Reference

"Transaction" nested within another transaction More...

#include <subtransaction.hxx>

Inheritance diagram for pqxx::subtransaction:

Public Member Functions

 subtransaction (dbtransaction &t, std::string const &name=std::string{})
 Nest a subtransaction nested in another transaction. More...
 
 subtransaction (subtransaction &t, std::string const &name=std::string{})
 Nest a subtransaction in another subtransaction. More...
 
virtual ~subtransaction () noexcept override
 
- Public Member Functions inherited from pqxx::internal::transactionfocus
 transactionfocus (transaction_base &t)
 
 transactionfocus ()=delete
 
 transactionfocus (transactionfocus const &)=delete
 
transactionfocusoperator= (transactionfocus const &)=delete
 
- Public Member Functions inherited from pqxx::internal::namedclass
 namedclass (std::string_view classname)
 
 namedclass (std::string_view classname, std::string_view name)
 
 namedclass (std::string_view classname, char const name[])
 
 namedclass (std::string_view classname, std::string &&name)
 
std::string const & name () const noexcept
 Object name, or the empty string if no name was given. More...
 
std::string const & classname () const noexcept
 Class name. More...
 
std::string description () const
 Combination of class name and object name; or just class name. More...
 
- Public Member Functions inherited from pqxx::transaction_base
 transaction_base ()=delete
 
 transaction_base (transaction_base const &)=delete
 
transaction_baseoperator= (transaction_base const &)=delete
 
virtual ~transaction_base ()=0
 
void commit ()
 Commit the transaction. More...
 
void abort ()
 Abort the transaction. More...
 
result exec (std::string_view query, std::string const &desc=std::string{})
 Execute query. More...
 
result exec (std::stringstream const &query, std::string const &desc=std::string{})
 
result exec0 (std::string const &query, std::string const &desc=std::string{})
 Execute query, which should zero rows of data. More...
 
row exec1 (std::string const &query, std::string const &desc=std::string{})
 Execute query returning a single row of data. More...
 
result exec_n (result::size_type rows, std::string const &query, std::string const &desc=std::string{})
 Execute query, expect given number of rows. More...
 
template<typename TYPE >
TYPE query_value (std::string const &query, std::string const &desc=std::string{})
 Execute query, expecting exactly 1 row with 1 field. More...
 
connectionconn () const
 The connection in which this transaction lives. More...
 
void set_variable (std::string_view var, std::string_view value)
 Set session variable using SQL "SET" command. More...
 
std::string get_variable (std::string_view)
 Read session variable using SQL "SHOW" command. More...
 
std::string esc (char const text[]) const
 Escape string for use as SQL string literal in this transaction. More...
 
std::string esc (char const text[], size_t maxlen) const
 Escape string for use as SQL string literal in this transaction. More...
 
std::string esc (std::string const &text) const
 Escape string for use as SQL string literal in this transaction. More...
 
std::string esc (std::string_view text) const
 Escape string for use as SQL string literal in this transaction. More...
 
std::string esc_raw (unsigned char const data[], size_t len) const
 Escape binary data for use as SQL string literal in this transaction. More...
 
std::string esc_raw (std::string const &) const
 Escape binary data for use as SQL string literal in this transaction. More...
 
std::string unesc_raw (std::string const &text) const
 Unescape binary data, e.g. from a table field or notification payload. More...
 
std::string unesc_raw (char const *text) const
 Unescape binary data, e.g. from a table field or notification payload. More...
 
template<typename T >
std::string quote (T const &t) const
 Represent object as SQL string, including quoting & escaping. More...
 
std::string quote_raw (unsigned char const bin[], size_t len) const
 Binary-escape and quote a binarystring for use as an SQL constant. More...
 
std::string quote_raw (std::string const &bin) const
 
std::string quote_name (std::string_view identifier) const
 Escape an SQL identifier for use in a query. More...
 
std::string esc_like (std::string const &bin, char escape_char='\\') const
 Escape string for literal LIKE match. More...
 
template<typename... Args>
result exec_params (std::string const &query, Args &&... args)
 Execute an SQL statement with parameters. More...
 
template<typename... Args>
row exec_params1 (std::string const &query, Args &&... args)
 
template<typename... Args>
result exec_params0 (std::string const &query, Args &&... args)
 
template<typename... Args>
result exec_params_n (size_t rows, std::string const &query, Args &&... args)
 
template<typename... Args>
result exec_prepared (std::string const &statement, Args &&... args)
 Execute a prepared statement, with optional arguments. More...
 
template<typename... Args>
result exec_prepared (zview statement, Args &&... args)
 
template<typename... Args>
row exec_prepared1 (std::string const &statement, Args &&... args)
 Execute a prepared statement, and expect a single-row result. More...
 
template<typename... Args>
row exec_prepared1 (zview statement, Args &&... args)
 
template<typename... Args>
result exec_prepared0 (std::string const &statement, Args &&... args)
 Execute a prepared statement, and expect a result with zero rows. More...
 
template<typename... Args>
result exec_prepared0 (zview statement, Args &&... args)
 
template<typename... Args>
result exec_prepared_n (result::size_type rows, std::string const &statement, Args &&... args)
 Execute a prepared statement, expect a result with given number of rows. More...
 
template<typename... Args>
result exec_prepared_n (result::size_type rows, zview statement, Args &&... args)
 
void process_notice (char const msg[]) const
 Have connection process a warning message. More...
 
void process_notice (std::string const &msg) const
 Have connection process a warning message. More...
 

Additional Inherited Members

- Protected Member Functions inherited from pqxx::internal::transactionfocus
void register_me ()
 
void unregister_me () noexcept
 
void reg_pending_error (std::string const &) noexcept
 
bool registered () const noexcept
 
- Protected Member Functions inherited from pqxx::dbtransaction
 dbtransaction (connection &c)
 
- Protected Member Functions inherited from pqxx::transaction_base
 transaction_base (connection &c)
 Create a transaction (to be called by implementation classes only). More...
 
void register_transaction ()
 Register this transaction with the connection. More...
 
void close () noexcept
 End transaction. To be called by implementing class' destructor. More...
 
result direct_exec (std::string_view)
 Execute query on connection directly. More...
 
result direct_exec (std::shared_ptr< std::string >)
 
- Protected Attributes inherited from pqxx::internal::transactionfocus
transaction_basem_trans
 

Detailed Description

"Transaction" nested within another transaction

A subtransaction can be executed inside a backend transaction, or inside another subtransaction. This can be useful when, for example, statements in a transaction may harmlessly fail and you don't want them to abort the entire transaction. Here's an example of how a temporary table may be dropped before re-creating it, without failing if the table did not exist:

void do_job(connection &C)
{
string const temptable = "fleetingtable";
work W(C, "do_job");
do_firstpart(W);
// Attempt to delete our temporary table if it already existed.
try
{
subtransaction S(W, "droptemp");
S.exec0("DROP TABLE " + temptable);
S.commit();
}
catch (undefined_table const &)
{
// Table did not exist. Which is what we were hoping to achieve anyway.
// Carry on without regrets.
}
// S may have gone into a failed state and been destroyed, but the
// upper-level transaction W is still fine. We can continue to use it.
W.exec0("CREATE TEMP TABLE " + temptable + "(bar integer, splat
varchar)");
do_lastpart(W);
}

(This is just an example. If you really wanted to do drop a table without an error if it doesn't exist, you'd use DROP TABLE IF EXISTS.)

There are no isolation levels inside a transaction. They are not needed because all actions within the same backend transaction are always performed sequentially anyway.

Constructor & Destructor Documentation

◆ subtransaction() [1/2]

pqxx::subtransaction::subtransaction ( dbtransaction t,
std::string const &  Name = std::string{} 
)
explicit

Nest a subtransaction nested in another transaction.

Implementation of the pqxx::subtransaction class.

pqxx::transaction is a nested transaction, i.e. one within a transaction

Copyright (c) 2000-2020, Jeroen T. Vermeulen.

See COPYING for copyright license. If you did not receive a file called COPYING with this source code, please notify the distributor of this mistake, or contact the author.

References pqxx::connection::adorn_name(), pqxx::transaction_base::conn(), pqxx::dbtransaction::dbtransaction(), pqxx::transaction_base::direct_exec(), and pqxx::internal::transactionfocus::transactionfocus().

◆ subtransaction() [2/2]

pqxx::subtransaction::subtransaction ( subtransaction t,
std::string const &  name = std::string{} 
)
explicit

Nest a subtransaction in another subtransaction.

References pqxx::transaction_base::direct_exec().

◆ ~subtransaction()

virtual pqxx::subtransaction::~subtransaction ( )
overridevirtualnoexcept

The documentation for this class was generated from the following files: