MySQL Guide

SQLAPI++ allows to work with a number of SQL database servers. It provides common mechanisms to access database, and as a general rule they work for any database server. 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.

This page collects all specific information that concerns working with MySQL server using SQLAPI++ Library. Full information about using SQLAPI++ see in How To and Documentation.

Available topics:

Connecting to a database

To connect to a database you should create a connection object and then connect it. A connection object is represented by SAConnection class. After the connection is created you need to connect it to MySQL server using SAConnection::Connect method:

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

One of the following formats:

  • "" 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> in its turn can have the following formats:

  • hostname[,port]
  • pathname of the Unix socket that is used to connect to the server
sUserIDA string containing a user name to use when establishing the connection.
sPasswordA string containing a password to use when establishing the connection.
eSAClientOptional. 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.

The SQLAPI++ Library requires MySQL C API version 3.23.x or higher.

For more details see How To - 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 server in the following way:

SA_ReadUncommitted = 'READ UNCOMMITTED'
SA_ReadCommitted = 'READ COMMITTED'
SA_RepeatableRead = 'REPEATABLE READ'
SA_Serializable = 'SERIALIZABLE'

For more details see SAConnection::setIsolationLevel.

Working with Long or Lob (CLob, BLob) data

SQLAPI++ supports four types for working with Long or Lob (CLob, BLob) data:

NameC++ Enum
LongBinarySA_dtLongBinary
LongCharSA_dtLongChar
BLob (Binary Large object)SA_dtBLob
CLob (Character Large object)SA_dtCLob

The table below shows how SQLAPI++ data types correspond with servers original data types:

SA_dtLongBinary <=> BLOB
SA_dtLongChar <=> TEXT
SA_dtBLob => BLOB
SA_dtCLob => TEXT

For more details see How To - Working with Long or Lob (CLob, BLob) data.

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 more details see SACommand::Execute, SAParam object, How To - Get output parameters.

Cancelling queries

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

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

For modern MySQL versions SQLAPI++ executes 'KILL QUERY ...' command for older ones it calls mysql_kill function to cancel a query, and then calls mysql_ping function to restore connection with server. Make attention that usually 'KILL QUERY ...' or mysql_kill should be executed with different connection that have permittions to cancel the related 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 more details see SACommand::Cancel.

Connection, command, parameter and field options

A server specific option can relate to a connection, command, parameter or field. We recommend you specify each option in an appropriate object, although it is possible to specify them in the parental object as well. In that case the option affects all the child objects.

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

A connection related option may be specified in either SAPI object or SAConnection object. If it is specified in SAPI object it affects all the connections on that API.

A command related option may be specified in SAPI object, SAConnection object or SACommand object. If it is specified in a parent object it affects all the commands on that SAPI, SAConnection objects.

A parameter related option may be specified in SAPI object, SAConnection object, SACommand object or SAParam object. If it is specified in a parent object it affects all the parameters on that SAPI, SAConnection, SACommand objects.

A field related option may be specified in SAPI object, SAConnection object, SACommand object or SAField object. If it is specified in a parent object it affects all the fields on that on that SAPI, SAConnection, SACommand objects.

Specific options for MySQL:

Option name / ScopeDescription

MYSQL.LIBS

API related. Should be specified before the API is initialized.

Forces SQLAPI++ Library to use specified MySQL client library.

Valid values: Any valid MySQL client 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 value: "STATIC", forces using the linked MySQL client API functions when the Library compiled with SA_STATIC_MYSQL build option.

SkipServerInit

API related. Should be specified before the API is initialized.

Forces SQLAPI++ Library do not call mysql_server_init API function when mysqlclient library loaded. So it should be called additionaly but with 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 related. Should be specified before the connection is made.

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 related. Should be specified before the connection is made.

Allows to set the client connection character set.

Valid values: Any MySQL allowed character set name

Default value: not specified

UseMySQLKillQueryConnection

Connection related. Should be specified before the connection is made.

Orders to create an additional connection that is used with 'KILL QUERY ..' command when SACommand::Cancel method executed.

Valid values: "true" or "false"

Default value: "false"

MYSQL_OPT_CONNECT_TIMEOUT
MYSQL_OPT_READ_TIMEOUT
MYSQL_OPT_WRITE_TIMEOUT

Connection related. Should be specified before the connection is made.

Allows to specify timeout values those are used with mysql_options. See MySQL documentation for more information how these options affect underlying connection behaviour.

Valid values: numeric string that represents an integer value

Default value: not specified

MYSQL_OPT_RECONNECT
MYSQL_SECURE_AUTH

Connection related. Should be specified before the connection is made.

Allows to specify reconnect option that will be used with mysql_options. See MySQL documentation for more information how this option affect underlying connection behaviour.

Valid values: "true" to specify a flag, "false"

Default value: not specified

MYSQL_SSL_KEY
MYSQL_SSL_CERT
MYSQL_SSL_CA
MYSQL_SSL_CAPATH
MYSQL_SSL_CIPHER

Connection related. Should be specified before the connection is made.

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 related. Should be specified before describing parameters or command execution.

Determines whether the result set will be stored in a temporary table or local bufer 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 (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 related. Should be specified before describing parameters or command execution.

Determines whether MySQL statement API should be used (if supported by MySQL client library). Make attention that statement API cannot be used with multi-statement, multi-result queries and have another restrictions.

Valid values: "true" or "false"

Default value: "false"

PreFetchRows

Command related. Should be specified before command execution.

Forces SQLAPI++ Library to fetch rows in bulk, rather than retrieving records one by one. Actial only if the MySQL statement API used.

Valid values: String containing number of rows in the fetch buffer

Default value: "1"

UseDynamicCursor or
Scrollable

Command related. Should be specified before describing parameters or command execution.

Forces SQLAPI++ to store MySQL result set that allows to scroll the data rows.

Valid values: "True", "1"

Default value: "False"

For more details see SAConnection::setOption, SACommand::setOption, SAField::setOption, SAParam::setOption.

Using native MySQL 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 API.

Cast the result to class myAPI:

#include "myAPI.h"
 
IsaAPI *pResult = con.NativeAPI();
myAPI *p_myAPI = (myAPI *)pResult;

To get more information about DBMS API functions see this DBMS specific documentation.

For more details see SAConnection::NativeAPI.

Getting native MySQL connection related handles

You have to use native API handles when you want to call specific MySQL API functions which are not directly supported by the Library. API functions usually need to receive one or more active handles as a parameter(s). 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. The following example shows what type cast you have to make and what additional header file you have to include to work with specific MySQL API.

Cast the result to class myConnectionHandles:

#include "myAPI.h"
 
saConnectionHandles *pResult = con.NativeHandles();
myConnectionHandles *p_myCH = (myConnectionHandles*)pResult;

To get more information about DBMS API functions and handles see this DBMS specific documentation.

For more details see SAConnection::NativeHandles.

Getting native MySQL command related handles

You have to use native API handles when you want to call specific MySQL API functions which are not directly supported by the Library. API functions usually need to receive one or more active handles as a parameter(s). 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. The following example shows what type cast you have to make and what additional header file you have to include to work with specific MySQL API.

Cast the result to class myCommandHandles:

#include "myAPI.h"
 
saCommandHandles *pResult = cmd.NativeHandles();
myCommandHandles *p_myCH = (myCommandHandles*)pResult;

To get more information about DBMS API functions and handles see this DBMS specific documentation.

For more details see SACommand::NativeHandles.

Error handling

When an error occurs inside SQLAPI++ Library it throws an exception of type SAException.

SAException::ErrPos method gets an error position in SQL statement. In MySQL server SAException::ErrPos method returns -1 because MySQL does not support this function.

For more details see How To - Error handling, SAException object.

Special header files - Compile time

The header files are in the include subdirectory of SQLAPI++ distributions:

  • #include "SQLAPI.h" - main header, should be used whenever SQLAPI++ is used
  • #include "myAPI.h" - MySQL API, should be included if direct MySQL C API calls are required

For more details see Documentation - Compiling and Linking Applications with SQLAPI++