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_transactionfocus;
50 using namespace std::literals;
107 [[nodiscard]] std::string esc(
char const text[])
const
110 return conn().esc(text);
113 [[nodiscard]] std::string
esc(
char const text[], std::size_t maxlen)
const
115 return conn().esc(text, maxlen);
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(
zview)
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(
zview 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_view bin,
char escape_char =
'\\')
const
187 return conn().esc_like(bin, escape_char);
207 exec(std::string_view query, std::string_view desc = std::string_view{});
210 std::stringstream
const &query, std::string_view desc = std::string_view{})
212 return exec(query.str(), desc);
223 return exec_n(0, query, desc);
235 return exec_n(1, query, desc).
front();
246 std::string_view desc = std::string_view{});
249 template<
typename TYPE>
252 row const r{exec1(query, desc)};
253 if (std::size(r) != 1)
254 throw usage_error{internal::concat(
255 "Queried single value from result with ", std::size(r),
" columns.")};
256 return r[0].
as<TYPE>();
300 template<
typename... TYPE> [[nodiscard]]
auto stream(std::string_view query)
302 return pqxx::internal::owning_stream_input_iteration<TYPE...>{
303 std::make_unique<pqxx::stream_from>(*
this,
from_query_t{}, query)};
335 template<
typename... Args>
result exec_params(
zview query, Args &&...args)
338 return internal_exec_params(
339 query, internal::params(std::forward<Args>(args)...));
347 return exec_params_n(1, query, std::forward<Args>(args)...).
front();
355 return exec_params_n(0, query, std::forward<Args>(args)...);
361 template<
typename... Args>
364 auto const r{exec_params(query, std::forward<Args>(args)...)};
365 check_rowcount_params(rows, std::size(r));
400 template<
typename... Args>
403 return internal_exec_prepared(
404 statement, internal::params(std::forward<Args>(args)...));
410 template<
typename... Args>
413 return exec_prepared_n(1, statement, std::forward<Args>(args)...).
front();
419 template<
typename... Args>
422 return exec_prepared_n(0, statement, std::forward<Args>(args)...);
429 template<
typename... Args>
433 auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
434 check_rowcount_prepared(statement, rows, std::size(r));
444 void process_notice(
char const msg[])
const { m_conn.process_notice(msg); }
464 void set_variable(std::string_view var, std::string_view value);
470 std::string get_variable(std::string_view);
473 [[nodiscard]] std::string_view
name() const noexcept {
return m_name; }
482 std::shared_ptr<std::string> rollback_cmd) :
483 m_conn{c}, m_name{tname}, m_rollback_cmd{rollback_cmd}
492 transaction_base(connection &c, std::string_view tname);
495 explicit transaction_base(connection &c);
498 void register_transaction();
501 void close() noexcept;
504 virtual
void do_commit() = 0;
510 virtual
void do_abort();
513 void set_rollback_cmd(std::shared_ptr<std::
string> cmd)
515 m_rollback_cmd = cmd;
519 result direct_exec(std::string_view);
520 result direct_exec(std::shared_ptr<std::string>);
531 PQXX_PRIVATE
void check_pending_error();
533 template<
typename T>
bool parm_is_null(T *p)
const noexcept
537 template<
typename T>
bool parm_is_null(T)
const noexcept {
return false; }
539 result internal_exec_prepared(zview statement, internal::params
const &args);
541 result internal_exec_params(zview query, internal::params
const &args);
544 void check_rowcount_prepared(
550 check_rowcount_params(std::size_t expected_rows, std::size_t actual_rows);
553 [[nodiscard]] std::string description()
const;
555 friend class pqxx::internal::gate::transaction_transactionfocus;
556 PQXX_PRIVATE
void register_focus(internal::transactionfocus *);
557 PQXX_PRIVATE
void unregister_focus(internal::transactionfocus *) noexcept;
558 PQXX_PRIVATE
void register_pending_error(
zview) noexcept;
559 PQXX_PRIVATE
void register_pending_error(std::string &&) noexcept;
567 internal::transactionfocus const *m_focus =
nullptr;
569 status m_status = status::active;
570 bool m_registered = false;
572 std::
string m_pending_error;
575 std::shared_ptr<std::
string> m_rollback_cmd;
577 constexpr static std::string_view s_type_name{
"transaction"sv};
584 std::string_view transaction_base::query_value<std::string_view>(
585 zview query, std::string_view desc) =
delete;
588 zview transaction_base::query_value<zview>(
589 zview query, std::string_view desc) =
delete;
597 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
602 inline constexpr
zview begin_cmd<read_committed, write_policy::read_write>{
605 inline constexpr zview begin_cmd<read_committed, write_policy::read_only>{
606 "BEGIN READ ONLY"_zv};
608 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_write>{
609 "BEGIN ISOLATION LEVEL REPEATABLE READ"_zv};
611 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_only>{
612 "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"_zv};
614 inline constexpr zview begin_cmd<serializable, write_policy::read_write>{
615 "BEGIN ISOLATION LEVEL SERIALIZABLE"_zv};
617 inline constexpr zview begin_cmd<serializable, write_policy::read_only>{
618 "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"_zv};
621 #include "pqxx/internal/compiler-internal-post.hxx"
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:431
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
row exec_prepared1(zview statement, Args &&...args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:411
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
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
std::string unesc_raw(zview text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:147
void abort()
Abort the transaction.
Definition: transaction_base.cxx:172
result exec(std::stringstream const &query, std::string_view desc=std::string_view{})
Definition: transaction_base.hxx:209
row exec_params1(zview query, Args &&...args)
Definition: transaction_base.hxx:345
Definition: connection.hxx:95
result exec_prepared0(zview statement, Args &&...args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:420
std::string esc_like(std::string_view bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:185
virtual void do_abort()
Transaction type-specific way of aborting a transaction.
Definition: transaction_base.cxx:165
void set_variable(std::string_view var, std::string_view value)
Set session variable using SQL "SET" command.
Definition: transaction_base.cxx:315
void check_unique_register(void const *old_guest, std::string_view old_class, std::string_view old_name, void const *new_guest, std::string_view new_class, std::string_view new_name)
Check validity of registering a new "guest" in a "host.".
Definition: util.cxx:58
Internal items for libpqxx' own use. Do not use these yourself.
Definition: composite.hxx:74
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:480
Connection to a database.
Definition: connection.hxx:164
result direct_exec(std::string_view)
Execute query on connection directly.
Definition: transaction_base.cxx:437
Query returned an unexpected number of rows.
Definition: except.hxx:200
Reference to one row in a result.
Definition: row.hxx:46
Internal error in libpqxx library.
Definition: except.hxx:158
std::string get_variable(std::string_view)
Read session variable using SQL "SHOW" command.
Definition: transaction_base.cxx:322
auto stream(std::string_view query)
Execute a query, and loop over the results row by row.
Definition: transaction_base.hxx:300
std::string_view name() const noexcept
Transaction name, if you passed one to the constructor; or empty string.
Definition: transaction_base.hxx:473
transaction_base & operator=(transaction_base &&)=delete
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:163
std::string esc(std::string_view text) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:118
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:165
result exec_n(result::size_type rows, zview query, std::string_view desc=std::string_view{})
Execute query, expect given number of rows.
Definition: transaction_base.cxx:258
Exception class for lost or failed backend connection.
Definition: except.hxx:68
TYPE query_value(zview query, std::string_view desc=std::string_view{})
Execute query, expecting exactly 1 row with 1 field.
Definition: transaction_base.hxx:250
result exec_prepared(zview statement, Args &&...args)
Execute a prepared statement, with optional arguments.
Definition: transaction_base.hxx:401
transaction_base(transaction_base const &)=delete
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:72
result exec_params_n(std::size_t rows, zview query, Args &&...args)
Definition: transaction_base.hxx:362
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:451
transaction_base & operator=(transaction_base const &)=delete
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:178
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
transaction_base(transaction_base &&)=delete
virtual ~transaction_base()=0
Definition: transaction_base.cxx:56
std::tuple< TYPE... > as() const
Definition: row.hxx:184
result_size_type size_type
Definition: result.hxx:73
result exec0(zview query, std::string_view desc=std::string_view{})
Execute query, which should zero rows of data.
Definition: transaction_base.hxx:221
void process_notice(zview msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:447
std::string describe_object(std::string_view class_name, std::string_view name)
Describe an object for humans, based on class name and optional name.
Definition: util.cxx:48
transaction_base()=delete
void commit()
Commit the transaction.
Definition: transaction_base.cxx:94
"Help, I don't know whether transaction was committed successfully!"
Definition: except.hxx:107
Run-time failure encountered by libpqxx, similar to std::runtime_error.
Definition: except.hxx:43
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:38
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:113
row exec1(zview query, std::string_view desc=std::string_view{})
Execute query returning a single row of data.
Definition: transaction_base.hxx:233
result exec_params0(zview query, Args &&...args)
Definition: transaction_base.hxx:353
constexpr char const * c_str() const noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition: zview.hxx:76
Marker for stream_from constructors: "stream from query.".
Definition: types.hxx:60
void check_unique_unregister(void const *old_guest, std::string_view old_class, std::string_view old_name, void const *new_guest, std::string_view new_class, std::string_view new_name)
Like check_unique_register, but for un-registering a guest.
Definition: util.cxx:75
void close() noexcept
End transaction. To be called by implementing class' destructor.
Definition: transaction_base.cxx:328
result exec(std::string_view query, std::string_view desc=std::string_view{})
Execute query.
Definition: transaction_base.cxx:227
void register_transaction()
Register this transaction with the connection.
Definition: transaction_base.cxx:86
const zview begin_cmd
The SQL command for starting a given type of transaction.
Result set containing data returned by a query or command.
Definition: result.hxx:71
reference front() const noexcept
Definition: row.cxx:54