|
libpqxx
The C++ client library for PostgreSQL
|
Connection to a database. More...
#include <connection.hxx>
Public Member Functions | |
Connecting to a database | |
You connect to a database by creating a If the attempt to connect fails, you will not get a connection object; the constructor will fail with a pqxx::broken_connection exception. You can control each of the details of how to connect (hostname, username, database name, etc.) in up to 4 ways. All are optional, and you can combine some or all of them:
For each individual item, the connection will take the value from the first item in this list that defines it.
Connection parameters are individual key/value pairs of strings. You can pass these as a The connection parameters are documented here: https://postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS Connection strings can be in one of two formats: a custom key-value format, or a RFC 3986 Uniform Resource Identifier (URI). They are documented here: https://postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING Environment variables for controlling how you connect are documented here: | |
| connection (sl loc=sl::current()) | |
| template<pqxx::ZString STRING, ZKey_ZValues MAPPING> | |
| connection (STRING const &connection_string, MAPPING &¶ms=empty_params_t(), sl=sl::current()) | |
| Connect to a database with both connection string and parameter pairs. More... | |
| template<pqxx::ZString STRING> | |
| connection (STRING const &connection_string, sl loc=sl::current()) | |
| Connect to a database, passing a connection string. More... | |
| template<ZKey_ZValues MAPPING> | |
| connection (MAPPING &¶ms, sl loc=sl::current()) | |
| Connect to a database, passing connection parameters. More... | |
| connection (connection &&rhs, sl=sl::current()) | |
| Move constructor. More... | |
| ~connection () | |
| connection & | operator= (connection &&rhs) |
| Move assignment. More... | |
| connection (connection const &)=delete | |
| connection & | operator= (connection const &)=delete |
| bool | is_open () const noexcept |
| Is this connection open at the moment? More... | |
| PQXX_ZARGS void | process_notice (char const[]) noexcept |
| Invoke notice processor function. The message should end in newline. More... | |
| void | process_notice (zview) noexcept |
| Invoke notice processor function. Newline at end is recommended. More... | |
| void | trace (std::FILE *) noexcept |
| Enable tracing to a given output stream, or nullptr to disable. More... | |
Connection properties | |
These are probably not of great interest, since most are derived from information supplied by the client program itself, but they are included for completeness. The connection needs to be currently active for these to work. | |
| char const * | dbname () const noexcept |
| Name of the database to which we're connected, if any. More... | |
| char const * | username () const noexcept |
| Database user ID under which we are connected, if any. More... | |
| PQXX_PURE char const * | hostname () const noexcept |
| Database server address, if given. More... | |
| PQXX_PURE char const * | port () const noexcept |
| Server port number on which we are connected to the database. More... | |
| PQXX_PURE std::optional< int > | port_number (sl loc=sl::current()) const |
| Server port number on which we are connected to the database, if any. More... | |
| PQXX_PURE int | backendpid () const &noexcept |
| Process ID for backend process, or 0 if inactive. More... | |
| PQXX_PURE int | sock () const &noexcept |
| Socket currently used for connection, or -1 for none. More... | |
| PQXX_PURE int | protocol_version () const noexcept |
| What version of the PostgreSQL protocol is this connection using? More... | |
| PQXX_PURE int | server_version () const noexcept |
| What version of the PostgreSQL server are we connected to? More... | |
Text encoding | |
| std::string | get_client_encoding (sl loc=sl::current()) const |
| Get client-side character encoding, by name. More... | |
| void | set_client_encoding (zview encoding, sl loc=sl::current()) & |
| Set client-side character encoding, by name. More... | |
| PQXX_ZARGS void | set_client_encoding (char const encoding[], sl=sl::current()) & |
| Set client-side character encoding, by name. More... | |
| int | encoding_id (sl=sl::current()) const |
| Get the connection's encoding, as a PostgreSQL-defined code. More... | |
| encoding_group | get_encoding_group (sl loc=sl::current()) const |
| Read the curent client encoding's pqxx::encoding_group. More... | |
| template<typename TYPE > | |
| void | set_session_var (std::string_view var, TYPE const &value, sl loc=sl::current()) & |
| Set one of the session variables to a new value. More... | |
| std::string | get_var (std::string_view var, sl loc=sl::current()) |
| Read currently applicable value of a configuration variable. More... | |
| template<not_borrowed TYPE> | |
| TYPE | get_var_as (std::string_view var, sl loc=sl::current()) |
| Read currently applicable value of a configuration variable. More... | |
Password encryption | |
Use this when setting a new password for the user if password encryption is enabled. Inputs are the SQL name for the user for whom you with to encrypt a password; the plaintext password; and the hash algorithm. The algorithm must be one of "md5", "scram-sha-256" (introduced in PostgreSQL 10), or
Thus you can change a user's password with: void setpw(transaction_base &t, string const &user, string const &pw)
{
t.exec0("ALTER USER " + user + " "
"PASSWORD '" + t.conn().encrypt_password(user,pw) + "'");
}
When building this against a libpq older than version 10, this will use an older function which only supports md5. In that case, requesting a different algorithm than md5 will result in a feature_not_supported exception. | |
| std::string | encrypt_password (zview user, zview password, zview algorithm) |
| Encrypt a password for a given user. More... | |
| PQXX_ZARGS std::string | encrypt_password (char const user[], char const password[], char const *algorithm=nullptr) |
| Encrypt a password for a given user. More... | |
Prepared statements | |
PostgreSQL supports prepared SQL statements, i.e. statements that you can register under a name you choose, optimized once by the backend, and executed any number of times under the given name. Prepared statement definitions are not sensitive to transaction boundaries. A statement defined inside a transaction will remain defined outside that transaction, even if the transaction itself is subsequently aborted. Once a statement has been prepared, it will only go away if you close the connection or explicitly "unprepare" the statement. Use the
A statement's definition can refer to its parameters as Here's an example of how to use prepared statements. using namespace pqxx;
void foo(connection &c)
{
c.prepare("findtable", "select * from pg_tables where name=$1");
work tx{c};
result r = tx.exec_prepared("findtable", "mytable");
if (std::empty(r)) throw runtime_error{"mytable not found!"};
}
transaction< isolation_level::read_committed, write_policy::read_write > work The default transaction type. Definition: transaction.hxx:121 | |
| class | connecting |
| class | internal::gate::const_connection_largeobject |
| class | internal::gate::connection_errorhandler |
| class | internal::gate::connection_transaction |
| class | internal::gate::connection_stream_from |
| class | internal::gate::connection_stream_to |
| class | internal::gate::connection_largeobject |
| class | internal::gate::connection_notification_receiver |
| class | internal::gate::connection_pipeline |
| class | internal::gate::connection_dbtransaction |
| class | internal::gate::connection_sql_cursor |
| void | prepare (zview name, zview definition, sl loc=sl::current()) & |
| Define a prepared statement. More... | |
| void | prepare (std::string const &name, std::string const &definition, sl loc=sl::current()) & |
| Define a prepared statement. More... | |
| PQXX_ZARGS void | prepare (char const name[], char const definition[], sl loc=sl::current()) & |
| Define a prepared statement. More... | |
| void | prepare (std::string_view name, std::string_view definition, sl loc=sl::current()) & |
| Define a prepared statement. More... | |
| PQXX_ZARGS void | prepare (char const definition[], sl loc=sl::current()) & |
| Define a nameless prepared statement. More... | |
| void | prepare (zview definition, sl loc=sl::current()) & |
| void | unprepare (std::string_view name, sl loc=sl::current()) |
| Drop prepared statement. More... | |
| std::string | adorn_name (std::string_view) |
| Suffix unique number to name to make it unique within session context. More... | |
| PQXX_ZARGS std::string | esc (char const text[], sl loc=sl::current()) const |
| Escape string for use as SQL string literal on this connection. More... | |
| std::string_view | esc (std::string_view text, std::span< char > buffer, sl loc=sl::current()) |
Escape string for use as SQL string literal, into buffer. More... | |
| std::string | esc (std::string_view text, sl loc=sl::current()) const |
| Escape string for use as SQL string literal on this connection. More... | |
| template<binary DATA> | |
| std::string | esc (DATA const &data) const |
| Escape binary string for use as SQL string literal on this connection. More... | |
| template<binary DATA> | |
| zview | esc (DATA const &data, std::span< char > buffer, sl loc=sl::current()) const |
Escape binary string for use as SQL string literal, into buffer. More... | |
| std::string | esc_raw (bytes_view) const |
| Escape binary string for use as SQL string literal on this connection. More... | |
| template<binary DATA> | |
| std::string | esc_raw (DATA const &data) const |
| Escape binary string for use as SQL string literal on this connection. More... | |
| template<binary DATA> | |
| zview | esc_raw (DATA const &data, std::span< char > buffer) const |
Escape binary string for use as SQL string literal, into buffer. More... | |
| bytes | unesc_bin (std::string_view text, sl loc=sl::current()) const |
Unescape binary data, e.g. from a bytea field. More... | |
| std::string | quote_raw (bytes_view) const |
| Escape and quote a string of binary data. More... | |
| template<binary DATA> | |
| std::string | quote_raw (DATA const &data) const |
| Escape and quote a string of binary data. More... | |
| std::string | quote_name (std::string_view identifier) const |
| Escape and quote an SQL identifier for use in a query. More... | |
| std::string | quote_table (std::string_view name) const |
| Escape and quote a table name. More... | |
| std::string | quote_table (table_path) const |
| Escape and quote a table path. More... | |
| template<pqxx::char_strings STRINGS> | |
| std::string | quote_columns (STRINGS const &columns, sl=sl::current()) const |
| Quote and comma-separate a series of column names. More... | |
| template<typename T > | |
| std::string | quote (T const &t, sl=sl::current()) const |
| Represent object as SQL string, including quoting & escaping. More... | |
| std::string | esc_like (std::string_view text, char escape_char='\\', sl loc=sl::current()) const |
| Escape string for literal LIKE match. More... | |
| void | cancel_query (sl=sl::current()) |
| Attempt to cancel the ongoing query, if any. More... | |
| void | set_verbosity (error_verbosity verbosity) &noexcept |
| Set session verbosity. More... | |
| void | set_notice_handler (std::function< void(zview)> handler) |
| Set a notice handler to the connection. More... | |
| std::vector< errorhandler * > | get_errorhandlers () const |
| std::string | connection_string () const |
| Return a connection string encapsulating this connection's options. More... | |
| void | close (sl=sl::current()) |
| Explicitly close the connection. More... | |
| internal::pq::PGconn * | release_raw_connection () && |
| Release the raw connection without closing it. More... | |
| void | set_variable (std::string_view var, std::string_view value, sl loc=sl::current()) & |
Set session variable, using SQL's SET command. More... | |
| std::string | get_variable (std::string_view, sl loc=sl::current()) |
Read session variable, using SQL's SHOW command. More... | |
| static connection | seize_raw_connection (internal::pq::PGconn *raw_conn, sl loc=sl::current()) |
| Seize control of a raw libpq connection. More... | |
Notifications and Receivers | |
This is PostgreSQL-specific extension that goes beyond standard SQL. It's a communications mechanism between clients on a database, akin to a transactional message bus. A notification happens on a channel, identified by a name. You can set a connection to listen for notifications on the channel, using the connection's listen() function. (Internally this will issue a Notifications can carry an optional payload string. This is free-form text which carries additional information to the receiver.
Case sensitivityChannel names are case-sensitive. By default, however, PostgreSQL does convert the channel name in a Thus, a You can prevent this conversion by putting the name in double quotes, as quote_name() does. This is what libpqxx's notification functions do. If you use libpqxx to lisen on Confused? Safest thing to do is to use only lower-case letters in the channel names! TransactionsBoth listening and notifying are transactional in the backend: they only take effect once the back-end transaction in which you do them is committed. For an outgoing notification, this means that the transaction holds on to the outgoing message until you commit. (A nontransaction does not start a backend transaction, so if that's the transaction type you're using, the message does go out immediately.) For listening to incoming notifications, it gets a bit more complicated. To avoid complicating its internal bookkeeping, libpqxx only lets you start listening while no transaction is open. No notifications will come in while you're in a transaction... again unless it's a nontransaction of course, because that does not open a transaction on the backend. ExceptionsIf your handler throws an exception, that will simply propagate up the call chain to wherever you were when you received it. This is differnt from the old EncodingWhen a client sends a notification, it does so in its client encoding. If necessary, the back-end converts them to its internal encoding. And then when a client receives the notification, the database converts it to the receiver's client encoding. Simple enough, right? However if you should change your connection's client encoding after you start listening on a channel, then any notifications you receive may have different channel names than the ones for which you are listening. If this could be a problem in your scenario, stick to names in pure ASCII. Those will look the same in all the encodings postgres supports. | |
| using | notification_handler = std::function< void(notification)> |
| A handler callback for incoming notifications on a given channel. More... | |
| int | get_notifs (sl=sl::current()) |
| Check for pending notifications and take appropriate action. More... | |
| int | await_notification (sl=sl::current()) |
| Wait briefly for a notification to come in. More... | |
| int | await_notification (std::time_t seconds=10, long microseconds=0, sl=sl::current()) |
| Wait for a notification to come in, or for given timeout to pass. More... | |
| void | listen (std::string_view channel, notification_handler handler={}, sl=sl::current()) |
| Attach a handler to a notification channel. More... | |
Connection to a database.
This is the first class to look at when you wish to work with a database through libpqxx. As per RAII principles, the connection opens during construction, and closes upon destruction.
To query or manipulate the database once connected, use one of the transaction classes. Their API is defined in pqxx::transaction_base.
When a connection breaks, or fails to establish itself in the first place, you will typically get a broken_connection exception. In some cases when a physical network connection disappears, it can take minutes before this happens, as various layers of software try to reconnect.
signal(SIGPIPE, SIG_IGN) if you want your program to continue running after a connection fails. | using pqxx::connection::notification_handler = std::function<void(notification)> |
A handler callback for incoming notifications on a given channel.
Your callback must accept a Notifications and Receivers object. This object can and will exist only for the duration of the handling of that one incoming notification.
The handler can be "empty," i.e. contain no code. Setting an empty handler on a channel disables listening on that channel.
|
inlineexplicit |
|
inlineexplicit |
Connect to a database with both connection string and parameter pairs.
If a parameter is defined both in connection_string and in params, the value in params takes hold.
If a parameter is defined more than once in params, the last definition takes hold.
|
inlineexplicit |
Connect to a database, passing a connection string.
If a parameter is defined both in connection_string and in params, the value in params takes hold.
If a parameter is defined more than once in params, the last definition takes hold.
|
inlineexplicit |
Connect to a database, passing connection parameters.
| pqxx::connection::connection | ( | connection && | rhs, |
| sl | loc = sl::current() |
||
| ) |
Move constructor.
Moving a connection is not allowed if it has an open transaction, or has error handlers or is listening for notifications. In those situations, other objects may hold references to the old object which would become invalid and might produce hard-to-diagnose bugs.
|
inline |
|
delete |
| std::string pqxx::connection::adorn_name | ( | std::string_view | n | ) |
Suffix unique number to name to make it unique within session context.
Used internally to generate identifiers for SQL objects (such as cursors and nested transactions) based on a given human-readable base name.
| int pqxx::connection::await_notification | ( | sl | loc = sl::current() | ) |
Wait briefly for a notification to come in.
There are other events that will also cancel the wait, such as the backend failing, or some kinds of signal coming in. After a while the function just returns anyway.
This means that the function can return early, before any notification comes in or the timeout expires. Your code MUST be ready to handle such early returns.
If a notification does come in, this function will immediately process it, along with any other notifications that may have been pending, calling any handlers you may have set for them. It will then return.
To wait for notifications from your own event loop instead, wait until there is incoming data on the connection's socket to be read, then call get_notifs repeatedly until it returns zero. This allows you to handle other events besides notifications with a single wait point.
If your notifcation handler throws an exception, this function will just propagate it on up to you. (This is different from the old notification_receiver mechanism, which would merely log them.)
| int pqxx::connection::await_notification | ( | std::time_t | seconds = 10, |
| long | microseconds = 0, |
||
| sl | loc = sl::current() |
||
| ) |
Wait for a notification to come in, or for given timeout to pass.
There are other events that will also cancel the wait, such as the backend failing, or some kinds of signal coming in.
This means that the function can return early, before any notification comes in or the timeout expires. Your code MUST be ready to handle such early returns.
If a notification does come in, this function will immediately process it, along with any other notifications that may have been pending, calling any handlers you may have set for them. It will then return.
To wait for notifications from your own event loop instead, wait until there is incoming data on the connection's socket to be read, then call get_notifs repeatedly until it returns zero. This allows you to handle other events besides notifications with a single wait point.
If your notifcation handler throws an exception, this function will just propagate it on up to you. (This is different from the old notification_receiver mechanism, which would merely log them.)
|
noexcept |
Process ID for backend process, or 0 if inactive.
| void pqxx::connection::cancel_query | ( | sl | loc = sl::current() | ) |
Attempt to cancel the ongoing query, if any.
You can use this from another thread, and/or while a query is executing in a pipeline, but it's up to you to ensure that you're not canceling the wrong query. This may involve locking.
| void pqxx::connection::close | ( | sl | = sl::current() | ) |
Explicitly close the connection.
The destructor will do this for you automatically. Still, there is a reason to close() objects explicitly where possible: if an error should occur while closing, close() can throw an exception. A destructor cannot.
Closing a connection is idempotent. Closing a connection that's already closed does nothing.
| std::string pqxx::connection::connection_string | ( | ) | const |
Return a connection string encapsulating this connection's options.
The connection must be currently open for this to work.
Returns a reconstruction of this connection's connection string. It may not exactly match the connection string you passed in when creating this connection.
|
noexcept |
Name of the database to which we're connected, if any.
Returns nullptr when not connected.
| int pqxx::connection::encoding_id | ( | sl | loc = sl::current() | ) | const |
Get the connection's encoding, as a PostgreSQL-defined code.
| std::string pqxx::connection::encrypt_password | ( | char const | user[], |
| char const | password[], | ||
| char const * | algorithm = nullptr |
||
| ) |
Encrypt a password for a given user.
|
inline |
Encrypt a password for a given user.
|
inline |
Escape string for use as SQL string literal on this connection.
|
inline |
Escape binary string for use as SQL string literal on this connection.
This is identical to esc_raw(data).
|
inline |
Escape binary string for use as SQL string literal, into buffer.
Use this variant when you want to re-use the same buffer across multiple calls. If that's not the case, or convenience and simplicity are more important, use the single-argument variant.
For every byte in data, there must be at least two bytes of space in buffer; plus there must be two bytes of space for a header and one for a trailing zero. Throws range_error if this space is not available.
Returns a reference to the escaped string, which is actually stored in buffer.
| std::string pqxx::connection::esc | ( | std::string_view | text, |
| sl | loc = sl::current() |
||
| ) | const |
Escape string for use as SQL string literal on this connection.
|
inline |
Escape string for use as SQL string literal, into buffer.
Use this variant when you want to re-use the same buffer across multiple calls. If that's not the case, or convenience and simplicity are more important, use the single-argument variant.
For every byte in text, there must be at least 2 bytes of space in buffer; plus there must be one byte of space for a trailing zero. Throws range_error if this space is not available.
Returns a reference to the escaped string, which is actually stored in buffer.
| std::string pqxx::connection::esc_like | ( | std::string_view | text, |
| char | escape_char = '\\', |
||
| sl | loc = sl::current() |
||
| ) | const |
Escape string for literal LIKE match.
Use this when part of an SQL "LIKE" pattern should match only as a literal string, not as a pattern, even if it contains "%" or "_" characters that would normally act as wildcards.
The string does not get string-escaped or quoted. You do that later.
For instance, let's say you have a string name entered by the user, and you're searching a file column for items that match name followed by a dot and three letters. Even if name contains wildcard characters "%" or "_", you only want those to match literally, so "_" only matches "_" and "%" only matches a single "%".
You do that by "like-escaping" name, appending the wildcard pattern ".___", and finally, escaping and quoting the result for inclusion in your query:
The SQL "LIKE" operator also lets you choose your own escape character. This is supported, but must be a single-byte character.
| std::string pqxx::connection::esc_raw | ( | bytes_view | bin | ) | const |
Escape binary string for use as SQL string literal on this connection.
You can also just use esc with a binary string.
|
inline |
Escape binary string for use as SQL string literal on this connection.
You can also just use esc with a binary string.
|
inline |
Escape binary string for use as SQL string literal, into buffer.
| std::string pqxx::connection::get_client_encoding | ( | sl | loc = sl::current() | ) | const |
Get client-side character encoding, by name.
Each connection is governed by a "client encoding," which dictates how strings and other text is represented in bytes. The database server will send text data to you in this encoding, and you should use it for the queries and data which you send to the server.
Search the PostgreSQL documentation for "character set encodings" to find out more about the available encodings, how to extend them, and how to use them. Not all server-side encodings are compatible with all client-side encodings or vice versa.
Encoding names are case-insensitive, so e.g. "UTF8" is equivalent to "utf8".
You can change the client encoding, but this may not work when the connection is in a special state, such as when streaming a table. It's not clear what happens if you change the encoding during a transaction, and then abort the transaction.
|
inline |
Read the curent client encoding's pqxx::encoding_group.
| std::vector< pqxx::errorhandler * > pqxx::connection::get_errorhandlers | ( | ) | const |
The entries are ordered from oldest to newest handler.
The pointers point to the real errorhandlers. The container it returns however is a copy of the one internal to the connection, not a reference.
| int pqxx::connection::get_notifs | ( | sl | loc = sl::current() | ) |
Check for pending notifications and take appropriate action.
This does not block. To wait for incoming notifications, either call await_notification() (it calls this function); or wait for incoming data on the connection's socket (i.e. wait to read), and then call this function repeatedly until it returns zero. After that, there are no more pending notifications so you may want to wait again, or move on and do other work.
If any notifications are pending when you call this function, it processes them by checking for a matching notification handler, and if it finds one, invoking it. If there is no matching handler, nothing happens.
If your notifcation handler throws an exception, get_notifs() will just propagate it back to you. (This is different from the old notification_receiver mechanism, which would merely log them.)
| std::string pqxx::connection::get_var | ( | std::string_view | var, |
| sl | loc = sl::current() |
||
| ) |
Read currently applicable value of a configuration variable.
This function executes an SQL statement, so it won't work while a pipeline, query stream, or other object derived from transaction_focus is active on the connection.
|
inline |
Read currently applicable value of a configuration variable.
This function executes an SQL statement, so it won't work while a pipeline, query stream, or other object derived from transaction_focus is active on the connection.
std::string_view, a std::span, a pqxx::zview, or anything like that... the string value will no longer be valid by the time you receive it! If you want to read the variable as a string value, use std::string. | std::string pqxx::connection::get_variable | ( | std::string_view | var, |
| sl | loc = sl::current() |
||
| ) |
Read session variable, using SQL's SHOW command.
|
noexcept |
Database server address, if given.
This may be an IP address, or a hostname, or (for a Unix domain socket) a socket path. Returns nullptr when not connected.
|
noexcept |
Is this connection open at the moment?
| void pqxx::connection::listen | ( | std::string_view | channel, |
| notification_handler | handler = {}, |
||
| sl | loc = sl::current() |
||
| ) |
Attach a handler to a notification channel.
Issues a LISTEN SQL command for channel channel, and stores handler as the callback for when a notification comes in on that channel.
The connection can call this handler when you call get_notifs() or await_notification() on the connection. Some internal functions may also call these functions. The client-side handling is fully synchronous and notifications only come in while the connection is not in a back-end transaction.
The handler is a std::function (see notification_handler), but you can simply pass in a lambda with the right parameters, or a function, or an object of a type you define that happens to implemnt the right function call operator.
Your handler probably needs to interact with your application's data; the simple way to get that working is to pass a lambda with a closure referencing the data items you need.
If the handler is empty (the default), then that stops the connection listening on the channel. It cancels your subscription, so to speak. You can do that as many times as you like, even when you never started listening to that channel in the first place.
A connection can only have one handler per channel, so if you register two different handlers on the same channel, then the second overwrites the first.
| pqxx::connection & pqxx::connection::operator= | ( | connection && | rhs | ) |
Move assignment.
Neither connection can have an open transaction, errorhandler, or notification_receiver.
If libpqxx needs to throw an exception during this operation, its error message will state the std::source_location not for where you call it, since there is no way to pass a std::source_location parameter in the assignment operator. Instead, it will report the location where the current connection was created.
|
delete |
|
noexcept |
Server port number on which we are connected to the database.
| std::optional< int > pqxx::connection::port_number | ( | sl | loc = sl::current() | ) | const |
Server port number on which we are connected to the database, if any.
| void pqxx::connection::prepare | ( | char const | definition[], |
| sl | loc = sl::current() |
||
| ) | & |
Define a nameless prepared statement.
| void pqxx::connection::prepare | ( | char const | name[], |
| char const | definition[], | ||
| sl | loc = sl::current() |
||
| ) | & |
Define a prepared statement.
| name | unique name for the new prepared statement. |
| definition | SQL statement to prepare. |
|
inline |
Define a prepared statement.
Extra overload for disambiguation. It may also help avoid the slightly less efficient string_view-based overload.
| name | unique name for the new prepared statement. |
| definition | SQL statement to prepare. |
|
inline |
Define a prepared statement.
This is a convenience overload, slightly less efficient than the versions that take std::string, pqxx::zview or raw C strings. The reason is that these functions internally call a C-level function that expects zero-terminated strings.
If you cannot guarantee that these strings are zero-terminated, the function will have to create temporary internal copies of these strings in order to ensure the terminating zero.
| name | unique name for the new prepared statement. |
| definition | SQL statement to prepare. |
Define a prepared statement.
| name | unique name for the new prepared statement. |
| definition | SQL statement to prepare. |
|
noexcept |
Invoke notice processor function. The message should end in newline.
|
noexcept |
Invoke notice processor function. Newline at end is recommended.
The zview variant, with a message ending in newline, is the most efficient way to call process_notice.
|
noexcept |
What version of the PostgreSQL protocol is this connection using?
The answer can be 0 (when there is no connection); 3 for protocol 3.0; or possibly higher values as newer protocol versions come into use.
|
inline |
Represent object as SQL string, including quoting & escaping.
Recognises nulls and represents them as SQL nulls. They get no quotes.
|
inline |
Quote and comma-separate a series of column names.
Use this to save a bit of work in cases where you repeatedly need to pass the same list of column names, e.g. with stream_to and stream_from. Some functions that need to quote the columns list internally, will have a "raw" alternative which let you do the quoting yourself. It's a bit of extra work, but it can in rare cases let you eliminate some duplicate work in quoting them repeatedly.
| std::string pqxx::connection::quote_name | ( | std::string_view | identifier | ) | const |
Escape and quote an SQL identifier for use in a query.
| std::string pqxx::connection::quote_raw | ( | bytes_view | bytes | ) | const |
Escape and quote a string of binary data.
You can also just use quote with binary data.
|
inline |
Escape and quote a string of binary data.
You can also just use quote with binary data.
| std::string pqxx::connection::quote_table | ( | std::string_view | name | ) | const |
Escape and quote a table name.
When passing just a table name, this is just another name for quote_name.
| std::string pqxx::connection::quote_table | ( | table_path | path | ) | const |
Escape and quote a table path.
A table path consists of a table name, optionally prefixed by a schema name; and if both are given, they are in turn optionally prefixed by a database name.
Each portion of the path (database name, schema name, table name) will be quoted separately, and they will be joined together by dots. So for example, myschema.mytable will become "myschema"."mytable".
|
inline |
Release the raw connection without closing it.
The connection object becomes unusable after this.
In order to use the resulting pointer, for most purposes you'll have to cast it to its original C type: ::PGconn * as defined by libpq.
|
inlinestatic |
Seize control of a raw libpq connection.
| raw_conn | a raw libpq ::PQconn pointer, cast to a pqxx::internal::pq::PGconn pointer. |
|
noexcept |
What version of the PostgreSQL server are we connected to?
The result is a bit complicated: each of the major, medium, and minor release numbers is written as a two-digit decimal number, and the three are then concatenated. Thus server version 9.4.2 will be returned as the decimal number 90402. If there is no connection to the server, this returns zero.
| void pqxx::connection::set_client_encoding | ( | char const | encoding[], |
| sl | loc = sl::current() |
||
| ) | & |
Set client-side character encoding, by name.
| encoding | Name of the character set encoding to use. |
Set client-side character encoding, by name.
| encoding | Name of the character set encoding to use. |
|
inline |
Set a notice handler to the connection.
When a notice comes in (a warning or error message), the connection or result object on which it happens will call the notice handler, passing the message as its argument.
The handler must not throw any exceptions. If it does, the program will terminate.
connection that can call a notice handler, but any of the result objects that it produces as well. So, be prepared for the possibility that the handler may still receive a call after the connection has been closed.
|
inline |
Set one of the session variables to a new value.
This executes SQL, so do not do it while a pipeline or stream is active on the connection.
The value you set here will last for the rest of the connection's duration, or until you set a new value.
If you set the value while in a dbtransaction (i.e. any transaction that is not a nontransaction), then rolling back the transaction will undo the change.
All applies to setting session variables. You can also set the same variables as local variables, in which case they will always revert to their previous value when the transaction ends (or when you overwrite them of course). To set a local variable, simply execute an SQL statement along the lines of "`SET LOCAL var = 'value'`" inside your transaction.
| var | The variable to set. |
| value | The new value for the variable. |
| void pqxx::connection::set_variable | ( | std::string_view | var, |
| std::string_view | value, | ||
| sl | loc = sl::current() |
||
| ) | & |
Set session variable, using SQL's SET command.
SET command.| var | Variable to set. |
| value | New value for Var. This can be any SQL expression. If it's a string, be sure that it's properly escaped and quoted. |
|
noexcept |
Set session verbosity.
Set the verbosity of error messages to "terse", "normal" (the default), or "verbose."
This affects the notices that the connection and its result objects will pass to your notice handler.
If "terse", returned messages include severity, primary text, and position only; this will normally fit on a single line. "normal" produces messages that include the above plus any detail, hint, or context fields (these might span multiple lines). "verbose" includes all available fields.
|
noexcept |
Socket currently used for connection, or -1 for none.
Query the current socket number. This is intended for event loops based on functions such as select() or poll(), where you're waiting for any of multiple file descriptors to become ready for communication.
Please try to stay away from this function. It is really only meant for event loops that need to wait on more than one file descriptor. If all you need is to block until a notification arrives, for instance, use await_notification(). If you want to issue queries and retrieve results in nonblocking fashion, check out the pipeline class.
|
noexcept |
Enable tracing to a given output stream, or nullptr to disable.
Unescape binary data, e.g. from a bytea field.
Takes a binary string as escaped by PostgreSQL, and returns a restored copy of the original binary data.
(The data must be encoded in PostgreSQL's "hex" format. The legacy "bytea" escape format, used prior to PostgreSQL 9.0, is no longer supported.)
| void pqxx::connection::unprepare | ( | std::string_view | name, |
| sl | loc = sl::current() |
||
| ) |
Drop prepared statement.
|
noexcept |
Database user ID under which we are connected, if any.
Returns nullptr when not connected.
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |