MySQL 8.3.0
Source Code Documentation
mysqlrouter::MySQLSession Class Reference

#include <mysql_session.h>

Classes

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)
 
mysql_ssl_mode ssl_mode () const
 
std::string tls_version () const
 
std::string ssl_cipher () const
 
std::string ssl_ca () const
 
std::string ssl_capath () const
 
std::string ssl_crl () const
 
std::string ssl_crlpath () const
 
std::string ssl_cert () const
 
std::string ssl_key () const
 
int connect_timeout () const
 
int read_timeout () const
 
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) const
 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 (const MySQLSession &other, const std::string &username, const std::string &password)
 Connect using the same settings and parameters that were used for the last other.connect() using provided credentials. More...
 
virtual void execute (const std::string &query)
 
virtual void query (const std::string &query, const RowProcessor &processor, const FieldValidator &validator)
 
virtual std::unique_ptr< MySQLSession::ResultRowquery_one (const std::string &query, const FieldValidator &validator)
 
void query (const std::string &stmt, const RowProcessor &processor)
 
std::unique_ptr< MySQLSession::ResultRowquery_one (const std::string &stmt)
 
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 ()
 
virtual bool is_ssl_session_reused ()
 
virtual unsigned long server_version ()
 

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_
 

Private Types

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

Private Member Functions

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

struct {
   std::string   host
 
   unsigned int   port {}
 
   std::string   unix_socket
 
   std::string   default_schema
 
connect_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() [1/2]

void MySQLSession::connect ( const MySQLSession other,
const std::string &  username,
const std::string &  password 
)
virtual

Connect using the same settings and parameters that were used for the last other.connect() using provided credentials.

◆ connect() [2/2]

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

int MySQLSession::connect_timeout ( ) const

◆ 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_option()

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

get a mysql option.

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

◆ is_connected()

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

◆ is_ssl_session_reused()

bool MySQLSession::is_ssl_session_reused ( )
virtual

◆ 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() [1/2]

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

◆ query() [2/2]

void mysqlrouter::MySQLSession::query ( const std::string &  stmt,
const RowProcessor processor 
)
inline

◆ query_one() [1/2]

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

◆ query_one() [2/2]

std::unique_ptr< MySQLSession::ResultRow > mysqlrouter::MySQLSession::query_one ( const std::string &  stmt)
inline

◆ quote()

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

◆ read_timeout()

int MySQLSession::read_timeout ( ) const

◆ 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 success, MysqlError on error

◆ server_version()

unsigned long MySQLSession::server_version ( )
virtual

◆ 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:367
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_ca()

std::string MySQLSession::ssl_ca ( ) const

◆ ssl_capath()

std::string MySQLSession::ssl_capath ( ) const

◆ ssl_cert()

std::string MySQLSession::ssl_cert ( ) const

◆ ssl_cipher() [1/2]

const char * MySQLSession::ssl_cipher ( )
virtual

◆ ssl_cipher() [2/2]

std::string MySQLSession::ssl_cipher ( ) const

◆ ssl_crl()

std::string MySQLSession::ssl_crl ( ) const

◆ ssl_crlpath()

std::string MySQLSession::ssl_crlpath ( ) const

◆ ssl_key()

std::string MySQLSession::ssl_key ( ) const

◆ ssl_mode()

mysql_ssl_mode MySQLSession::ssl_mode ( ) const

◆ ssl_mode_to_string()

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

◆ tls_version()

std::string MySQLSession::tls_version ( ) const

◆ warning_count()

unsigned MySQLSession::warning_count ( )
virtualnoexcept

Member Data Documentation

◆ 

struct { ... } mysqlrouter::MySQLSession::connect_params_

◆ connected_

bool mysqlrouter::MySQLSession::connected_
private

◆ connection_

MYSQL* mysqlrouter::MySQLSession::connection_
private

◆ connection_address_

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

◆ default_schema

std::string mysqlrouter::MySQLSession::default_schema

◆ host

std::string mysqlrouter::MySQLSession::host

◆ 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

◆ port

unsigned int mysqlrouter::MySQLSession::port {}

◆ unix_socket

std::string mysqlrouter::MySQLSession::unix_socket

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