14 #ifndef PQXX_H_TRANSACTION_BASE 15 #define PQXX_H_TRANSACTION_BASE 17 #include "pqxx/compiler-public.hxx" 18 #include "pqxx/internal/compiler-internal-pre.hxx" 20 #include <string_view> 31 #include "pqxx/connection.hxx" 32 #include "pqxx/internal/encoding_group.hxx" 33 #include "pqxx/isolation.hxx" 34 #include "pqxx/result.hxx" 35 #include "pqxx/row.hxx" 56 void unregister_me() noexcept;
57 void reg_pending_error(std::string
const &) noexcept;
58 bool registered() const noexcept {
return m_registered; }
63 bool m_registered =
false;
70 class transaction_subtransaction;
71 class transaction_sql_cursor;
72 class transaction_stream_to;
73 class transaction_transactionfocus;
132 [[nodiscard]] std::string esc(
char const text[])
const 135 return conn().esc(text);
138 [[nodiscard]] std::string
esc(
char const text[],
size_t maxlen)
const 140 return conn().esc(text, maxlen);
143 [[nodiscard]] std::string
esc(std::string
const &text)
const 145 return conn().esc(text);
160 [[nodiscard]] std::string
161 esc_raw(
unsigned char const data[],
size_t len)
const 163 return conn().esc_raw(data, len);
166 [[nodiscard]] std::string esc_raw(std::string
const &)
const;
172 [[nodiscard]] std::string
unesc_raw(std::string
const &text)
const 174 return conn().unesc_raw(text);
181 [[nodiscard]] std::string
unesc_raw(
char const *text)
const 183 return conn().unesc_raw(text);
188 template<
typename T>[[nodiscard]] std::string
quote(T
const &t)
const 190 return conn().quote(t);
194 [[nodiscard]] std::string
197 return conn().quote_raw(bin, len);
200 [[nodiscard]] std::string quote_raw(std::string
const &bin)
const;
203 [[nodiscard]] std::string
quote_name(std::string_view identifier)
const 205 return conn().quote_name(identifier);
209 [[nodiscard]] std::string
210 esc_like(std::string
const &bin,
char escape_char =
'\\')
const 212 return conn().esc_like(bin, escape_char);
231 result exec(std::string_view query, std::string
const &desc = std::string{});
234 exec(std::stringstream
const &query, std::string
const &desc = std::string{})
236 return exec(query.str(), desc);
246 exec0(std::string
const &query, std::string
const &desc = std::string{})
248 return exec_n(0, query, desc);
258 row exec1(std::string
const &query, std::string
const &desc = std::string{})
260 return exec_n(1, query, desc).
front();
271 std::string
const &desc = std::string{});
274 template<
typename TYPE>
276 std::string
const &query, std::string
const &desc = std::string{})
278 row const r{exec1(query, desc)};
280 throw usage_error{
"Queried single value from result with " +
282 return r[0].as<TYPE>();
314 template<
typename... Args>
318 return internal_exec_params(
319 query, internal::params(std::forward<Args>(args)...));
325 template<
typename... Args>
328 return exec_params_n(1, query, std::forward<Args>(args)...).
front();
334 template<
typename... Args>
337 return exec_params_n(0, query, std::forward<Args>(args)...);
343 template<
typename... Args>
346 auto const r{exec_params(query, std::forward<Args>(args)...)};
347 check_rowcount_params(rows, r.size());
381 template<
typename... Args>
384 return internal_exec_prepared(
386 internal::params(std::forward<Args>(args)...));
389 template<
typename... Args>
392 return internal_exec_prepared(
393 statement, internal::params(std::forward<Args>(args)...));
399 template<
typename... Args>
402 return exec_prepared_n(1, statement, std::forward<Args>(args)...).
front();
405 template<
typename... Args>
408 return exec_prepared_n(1, statement, std::forward<Args>(args)...).
front();
414 template<
typename... Args>
417 return exec_prepared_n(0, statement, std::forward<Args>(args)...);
420 template<
typename... Args>
423 return exec_prepared_n(0, statement, std::forward<Args>(args)...);
430 template<
typename... Args>
434 auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
435 check_rowcount_prepared(statement, rows, r.size());
439 template<
typename... Args>
443 auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
444 check_rowcount_prepared(statement, rows, r.size());
454 void process_notice(
char const msg[])
const { m_conn.process_notice(msg); }
459 m_conn.process_notice(msg);
477 void set_variable(std::string_view var, std::string_view value);
483 std::string get_variable(std::string_view);
493 void register_transaction();
496 void close() noexcept;
499 virtual void do_commit() = 0;
501 virtual void do_abort() = 0;
506 result direct_exec(std::string_view);
507 result direct_exec(std::shared_ptr<std::string>);
519 PQXX_PRIVATE
void check_pending_error();
521 template<
typename T>
bool parm_is_null(T *p)
const noexcept
525 template<
typename T>
bool parm_is_null(T)
const noexcept {
return false; }
527 result internal_exec_prepared(
zview statement, internal::params
const &args);
530 internal_exec_params(std::string
const &query, internal::params
const &args);
533 void check_rowcount_prepared(
538 void check_rowcount_params(
size_t expected_rows,
size_t actual_rows);
540 friend class pqxx::internal::gate::transaction_transactionfocus;
543 PQXX_PRIVATE
void register_pending_error(std::string
const &) noexcept;
548 status m_status = status::active;
549 bool m_registered =
false;
550 std::string m_pending_error;
558 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
563 inline constexpr
zview begin_cmd<read_committed, write_policy::read_write>{
566 inline constexpr
zview begin_cmd<read_committed, write_policy::read_only>{
569 inline constexpr
zview begin_cmd<repeatable_read, write_policy::read_write>{
570 "BEGIN ISOLATION LEVEL REPEATABLE READ"};
572 inline constexpr
zview begin_cmd<repeatable_read, write_policy::read_only>{
573 "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"};
575 inline constexpr
zview begin_cmd<serializable, write_policy::read_write>{
576 "BEGIN ISOLATION LEVEL SERIALIZABLE"};
578 inline constexpr
zview begin_cmd<serializable, write_policy::read_only>{
579 "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"};
582 #include "pqxx/internal/compiler-internal-post.hxx" transactionfocus(transaction_base &t)
Definition: transaction_base.hxx:45
result exec_prepared(std::string const &statement, Args &&... args)
Execute a prepared statement, with optional arguments.
Definition: transaction_base.hxx:382
const zview begin_cmd
The SQL command for starting a given type of transaction.
Private namespace for libpqxx's internal use; do not access.
Definition: connection.hxx:59
row exec_params1(std::string const &query, Args &&... args)
Definition: transaction_base.hxx:326
row exec1(std::string const &query, std::string const &desc=std::string{})
Execute query returning a single row of data.
Definition: transaction_base.hxx:258
std::string esc_like(std::string const &bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:210
result exec_params0(std::string const &query, Args &&... args)
Definition: transaction_base.hxx:335
std::string esc(std::string const &text) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:143
std::string esc_raw(unsigned char const data[], size_t len) const
Escape binary data for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:161
row exec_prepared1(std::string const &statement, Args &&... args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:400
result exec_prepared0(zview statement, Args &&... args)
Definition: transaction_base.hxx:421
bool registered() const noexcept
Definition: transaction_base.hxx:58
std::string quote_raw(unsigned char const bin[], size_t len) const
Binary-escape and quote a binarystring for use as an SQL constant.
Definition: transaction_base.hxx:195
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:181
result exec_prepared_n(result::size_type rows, zview statement, Args &&... args)
Definition: transaction_base.hxx:441
TYPE query_value(std::string const &query, std::string const &desc=std::string{})
Execute query, expecting exactly 1 row with 1 field.
Definition: transaction_base.hxx:275
std::string esc(char const text[], size_t maxlen) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:138
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:478
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
Connection to a database.
Definition: connection.hxx:135
std::string unesc_raw(std::string const &text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:172
constexpr char const * c_str() const noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition: zview.hxx:41
Ensure proper opening/closing of GUEST objects related to a "host" object.
Definition: util.hxx:252
reference front() const noexcept
Definition: row.cxx:56
result exec_params_n(size_t rows, std::string const &query, Args &&... args)
Definition: transaction_base.hxx:344
void process_notice(std::string const &msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:457
result exec0(std::string const &query, std::string const &desc=std::string{})
Execute query, which should zero rows of data.
Definition: transaction_base.hxx:246
result exec_prepared0(std::string const &statement, Args &&... args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:415
result exec_params(std::string const &query, Args &&... args)
Execute an SQL statement with parameters.
Definition: transaction_base.hxx:316
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:97
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:203
Definition: connection.hxx:65
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:464
result exec(std::stringstream const &query, std::string const &desc=std::string{})
Definition: transaction_base.hxx:234
Result set containing data returned by a query or command.
Definition: result.hxx:70
result_size_type size_type
Definition: result.hxx:73
Helper base class: object descriptions for error messages and such.
Definition: util.hxx:208
Definition: transaction_base.hxx:42
result exec_prepared(zview statement, Args &&... args)
Definition: transaction_base.hxx:390
transaction_base & m_trans
Definition: transaction_base.hxx:60
Reference to one row in a result.
Definition: row.hxx:38
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:164
row exec_prepared1(zview statement, Args &&... args)
Definition: transaction_base.hxx:406
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:32
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:188
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.
Definition: transaction_base.hxx:431