MySQL 8.0.30
Source Code Documentation
mysqlrouter::MySQLSession Class Reference

#include <mysql_session.h>

Classes

struct  ConnectionParameters
 
class  Error
 
struct  LoggingStrategy
 
struct  LoggingStrategyDebugLogger
 
struct  LoggingStrategyNone
 
class  MYSQL_RES_Deleter
 
class  ResultRow
 
class  Transaction
 

Public Types

typedef std::vector< const char * > Row
 
typedef std::function< bool(const Row &)> RowProcessor
 
typedef std::function< void(unsigned, MYSQL_FIELD *)> FieldValidator
 
template<mysql_option Opt>
using BooleanOption = impl::Option< Opt, bool >
 
template<mysql_option Opt>
using IntegerOption = impl::Option< Opt, unsigned int >
 
template<mysql_option Opt>
using LongOption = impl::Option< Opt, unsigned long >
 
template<mysql_option Opt>
using ConstCharOption = impl::Option< Opt, const char * >
 
using DefaultAuthentication = ConstCharOption< MYSQL_DEFAULT_AUTH >
 
using EnableCleartextPlugin = BooleanOption< MYSQL_ENABLE_CLEARTEXT_PLUGIN >
 
using InitCommand = ConstCharOption< MYSQL_INIT_COMMAND >
 
using BindAddress = ConstCharOption< MYSQL_OPT_BIND >
 
using CanHandleExpiredPasswords = BooleanOption< MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS >
 
using Compress = BooleanOption< MYSQL_OPT_COMPRESS >
 
using CompressionAlgorithms = ConstCharOption< MYSQL_OPT_COMPRESSION_ALGORITHMS >
 
using ConnectAttributeReset = BooleanOption< MYSQL_OPT_CONNECT_ATTR_RESET >
 
using ConnectAttributeDelete = BooleanOption< MYSQL_OPT_CONNECT_ATTR_DELETE >
 
using ConnectTimeout = IntegerOption< MYSQL_OPT_CONNECT_TIMEOUT >
 
using GetServerPublicKey = BooleanOption< MYSQL_OPT_GET_SERVER_PUBLIC_KEY >
 
using LoadDataLocalDir = ConstCharOption< MYSQL_OPT_LOAD_DATA_LOCAL_DIR >
 
using LocalInfile = IntegerOption< MYSQL_OPT_LOCAL_INFILE >
 
using MaxAllowedPacket = LongOption< MYSQL_OPT_MAX_ALLOWED_PACKET >
 
using NamedPipe = BooleanOption< MYSQL_OPT_NAMED_PIPE >
 
using NetBufferLength = LongOption< MYSQL_OPT_NET_BUFFER_LENGTH >
 
using OptionalResultsetMetadata = BooleanOption< MYSQL_OPT_OPTIONAL_RESULTSET_METADATA >
 
using Protocol = IntegerOption< MYSQL_OPT_PROTOCOL >
 
using ReadTimeout = IntegerOption< MYSQL_OPT_READ_TIMEOUT >
 
using Reconnect = BooleanOption< MYSQL_OPT_RECONNECT >
 
using RetryCount = IntegerOption< MYSQL_OPT_RETRY_COUNT >
 
using SslCa = ConstCharOption< MYSQL_OPT_SSL_CA >
 
using SslCaPath = ConstCharOption< MYSQL_OPT_SSL_CAPATH >
 
using SslCert = ConstCharOption< MYSQL_OPT_SSL_CERT >
 
using SslCipher = ConstCharOption< MYSQL_OPT_SSL_CIPHER >
 
using SslCrl = ConstCharOption< MYSQL_OPT_SSL_CRL >
 
using SslCrlPath = ConstCharOption< MYSQL_OPT_SSL_CRLPATH >
 
using SslFipsMode = IntegerOption< MYSQL_OPT_SSL_FIPS_MODE >
 
using SslKey = ConstCharOption< MYSQL_OPT_SSL_KEY >
 
using SslMode = IntegerOption< MYSQL_OPT_SSL_MODE >
 
using TlsCipherSuites = ConstCharOption< MYSQL_OPT_TLS_CIPHERSUITES >
 
using TlsVersion = ConstCharOption< MYSQL_OPT_TLS_VERSION >
 
using WriteTimeout = IntegerOption< MYSQL_OPT_WRITE_TIMEOUT >
 
using ZstdCompressionLevel = IntegerOption< MYSQL_OPT_ZSTD_COMPRESSION_LEVEL >
 
using PluginDir = ConstCharOption< MYSQL_PLUGIN_DIR >
 
using ReportDataTruncation = BooleanOption< MYSQL_REPORT_DATA_TRUNCATION >
 
using ServerPluginKey = ConstCharOption< MYSQL_SERVER_PUBLIC_KEY >
 
using ReadDefaultFile = ConstCharOption< MYSQL_READ_DEFAULT_FILE >
 
using ReadDefaultGroup = ConstCharOption< MYSQL_READ_DEFAULT_GROUP >
 
using CharsetDir = ConstCharOption< MYSQL_SET_CHARSET_DIR >
 
using CharsetName = ConstCharOption< MYSQL_SET_CHARSET_NAME >
 
using SharedMemoryBasename = ConstCharOption< MYSQL_SHARED_MEMORY_BASE_NAME >
 

Public Member Functions

 MySQLSession (std::unique_ptr< LoggingStrategy > logging_strategy=std::make_unique< LoggingStrategyNone >())
 
virtual ~MySQLSession ()
 
virtual void set_ssl_options (mysql_ssl_mode ssl_mode, const std::string &tls_version, const std::string &ssl_cipher, const std::string &ca, const std::string &capath, const std::string &crl, const std::string &crlpath)
 
virtual void set_ssl_cert (const std::string &cert, const std::string &key)
 
template<class SettableMysqlOption >
stdx::expected< void, MysqlErrorset_option (const SettableMysqlOption &opt)
 set a mysql option. More...
 
template<class GettableMysqlOption >
bool get_option (GettableMysqlOption &opt)
 get a mysql option. More...
 
virtual void connect (const std::string &host, unsigned int port, const std::string &username, const std::string &password, const std::string &unix_socket, const std::string &default_schema, int connect_timeout=kDefaultConnectTimeout, int read_timeout=kDefaultReadTimeout)
 
virtual void disconnect ()
 
virtual void connect_and_set_opts (const ConnectionParameters &conn_params)
 This is an alternative way to initialise a new connection. More...
 
virtual ConnectionParameters get_connection_parameters ()
 Returns connection parameters which could be used as a template for spawning new connections. More...
 
virtual void execute (const std::string &query)
 
virtual void query (const std::string &query, const RowProcessor &processor, const FieldValidator &validator=null_field_validator)
 
virtual std::unique_ptr< MySQLSession::ResultRowquery_one (const std::string &query, const FieldValidator &validator=null_field_validator)
 
virtual uint64_t last_insert_id () noexcept
 
virtual unsigned warning_count () noexcept
 
virtual std::string quote (const std::string &s, char qchar='\'') const
 
virtual bool is_connected () noexcept
 
const std::string & get_address () noexcept
 
virtual const char * last_error ()
 
virtual unsigned int last_errno ()
 
virtual const char * ssl_cipher ()
 

Static Public Member Functions

static mysql_ssl_mode parse_ssl_mode (std::string ssl_mode)
 
static const char * ssl_mode_to_string (mysql_ssl_mode ssl_mode) noexcept
 

Static Public Attributes

static constexpr int kDefaultConnectTimeout = 5
 
static constexpr int kDefaultReadTimeout = 30
 
static const char kSslModeDisabled [] = "DISABLED"
 
static const char kSslModePreferred [] = "PREFERRED"
 
static const char kSslModeRequired [] = "REQUIRED"
 
static const char kSslModeVerifyCa [] = "VERIFY_CA"
 
static const char kSslModeVerifyIdentity []
 

Protected Attributes

std::unique_ptr< LoggingStrategylogging_strategy_
 

Static Protected Attributes

static const std::function< void(unsigned, MYSQL_FIELD *)> null_field_validator = [](unsigned, MYSQL_FIELD *) {}
 

Private Types

using mysql_result_type = std::unique_ptr< MYSQL_RES, MYSQL_RES_Deleter >
 

Private Member Functions

virtual MYSQLraw_mysql () noexcept
 
stdx::expected< mysql_result_type, MysqlErrorreal_query (const std::string &q)
 run query. More...
 
stdx::expected< mysql_result_type, MysqlErrorlogged_real_query (const std::string &q)
 log query before running it. More...
 

Private Attributes

ConnectionParameters conn_params_
 
MYSQLconnection_
 
bool connected_
 
std::string connection_address_
 
SQLLogFilter log_filter_
 

Member Typedef Documentation

◆ BindAddress

◆ BooleanOption

◆ CanHandleExpiredPasswords

◆ CharsetDir

◆ CharsetName

◆ Compress

◆ CompressionAlgorithms

◆ ConnectAttributeDelete

◆ ConnectAttributeReset

◆ ConnectTimeout

◆ ConstCharOption

template<mysql_option Opt>
using mysqlrouter::MySQLSession::ConstCharOption = impl::Option<Opt, const char *>

◆ DefaultAuthentication

◆ EnableCleartextPlugin

◆ FieldValidator

typedef std::function<void(unsigned, MYSQL_FIELD *)> mysqlrouter::MySQLSession::FieldValidator

◆ GetServerPublicKey

◆ InitCommand

◆ IntegerOption

template<mysql_option Opt>
using mysqlrouter::MySQLSession::IntegerOption = impl::Option<Opt, unsigned int>

◆ LoadDataLocalDir

◆ LocalInfile

◆ LongOption

template<mysql_option Opt>
using mysqlrouter::MySQLSession::LongOption = impl::Option<Opt, unsigned long>

◆ MaxAllowedPacket

◆ mysql_result_type

◆ NamedPipe

◆ NetBufferLength

◆ OptionalResultsetMetadata

◆ PluginDir

◆ Protocol

◆ ReadDefaultFile

◆ ReadDefaultGroup

◆ ReadTimeout

◆ Reconnect

◆ ReportDataTruncation

◆ RetryCount

◆ Row

typedef std::vector<const char *> mysqlrouter::MySQLSession::Row

◆ RowProcessor

typedef std::function<bool(const Row &)> mysqlrouter::MySQLSession::RowProcessor

◆ ServerPluginKey

◆ SharedMemoryBasename

◆ SslCa

◆ SslCaPath

◆ SslCert

◆ SslCipher

◆ SslCrl

◆ SslCrlPath

◆ SslFipsMode

◆ SslKey

◆ SslMode

◆ TlsCipherSuites

◆ TlsVersion

◆ WriteTimeout

◆ ZstdCompressionLevel

Constructor & Destructor Documentation

◆ MySQLSession()

MySQLSession::MySQLSession ( std::unique_ptr< LoggingStrategy logging_strategy = std::make_unique<LoggingStrategyNone>())

◆ ~MySQLSession()

MySQLSession::~MySQLSession ( )
virtual

Member Function Documentation

◆ connect()

void MySQLSession::connect ( const std::string &  host,
unsigned int  port,
const std::string &  username,
const std::string &  password,
const std::string &  unix_socket,
const std::string &  default_schema,
int  connect_timeout = kDefaultConnectTimeout,
int  read_timeout = kDefaultReadTimeout 
)
virtual

◆ connect_and_set_opts()

void MySQLSession::connect_and_set_opts ( const ConnectionParameters conn_params)
virtual

This is an alternative way to initialise a new connection.

It calls connect() and several other methods under the hood. Along with its counterpart get_connection_parameters(), it's useful for spawning new connections using an existing connection as a template.

Parameters
conn_paramsConnection parameters
See also
get_connection_parameters()

◆ disconnect()

void MySQLSession::disconnect ( )
virtual

◆ execute()

void MySQLSession::execute ( const std::string &  query)
virtual

◆ get_address()

const std::string & mysqlrouter::MySQLSession::get_address ( )
inlinenoexcept

◆ get_connection_parameters()

virtual ConnectionParameters mysqlrouter::MySQLSession::get_connection_parameters ( )
inlinevirtual

Returns connection parameters which could be used as a template for spawning new connections.

See also
connect_and_set_opts()
Returns
parameters used to create current connection

◆ get_option()

template<class GettableMysqlOption >
bool mysqlrouter::MySQLSession::get_option ( GettableMysqlOption &  opt)
inline

get a mysql option.

if (res) {
std::cerr << opt_connect_timeout.value() << std::endl;
}
IntegerOption< MYSQL_OPT_CONNECT_TIMEOUT > ConnectTimeout
Definition: mysql_session.h:230
bool get_option(GettableMysqlOption &opt)
get a mysql option.
Definition: mysql_session.h:402
static ulong opt_connect_timeout
Definition: mysql.cc:200
Parameters
[in,out]optoption to query.
Return values
trueon success.
falseif option is not known.

◆ is_connected()

virtual bool mysqlrouter::MySQLSession::is_connected ( )
inlinevirtualnoexcept

◆ last_errno()

unsigned int MySQLSession::last_errno ( )
virtual

◆ last_error()

const char * MySQLSession::last_error ( )
virtual

◆ last_insert_id()

uint64_t MySQLSession::last_insert_id ( )
virtualnoexcept

◆ logged_real_query()

stdx::expected< MySQLSession::mysql_result_type, MysqlError > MySQLSession::logged_real_query ( const std::string &  q)
private

log query before running it.

◆ parse_ssl_mode()

mysql_ssl_mode MySQLSession::parse_ssl_mode ( std::string  ssl_mode)
static

◆ query()

void MySQLSession::query ( const std::string &  query,
const RowProcessor processor,
const FieldValidator validator = null_field_validator 
)
virtual

◆ query_one()

std::unique_ptr< MySQLSession::ResultRow > MySQLSession::query_one ( const std::string &  query,
const FieldValidator validator = null_field_validator 
)
virtual

◆ quote()

std::string MySQLSession::quote ( const std::string &  s,
char  qchar = '\'' 
) const
virtual

◆ raw_mysql()

virtual MYSQL * mysqlrouter::MySQLSession::raw_mysql ( )
inlineprivatevirtualnoexcept

◆ real_query()

stdx::expected< MySQLSession::mysql_result_type, MysqlError > MySQLSession::real_query ( const std::string &  q)
private

run query.

There are 3 cases:

  1. query returns a resultset
  2. query returns no resultset
  1. query fails with an error
Parameters
qstmt to execute
Returns
resultset on sucess, MysqlError on error

◆ set_option()

template<class SettableMysqlOption >
stdx::expected< void, MysqlError > mysqlrouter::MySQLSession::set_option ( const SettableMysqlOption &  opt)
inline

set a mysql option.

auto res = set_option(ConnectTimeout(10));
stdx::expected< void, MysqlError > set_option(const SettableMysqlOption &opt)
set a mysql option.
Definition: mysql_session.h:376
Note
on error the MysqlError may not always contain the right error-code.
Parameters
[in]optoption to set.
Returns
a MysqlError on error
Return values
trueon success

◆ set_ssl_cert()

void MySQLSession::set_ssl_cert ( const std::string &  cert,
const std::string &  key 
)
virtual

◆ set_ssl_options()

void MySQLSession::set_ssl_options ( mysql_ssl_mode  ssl_mode,
const std::string &  tls_version,
const std::string &  ssl_cipher,
const std::string &  ca,
const std::string &  capath,
const std::string &  crl,
const std::string &  crlpath 
)
virtual

◆ ssl_cipher()

const char * MySQLSession::ssl_cipher ( )
virtual

◆ ssl_mode_to_string()

const char * MySQLSession::ssl_mode_to_string ( mysql_ssl_mode  ssl_mode)
staticnoexcept

◆ warning_count()

unsigned MySQLSession::warning_count ( )
virtualnoexcept

Member Data Documentation

◆ conn_params_

ConnectionParameters mysqlrouter::MySQLSession::conn_params_
private

◆ connected_

bool mysqlrouter::MySQLSession::connected_
private

◆ connection_

MYSQL* mysqlrouter::MySQLSession::connection_
private

◆ connection_address_

std::string mysqlrouter::MySQLSession::connection_address_
private

◆ kDefaultConnectTimeout

constexpr int mysqlrouter::MySQLSession::kDefaultConnectTimeout = 5
staticconstexpr

◆ kDefaultReadTimeout

constexpr int mysqlrouter::MySQLSession::kDefaultReadTimeout = 30
staticconstexpr

◆ kSslModeDisabled

const char MySQLSession::kSslModeDisabled = "DISABLED"
static

◆ kSslModePreferred

const char MySQLSession::kSslModePreferred = "PREFERRED"
static

◆ kSslModeRequired

const char MySQLSession::kSslModeRequired = "REQUIRED"
static

◆ kSslModeVerifyCa

const char MySQLSession::kSslModeVerifyCa = "VERIFY_CA"
static

◆ kSslModeVerifyIdentity

const char MySQLSession::kSslModeVerifyIdentity
static
Initial value:
=
"VERIFY_IDENTITY"

◆ log_filter_

SQLLogFilter mysqlrouter::MySQLSession::log_filter_
private

◆ logging_strategy_

std::unique_ptr<LoggingStrategy> mysqlrouter::MySQLSession::logging_strategy_
protected

◆ null_field_validator

const std::function< void(unsigned, MYSQL_FIELD *)> MySQLSession::null_field_validator = [](unsigned, MYSQL_FIELD *) {}
staticprotected

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