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/concat.hxx"
33 #include "pqxx/internal/encoding_group.hxx"
34 #include "pqxx/isolation.hxx"
35 #include "pqxx/result.hxx"
36 #include "pqxx/row.hxx"
37 #include "pqxx/stream_from.hxx"
41 class transaction_subtransaction;
42 class transaction_sql_cursor;
43 class transaction_stream_to;
44 class transaction_transaction_focus;
50 using namespace std::literals;
53 class transaction_focus;
111 [[nodiscard]] std::string
esc(
char const text[])
const
113 return conn().esc(
text);
116 [[nodiscard]] std::string
esc(
char const text[], std::size_t maxlen)
const
118 return conn().esc(
text, maxlen);
121 [[nodiscard]] std::string
esc(std::string_view text)
const
123 return conn().esc(
text);
138 [[nodiscard]] std::string
139 esc_raw(
unsigned char const data[], std::size_t len)
const
141 return to_string(std::basic_string_view<std::byte>{
142 reinterpret_cast<std::byte
const *
>(data), len});
145 [[nodiscard]] std::string esc_raw(
zview)
const;
153 return conn().unesc_raw(
text);
160 [[nodiscard]] std::string
unesc_raw(
char const *text)
const
162 return conn().unesc_raw(
text);
167 template<
typename T> [[nodiscard]] std::string
quote(T
const &t)
const
169 return conn().quote(t);
173 "Use std::basic_string<std::byte> instead of binarystring.")]] std::string
180 [[deprecated(
"Use quote(std::basic_string_view<std::byte>).")]] std::string
181 quote_raw(
unsigned char const bin[], std::size_t len)
const
183 return quote(std::basic_string_view<std::byte>{
184 reinterpret_cast<std::byte
const *
>(bin), len});
188 [[deprecated(
"Use quote(std::basic_string_view<std::byte>).")]] std::string
189 quote_raw(
zview bin)
const;
192 [[nodiscard]] std::string
quote_name(std::string_view identifier)
const
194 return conn().quote_name(identifier);
198 [[nodiscard]] std::string
199 esc_like(std::string_view bin,
char escape_char =
'\\')
const
201 return conn().esc_like(bin, escape_char);
232 exec(std::string_view query, std::string_view desc = std::string_view{});
241 std::stringstream
const &query, std::string_view desc = std::string_view{})
243 return exec(query.str(), desc);
254 return exec_n(0, query, desc);
266 return exec_n(1, query, desc).
front();
277 std::string_view desc = std::string_view{});
283 template<
typename TYPE>
286 row const r{exec1(query, desc)};
287 if (std::size(r) != 1)
288 throw usage_error{internal::concat(
289 "Queried single value from result with ", std::size(r),
" columns.")};
290 return r[0].
as<TYPE>();
342 template<
typename... TYPE> [[nodiscard]]
auto stream(std::string_view query)
345 return pqxx::internal::owning_stream_input_iteration<TYPE...>{
346 std::unique_ptr<stream_from>{
391 return exec_params_n(1, query, std::forward<Args>(args)...).
front();
399 return exec_params_n(0, query, std::forward<Args>(args)...);
405 template<
typename... Args>
408 auto const r{exec_params(query, std::forward<Args>(args)...)};
409 check_rowcount_params(rows, std::size(r));
444 template<
typename... Args>
448 return internal_exec_prepared(statement, pp.
make_c_params());
454 template<
typename... Args>
457 return exec_prepared_n(1, statement, std::forward<Args>(args)...).
front();
463 template<
typename... Args>
466 return exec_prepared_n(0, statement, std::forward<Args>(args)...);
473 template<
typename... Args>
477 auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
478 check_rowcount_prepared(statement, rows, std::size(r));
508 void set_variable(std::string_view var, std::string_view value);
514 std::string get_variable(std::string_view);
517 [[nodiscard]] std::string_view
name() const noexcept {
return m_name; }
526 std::shared_ptr<std::string> rollback_cmd) :
527 m_conn{c}, m_name{tname}, m_rollback_cmd{rollback_cmd}
542 void register_transaction();
545 void close() noexcept;
548 virtual
void do_commit() = 0;
554 virtual
void do_abort();
557 void set_rollback_cmd(std::shared_ptr<std::
string> cmd)
559 m_rollback_cmd = cmd;
563 result direct_exec(std::string_view, std::string_view desc =
""sv);
565 direct_exec(std::shared_ptr<std::string>, std::string_view desc =
""sv);
576 PQXX_PRIVATE
void check_pending_error();
578 template<
typename T>
bool parm_is_null(T *p)
const noexcept
582 template<
typename T>
bool parm_is_null(T)
const noexcept {
return false; }
585 internal_exec_prepared(zview statement, internal::c_params
const &args);
587 result internal_exec_params(zview query, internal::c_params
const &args);
590 void check_rowcount_prepared(
596 check_rowcount_params(std::size_t expected_rows, std::size_t actual_rows);
599 [[nodiscard]] std::string description()
const;
601 friend class pqxx::internal::gate::transaction_transaction_focus;
604 PQXX_PRIVATE
void register_pending_error(
zview) noexcept;
605 PQXX_PRIVATE
void register_pending_error(std::string &&) noexcept;
615 status m_status = status::active;
616 bool m_registered = false;
618 std::
string m_pending_error;
621 std::shared_ptr<std::
string> m_rollback_cmd;
623 constexpr static std::string_view s_type_name{
"transaction"sv};
630 std::string_view transaction_base::query_value<std::string_view>(
631 zview query, std::string_view desc) =
delete;
634 zview transaction_base::query_value<zview>(
635 zview query, std::string_view desc) =
delete;
643 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
648 inline constexpr
zview begin_cmd<read_committed, write_policy::read_write>{
651 inline constexpr zview begin_cmd<read_committed, write_policy::read_only>{
652 "BEGIN READ ONLY"_zv};
654 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_write>{
655 "BEGIN ISOLATION LEVEL REPEATABLE READ"_zv};
657 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_only>{
658 "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"_zv};
660 inline constexpr zview begin_cmd<serializable, write_policy::read_write>{
661 "BEGIN ISOLATION LEVEL SERIALIZABLE"_zv};
663 inline constexpr zview begin_cmd<serializable, write_policy::read_only>{
664 "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"_zv};
667 #include "pqxx/internal/compiler-internal-post.hxx"
std::string esc(char const text[]) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:111
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:503
Internal items for libpqxx' own use. Do not use these yourself.
Definition: composite.hxx:74
const zview begin_cmd
The SQL command for starting a given type of transaction.
Definition: connection.hxx:98
Binary data corresponding to PostgreSQL's "BYTEA" binary-string type.
Definition: binarystring.hxx:57
std::basic_string_view< std::byte > bytes_view() const
Read data as a std::basic_string_view<std::byte>.
Definition: binarystring.hxx:176
Connection to a database.
Definition: connection.hxx:184
Build a parameter list for a parameterised or prepared statement.
Definition: prepared_statement.hxx:122
pqxx::internal::c_params make_c_params() const
For internal use: Generate a params object for use in calls.
Definition: params.cxx:92
Result set containing data returned by a query or command.
Definition: result.hxx:71
result_size_type size_type
Definition: result.hxx:73
Reference to one row in a result.
Definition: row.hxx:46
reference front() const noexcept
Definition: row.cxx:54
std::tuple< TYPE... > as() const
Definition: row.hxx:184
Stream data from the database.
Definition: stream_from.hxx:73
Use std::string_view query
Definition: stream_from.hxx:139
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:75
result exec_prepared(zview statement, Args &&...args)
Execute a prepared statement, with optional arguments.
Definition: transaction_base.hxx:445
row exec1(zview query, std::string_view desc=std::string_view{})
Execute command returning a single row of data.
Definition: transaction_base.hxx:264
void process_notice(zview msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:491
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:160
transaction_base & operator=(transaction_base &&)=delete
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:475
transaction_base(transaction_base const &)=delete
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:284
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:524
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:167
row exec_params1(zview query, Args &&...args)
Definition: transaction_base.hxx:389
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:116
std::string quote(binarystring const &t) const
Definition: transaction_base.hxx:174
std::string esc(std::string_view text) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:121
auto stream(std::string_view query)
Execute a query, and loop over the results row by row.
Definition: transaction_base.hxx:342
transaction_base(transaction_base &&)=delete
result exec_params(zview query, Args &&...args)
Execute an SQL statement with parameters.
Definition: transaction_base.hxx:380
std::string_view name() const noexcept
Transaction name, if you passed one to the constructor; or empty string.
Definition: transaction_base.hxx:517
transaction_base()=delete
result exec_params0(zview query, Args &&...args)
Definition: transaction_base.hxx:397
std::string unesc_raw(zview text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:151
result exec_prepared0(zview statement, Args &&...args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:464
std::string esc_like(std::string_view bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:199
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:495
result exec0(zview query, std::string_view desc=std::string_view{})
Execute command, which should return zero rows of data.
Definition: transaction_base.hxx:252
result exec(std::stringstream const &query, std::string_view desc=std::string_view{})
Execute a command.
Definition: transaction_base.hxx:240
row exec_prepared1(zview statement, Args &&...args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:455
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:192
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:181
result exec_params_n(std::size_t rows, zview query, Args &&...args)
Definition: transaction_base.hxx:406
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:139
transaction_base & operator=(transaction_base const &)=delete
void process_notice(char const msg[]) const
Have connection process a warning message.
Definition: transaction_base.hxx:489
Base class for things that monopolise a transaction's attention.
Definition: transaction_focus.hxx:28
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:38