|
| subtransaction (dbtransaction &T, const PGSTD::string &Name=PGSTD::string()) |
| Nest a subtransaction nested in another transaction. More...
|
|
| subtransaction (subtransaction &T, const PGSTD::string &Name=PGSTD::string()) |
| Nest a subtransaction in another subtransaction. More...
|
|
void | abort () |
| Abort the transaction. More...
|
|
const PGSTD::string & | classname () const throw () |
|
void | commit () |
| Commit the transaction. More...
|
|
connection_base & | conn () const |
| Connection this transaction is running in. More...
|
|
PGSTD::string | description () const |
|
PGSTD::string | esc (const char str[]) const |
| Escape string for use as SQL string literal in this transaction. More...
|
|
PGSTD::string | esc (const char str[], size_t maxlen) const |
| Escape string for use as SQL string literal in this transaction. More...
|
|
PGSTD::string | esc (const PGSTD::string &str) const |
| Escape string for use as SQL string literal in this transaction. More...
|
|
PGSTD::string | esc_raw (const unsigned char str[], size_t len) const |
| Escape binary data for use as SQL string literal in this transaction. More...
|
|
PGSTD::string | esc_raw (const PGSTD::string &) const |
| Escape binary data for use as SQL string literal in this transaction. More...
|
|
result | exec (const PGSTD::string &Query, const PGSTD::string &Desc=PGSTD::string()) |
| Execute query. More...
|
|
result | exec (const PGSTD::stringstream &Query, const PGSTD::string &Desc=PGSTD::string()) |
|
PGSTD::string | get_variable (const PGSTD::string &) |
| Get currently applicable value of variable. More...
|
|
const PGSTD::string & | name () const throw () |
|
internal::parameterized_invocation | parameterized (const PGSTD::string &query) |
| Parameterize a statement. More...
|
|
template<typename T > |
PGSTD::string | quote (const T &t) const |
| Represent object as SQL string, including quoting & escaping. More...
|
|
PGSTD::string | quote_name (const PGSTD::string &identifier) const |
| Escape an SQL identifier for use in a query. More...
|
|
PGSTD::string | quote_raw (const unsigned char str[], size_t len) const |
| Binary-escape and quote a binarystring for use as an SQL constant. More...
|
|
PGSTD::string | quote_raw (const PGSTD::string &str) const |
|
void | set_variable (const PGSTD::string &Var, const PGSTD::string &Val) |
| Set session variable in this connection. More...
|
|
|
prepare::invocation | prepared (const PGSTD::string &statement=PGSTD::string()) |
| Execute prepared statement. More...
|
|
|
void | process_notice (const char Msg[]) const |
| Have connection process warning message. More...
|
|
void | process_notice (const PGSTD::string &Msg) const |
| Have connection process warning message. More...
|
|
"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_base &C)
{
const string temptable = "fleetingtable";
C.inhibit_reactivation(true);
do_firstpart(W);
try
{
S.exec("DROP TABLE " + temptable);
S.commit();
}
catch (const undefined_table &)
{
}
W.exec("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.
void pqxx::transaction_base::commit |
( |
| ) |
|
|
inherited |
Commit the transaction.
Unless this function is called explicitly, the transaction will not be committed (actually the nontransaction implementation breaks this rule, hence the name).
Once this function returns, the whole transaction will typically be irrevocably completed in the database. There is also, however, a minute risk that the connection to the database may be lost at just the wrong moment. In that case, libpqxx may be unable to determine whether the transaction was completed or aborted and an in_doubt_error will be thrown to make this fact known to the caller. The robusttransaction implementation takes some special precautions to reduce this risk.
prepare::invocation pqxx::transaction_base::prepared |
( |
const PGSTD::string & |
statement = PGSTD::string() | ) |
|
|
inherited |
Execute prepared statement.
Prepared statements are defined using the connection classes' prepare() function, and continue to live on in the ongoing session regardless of the context they were defined in (unless explicitly dropped using the connection's unprepare() function). Their execution however, like other forms of query execution, requires a transaction object.
Just like param_declaration is a helper class that lets you tag parameter declarations onto the statement declaration, the invocation class returned here lets you tag parameter values onto the call:
{
return T.prepared("mystatement")("param1")(2)()(4).exec();
}
Here, parameter 1 (written as "<tt>$1</tt>" in the statement's body) is a string that receives the value "param1"; the second parameter is an integer with the value 2; the third receives a null, making its type irrelevant; and number 4 again is an integer. The ultimate invocation of exec() is essential; if you forget this, nothing happens.
To see whether any prepared statement has been defined under a given name, use:
T.prepared("mystatement").exists()
- Warning
- Do not try to execute a prepared statement manually through direct SQL statements. This is likely not to work, and even if it does, is likely to be slower than using the proper libpqxx functions. Also, libpqxx knows how to emulate prepared statements if some part of the infrastructure does not support them.
-
Actual definition of the prepared statement on the backend may be deferred until its first use, which means that any errors in the prepared statement may not show up until it is executed–and perhaps abort the ongoing transaction in the process.
If you leave out the statement name, the call refers to the nameless statement instead.