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" 56 void unregister_me() noexcept;
57 void reg_pending_error(std::string
const &) noexcept;
58 bool registered() const noexcept {
return m_registered; }
63 bool m_registered =
false;
70 class transaction_subtransaction;
71 class transaction_sql_cursor;
72 class transaction_stream_to;
73 class transaction_transactionfocus;
132 [[nodiscard]] std::string esc(
char const text[])
const 135 return conn().esc(text);
138 [[nodiscard]] std::string
esc(
char const text[],
size_t maxlen)
const 140 return conn().esc(text, maxlen);
143 [[nodiscard]] std::string
esc(std::string
const &text)
const 145 return conn().esc(text);
148 [[nodiscard]] std::string
esc(std::string_view text)
const 150 return conn().esc(text);
165 [[nodiscard]] std::string
166 esc_raw(
unsigned char const data[],
size_t len)
const 168 return conn().esc_raw(data, len);
171 [[nodiscard]] std::string esc_raw(std::string
const &)
const;
177 [[nodiscard]] std::string
unesc_raw(std::string
const &text)
const 179 return conn().unesc_raw(text);
186 [[nodiscard]] std::string
unesc_raw(
char const *text)
const 188 return conn().unesc_raw(text);
193 template<
typename T>[[nodiscard]] std::string
quote(T
const &t)
const 195 return conn().quote(t);
199 [[nodiscard]] std::string
202 return conn().quote_raw(bin, len);
205 [[nodiscard]] std::string quote_raw(std::string
const &bin)
const;
208 [[nodiscard]] std::string
quote_name(std::string_view identifier)
const 210 return conn().quote_name(identifier);
214 [[nodiscard]] std::string
215 esc_like(std::string
const &bin,
char escape_char =
'\\')
const 217 return conn().esc_like(bin, escape_char);
236 result exec(std::string_view query, std::string
const &desc = std::string{});
239 exec(std::stringstream
const &query, std::string
const &desc = std::string{})
241 return exec(query.str(), desc);
251 exec0(std::string
const &query, std::string
const &desc = std::string{})
253 return exec_n(0, query, desc);
263 row exec1(std::string
const &query, std::string
const &desc = std::string{})
265 return exec_n(1, query, desc).
front();
276 std::string
const &desc = std::string{});
279 template<
typename TYPE>
281 std::string
const &query, std::string
const &desc = std::string{})
283 row const r{exec1(query, desc)};
285 throw usage_error{
"Queried single value from result with " +
287 return r[0].as<TYPE>();
319 template<
typename... Args>
323 return internal_exec_params(
324 query, internal::params(std::forward<Args>(args)...));
330 template<
typename... Args>
333 return exec_params_n(1, query, std::forward<Args>(args)...).
front();
339 template<
typename... Args>
342 return exec_params_n(0, query, std::forward<Args>(args)...);
348 template<
typename... Args>
351 auto const r{exec_params(query, std::forward<Args>(args)...)};
352 check_rowcount_params(rows, r.size());
386 template<
typename... Args>
389 return internal_exec_prepared(
391 internal::params(std::forward<Args>(args)...));
394 template<
typename... Args>
397 return internal_exec_prepared(
398 statement, internal::params(std::forward<Args>(args)...));
404 template<
typename... Args>
407 return exec_prepared_n(1, statement, std::forward<Args>(args)...).
front();
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)...);
425 template<
typename... Args>
428 return exec_prepared_n(0, statement, std::forward<Args>(args)...);
435 template<
typename... Args>
439 auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
440 check_rowcount_prepared(statement, rows, r.size());
444 template<
typename... Args>
448 auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
449 check_rowcount_prepared(statement, rows, r.size());
459 void process_notice(
char const msg[])
const { m_conn.process_notice(msg); }
464 m_conn.process_notice(msg);
482 void set_variable(std::string_view var, std::string_view value);
488 std::string get_variable(std::string_view);
498 void register_transaction();
501 void close() noexcept;
504 virtual void do_commit() = 0;
506 virtual void do_abort() = 0;
511 result direct_exec(std::string_view);
512 result direct_exec(std::shared_ptr<std::string>);
524 PQXX_PRIVATE
void check_pending_error();
526 template<
typename T>
bool parm_is_null(T *p)
const noexcept
530 template<
typename T>
bool parm_is_null(T)
const noexcept {
return false; }
532 result internal_exec_prepared(
zview statement, internal::params
const &args);
535 internal_exec_params(std::string
const &query, internal::params
const &args);
538 void check_rowcount_prepared(
543 void check_rowcount_params(
size_t expected_rows,
size_t actual_rows);
545 friend class pqxx::internal::gate::transaction_transactionfocus;
548 PQXX_PRIVATE
void register_pending_error(std::string
const &) noexcept;
553 status m_status = status::active;
554 bool m_registered =
false;
555 std::string m_pending_error;
563 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
568 inline constexpr
zview begin_cmd<read_committed, write_policy::read_write>{
571 inline constexpr
zview begin_cmd<read_committed, write_policy::read_only>{
574 inline constexpr
zview begin_cmd<repeatable_read, write_policy::read_write>{
575 "BEGIN ISOLATION LEVEL REPEATABLE READ"};
577 inline constexpr
zview begin_cmd<repeatable_read, write_policy::read_only>{
578 "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"};
580 inline constexpr
zview begin_cmd<serializable, write_policy::read_write>{
581 "BEGIN ISOLATION LEVEL SERIALIZABLE"};
583 inline constexpr
zview begin_cmd<serializable, write_policy::read_only>{
584 "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"};
587 #include "pqxx/internal/compiler-internal-post.hxx" transactionfocus(transaction_base &t)
Definition: transaction_base.hxx:45
result exec_prepared(std::string const &statement, Args &&... args)
Execute a prepared statement, with optional arguments.
Definition: transaction_base.hxx:387
const zview begin_cmd
The SQL command for starting a given type of transaction.
Private namespace for libpqxx's internal use; do not access.
Definition: connection.hxx:59
row exec_params1(std::string const &query, Args &&... args)
Definition: transaction_base.hxx:331
row exec1(std::string const &query, std::string const &desc=std::string{})
Execute query returning a single row of data.
Definition: transaction_base.hxx:263
std::string esc_like(std::string const &bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:215
result exec_params0(std::string const &query, Args &&... args)
Definition: transaction_base.hxx:340
std::string esc(std::string const &text) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:143
std::string esc_raw(unsigned char const data[], size_t len) const
Escape binary data for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:166
row exec_prepared1(std::string const &statement, Args &&... args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:405
result exec_prepared0(zview statement, Args &&... args)
Definition: transaction_base.hxx:426
bool registered() const noexcept
Definition: transaction_base.hxx:58
std::string quote_raw(unsigned char const bin[], size_t len) const
Binary-escape and quote a binarystring for use as an SQL constant.
Definition: transaction_base.hxx:200
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:186
result exec_prepared_n(result::size_type rows, zview statement, Args &&... args)
Definition: transaction_base.hxx:446
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:280
std::string esc(char const text[], size_t maxlen) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:138
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:478
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
Connection to a database.
Definition: connection.hxx:135
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:177
constexpr char const * c_str() const noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition: zview.hxx:41
Ensure proper opening/closing of GUEST objects related to a "host" object.
Definition: util.hxx:253
reference front() const noexcept
Definition: row.cxx:56
result exec_params_n(size_t rows, std::string const &query, Args &&... args)
Definition: transaction_base.hxx:349
void process_notice(std::string const &msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:462
result exec0(std::string const &query, std::string const &desc=std::string{})
Execute query, which should zero rows of data.
Definition: transaction_base.hxx:251
result exec_prepared0(std::string const &statement, Args &&... args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:420
result exec_params(std::string const &query, Args &&... args)
Execute an SQL statement with parameters.
Definition: transaction_base.hxx:321
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:97
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:208
Definition: connection.hxx:65
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:469
result exec(std::stringstream const &query, std::string const &desc=std::string{})
Definition: transaction_base.hxx:239
Result set containing data returned by a query or command.
Definition: result.hxx:70
result_size_type size_type
Definition: result.hxx:73
Helper base class: object descriptions for error messages and such.
Definition: util.hxx:209
Definition: transaction_base.hxx:42
result exec_prepared(zview statement, Args &&... args)
Definition: transaction_base.hxx:395
transaction_base & m_trans
Definition: transaction_base.hxx:60
Reference to one row in a result.
Definition: row.hxx:38
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:164
std::string esc(std::string_view text) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:148
row exec_prepared1(zview statement, Args &&... args)
Definition: transaction_base.hxx:411
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:32
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:193
result exec_prepared_n(result::size_type rows, std::string const &statement, Args &&... args)
Execute a prepared statement, expect a result with given number of rows.
Definition: transaction_base.hxx:436