14 #ifndef PQXX_H_TRANSACTION_BASE 15 #define PQXX_H_TRANSACTION_BASE 17 #include <string_view> 29 #include "pqxx/connection.hxx" 30 #include "pqxx/internal/concat.hxx" 31 #include "pqxx/internal/encoding_group.hxx" 32 #include "pqxx/isolation.hxx" 33 #include "pqxx/result.hxx" 34 #include "pqxx/row.hxx" 35 #include "pqxx/stream_from.hxx" 39 class transaction_subtransaction;
40 class transaction_sql_cursor;
41 class transaction_stream_to;
42 class transaction_transaction_focus;
51 class transaction_focus;
115 template<
typename... ARGS> [[nodiscard]]
auto esc(ARGS &&...args)
const 118 return conn().esc(std::forward<ARGS>(args)...);
133 template<
typename... ARGS> [[nodiscard]]
auto esc_raw(ARGS &&...args)
const 135 return conn().esc_raw(std::forward<ARGS>(args)...);
142 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
145 #include "pqxx/internal/ignore-deprecated-pre.hxx" 146 return conn().unesc_raw(text);
147 #include "pqxx/internal/ignore-deprecated-post.hxx" 156 return conn().unesc_bin(text);
163 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
166 #include "pqxx/internal/ignore-deprecated-pre.hxx" 167 return conn().unesc_raw(text);
168 #include "pqxx/internal/ignore-deprecated-post.hxx" 175 [[nodiscard]] std::basic_string<std::byte>
unesc_bin(
char const text[])
177 return conn().unesc_bin(text);
182 template<
typename T> [[nodiscard]] std::string
quote(T
const &t)
const 184 return conn().quote(t);
188 "Use std::basic_string<std::byte> instead of binarystring.")]] std::string
195 [[deprecated(
"Use quote(std::basic_string_view<std::byte>).")]] std::string
196 quote_raw(
unsigned char const bin[], std::size_t len)
const 202 [[deprecated(
"Use quote(std::basic_string_view<std::byte>).")]] std::string
203 quote_raw(
zview bin)
const;
205 #if defined(PQXX_HAVE_CONCEPTS) 208 template<binary DATA>
209 [[nodiscard]] std::string quote_raw(DATA
const &data)
const 211 return conn().quote_raw(data);
216 [[nodiscard]] std::string
quote_name(std::string_view identifier)
const 218 return conn().quote_name(identifier);
222 [[nodiscard]] std::string
223 esc_like(std::string_view bin,
char escape_char =
'\\')
const 225 return conn().esc_like(bin, escape_char);
256 exec(std::string_view query, std::string_view desc = std::string_view{});
265 std::stringstream
const &query, std::string_view desc = std::string_view{})
267 return exec(query.str(), desc);
278 return exec_n(0, query, desc);
290 return exec_n(1, query, desc).
front();
301 std::string_view desc = std::string_view{});
307 template<
typename TYPE>
310 row const r{exec1(query, desc)};
311 if (std::size(r) != 1)
313 "Queried single value from result with ", std::size(r),
" columns.")};
314 return r[0].as<TYPE>();
366 template<
typename... TYPE>
367 [[nodiscard]]
auto stream(std::string_view query) &
370 return pqxx::internal::owning_stream_input_iteration<TYPE...>{
371 std::unique_ptr<stream_from>{
404 template<
typename... Args>
result exec_params(
zview query, Args &&...args)
416 return exec_params_n(1, query, std::forward<Args>(args)...).
front();
424 return exec_params_n(0, query, std::forward<Args>(args)...);
430 template<
typename... Args>
433 auto const r{exec_params(query, std::forward<Args>(args)...)};
434 check_rowcount_params(rows, std::size(r));
472 template<
typename... Args>
476 return internal_exec_prepared(statement, pp.
make_c_params());
482 template<
typename... Args>
485 return exec_prepared_n(1, statement, std::forward<Args>(args)...).
front();
491 template<
typename... Args>
494 return exec_prepared_n(0, statement, std::forward<Args>(args)...);
501 template<
typename... Args>
505 auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
506 check_rowcount_prepared(statement, rows, std::size(r));
516 void process_notice(
char const msg[])
const { m_conn.process_notice(msg); }
536 void set_variable(std::string_view var, std::string_view value);
542 std::string get_variable(std::string_view);
545 [[nodiscard]] std::string_view
name() const &noexcept {
return m_name; }
554 std::shared_ptr<std::string> rollback_cmd) :
555 m_conn{c}, m_name{tname}, m_rollback_cmd{rollback_cmd}
570 void register_transaction();
573 void close() noexcept;
576 virtual void do_commit() = 0;
582 virtual void do_abort();
587 m_rollback_cmd = cmd;
591 result direct_exec(std::string_view, std::string_view desc =
""sv);
593 direct_exec(std::shared_ptr<std::string>, std::string_view desc =
""sv);
604 PQXX_PRIVATE
void check_pending_error();
606 template<
typename T>
bool parm_is_null(T *p)
const noexcept
610 template<
typename T>
bool parm_is_null(T)
const noexcept {
return false; }
613 internal_exec_prepared(
zview statement, internal::c_params
const &args);
615 result internal_exec_params(
zview query, internal::c_params
const &args);
618 void check_rowcount_prepared(
624 check_rowcount_params(std::size_t expected_rows, std::size_t actual_rows);
627 [[nodiscard]] std::string description()
const;
629 friend class pqxx::internal::gate::transaction_transaction_focus;
632 PQXX_PRIVATE
void register_pending_error(
zview) noexcept;
633 PQXX_PRIVATE
void register_pending_error(std::string &&) noexcept;
643 status m_status = status::active;
644 bool m_registered = false;
646 std::
string m_pending_error;
649 std::shared_ptr<
std::
string> m_rollback_cmd;
652 static constexpr
std::string_view s_type_name{
"transaction"sv};
659 std::string_view transaction_base::query_value<std::string_view>(
660 zview query, std::string_view desc) =
delete;
663 zview transaction_base::query_value<zview>(
664 zview query, std::string_view desc) =
delete;
672 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
678 inline constexpr
zview begin_cmd<read_committed, write_policy::read_write>{
681 inline constexpr
zview begin_cmd<read_committed, write_policy::read_only>{
682 "BEGIN READ ONLY"_zv};
684 inline constexpr
zview begin_cmd<repeatable_read, write_policy::read_write>{
685 "BEGIN ISOLATION LEVEL REPEATABLE READ"_zv};
687 inline constexpr
zview begin_cmd<repeatable_read, write_policy::read_only>{
688 "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"_zv};
690 inline constexpr
zview begin_cmd<serializable, write_policy::read_write>{
691 "BEGIN ISOLATION LEVEL SERIALIZABLE"_zv};
693 inline constexpr
zview begin_cmd<serializable, write_policy::read_only>{
694 "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"_zv};
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:72
void set_rollback_cmd(std::shared_ptr< std::string > cmd)
Set the rollback command.
Definition: transaction_base.hxx:585
Binary data corresponding to PostgreSQL's "BYTEA" binary-string type.
Definition: binarystring.hxx:54
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:182
result exec(std::stringstream const &query, std::string_view desc=std::string_view{})
Execute a command.
Definition: transaction_base.hxx:264
Result set containing data returned by a query or command.
Definition: result.hxx:67
result exec_prepared(zview statement, Args &&...args)
Execute a prepared statement, with optional arguments.
Definition: transaction_base.hxx:473
transaction_base(connection &c, std::string_view tname, std::shared_ptr< std::string > rollback_cmd)
Create a transaction (to be called by implementation classes only).
Definition: transaction_base.hxx:552
std::basic_string< std::byte > unesc_bin(char const text[])
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:175
Definition: connection.hxx:94
std::basic_string_view< std::byte > bytes_view() const
Read data as a std::basic_string_view<std::byte>.
Definition: binarystring.hxx:174
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:503
pqxx::internal::c_params make_c_params() const
For internal use: Generate a params object for use in calls.
Definition: params.cxx:92
result_size_type size_type
Definition: result.hxx:70
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:164
Base class for things that monopolise a transaction's attention.
Definition: transaction_focus.hxx:24
Build a parameter list for a parameterised or prepared statement.
Definition: params.hxx:213
result exec_params0(zview query, Args &&...args)
Definition: transaction_base.hxx:422
const zview begin_cmd
The SQL command for starting a given type of transaction.
reference front() const noexcept
Definition: row.cxx:55
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:161
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:523
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:196
std::basic_string_view< std::byte > binary_cast(TYPE const &data)
Cast binary data to a type that libpqxx will recognise as binary.
Definition: util.hxx:289
row exec_prepared1(zview statement, Args &&...args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:483
static stream_from query(transaction_base &tx, std::string_view q)
Factory: Execute query, and stream the results.
Definition: stream_from.hxx:87
std::string unesc_raw(zview text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:143
auto esc_raw(ARGS &&...args) const
Escape binary data for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:133
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:37
result exec_prepared0(zview statement, Args &&...args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:492
Internal items for libpqxx' own use. Do not use these yourself.
Definition: composite.hxx:79
TYPE query_value(zview query, std::string_view desc=std::string_view{})
Perform query, expecting exactly 1 row with 1 field, and convert it.
Definition: transaction_base.hxx:308
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:216
std::basic_string< std::byte > unesc_bin(zview text)
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:154
Connection to a database.
Definition: connection.hxx:180
result exec0(zview query, std::string_view desc=std::string_view{})
Execute command, which should return zero rows of data.
Definition: transaction_base.hxx:276
void process_notice(zview msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:519
std::string quote(binarystring const &t) const
Definition: transaction_base.hxx:189
std::string_view name() const &noexcept
Transaction name, if you passed one to the constructor; or empty string.
Definition: transaction_base.hxx:545
result exec_params_n(std::size_t rows, zview query, Args &&...args)
Definition: transaction_base.hxx:431
Reference to one row in a result.
Definition: row.hxx:42
row exec_params1(zview query, Args &&...args)
Definition: transaction_base.hxx:414
auto stream(std::string_view query) &
Execute a query, and loop over the results row by row.
Definition: transaction_base.hxx:367
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:22
row exec1(zview query, std::string_view desc=std::string_view{})
Execute command returning a single row of data.
Definition: transaction_base.hxx:288
Stream data from the database.
Definition: stream_from.hxx:71
std::string esc_like(std::string_view bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:223