The Battle for Wesnoth  1.15.12+dev
Public Member Functions | Private Member Functions | Private Attributes | List of all members
dbconn Class Reference

This class is responsible for handling the database connections as well as executing queries and handling any results. More...

#include <dbconn.hpp>

Public Member Functions

 dbconn (const config &c)
 Initializes the synchronous query connection as well as the account object that has the connection settings. More...
 
int async_test_query (int limit)
 
std::string get_uuid ()
 
std::string get_tournaments ()
 
std::unique_ptr< simple_wml::documentget_game_history (int player_id, int offset)
 This is an asynchronous query that is executed on a separate connection to retrieve the game history for the provided player. More...
 
bool user_exists (const std::string &name)
 
long get_forum_id (const std::string &name)
 
bool extra_row_exists (const std::string &name)
 
bool is_user_in_group (const std::string &name, int group_id)
 
std::string get_user_string (const std::string &table, const std::string &column, const std::string &name)
 
int get_user_int (const std::string &table, const std::string &column, const std::string &name)
 
void write_user_int (const std::string &column, const std::string &name, int value)
 The provided value is updated if a row exists or a new row inserted otherwise. More...
 
ban_check get_ban_info (const std::string &name, const std::string &ip)
 
void insert_game_info (const std::string &uuid, int game_id, const std::string &version, const std::string &name, int reload, int observers, int is_public, int has_password)
 
void update_game_end (const std::string &uuid, int game_id, const std::string &replay_location)
 
void insert_game_player_info (const std::string &uuid, int game_id, const std::string &username, int side_number, int is_host, const std::string &faction, const std::string &version, const std::string &source, const std::string &current_user)
 
void insert_game_content_info (const std::string &uuid, int game_id, const std::string &type, const std::string &name, const std::string &id, const std::string &source, const std::string &version)
 
void set_oos_flag (const std::string &uuid, int game_id)
 
bool topic_id_exists (int topic_id)
 
void insert_addon_info (const std::string &instance_version, const std::string &id, const std::string &name, const std::string &type, const std::string &version, bool forum_auth, int topic_id)
 
unsigned long long insert_login (const std::string &username, const std::string &ip)
 
void update_logout (unsigned long long login_id)
 
void get_users_for_ip (const std::string &ip, std::ostringstream *out)
 
void get_ips_for_user (const std::string &username, std::ostringstream *out)
 

Private Member Functions

void log_sql_exception (const std::string &text, const mariadb::exception::base &e)
 This is used to write out error text when an SQL-related exception occurs. More...
 
mariadb::connection_ref create_connection ()
 Creates a new connection object from the account. More...
 
template<typename... Args>
void get_complex_results (mariadb::connection_ref connection, rs_base &base, const std::string &sql, Args &&... args)
 Queries can return data with various types that can't be easily fit into a pre-determined structure. More...
 
template<typename... Args>
std::string get_single_string (mariadb::connection_ref connection, const std::string &sql, Args &&... args)
 
template<typename... Args>
long get_single_long (mariadb::connection_ref connection, const std::string &sql, Args &&... args)
 
template<typename... Args>
bool exists (mariadb::connection_ref connection, const std::string &sql, Args &&... args)
 
template<typename... Args>
mariadb::result_set_ref select (mariadb::connection_ref connection, const std::string &sql, Args &&... args)
 Executes a select statement. More...
 
template<typename... Args>
unsigned long long modify (mariadb::connection_ref connection, const std::string &sql, Args &&... args)
 Executes non-select statements (ie: insert, update, delete). More...
 
template<typename... Args>
mariadb::statement_ref query (mariadb::connection_ref connection, const std::string &sql, Args &&... args)
 Begins recursively unpacking of the parameter pack in order to be able to call the correct parameterized setters on the query. More...
 
template<typename Arg , typename... Args>
void prepare (mariadb::statement_ref stmt, int i, Arg arg, Args &&... args)
 The next parameter to be added is split off from the parameter pack. More...
 
template<typename Arg >
int prepare (mariadb::statement_ref stmt, int i, Arg arg)
 Specializations for each type of value to be parameterized. More...
 
void prepare (mariadb::statement_ref stmt, int i)
 Nothing left to parameterize, so break out of the recursion. More...
 

Private Attributes

mariadb::account_ref account_
 The account used to connect to the database. More...
 
mariadb::connection_ref connection_
 The actual connection to the database. More...
 
std::string db_users_table_
 The name of the table that contains forum user information. More...
 
std::string db_banlist_table_
 The name of the table that contains forum ban information. More...
 
std::string db_extra_table_
 The name of the table that contains additional user information. More...
 
std::string db_game_info_table_
 The name of the table that contains game-level information. More...
 
std::string db_game_player_info_table_
 The name of the table that contains player-level information per game. More...
 
std::string db_game_content_info_table_
 The name of the table that contains game content information. More...
 
std::string db_user_group_table_
 The name of the table that contains forum group information. More...
 
std::string db_tournament_query_
 The text of the SQL query to use to retrieve any currently active tournaments. More...
 
std::string db_topics_table_
 The name of the table that contains phpbb forum thread information. More...
 
std::string db_addon_info_table_
 The name of the table that contains add-on information. More...
 
std::string db_connection_history_table_
 The name of the table that contains user connection history. More...
 

Detailed Description

This class is responsible for handling the database connections as well as executing queries and handling any results.

Note
The account and connection should never ever be provided to anything outside of this class.
!DO NOT APPEND VALUES DIRECTLY TO THE SQL TEXT IN ANY QUERY!

Definition at line 36 of file dbconn.hpp.

Constructor & Destructor Documentation

◆ dbconn()

dbconn::dbconn ( const config c)

Initializes the synchronous query connection as well as the account object that has the connection settings.

Parameters
cThe config object to read information from.

Member Function Documentation

◆ async_test_query()

int dbconn::async_test_query ( int  limit)
See also
forum_user_handler::async_test_query().

◆ create_connection()

mariadb::connection_ref dbconn::create_connection ( )
private

Creates a new connection object from the account.

◆ exists()

template<typename... Args>
bool dbconn::exists ( mariadb::connection_ref  connection,
const std::string &  sql,
Args &&...  args 
)
private
Parameters
connectionThe database connecion that will be used to execute the query.
sqlThe SQL text to be executed.
argsThe parameterized values to be inserted into the query.
Returns
True if any data was returned by the query, otherwise false.

◆ extra_row_exists()

bool dbconn::extra_row_exists ( const std::string &  name)
Parameters
nameThe player's username.
Returns
Whether the player has a row in the extra table.

◆ get_ban_info()

ban_check dbconn::get_ban_info ( const std::string &  name,
const std::string &  ip 
)
See also
forum_user_handler::user_is_banned().

◆ get_complex_results()

template<typename... Args>
void dbconn::get_complex_results ( mariadb::connection_ref  connection,
rs_base base,
const std::string &  sql,
Args &&...  args 
)
private

Queries can return data with various types that can't be easily fit into a pre-determined structure.

Therefore for queries that can return multiple rows with multiple columns, a class that extends rs_base handles reading the results.

Parameters
connectionThe database connecion that will be used to execute the query.
baseThe class that will handle reading the results.
sqlThe SQL text to be executed.
argsThe parameterized values to be inserted into the query.

◆ get_forum_id()

long dbconn::get_forum_id ( const std::string &  name)
See also
forum_user_handler::get_forum_id().

◆ get_game_history()

std::unique_ptr<simple_wml::document> dbconn::get_game_history ( int  player_id,
int  offset 
)

This is an asynchronous query that is executed on a separate connection to retrieve the game history for the provided player.

Parameters
player_idThe forum ID of the player to get the game history for.
offsetThe offset to provide to the database for where to start returning rows from.
Returns
The simple_wml document containing the query results, or simply the error attribute if an exception is thrown.

◆ get_ips_for_user()

void dbconn::get_ips_for_user ( const std::string &  username,
std::ostringstream *  out 
)
See also
forum_user_handler::get_ips_for_users().

◆ get_single_long()

template<typename... Args>
long dbconn::get_single_long ( mariadb::connection_ref  connection,
const std::string &  sql,
Args &&...  args 
)
private
Parameters
connectionThe database connecion that will be used to execute the query.
sqlThe SQL text to be executed.
argsThe parameterized values to be inserted into the query.
Returns
The single long value queried.
Exceptions
mariadb::exception::basewhen the query finds no value to be retrieved.

◆ get_single_string()

template<typename... Args>
std::string dbconn::get_single_string ( mariadb::connection_ref  connection,
const std::string &  sql,
Args &&...  args 
)
private
Parameters
connectionThe database connecion that will be used to execute the query.
sqlThe SQL text to be executed.
argsThe parameterized values to be inserted into the query.
Returns
The single string value queried.
Exceptions
mariadb::exception::basewhen the query finds no value to be retrieved.

◆ get_tournaments()

std::string dbconn::get_tournaments ( )
See also
forum_user_handler::get_tournaments().

◆ get_user_int()

int dbconn::get_user_int ( const std::string &  table,
const std::string &  column,
const std::string &  name 
)
Parameters
tableThe table that will be queried.
columnThe column that will be selected.
nameThe player's username.
Returns
The int value in the provided table and column for the provided username.

◆ get_user_string()

std::string dbconn::get_user_string ( const std::string &  table,
const std::string &  column,
const std::string &  name 
)
Parameters
tableThe table that will be queried.
columnThe column that will be selected.
nameThe player's username.
Returns
The string value in the provided table and column for the provided username.

◆ get_users_for_ip()

void dbconn::get_users_for_ip ( const std::string &  ip,
std::ostringstream *  out 
)
See also
forum_user_handler::get_users_for_ip().

◆ get_uuid()

std::string dbconn::get_uuid ( )
See also
forum_user_handler::get_uuid().

◆ insert_addon_info()

void dbconn::insert_addon_info ( const std::string &  instance_version,
const std::string &  id,
const std::string &  name,
const std::string &  type,
const std::string &  version,
bool  forum_auth,
int  topic_id 
)
See also
forum_user_handler::db_insert_addon_info().

◆ insert_game_content_info()

void dbconn::insert_game_content_info ( const std::string &  uuid,
int  game_id,
const std::string &  type,
const std::string &  name,
const std::string &  id,
const std::string &  source,
const std::string &  version 
)
See also
forum_user_handler::db_insert_game_content_info().

◆ insert_game_info()

void dbconn::insert_game_info ( const std::string &  uuid,
int  game_id,
const std::string &  version,
const std::string &  name,
int  reload,
int  observers,
int  is_public,
int  has_password 
)
See also
forum_user_handler::db_insert_game_info().

◆ insert_game_player_info()

void dbconn::insert_game_player_info ( const std::string &  uuid,
int  game_id,
const std::string &  username,
int  side_number,
int  is_host,
const std::string &  faction,
const std::string &  version,
const std::string &  source,
const std::string &  current_user 
)
See also
forum_user_handler::db_insert_game_player_info().

◆ insert_login()

unsigned long long dbconn::insert_login ( const std::string &  username,
const std::string &  ip 
)
See also
forum_user_handler::db_insert_login().

◆ is_user_in_group()

bool dbconn::is_user_in_group ( const std::string &  name,
int  group_id 
)
See also
forum_user_handler::is_user_in_group().

◆ log_sql_exception()

void dbconn::log_sql_exception ( const std::string &  text,
const mariadb::exception::base &  e 
)
private

This is used to write out error text when an SQL-related exception occurs.

Parameters
textSome custom text to log.
eThe exception that occurred which has information about what went wrong.

◆ modify()

template<typename... Args>
unsigned long long dbconn::modify ( mariadb::connection_ref  connection,
const std::string &  sql,
Args &&...  args 
)
private

Executes non-select statements (ie: insert, update, delete).

Parameters
connectionThe database connecion that will be used to execute the query.
sqlThe SQL text to be executed.
argsThe parameterized values to be inserted into the query.
Returns
The number of rows modified.

◆ prepare() [1/3]

template<typename Arg , typename... Args>
void dbconn::prepare ( mariadb::statement_ref  stmt,
int  i,
Arg  arg,
Args &&...  args 
)
private

The next parameter to be added is split off from the parameter pack.

Parameters
stmtThe statement that will have parameterized values set on it.
iThe index of the current parameterized value.
argThe next parameter to be added.
argsThe remaining parameters to be added.

◆ prepare() [2/3]

template<typename Arg >
int dbconn::prepare ( mariadb::statement_ref  stmt,
int  i,
Arg  arg 
)
private

Specializations for each type of value to be parameterized.

There are other parameter setters than those currently implemented, but so far there hasn't been a reason to add them.

Parameters
stmtThe statement that will have parameterized values set on it.
iThe index of the current parameterized value.
argThe next parameter to be added.
Returns
The index of the next parameter to add.

◆ prepare() [3/3]

void dbconn::prepare ( mariadb::statement_ref  stmt,
int  i 
)
private

Nothing left to parameterize, so break out of the recursion.

◆ query()

template<typename... Args>
mariadb::statement_ref dbconn::query ( mariadb::connection_ref  connection,
const std::string &  sql,
Args &&...  args 
)
private

Begins recursively unpacking of the parameter pack in order to be able to call the correct parameterized setters on the query.

Parameters
connectionThe database connecion that will be used to execute the query.
sqlThe SQL text to be executed.
argsThe parameterized values to be inserted into the query.
Returns
A statement object with all parameterized values added. This will then be executed by either modify() or select().

◆ select()

template<typename... Args>
mariadb::result_set_ref dbconn::select ( mariadb::connection_ref  connection,
const std::string &  sql,
Args &&...  args 
)
private

Executes a select statement.

Parameters
connectionThe database connecion that will be used to execute the query.
sqlThe SQL text to be executed.
argsThe parameterized values to be inserted into the query.
Returns
A result set containing the results of the select statement executed.

◆ set_oos_flag()

void dbconn::set_oos_flag ( const std::string &  uuid,
int  game_id 
)
See also
forum_user_handler::db_set_oos_flag().

◆ topic_id_exists()

bool dbconn::topic_id_exists ( int  topic_id)
See also
forum_user_handler::db_topic_id_exists().

◆ update_game_end()

void dbconn::update_game_end ( const std::string &  uuid,
int  game_id,
const std::string &  replay_location 
)
See also
forum_user_handler::db_update_game_end().

◆ update_logout()

void dbconn::update_logout ( unsigned long long  login_id)
See also
forum_user_handler::db_update_logout().

◆ user_exists()

bool dbconn::user_exists ( const std::string &  name)
See also
forum_user_handler::user_exists().

◆ write_user_int()

void dbconn::write_user_int ( const std::string &  column,
const std::string &  name,
int  value 
)

The provided value is updated if a row exists or a new row inserted otherwise.

Parameters
columnThe column that the value will be put into.
nameThe player's username.
valueThe value to be put into the column.

Member Data Documentation

◆ account_

mariadb::account_ref dbconn::account_
private

The account used to connect to the database.

Also contains the connection settings.

Note
settings put on the connection, rather than the account, are NOT kept if a reconnect occurs!

Definition at line 182 of file dbconn.hpp.

◆ connection_

mariadb::connection_ref dbconn::connection_
private

The actual connection to the database.

Definition at line 184 of file dbconn.hpp.

◆ db_addon_info_table_

std::string dbconn::db_addon_info_table_
private

The name of the table that contains add-on information.

Definition at line 205 of file dbconn.hpp.

◆ db_banlist_table_

std::string dbconn::db_banlist_table_
private

The name of the table that contains forum ban information.

Definition at line 189 of file dbconn.hpp.

◆ db_connection_history_table_

std::string dbconn::db_connection_history_table_
private

The name of the table that contains user connection history.

Definition at line 207 of file dbconn.hpp.

◆ db_extra_table_

std::string dbconn::db_extra_table_
private

The name of the table that contains additional user information.

Definition at line 191 of file dbconn.hpp.

◆ db_game_content_info_table_

std::string dbconn::db_game_content_info_table_
private

The name of the table that contains game content information.

Definition at line 197 of file dbconn.hpp.

◆ db_game_info_table_

std::string dbconn::db_game_info_table_
private

The name of the table that contains game-level information.

Definition at line 193 of file dbconn.hpp.

◆ db_game_player_info_table_

std::string dbconn::db_game_player_info_table_
private

The name of the table that contains player-level information per game.

Definition at line 195 of file dbconn.hpp.

◆ db_topics_table_

std::string dbconn::db_topics_table_
private

The name of the table that contains phpbb forum thread information.

Definition at line 203 of file dbconn.hpp.

◆ db_tournament_query_

std::string dbconn::db_tournament_query_
private

The text of the SQL query to use to retrieve any currently active tournaments.

Definition at line 201 of file dbconn.hpp.

◆ db_user_group_table_

std::string dbconn::db_user_group_table_
private

The name of the table that contains forum group information.

Definition at line 199 of file dbconn.hpp.

◆ db_users_table_

std::string dbconn::db_users_table_
private

The name of the table that contains forum user information.

Definition at line 187 of file dbconn.hpp.


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