libdballe 9.6
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
dballe::sql::PostgreSQLConnection Class Reference

Database connection. More...

#include <postgresql.h>

Inheritance diagram for dballe::sql::PostgreSQLConnection:
dballe::sql::Connection

Public Member Functions

 PostgreSQLConnection (const PostgreSQLConnection &)=delete
 
 PostgreSQLConnection (const PostgreSQLConnection &&)=delete
 
PostgreSQLConnectionoperator= (const PostgreSQLConnection &)=delete
 
 operator PGconn * ()
 
void open_url (const std::string &connection_string)
 Connect to PostgreSQL using a connection URI.
 
void open_test ()
 
std::unique_ptr< Transactiontransaction (bool readonly=false) override
 Begin a transaction.
 
void prepare (const std::string &name, const std::string &query)
 Precompile a query.
 
postgresql::Result exec_unchecked (const char *query)
 
postgresql::Result exec_unchecked (const std::string &query)
 
template<typename STRING >
void exec_no_data (STRING query)
 
template<typename STRING >
postgresql::Result exec (STRING query)
 
template<typename STRING >
postgresql::Result exec_one_row (STRING query)
 
template<typename ... ARGS>
postgresql::Result exec_unchecked (const char *query, ARGS... args)
 
template<typename ... ARGS>
postgresql::Result exec_unchecked (const std::string &query, ARGS... args)
 
template<typename STRING , typename ... ARGS>
void exec_no_data (STRING query, ARGS... args)
 
template<typename STRING , typename ... ARGS>
postgresql::Result exec (STRING query, ARGS... args)
 
template<typename STRING , typename ... ARGS>
postgresql::Result exec_one_row (STRING query, ARGS... args)
 
postgresql::Result exec_prepared_unchecked (const char *name)
 
postgresql::Result exec_prepared_unchecked (const std::string &name)
 
template<typename STRING >
void exec_prepared_no_data (STRING name)
 
template<typename STRING >
postgresql::Result exec_prepared (STRING name)
 
template<typename STRING >
postgresql::Result exec_prepared_one_row (STRING name)
 
template<typename ... ARGS>
postgresql::Result exec_prepared_unchecked (const char *name, ARGS... args)
 
template<typename ... ARGS>
postgresql::Result exec_prepared_unchecked (const std::string &name, ARGS... args)
 
template<typename STRING , typename ... ARGS>
void exec_prepared_no_data (STRING name, ARGS... args)
 
template<typename STRING , typename ... ARGS>
postgresql::Result exec_prepared (STRING name, ARGS... args)
 
template<typename STRING , typename ... ARGS>
postgresql::Result exec_prepared_one_row (STRING name, ARGS... args)
 
void cancel_running_query_nothrow () noexcept
 Send a cancellation command to the server.
 
void discard_all_input_nothrow () noexcept
 Discard all input from an asynchronous request.
 
bool has_table (const std::string &name) override
 Check if the database contains a table.
 
std::string get_setting (const std::string &key) override
 Get a value from the settings table.
 
void set_setting (const std::string &key, const std::string &value) override
 Set a value in the settings table.
 
void drop_settings () override
 Drop the settings table.
 
void execute (const std::string &query) override
 Execute a query without reading its results.
 
void explain (const std::string &query, FILE *out) override
 Format and print the EXPLAIN output for the query to the given file.
 
void drop_table_if_exists (const char *name)
 Delete a table in the database if it exists, otherwise do nothing.
 
int changes ()
 Count the number of rows modified by the last query that was run.
 
void pqexec (const std::string &query)
 Wrap PQexec.
 
void pqexec_nothrow (const std::string &query) noexcept
 Wrap PQexec but do not throw an exception in case of errors.
 
void run_single_row_mode (const std::string &query_desc, std::function< void(const postgresql::Result &)> dest)
 Retrieve query results in single row mode.
 
void append_escaped (Querybuf &qb, const char *str)
 Escape the string as a literal value and append it to qb.
 
void append_escaped (Querybuf &qb, const std::string &str)
 Escape the string as a literal value and append it to qb.
 
void append_escaped (Querybuf &qb, const std::vector< uint8_t > &buf)
 Escape the buffer as bytea literal and append it to qb.
 
- Public Member Functions inherited from dballe::sql::Connection
const std::string & get_url () const
 
virtual std::unique_ptr< Transactiontransaction (bool readonly=false)=0
 Begin a transaction.
 
virtual bool has_table (const std::string &name)=0
 Check if the database contains a table.
 
virtual std::string get_setting (const std::string &key)=0
 Get a value from the settings table.
 
virtual void set_setting (const std::string &key, const std::string &value)=0
 Set a value in the settings table.
 
virtual void drop_settings ()=0
 Drop the settings table.
 
virtual void add_datetime (Querybuf &qb, const Datetime &dt) const
 Format a datetime and add it to the querybuf.
 
virtual void execute (const std::string &query)=0
 Execute a query without reading its results.
 
virtual void explain (const std::string &query, FILE *out)=0
 Format and print the EXPLAIN output for the query to the given file.
 

Static Public Member Functions

static std::shared_ptr< PostgreSQLConnectioncreate ()
 
- Static Public Member Functions inherited from dballe::sql::Connection
static std::shared_ptr< Connectioncreate (const DBConnectOptions &options)
 Create a new connection from a URL.
 

Protected Member Functions

void init_after_connect ()
 
void fork_prepare () override
 
void fork_parent () override
 
void fork_child () override
 
void check_connection ()
 
- Protected Member Functions inherited from dballe::sql::Connection
void register_atfork ()
 Register this connection to be notified in case of fork()-ing.
 
virtual void fork_prepare ()
 
virtual void fork_parent ()
 
virtual void fork_child ()
 

Protected Attributes

PGconn * db = nullptr
 Database connection.
 
std::unordered_set< std::string > prepared_names
 
bool forked = false
 Marker to catch attempts to reuse connections in forked processes.
 
- Protected Attributes inherited from dballe::sql::Connection
std::string url
 

Additional Inherited Members

- Public Attributes inherited from dballe::sql::Connection
ServerType server_type
 Type of SQL server we are connected to.
 

Detailed Description

Database connection.

Member Function Documentation

◆ drop_settings()

void dballe::sql::PostgreSQLConnection::drop_settings ( )
overridevirtual

Drop the settings table.

Implements dballe::sql::Connection.

◆ execute()

void dballe::sql::PostgreSQLConnection::execute ( const std::string &  query)
overridevirtual

Execute a query without reading its results.

Implements dballe::sql::Connection.

◆ explain()

void dballe::sql::PostgreSQLConnection::explain ( const std::string &  query,
FILE *  out 
)
overridevirtual

Format and print the EXPLAIN output for the query to the given file.

Implements dballe::sql::Connection.

◆ fork_child()

void dballe::sql::PostgreSQLConnection::fork_child ( )
overrideprotectedvirtual

Reimplemented from dballe::sql::Connection.

◆ fork_parent()

void dballe::sql::PostgreSQLConnection::fork_parent ( )
overrideprotectedvirtual

Reimplemented from dballe::sql::Connection.

◆ fork_prepare()

void dballe::sql::PostgreSQLConnection::fork_prepare ( )
overrideprotectedvirtual

Reimplemented from dballe::sql::Connection.

◆ get_setting()

std::string dballe::sql::PostgreSQLConnection::get_setting ( const std::string &  key)
overridevirtual

Get a value from the settings table.

Returns the empty string if the table does not exist.

Implements dballe::sql::Connection.

◆ has_table()

bool dballe::sql::PostgreSQLConnection::has_table ( const std::string &  name)
overridevirtual

Check if the database contains a table.

Implements dballe::sql::Connection.

◆ open_url()

void dballe::sql::PostgreSQLConnection::open_url ( const std::string &  connection_string)

Connect to PostgreSQL using a connection URI.

The syntax is described at: http://www.postgresql.org/docs/9.4/static/libpq-connect.html#AEN41094

◆ pqexec_nothrow()

void dballe::sql::PostgreSQLConnection::pqexec_nothrow ( const std::string &  query)
noexcept

Wrap PQexec but do not throw an exception in case of errors.

This is useful to be called in destructors. Errors will be printed to stderr.

◆ set_setting()

void dballe::sql::PostgreSQLConnection::set_setting ( const std::string &  key,
const std::string &  value 
)
overridevirtual

Set a value in the settings table.

The table is created if it does not exist.

Implements dballe::sql::Connection.

◆ transaction()

std::unique_ptr< Transaction > dballe::sql::PostgreSQLConnection::transaction ( bool  readonly = false)
overridevirtual

Begin a transaction.

The transaction will be controller by the returned Transaction object, and will end when its destuctor is called.

Implements dballe::sql::Connection.


The documentation for this class was generated from the following file: