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);
160 [[nodiscard]] std::string
161 esc_raw(
unsigned char const data[],
size_t len)
const 163 return conn().esc_raw(data, len);
166 [[nodiscard]] std::string esc_raw(std::string
const &)
const;
172 [[nodiscard]] std::string
unesc_raw(std::string
const &text)
const 174 return conn().unesc_raw(text);
181 [[nodiscard]] std::string
unesc_raw(
char const *text)
const 183 return conn().unesc_raw(text);
188 template<
typename T>[[nodiscard]] std::string
quote(T
const &t)
const 190 return conn().quote(t);
194 [[nodiscard]] std::string
197 return conn().quote_raw(bin, len);
200 [[nodiscard]] std::string quote_raw(std::string
const &bin)
const;
203 [[nodiscard]] std::string
quote_name(std::string_view identifier)
const 205 return conn().quote_name(identifier);
209 [[nodiscard]] std::string
210 esc_like(std::string
const &bin,
char escape_char =
'\\')
const 212 return conn().esc_like(bin, escape_char);
231 result exec(std::string_view query, std::string
const &desc = std::string{});
234 exec(std::stringstream
const &query, std::string
const &desc = std::string{})
236 return exec(query.str(), desc);
246 exec0(std::string
const &query, std::string
const &desc = std::string{})
248 return exec_n(0, query, desc);
258 row exec1(std::string
const &query, std::string
const &desc = std::string{})
260 return exec_n(1, query, desc).
front();
271 std::string
const &desc = std::string{});
302 template<
typename... Args>
306 return internal_exec_params(
307 query, internal::params(std::forward<Args>(args)...));
313 template<
typename... Args>
316 return exec_params_n(1, query, std::forward<Args>(args)...).
front();
322 template<
typename... Args>
325 return exec_params_n(0, query, std::forward<Args>(args)...);
331 template<
typename... Args>
334 auto const r{exec_params(query, std::forward<Args>(args)...)};
335 check_rowcount_params(rows, r.size());
369 template<
typename... Args>
372 return internal_exec_prepared(
374 internal::params(std::forward<Args>(args)...));
377 template<
typename... Args>
380 return internal_exec_prepared(
381 statement, internal::params(std::forward<Args>(args)...));
387 template<
typename... Args>
390 return exec_prepared_n(1, statement, std::forward<Args>(args)...).
front();
393 template<
typename... Args>
396 return exec_prepared_n(1, statement, std::forward<Args>(args)...).
front();
402 template<
typename... Args>
405 return exec_prepared_n(0, statement, std::forward<Args>(args)...);
408 template<
typename... Args>
411 return exec_prepared_n(0, statement, std::forward<Args>(args)...);
418 template<
typename... Args>
422 auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
423 check_rowcount_prepared(statement, rows, r.size());
427 template<
typename... Args>
431 auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
432 check_rowcount_prepared(statement, rows, r.size());
442 void process_notice(
char const msg[])
const { m_conn.process_notice(msg); }
447 m_conn.process_notice(msg);
465 void set_variable(std::string_view var, std::string_view value);
471 std::string get_variable(std::string_view);
481 void register_transaction();
484 void close() noexcept;
487 virtual void do_commit() = 0;
489 virtual void do_abort() = 0;
494 result direct_exec(std::string_view);
495 result direct_exec(std::shared_ptr<std::string>);
507 PQXX_PRIVATE
void check_pending_error();
509 template<
typename T>
bool parm_is_null(T *p)
const noexcept
513 template<
typename T>
bool parm_is_null(T)
const noexcept {
return false; }
515 result internal_exec_prepared(
zview statement, internal::params
const &args);
518 internal_exec_params(std::string
const &query, internal::params
const &args);
521 void check_rowcount_prepared(
526 void check_rowcount_params(
size_t expected_rows,
size_t actual_rows);
528 friend class pqxx::internal::gate::transaction_transactionfocus;
531 PQXX_PRIVATE
void register_pending_error(std::string
const &) noexcept;
536 status m_status = status::active;
537 bool m_registered =
false;
538 std::string m_pending_error;
546 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
551 inline constexpr
zview begin_cmd<read_committed, write_policy::read_write>{
554 inline constexpr
zview begin_cmd<read_committed, write_policy::read_only>{
557 inline constexpr
zview begin_cmd<repeatable_read, write_policy::read_write>{
558 "BEGIN ISOLATION LEVEL REPEATABLE READ"};
560 inline constexpr
zview begin_cmd<repeatable_read, write_policy::read_only>{
561 "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"};
563 inline constexpr
zview begin_cmd<serializable, write_policy::read_write>{
564 "BEGIN ISOLATION LEVEL SERIALIZABLE"};
566 inline constexpr
zview begin_cmd<serializable, write_policy::read_only>{
567 "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"};
570 #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:370
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:314
row exec1(std::string const &query, std::string const &desc=std::string{})
Execute query returning a single row of data.
Definition: transaction_base.hxx:258
std::string esc_like(std::string const &bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:210
result exec_params0(std::string const &query, Args &&... args)
Definition: transaction_base.hxx:323
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:161
row exec_prepared1(std::string const &statement, Args &&... args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:388
result exec_prepared0(zview statement, Args &&... args)
Definition: transaction_base.hxx:409
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:195
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:181
result exec_prepared_n(result::size_type rows, zview statement, Args &&... args)
Definition: transaction_base.hxx:429
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
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:172
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:252
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:332
void process_notice(std::string const &msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:445
result exec0(std::string const &query, std::string const &desc=std::string{})
Execute query, which should zero rows of data.
Definition: transaction_base.hxx:246
result exec_prepared0(std::string const &statement, Args &&... args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:403
result exec_params(std::string const &query, Args &&... args)
Execute an SQL statement with parameters.
Definition: transaction_base.hxx:304
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:203
Definition: connection.hxx:65
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:452
result exec(std::stringstream const &query, std::string const &desc=std::string{})
Definition: transaction_base.hxx:234
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:208
Definition: transaction_base.hxx:42
result exec_prepared(zview statement, Args &&... args)
Definition: transaction_base.hxx:378
transaction_base & m_trans
Definition: transaction_base.hxx:60
Reference to one row in a result.
Definition: row.hxx:38
row exec_prepared1(zview statement, Args &&... args)
Definition: transaction_base.hxx:394
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:188
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:419