Wraps a Database connection. More...
#include <connection.hpp>
Public Member Functions | |
virtual | ~connection () |
Destroys connection and automatically disconnects. More... | |
bool | connect () |
Actually connects to the database using given account, sets SSL and additional options as well as auto commit. More... | |
void | disconnect () |
Disconnects from the database. More... | |
bool | connected () const |
Indicates whether the connection is active. More... | |
account_ref | account () const |
Gets the account associated with this connection. More... | |
const std::string & | schema () const |
Gets the schema (database name) More... | |
bool | set_schema (const std::string &schema) |
Sets the schema (database name). More... | |
const std::string & | charset () const |
Gets the charset associated with this connection. More... | |
bool | set_charset (const std::string &value) |
Sets the charset. More... | |
u64 | execute (const std::string &query) |
Execute a query without interest in a result. More... | |
u64 | insert (const std::string &query) |
Execute a query (usually, but not limited to INSERT) with interest for the last row id. More... | |
result_set_ref | query (const std::string &query) |
Execute a query with an result (if no result is returned, the result_set will be empty). More... | |
bool | auto_commit () const |
Gets the status of the auto_commit setting. More... | |
bool | set_auto_commit (bool auto_commit) |
Sets the auto_commit setting. More... | |
statement_ref | create_statement (const std::string &query) |
Create a prepared statement. More... | |
transaction_ref | create_transaction (isolation::level level=isolation::repeatable_read, bool consistent_snapshot=true) |
Create a transaction. More... | |
![]() | |
last_error () | |
u32 | error_no () const |
const std::string & | error () const |
Static Public Member Functions | |
static connection_ref | create (const account_ref &account) |
Creates a new connection using the given account. More... | |
Private Member Functions | |
connection (const account_ref &account) | |
Private constructor used to create a connection with the given account. More... | |
Private Attributes | |
MYSQL * | m_mysql |
bool | m_auto_commit |
std::string | m_schema |
std::string | m_charset |
account_ref | m_account |
Friends | |
class | result_set |
class | statement |
class | transaction |
class | save_point |
Additional Inherited Members | |
![]() | |
u32 | m_last_error_no |
std::string | m_last_error |
Wraps a Database connection.
Definition at line 27 of file connection.hpp.
|
virtual |
Destroys connection and automatically disconnects.
Definition at line 32 of file connection.cpp.
References disconnect().
|
private |
Private constructor used to create a connection with the given account.
Definition at line 25 of file connection.cpp.
Referenced by create().
account_ref connection::account | ( | ) | const |
Gets the account associated with this connection.
Definition at line 63 of file connection.cpp.
References m_account.
Referenced by mariadb::result_set::result_set().
bool connection::auto_commit | ( | ) | const |
Gets the status of the auto_commit setting.
Definition at line 65 of file connection.cpp.
References m_auto_commit.
Referenced by set_auto_commit().
const std::string & connection::charset | ( | ) | const |
Gets the charset associated with this connection.
Definition at line 45 of file connection.cpp.
References m_charset.
bool connection::connect | ( | ) |
Actually connects to the database using given account, sets SSL and additional options as well as auto commit.
Definition at line 78 of file connection.cpp.
References connected(), execute(), m_account, m_mysql, MARIADB_ERROR, MYSQL_ERROR, MYSQL_ERROR_DISCONNECT, set_auto_commit(), and set_schema().
Referenced by create_statement(), create_transaction(), execute(), insert(), query(), set_auto_commit(), set_charset(), and set_schema().
bool connection::connected | ( | ) | const |
Indicates whether the connection is active.
Also detects stale connections
Definition at line 56 of file connection.cpp.
References m_mysql.
Referenced by connect().
|
static |
Creates a new connection using the given account.
account | The account used to provide the connection information |
Definition at line 28 of file connection.cpp.
References connection().
Referenced by mariadb::concurrency::create_statement(), mariadb::worker::execute(), and SkeletonTest::SetUp().
statement_ref connection::create_statement | ( | const std::string & | query | ) |
Create a prepared statement.
Use "?" to issue bindings in the query which can then be filled in. For information on how to properly use prepared statements please refer to the MariaDB manual. The connection needs to be established. Note that "?" bindings can only be established at certain locations in a SQL statement. A misplaced "?" will result in an error when executing the statement. The statement is only valid as long as the connection is valid.
Definition at line 190 of file connection.cpp.
transaction_ref connection::create_transaction | ( | isolation::level | level = isolation::repeatable_read , |
bool | consistent_snapshot = true |
||
) |
Create a transaction.
Any change to the database will be held back until you COMMIT the transaction. A not committed transaction automatically rolls all changes back on destruction. Note: the transaction is only valid as long as this connection is valid.
level | The level of isolation to set while using this transaction. Defaults to repeatable read |
consistent_snapshot | Indicates whether to require a consistent snapshot before entering the transaction. Note: refer to MariaDB manual for further information |
Definition at line 196 of file connection.cpp.
References connect(), and transaction.
void connection::disconnect | ( | ) |
Disconnects from the database.
Definition at line 128 of file connection.cpp.
References m_mysql.
Referenced by ~connection().
u64 connection::execute | ( | const std::string & | query | ) |
Execute a query without interest in a result.
The connection needs to be established.
query | SQL query to execute |
Definition at line 150 of file connection.cpp.
References connect(), m_mysql, and MYSQL_ERROR_NO_BRAKET.
Referenced by mariadb::save_point::commit(), connect(), mariadb::save_point::save_point(), mariadb::transaction::transaction(), and mariadb::save_point::~save_point().
u64 connection::insert | ( | const std::string & | query | ) |
Execute a query (usually, but not limited to INSERT) with interest for the last row id.
The connection needs to be established.
query | SQL query to execute |
Definition at line 180 of file connection.cpp.
References connect(), m_mysql, and MYSQL_ERROR_NO_BRAKET.
result_set_ref connection::query | ( | const std::string & | query | ) |
Execute a query with an result (if no result is returned, the result_set will be empty).
The connection needs to be established. Note: The result is only valid as long as the connection is valid.
query | SQL query to execute |
Definition at line 137 of file connection.cpp.
References connect(), m_mysql, MYSQL_ERROR_NO_BRAKET, and result_set.
const std::string & connection::schema | ( | ) | const |
Gets the schema (database name)
Definition at line 34 of file connection.cpp.
References m_schema.
Referenced by set_schema().
bool connection::set_auto_commit | ( | bool | auto_commit | ) |
Sets the auto_commit setting.
Default MariaDB setting is TRUE. The connection needs to be established. This setting controls the default behavior of holding all changes back until a COMMIT is issued. See MariaDB documentation for further information on this setting.
Definition at line 67 of file connection.cpp.
References auto_commit(), connect(), m_auto_commit, m_mysql, and MYSQL_ERROR.
Referenced by connect().
bool connection::set_charset | ( | const std::string & | value | ) |
Sets the charset.
The connection needs to be already established
value | The new charset |
Definition at line 47 of file connection.cpp.
References connect(), m_charset, m_mysql, and MYSQL_ERROR.
bool connection::set_schema | ( | const std::string & | schema | ) |
Sets the schema (database name).
The connection needs to be already established
schema | The new schema name |
Definition at line 36 of file connection.cpp.
References connect(), m_mysql, m_schema, MYSQL_ERROR, and schema().
Referenced by connect().
|
friend |
Definition at line 28 of file connection.hpp.
Referenced by query().
|
friend |
Definition at line 31 of file connection.hpp.
|
friend |
Definition at line 29 of file connection.hpp.
Referenced by create_statement().
|
friend |
Definition at line 30 of file connection.hpp.
Referenced by create_transaction().
|
private |
Definition at line 200 of file connection.hpp.
|
private |
Definition at line 194 of file connection.hpp.
Referenced by auto_commit(), and set_auto_commit().
|
private |
Definition at line 198 of file connection.hpp.
Referenced by charset(), and set_charset().
|
private |
Definition at line 191 of file connection.hpp.
Referenced by mariadb::transaction::commit(), connect(), connected(), disconnect(), execute(), insert(), query(), set_auto_commit(), set_charset(), set_schema(), mariadb::statement::statement(), and mariadb::transaction::~transaction().
|
private |
Definition at line 196 of file connection.hpp.
Referenced by schema(), and set_schema().