The Battle for Wesnoth  1.15.8+dev
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
mariadb::connection Class Reference

Wraps a Database connection. More...

#include <connection.hpp>

Inheritance diagram for mariadb::connection:
Inheritance graph

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...
 
- Public Member Functions inherited from mariadb::last_error
 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

- Protected Attributes inherited from mariadb::last_error
u32 m_last_error_no
 
std::string m_last_error
 

Detailed Description

Wraps a Database connection.

Definition at line 27 of file connection.hpp.

Constructor & Destructor Documentation

◆ ~connection()

connection::~connection ( )
virtual

Destroys connection and automatically disconnects.

Definition at line 32 of file connection.cpp.

References disconnect().

◆ connection()

connection::connection ( const account_ref account)
private

Private constructor used to create a connection with the given account.

Definition at line 25 of file connection.cpp.

Referenced by create().

Member Function Documentation

◆ account()

account_ref connection::account ( ) const

Gets the account associated with this connection.

Returns
Reference to the account

Definition at line 63 of file connection.cpp.

References m_account.

Referenced by mariadb::result_set::result_set().

◆ auto_commit()

bool connection::auto_commit ( ) const

Gets the status of the auto_commit setting.

Returns
True if auto_commit is enabled

Definition at line 65 of file connection.cpp.

References m_auto_commit.

Referenced by set_auto_commit().

◆ charset()

const std::string & connection::charset ( ) const

Gets the charset associated with this connection.

Returns
String containg the charset (see documentation of MariaDB for possible values)

Definition at line 45 of file connection.cpp.

References m_charset.

◆ connect()

bool connection::connect ( )

Actually connects to the database using given account, sets SSL and additional options as well as auto commit.

Returns
True on success

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().

◆ connected()

bool connection::connected ( ) const

Indicates whether the connection is active.

Also detects stale connections

Returns
True on active connection

Definition at line 56 of file connection.cpp.

References m_mysql.

Referenced by connect().

◆ create()

connection_ref connection::create ( const account_ref account)
static

Creates a new connection using the given account.

Parameters
accountThe account used to provide the connection information
Returns
Reference to the newly created connection

Definition at line 28 of file connection.cpp.

References connection().

Referenced by mariadb::concurrency::create_statement(), mariadb::worker::execute(), and SkeletonTest::SetUp().

◆ create_statement()

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.

Returns
Reference to the created statement.

Definition at line 190 of file connection.cpp.

References connect(), and statement.

◆ create_transaction()

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.

Parameters
levelThe level of isolation to set while using this transaction. Defaults to repeatable read
consistent_snapshotIndicates whether to require a consistent snapshot before entering the transaction. Note: refer to MariaDB manual for further information
Returns
Reference to the created transaction.

Definition at line 196 of file connection.cpp.

References connect(), and transaction.

◆ disconnect()

void connection::disconnect ( )

Disconnects from the database.

Definition at line 128 of file connection.cpp.

References m_mysql.

Referenced by ~connection().

◆ execute()

u64 connection::execute ( const std::string &  query)

Execute a query without interest in a result.

The connection needs to be established.

Parameters
querySQL query to execute
Returns
The number of actually affected rows. 0 on error

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().

◆ insert()

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.

Parameters
querySQL query to execute
Returns
Last row id of the inserted row. 0 on error

Definition at line 180 of file connection.cpp.

References connect(), m_mysql, and MYSQL_ERROR_NO_BRAKET.

◆ query()

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.

Parameters
querySQL query to execute
Returns
Result of the query as result_set.

Definition at line 137 of file connection.cpp.

References connect(), m_mysql, MYSQL_ERROR_NO_BRAKET, and result_set.

◆ schema()

const std::string & connection::schema ( ) const

Gets the schema (database name)

Returns
String containing the schema

Definition at line 34 of file connection.cpp.

References m_schema.

Referenced by set_schema().

◆ set_auto_commit()

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.

Returns
True on success

Definition at line 67 of file connection.cpp.

References auto_commit(), connect(), m_auto_commit, m_mysql, and MYSQL_ERROR.

Referenced by connect().

◆ set_charset()

bool connection::set_charset ( const std::string &  value)

Sets the charset.

The connection needs to be already established

Parameters
valueThe new charset
Returns
True on success

Definition at line 47 of file connection.cpp.

References connect(), m_charset, m_mysql, and MYSQL_ERROR.

◆ set_schema()

bool connection::set_schema ( const std::string &  schema)

Sets the schema (database name).

The connection needs to be already established

Parameters
schemaThe new schema name
Returns
True on success

Definition at line 36 of file connection.cpp.

References connect(), m_mysql, m_schema, MYSQL_ERROR, and schema().

Referenced by connect().

Friends And Related Function Documentation

◆ result_set

friend class result_set
friend

Definition at line 28 of file connection.hpp.

Referenced by query().

◆ save_point

friend class save_point
friend

Definition at line 31 of file connection.hpp.

◆ statement

friend class statement
friend

Definition at line 29 of file connection.hpp.

Referenced by create_statement().

◆ transaction

friend class transaction
friend

Definition at line 30 of file connection.hpp.

Referenced by create_transaction().

Member Data Documentation

◆ m_account

account_ref mariadb::connection::m_account
private

Definition at line 200 of file connection.hpp.

Referenced by account(), and connect().

◆ m_auto_commit

bool mariadb::connection::m_auto_commit
private

Definition at line 194 of file connection.hpp.

Referenced by auto_commit(), and set_auto_commit().

◆ m_charset

std::string mariadb::connection::m_charset
private

Definition at line 198 of file connection.hpp.

Referenced by charset(), and set_charset().

◆ m_mysql

MYSQL* mariadb::connection::m_mysql
private

◆ m_schema

std::string mariadb::connection::m_schema
private

Definition at line 196 of file connection.hpp.

Referenced by schema(), and set_schema().


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