Home How To Online Documentation Support Download Order
 

 


SACommand
SAConnection
SAException
SAField
SAParam
SAString
SADateTime
SANumeric

Enums and Typedefs


Server Specific Guide

MySQL

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 Online Documentation.

Available topics:

Connecting to a database

Transaction isolation levels

Working with Long or Lob(CLob, BLob) data

Returning output parameters

Cancelling queries

Connection, command, parameter and field options

Getting native MySQL API

Getting native MySQL connection related handles

Getting native MySQL command related handles

Error handling

Special header files - Compile time

 

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 );

with the following 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.
  • <server 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 

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 eSAClientparameter is omitted.  You can use this default value only if you have call SAConnection::setClient method 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 - Connecting to databases, 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:

Name C enum constant
LongBinary SA_dtLongBinary
LongChar SA_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, SACommand::FetchNext, SAParam object, How To - Returning 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.

SQLAPI++ calls mysql_kill function to cancel a query, and then calls mysql_ping function to restore connection with server. To get more details see 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.

A connection related option must be specified in a  SAConnection object.

A command related option may be specified in either SAConnection object or SACommand object. If it is specified in SAConnection object it affects all the commands on that connection.

A parameter related option may be specified in SAConnection object, SACommand object or SAParam object. If it is specified in SAConnection object it affects all the commands and therefore all the parameters on that connection. If it is specified in SACommand object it affects all the parameters on that command.

A field related option may be specified in SAConnection object, SACommand object or SAField object. If it is specified in SAConnection object it affects all the commands and therefore all the fields on that connection. If it is specified in SACommand object it affects all the fields on that command.

Specific options for MySQL:

Option name / Scope 

Description

MYSQL.LIBS

Connection related. Should be specified before the first connection is made.
Forces SQLAPI++ Library to use specified MySQL client library.

Valid values : Any valid MySQL library name list. Names separated by ';' on Windows or ':' on other OS.
Default value: Windows - "libmySQL.dll", Linux -  "libmysqlclient.so:libmysqlclient.so.15:libmysqlclient.so.16".

SkipServerInit

Connection related. Should be specified before the first connection is made.
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 connection attempted.
Allows to set the client connection character set.

Default value : not specified.

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 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 command opened.
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".

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.

 

Getting 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 saAPI pointer to the appropriate type and use its implementation-specific members. The following table shows what type cast you have to make and what additional header file you have to include to work with MySQL API.

Type casting

Additional
include file

Cast the result to class myAPI:

saAPI *pResult = con.NativeAPI();
myAPI *p_myAPI = (myAPI *)pResult;

#include <myAPI.h>

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 table shows what type cast you have to make and what additional header file you have to include to work with specific MySQL API.

Type casting

Cast the result to class myConnectionHandles:

#include <myAPI.h>

saConnectionHandles *pResult = con.NativeHandles();
myConnectionHandles *p_myCH =
                                (myConnectionHandles *)pResult;

Available handles:

  • MYSQL *mysql;

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 table shows what type cast you have to make and what additional header file you have to include to work with specific MySQL API.

Type casting

Cast the result to class myCommandHandles:

#include <myAPI.h>

saCommandHandles *pResult = cmd.NativeHandles();
myCommandHandles *p_myCH =
                                (myCommandHandles *)pResult;

Available handles:

  • MYSQL_RES *result;

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 , should be included if direct MySQL C API calls are required.

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

 

Problems and Questions

If you haven't found the answer to you questions or have some problems on using the Library, please, send e-mail to howto@sqlapi.com.