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" 36 #include "pqxx/stream_from.hxx" 40 class transaction_subtransaction;
41 class transaction_sql_cursor;
42 class transaction_stream_to;
43 class transaction_transactionfocus;
102 [[nodiscard]] std::string esc(
char const text[])
const 105 return conn().esc(text);
108 [[nodiscard]] std::string
esc(
char const text[], std::size_t maxlen)
const 110 return conn().esc(text, maxlen);
113 [[nodiscard]] std::string
esc(std::string
const &text)
const 115 return conn().esc(text);
118 [[nodiscard]] std::string
esc(std::string_view text)
const 120 return conn().esc(text);
135 [[nodiscard]] std::string
136 esc_raw(
unsigned char const data[], std::size_t len)
const 138 return conn().esc_raw(data, len);
141 [[nodiscard]] std::string esc_raw(std::string
const &)
const;
147 [[nodiscard]] std::string
unesc_raw(std::string
const &text)
const 149 return conn().unesc_raw(text);
156 [[nodiscard]] std::string
unesc_raw(
char const *text)
const 158 return conn().unesc_raw(text);
163 template<
typename T>[[nodiscard]] std::string
quote(T
const &t)
const 165 return conn().quote(t);
169 [[nodiscard]] std::string
170 quote_raw(
unsigned char const bin[], std::size_t len)
const 172 return conn().quote_raw(bin, len);
175 [[nodiscard]] std::string quote_raw(std::string
const &bin)
const;
178 [[nodiscard]] std::string
quote_name(std::string_view identifier)
const 180 return conn().quote_name(identifier);
184 [[nodiscard]] std::string
185 esc_like(std::string
const &bin,
char escape_char =
'\\')
const 187 return conn().esc_like(bin, escape_char);
206 result exec(std::string_view query, std::string
const &desc = std::string{});
209 exec(std::stringstream
const &query, std::string
const &desc = std::string{})
211 return exec(query.str(), desc);
221 exec0(std::string
const &query, std::string
const &desc = std::string{})
223 return exec_n(0, query, desc);
233 row exec1(std::string
const &query, std::string
const &desc = std::string{})
235 return exec_n(1, query, desc).
front();
246 std::string
const &desc = std::string{});
249 template<
typename TYPE>
251 std::string
const &query, std::string
const &desc = std::string{})
253 row const r{exec1(query, desc)};
254 if (std::size(r) != 1)
256 "Queried single value from result with " +
to_string(std::size(r)) +
258 return r[0].as<TYPE>();
301 template<
typename... TYPE>[[nodiscard]]
auto stream(std::string_view query)
303 return pqxx::internal::owning_stream_input_iteration<TYPE...>{
304 std::make_unique<pqxx::stream_from>(*
this,
from_query_t{}, query)};
336 template<
typename... Args>
340 return internal_exec_params(
341 query, internal::params(std::forward<Args>(args)...));
347 template<
typename... Args>
350 return exec_params_n(1, query, std::forward<Args>(args)...).
front();
356 template<
typename... Args>
359 return exec_params_n(0, query, std::forward<Args>(args)...);
365 template<
typename... Args>
369 auto const r{exec_params(query, std::forward<Args>(args)...)};
370 check_rowcount_params(rows, std::size(r));
405 template<
typename... Args>
408 return internal_exec_prepared(
409 statement, internal::params(std::forward<Args>(args)...));
415 template<
typename... Args>
418 return exec_prepared_n(1, statement, std::forward<Args>(args)...).
front();
424 template<
typename... Args>
427 return exec_prepared_n(0, statement, std::forward<Args>(args)...);
434 template<
typename... Args>
438 auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
439 check_rowcount_prepared(statement, rows, std::size(r));
449 void process_notice(
char const msg[])
const { m_conn.process_notice(msg); }
454 m_conn.process_notice(msg);
472 void set_variable(std::string_view var, std::string_view value);
478 std::string get_variable(std::string_view);
488 void register_transaction();
491 void close() noexcept;
494 virtual void do_commit() = 0;
496 virtual void do_abort() = 0;
501 result direct_exec(std::string_view);
502 result direct_exec(std::shared_ptr<std::string>);
514 PQXX_PRIVATE
void check_pending_error();
516 template<
typename T>
bool parm_is_null(T *p)
const noexcept
520 template<
typename T>
bool parm_is_null(T)
const noexcept {
return false; }
522 result internal_exec_prepared(
zview statement, internal::params
const &args);
525 internal_exec_params(std::string
const &query, internal::params
const &args);
528 void check_rowcount_prepared(
534 check_rowcount_params(std::size_t expected_rows, std::size_t actual_rows);
536 friend class pqxx::internal::gate::transaction_transactionfocus;
537 PQXX_PRIVATE
void register_focus(internal::transactionfocus *);
538 PQXX_PRIVATE
void unregister_focus(internal::transactionfocus *) noexcept;
539 PQXX_PRIVATE
void register_pending_error(std::string
const &) noexcept;
544 status m_status = status::active;
545 bool m_registered =
false;
546 std::string m_pending_error;
553 std::string_view transaction_base::query_value<std::string_view>(
554 std::string
const &query, std::string
const &desc) =
delete;
557 zview transaction_base::query_value<zview>(
558 std::string
const &query, std::string
const &desc) =
delete;
567 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
572 inline constexpr
zview begin_cmd<read_committed, write_policy::read_write>{
575 inline constexpr
zview begin_cmd<read_committed, write_policy::read_only>{
578 inline constexpr
zview begin_cmd<repeatable_read, write_policy::read_write>{
579 "BEGIN ISOLATION LEVEL REPEATABLE READ"};
581 inline constexpr
zview begin_cmd<repeatable_read, write_policy::read_only>{
582 "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"};
584 inline constexpr
zview begin_cmd<serializable, write_policy::read_write>{
585 "BEGIN ISOLATION LEVEL SERIALIZABLE"};
587 inline constexpr
zview begin_cmd<serializable, write_policy::read_only>{
588 "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"};
591 #include "pqxx/internal/compiler-internal-post.hxx" result exec_prepared0(zview statement, Args &&... args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:425
const zview begin_cmd
The SQL command for starting a given type of transaction.
result exec_params(std::string const &query, Args &&... args)
Execute an SQL statement with parameters.
Definition: transaction_base.hxx:338
Private namespace for libpqxx's internal use; do not access.
Definition: composite.hxx:70
std::string esc_like(std::string const &bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:185
row exec1(std::string const &query, std::string const &desc=std::string{})
Execute query returning a single row of data.
Definition: transaction_base.hxx:233
result exec_params0(std::string const &query, Args &&... args)
Definition: transaction_base.hxx:357
std::string esc(std::string const &text) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:113
std::string esc(char const text[], std::size_t maxlen) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:108
Marker for stream_from constructors: "stream from query.".
Definition: types.hxx:59
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:156
reference front() const noexcept
Definition: row.cxx:54
Connection to a database.
Definition: connection.hxx:164
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:250
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:498
Helper base class: object descriptions for error messages and such.
Definition: util.hxx:217
result exec_prepared(zview statement, Args &&... args)
Execute a prepared statement, with optional arguments.
Definition: transaction_base.hxx:406
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
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:147
Reference to one row in a result.
Definition: row.hxx:43
auto stream(std::string_view query)
Execute a query, and loop over the results row by row.
Definition: transaction_base.hxx:301
result_size_type size_type
Definition: result.hxx:73
result exec(std::stringstream const &query, std::string const &desc=std::string{})
Definition: transaction_base.hxx:209
result exec_prepared_n(result::size_type rows, zview statement, Args &&... args)
Execute a prepared statement, expect a result with given number of rows.
Definition: transaction_base.hxx:436
std::string esc_raw(unsigned char const data[], std::size_t len) const
Escape binary data for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:136
std::string quote_raw(unsigned char const bin[], std::size_t len) const
Binary-escape and quote a binary string for use as an SQL constant.
Definition: transaction_base.hxx:170
void process_notice(std::string const &msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:452
Result set containing data returned by a query or command.
Definition: result.hxx:70
row exec_prepared1(zview statement, Args &&... args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:416
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:36
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:178
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:67
Definition: connection.hxx:94
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:164
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:459
result exec_params_n(std::size_t rows, std::string const &query, Args &&... args)
Definition: transaction_base.hxx:367
row exec_params1(std::string const &query, Args &&... args)
Definition: transaction_base.hxx:348
std::string esc(std::string_view text) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:118
result exec0(std::string const &query, std::string const &desc=std::string{})
Execute query, which should zero rows of data.
Definition: transaction_base.hxx:221
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:163