14 #ifndef PQXX_H_TRANSACTION_BASE 15 #define PQXX_H_TRANSACTION_BASE 17 #if !defined(PQXX_HEADER_PRE) 18 # error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>." 21 #include <string_view> 33 #include "pqxx/connection.hxx" 34 #include "pqxx/internal/concat.hxx" 35 #include "pqxx/internal/encoding_group.hxx" 36 #include "pqxx/isolation.hxx" 37 #include "pqxx/result.hxx" 38 #include "pqxx/row.hxx" 39 #include "pqxx/stream_from.hxx" 43 class transaction_subtransaction;
44 class transaction_sql_cursor;
45 class transaction_stream_to;
46 class transaction_transaction_focus;
55 class transaction_focus;
119 template<
typename... ARGS> [[nodiscard]]
auto esc(ARGS &&...args)
const 122 return conn().esc(std::forward<ARGS>(args)...);
137 template<
typename... ARGS> [[nodiscard]]
auto esc_raw(ARGS &&...args)
const 139 return conn().esc_raw(std::forward<ARGS>(args)...);
146 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
149 #include "pqxx/internal/ignore-deprecated-pre.hxx" 150 return conn().unesc_raw(text);
151 #include "pqxx/internal/ignore-deprecated-post.hxx" 160 return conn().unesc_bin(text);
167 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
170 #include "pqxx/internal/ignore-deprecated-pre.hxx" 171 return conn().unesc_raw(text);
172 #include "pqxx/internal/ignore-deprecated-post.hxx" 179 [[nodiscard]] std::basic_string<std::byte>
unesc_bin(
char const text[])
181 return conn().unesc_bin(text);
186 template<
typename T> [[nodiscard]] std::string
quote(T
const &t)
const 188 return conn().quote(t);
192 "Use std::basic_string<std::byte> instead of binarystring.")]] std::string
199 [[deprecated(
"Use quote(std::basic_string_view<std::byte>).")]] std::string
200 quote_raw(
unsigned char const bin[], std::size_t len)
const 206 [[deprecated(
"Use quote(std::basic_string_view<std::byte>).")]] std::string
207 quote_raw(
zview bin)
const;
209 #if defined(PQXX_HAVE_CONCEPTS) 212 template<binary DATA>
213 [[nodiscard]] std::string quote_raw(DATA
const &data)
const 215 return conn().quote_raw(data);
220 [[nodiscard]] std::string
quote_name(std::string_view identifier)
const 222 return conn().quote_name(identifier);
226 [[nodiscard]] std::string
227 esc_like(std::string_view bin,
char escape_char =
'\\')
const 229 return conn().esc_like(bin, escape_char);
260 exec(std::string_view query, std::string_view desc = std::string_view{});
269 std::stringstream
const &query, std::string_view desc = std::string_view{})
271 return exec(query.str(), desc);
282 return exec_n(0, query, desc);
294 return exec_n(1, query, desc).
front();
305 std::string_view desc = std::string_view{});
311 template<
typename TYPE>
314 row const r{exec1(query, desc)};
315 if (std::size(r) != 1)
317 "Queried single value from result with ", std::size(r),
" columns.")};
318 return r[0].as<TYPE>();
370 template<
typename... TYPE>
371 [[nodiscard]]
auto stream(std::string_view query) &
374 return pqxx::internal::owning_stream_input_iteration<TYPE...>{
375 std::unique_ptr<stream_from>{
408 template<
typename... Args>
result exec_params(
zview query, Args &&...args)
420 return exec_params_n(1, query, std::forward<Args>(args)...).
front();
428 return exec_params_n(0, query, std::forward<Args>(args)...);
434 template<
typename... Args>
437 auto const r{exec_params(query, std::forward<Args>(args)...)};
438 check_rowcount_params(rows, std::size(r));
476 template<
typename... Args>
480 return internal_exec_prepared(statement, pp.
make_c_params());
486 template<
typename... Args>
489 return exec_prepared_n(1, statement, std::forward<Args>(args)...).
front();
495 template<
typename... Args>
498 return exec_prepared_n(0, statement, std::forward<Args>(args)...);
505 template<
typename... Args>
509 auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
510 check_rowcount_prepared(statement, rows, std::size(r));
520 void process_notice(
char const msg[])
const { m_conn.process_notice(msg); }
546 "Set transaction-local variables using SQL SET statements.")]]
void 547 set_variable(std::string_view var, std::string_view value);
553 [[deprecated(
"Read variables using SQL SHOW statements.")]] std::string
554 get_variable(std::string_view);
558 [[nodiscard]] std::string_view
name() const &noexcept {
return m_name; }
567 std::shared_ptr<std::string> rollback_cmd) :
568 m_conn{c}, m_name{tname}, m_rollback_cmd{rollback_cmd}
583 void register_transaction();
586 void close() noexcept;
589 virtual void do_commit() = 0;
595 virtual void do_abort();
600 m_rollback_cmd = cmd;
604 result direct_exec(std::string_view, std::string_view desc =
""sv);
606 direct_exec(std::shared_ptr<std::string>, std::string_view desc =
""sv);
617 PQXX_PRIVATE
void check_pending_error();
620 internal_exec_prepared(
zview statement, internal::c_params
const &args);
622 result internal_exec_params(
zview query, internal::c_params
const &args);
625 void check_rowcount_prepared(
631 check_rowcount_params(std::size_t expected_rows, std::size_t actual_rows);
634 [[nodiscard]] std::string description()
const;
636 friend class pqxx::internal::gate::transaction_transaction_focus;
639 PQXX_PRIVATE
void register_pending_error(
zview) noexcept;
640 PQXX_PRIVATE
void register_pending_error(std::string &&) noexcept;
650 status m_status = status::active;
651 bool m_registered = false;
653 std::
string m_pending_error;
656 std::shared_ptr<
std::
string> m_rollback_cmd;
658 static constexpr
std::string_view s_type_name{
"transaction"sv};
665 std::string_view transaction_base::query_value<std::string_view>(
666 zview query, std::string_view desc) =
delete;
669 zview transaction_base::query_value<zview>(
670 zview query, std::string_view desc) =
delete;
678 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
683 inline constexpr
zview begin_cmd<read_committed, write_policy::read_write>{
686 inline constexpr
zview begin_cmd<read_committed, write_policy::read_only>{
687 "BEGIN READ ONLY"_zv};
689 inline constexpr
zview begin_cmd<repeatable_read, write_policy::read_write>{
690 "BEGIN ISOLATION LEVEL REPEATABLE READ"_zv};
692 inline constexpr
zview begin_cmd<repeatable_read, write_policy::read_only>{
693 "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"_zv};
695 inline constexpr
zview begin_cmd<serializable, write_policy::read_write>{
696 "BEGIN ISOLATION LEVEL SERIALIZABLE"_zv};
698 inline constexpr
zview begin_cmd<serializable, write_policy::read_only>{
699 "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"_zv};
Stream data from the database.
Definition: stream_from.hxx:73
std::basic_string_view< std::byte > bytes_view() const
Read data as a std::basic_string_view<std::byte>.
Definition: binarystring.hxx:178
Base class for things that monopolise a transaction's attention.
Definition: transaction_focus.hxx:28
row exec_prepared1(zview statement, Args &&...args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:487
auto esc_raw(ARGS &&...args) const
Escape binary data for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:137
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:200
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
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:312
Reference to one row in a result.
Definition: row.hxx:46
static stream_from query(transaction_base &tx, std::string_view q)
Factory: Execute query, and stream the results.
Definition: stream_from.hxx:89
Connection to a database.
Definition: connection.hxx:185
std::string unesc_raw(zview text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:147
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:158
pqxx::internal::c_params make_c_params() const
For internal use: Generate a params object for use in calls.
Definition: params.cxx:94
row exec_params1(zview query, Args &&...args)
Definition: transaction_base.hxx:418
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:220
std::string quote(binarystring const &t) const
Definition: transaction_base.hxx:193
row exec1(zview query, std::string_view desc=std::string_view{})
Execute command returning a single row of data.
Definition: transaction_base.hxx:292
const zview begin_cmd
The SQL command for starting a given type of transaction.
result exec_prepared(zview statement, Args &&...args)
Execute a prepared statement, with optional arguments.
Definition: transaction_base.hxx:477
void process_notice(zview msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:523
result exec_params_n(std::size_t rows, zview query, Args &&...args)
Definition: transaction_base.hxx:435
std::string esc_like(std::string_view bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:227
Definition: connection.hxx:99
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:507
result_size_type size_type
Definition: result.hxx:75
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:173
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:186
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:76
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:179
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:565
Result set containing data returned by a query or command.
Definition: result.hxx:72
result exec0(zview query, std::string_view desc=std::string_view{})
Execute command, which should return zero rows of data.
Definition: transaction_base.hxx:280
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:37
Internal items for libpqxx' own use. Do not use these yourself.
Definition: composite.hxx:82
std::string_view name() const &noexcept
Transaction name, if you passed one to the constructor; or empty string.
Definition: transaction_base.hxx:558
reference front() const noexcept
Definition: row.cxx:60
void set_rollback_cmd(std::shared_ptr< std::string > cmd)
Set the rollback command.
Definition: transaction_base.hxx:598
result exec_prepared0(zview statement, Args &&...args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:496
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:168
constexpr connection & conn() const noexcept
The connection in which this transaction lives.
Definition: transaction_base.hxx:527
auto stream(std::string_view query) &
Execute a query, and loop over the results row by row.
Definition: transaction_base.hxx:371
Binary data corresponding to PostgreSQL's "BYTEA" binary-string type.
Definition: binarystring.hxx:58
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:297
result exec_params0(zview query, Args &&...args)
Definition: transaction_base.hxx:426
result exec(std::stringstream const &query, std::string_view desc=std::string_view{})
Execute a command.
Definition: transaction_base.hxx:268
Build a parameter list for a parameterised or prepared statement.
Definition: params.hxx:219