MySQL / MariaDB Guide

SQLAPI++ allows to seamlessly work with a variety of SQL database servers. It provides unified API to access any database, keeping your code portable. But each server has some specific features which a developer has to know in order to leverage server's unique features and avoid potential errors.

For complete information on using SQLAPI++ check out Getting Started and Documentation. This guide covers specific information related to working with MySQL / MariaDB server using SQLAPI++ library in the following areas:

Connecting to a database

To connect to a database you need to initialize a connection object. A connection object is represented by SAConnection class.

Minimum Version
SQLAPI++ library requires MySQL C API version 3.23.x or higher.

After the connection is created you need to call SAConnection::Connect method to establish connection with MySQL server:

void Connect(
    const SAString &sDBString,
    const SAString &sUserID, 
    const SAString &sPassword, 
    SAClient_t eSAClient = SA_Client_NotSpecified);
Parameters

sDBString

Connection string in the following format:

  • "" or "@" - empty string or '@' character, connects to a local server
  • <database_name> or @<database_name> - connects to a database with the specified name on local server
  • <serve_name>@ - connects to the specified server
  • <server_name>@<database_name> - connects to a database with the specified name on the specified server

<server_name> can have the following formats:

  • hostname[,port]
  • pathname of the Unix socket that is used to connect to the server

sUserID

A string containing a user name to use when establishing the connection.

sPassword

A string containing a password to use when establishing the connection.

eSAClient

Optional. One of the following values from SAClient_t enum:
  • SA_MySQL_Client MySQL client
  • SA_Client_NotSpecified – used by default if eSAClient parameter is omitted. You can use this default value only if you have SAConnection::setAPI method with SAPI object initialized with SA_MySQL_Client constant before

For more details see Getting Started - Connect to Database, SAConnection object, SAConnection::Connect.

Transaction isolation levels

SQL-92 defines four isolation levels, all of which are supported by SQLAPI++:

  • Read uncommitted (the lowest level where transactions are isolated just enough to ensure that physically corrupt data is not read)
  • Read committed
  • Repeatable read
  • Serializable (the highest level, where transactions are completely isolated from one another)

SQLAPI++ maps different isolation levels on MySQL / MariaDB in the following way:

SA_ReadUncommittedSQL_TXN_READ_UNCOMMITTED
SA_ReadCommittedSQL_TXN_READ_COMMITTED
SA_RepeatableReadSQL_TXN_REPEATABLE_READ
SA_SerializableSQL_TXN_SERIALIZABLE

In addition to the SQL-92 levels, if you specify 'snapshot' isolation level, it will be mapped as: SA_Snapshot SQL_TXN_SERIALIZABLE.

For more details see SAConnection::setIsolationLevel.

Working with Long or Lob (CLob, BLob) data

When fetching data SQLAPI++ detects data types of the columns in the result set and maps those types to internal library types. The mapping determines which native APIs the library will use for fetching LOB data.

The table below shows how SQLAPI++ maps MySQL server data types to Long/Lob library types:

BLOBSA_dtLongBinary
TEXTSA_dtLongChar

When binding input data from your program the reverse mapping is taking place. The SQLAPI++ data type you use for input markers determines what native API program types will be used for sending Long/Lob data to the server.

The table below shows how SQLAPI++ maps its internal library types to MySQL / MariaDB API data types:
SA_dtLongBinaryMYSQL_TYPE_BLOB
SA_dtLongCharMYSQL_TYPE_STRING
SA_dtBLobMYSQL_TYPE_BLOB
SA_dtCLobMYSQL_TYPE_STRING

For additional information see Getting Started - Handle Long/CLob/BLob.

Returning output parameters

MySQL stored procedures can have output parameters. SQLAPI++ gets the output parameters from the server after all result sets from stored procedure (if any) are completely processed using SACommand::FetchNext method.

For additional information see SACommand::Execute, SAParam object, Getting Started - Get Output Parameters.

Cancelling queries

Using SACommand::Cancel method you can cancel the following types of processing on a statement:

  • function running asynchronously on the statement
  • function running on the statement on another thread

For modern MySQL versions SQLAPI++ executes 'KILL QUERY ...' command; for older versions it calls mysql_kill function to cancel a query, and then calls mysql_ping function to restore connection with server.

Be aware that usually 'KILL QUERY ...' or mysql_kill should be executed on a different connection that have permissions to cancel other commands. This additional connection can be automatically created if "UseMySQLKillQueryConnection" option is set to "true".

To get more details see 'KILL QUERY ...', **mysql_kill** and **mysql_ping** functions description in **MySQL** documentation.

For additional information see SACommand::Cancel.

Connection, command, parameter and field options

Server specific options can be applied at the API, connection, command, parameter or field levels.

We recommend you specify each option at the appropriate level, although it is possible to specify them at the parent object level as well. In that case the option affects all the child objects.

API level options must be specified in SAPI object. If an internal SAPI object is used for the DBMS API initialization (implicit DBMS API initialization, see SAConnection::Connect method) the related DBMS specific options are taken from the initial connection object.

Connection level options may be specified in either SAPI object or SAConnection object. If specified in SAPI object an option affects all connections on that API.

Command level options may be specified in SAPI object, SAConnection object or SACommand object. If specified in a parent object an option affects all commands on that SAPI or SAConnection object.

Parameter level options may be specified in SAPI object, SAConnection object, SACommand object or SAParam object. If specified in a parent object an option affects all parameters on that SAPI, SAConnection or SACommand object.

Field related options may be specified in SAPI object, SAConnection object, SACommand object or SAField object. If specified in a parent object an option affects all fields on that SAPI , SAConnection or SACommand object.

Specific options applicable to MySQL / MariaDB:

MYSQL.LIBS
Api Scope
Forces SQLAPI++ Library to use specified MySQL client library.
Valid values: Any valid MySQL CLI library name list. Names separated by ';' on Windows or ':' on other operating systems.
Default value:
  • Windows - "libmySQL.dll;libmariadb.dll"
  • Linux - "libmysqlclient.so:libmysqlclient.so.15:libmysqlclient.so.16:libmysqlclient.so.18:libmariadb.so"
Special values: "STATIC", forces using the linked MySQL client API functions when the library is compiled with SA_STATIC_MYSQL build option.
SkipServerInit
Api Scope
Forces SQLAPI++ Library to not call mysql_server_init() API function when MySQL client library is loaded. It must then be called explicitly with desired user specific options. Useful for embedded MySQL library.
Valid values: "True", "1"
Default value: "False"
CLIENT_COMPRESS
CLIENT_FOUND_ROWS
CLIENT_IGNORE_SPACE
CLIENT_INTERACTIVE
CLIENT_LOCAL_FILES
CLIENT_NO_SCHEMA
CLIENT_ODBC
CLIENT_MULTI_STATEMENTS
CLIENT_MULTI_RESULTS
CLIENT_REMEMBER_OPTIONS
Connection Scope
Allows to specify various options that will be passed on to mysql_real_connect(). See MySQL documentation for more information how these options affect underlying connection behaviour.
Valid values: "true" to specify a flag, "false" (or not specified) to omit
Default value: not specified, but CLIENT_MULTI_STATEMENTS, CLIENT_MULTI_RESULTS and CLIENT_REMEMBER_OPTIONS are turned on
CharacterSet
Connection Scope
Allows to set the client connection character set.
Valid values: Any MySQL allowed character set name
Default value: not specified
UseMySQLKillQueryConnection
Connection Scope
Instructs SQLAPI++ to create an additional connection that is used for 'KILL QUERY ..' command when SACommand::Cancel method executed.
Valid values: "true", "false"
Default value: "false"
MYSQL_OPT_CONNECT_TIMEOUT
MYSQL_OPT_READ_TIMEOUT
MYSQL_OPT_WRITE_TIMEOUT
Connection Scope
Allows to specify timeout values that are used with mysql_options. See MySQL documentation for more information how these options affect underlying connection behaviour.
Valid values: string that represents an integer value
Default value: not specified
MYSQL_OPT_RECONNECT
Connection Scope
Allows to specify reconnect option that will be used with mysql_options. See MySQL documentation for more information on how this option affects underlying connection behaviour.
Valid values: "true" to specify a flag, "false" to omit
Default value: not specified
MYSQL_SSL_KEY
MYSQL_SSL_CERT
MYSQL_SSL_CA
MYSQL_SSL_CAPATH
MYSQL_SSL_CIPHER
Connection Scope
Allows to specify MySQL SSL parameters that will be used with mysql_ssl_set. MySQL API method called only when at least one parameter specified. See MySQL documentation for more information about these options.
Default value: not specified
HandleResult
Command Scope
Determines whether the result set will be stored in a temporary table or a local buffer on a client (it allows to open several result sets simultaneously in one transaction), or it will be read row by row directly from the server (somewhat faster and uses much less memory). For more information see MySQL documentation on mysql_store_result() and mysql_use_result() functions.
Valid values:
  • "use" to force SQLAPI++ to call mysql_use_result() function
  • "store" to force SQLAPI++ to call mysql_store_result() function
Default value: "use"
UseStatement
Command Scope
Determines whether MySQL statement API should be used (if supported by MySQL client library). Be aware that statement API cannot be used with multi-statement, multi-result queries and has some additional restrictions.
Valid values: "true", "false"
Default value: "false"
PreFetchRows
Command Scope
Forces SQLAPI++ library to fetch rows in bulk, rather than retrieving records one by one. Only has an effect if MySQL statement API is used (see UseStatement option).
Valid values: String containing number of rows in the fetch buffer
Default value: "1"
UseDynamicCursor
Scrollable
Command Scope
Forces SQLAPI++ to store MySQL result set that allows scrolling through the data rows.
Valid values: "True", "1"
Default value: "False"

For additional information see SAOptions::setOption.

Using native MySQL / MariaDB API

You can call client specific API functions which are not directly supported by SQLAPI++ library. SAConnection::NativeAPI method returns a pointer to the set of native API functions available for MySQL. To use the database API directly you have to downcast this IsaAPI pointer to the appropriate type and use its implementation-specific members. The following example shows what type cast you have to make and what additional header file you have to include to work with MySQL / MariaDB API. Note that using appropriate type casting depends on an API (that generally mean that you have to explicitly check client version before casting, see SAConnection::ClientVersion method).

To use native API you need to add MySQL / MariaDB specific #include and cast the result of SAConnection::NativeAPI to class myAPI:

#include "myAPI.h"

IsaAPI *pApi = con.NativeAPI();
myAPI *pNativeAPI = (myAPI *)pApi;

To get more information about MySQL API functions see MySQL / MariaDB documentation.

For additional information see SAConnection::NativeAPI.

Getting native MySQL / MariaDB connection related handles

You have to use native API handles when you want to call specific MySQL / MariaDB API functions which are not directly supported by the library. API functions usually need to receive one or more active handles as parameters. SAConnection::NativeHandles method returns a pointer to the set of native API connection related handles. To use API handles directly you have to downcast saConnectionHandles pointer to the appropriate type and use its implementation-specific members.

To access native connection handles you need to add MySQL / MariaDB specific #include and cast the result to class myConnectionHandles:

#include "myAPI.h"

saConnectionHandles *pHandles = con.NativeHandles();
myConnectionHandles *pNativeHandles = (myConnectionHandles*)pHandles;

To get more information about MySQL API functions and handles see MySQL / MariaDB specific documentation.

For additional information see SAConnection::NativeHandles.

Getting native MySQL / MariaDB command related handles

You have to use native API handles when you want to call specific MySQL / MariaDB API functions which are not directly supported by the library. API functions usually need to receive one or more active handles as parameters. SACommand::NativeHandles method returns a pointer to the set of native API command related handles. To use API handles directly you have to downcast saCommandHandles pointer to the appropriate type and use its implementation-specific members.

To access native command handles you need to add MySQL / MariaDB specific #include and cast the result to class myCommandHandles:

#include "myAPI.h"

saCommandHandles *pHandles = cmd.NativeHandles();
myCommandHandles *pNativeHandles = (myCommandHandles*)pHandles;

To get more information about MySQL API functions and handles see MySQL / MariaDB specific documentation.

For additional information see SACommand::NativeHandles.

Error handling

When an error occurs when executing a SQL statement SQLAPI++ library throws an exception of type SAException and SAException::ErrPos method returns error position in the SQL statement.

In MySQL server SAException::ErrPos method returns -1 because MySQL does not support this function.

For additional information see Getting Started - Error Handling, SAException object.