Let'S Explore Mysql Connector/C++: Giri Mandalika
Let'S Explore Mysql Connector/C++: Giri Mandalika
Let'S Explore Mysql Connector/C++: Giri Mandalika
MySQL Connector/C++
Giri Mandalika
Software Engineer
Sun Microsystems, Inc.
1
Agenda
• What is MySQL Connector/C++?
• High Level Architecture
• Installation, Dependencies
• Implemented Classes
• Transactions
• Stored Procedures
• Exceptions
• Debug Tracing
2
MySQL Connector/C++
• C++ API and database driver for MySQL connectivity
• Wrapper around C-API
• Follows JDBC 4.0
• Advantages
> Convenience of pure C++ -- no C function calls
> JDBC 4.0 like API
> Easy to learn & use
• Supported Platforms
> Solaris, Linux, Windows, Mac, FreeBSD, HP-UX, AIX
3
High Level Architecture
Client Application
C++ API
MySQL Connector/C++
C API
MySQL Client Library
Client/Server
Protocol
MySQL Server DB
4
Installation
• Pre-built binary
• Build from source
> Build-time dependencies
– CMake
2.6.2 on Windows
2.4.2 on rest of the platforms
5
Building & Installing Connector/C++
Example:
export CC=cc
export CXX=CC
export CFLAGS=-xO4
export CXXFLAGS=-xO4
cmake -DCMAKE_INSTALL_PREFIX=/path/to/connector/c++ \
-DCMAKE_BUILD_TYPE=Debug \
-DMYSQL_CONFIG_EXECUTABLE=/path/to/mysql_config \
-DMYSQLCPPCONN_TRACE_ENABLE:BOOL=1
make
make install
6
Dependencies, Compilers, Tools
• Runtime Dependencies
> MySQL Client Programming Support
> MySQL Connector/C++ driver
• Compilers
> GCC, Sun Studio, Visual Studio
• IDEs
> NetBeans, Visual Studio
7
Available Classes
Driver
Connection
ResultSet ParameterMetaData
ResultSetMetaData
8
Establishing a Database Connection
Example:
#include <mysql_driver.h>
#include <cppconn/connection.h>
using namespace sql;
Driver *driver;
Connection *con;
try {
driver = mysql::get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306/test",
"dummy", "dummy");
} catch (..) {}
9
Connect String (Connection URL) Syntax
TCP/IP: tcp://[hostname[:port]][/schema]
UNIX Domain Socket: unix://path/to/unix_socket_file
Named Pipes: pipe://path/to/the/pipe
10
Executing Queries using Statement Object
Example:
Connection *con;
Statement *stmt;
ResultSet *rs;
int updCnt;
bool retval;
stmt = con -> createStatement();
rs = stmt -> executeQuery ("SELECT .. ");
updCnt = stmt -> executeUpdate ("INSERT INTO .. ");
retval = stmt -> execute ("SELECT .. ");
if (retval) {
rs = stmt -> getResultSet();
} else {
updCnt = stmt -> getUpdateCount();
}
11
Prepared Statements
Example:
12
Result Sets
Example:
Statement *stmt;
ResultSet *rs;
13
Buffered Vs Unbuffered Result Sets
Example:
Statement *stmt;
ResultSet *rs;
/* Buffered Results */
rs = stmt->setResultSetType(ResultSetType::TYPE_SCROLL_INSENSITIVE)
-> executeQuery(..);
/* Unbuffered Results */
rs = stmt ->setResultSetType(ResultSetType::TYPE_FORWARD_ONLY)
-> executeQuery(..);
14
Result Set Meta Data
Example:
PreparedStatement *prep_stmt;
ResultSet *rs;
ResultSetMetaData *rs_meta, *ps_rs_meta;
15
Parameter Meta Data
Example:
PreparedStatement *prep_stmt;
ParameterMetaData *param_meta;
string Qry ("INSERT INTO City (CityName) VALUES (?)");
16
Database Meta Data
Example:
Connection *dbcon;
DatabaseMetaData *dbcon_meta = dbcon -> getMetaData();
17
Transactions
Example:
Connection *con;
PreparedStatement *prep_stmt;
string Qry ("INSERT INTO City (CityName) VALUES (?)");
18
Fine-Grain Transaction Control : Savepoint(s)
Example:
Connection *con;
PreparedStatement *prep_stmt;
Savepoint *savept;
string Qry ("INSERT INTO City (CityName) VALUES (?)");
MySQL Client:
mysql> delimiter //
mysql> CREATE PROCEDURE addCity (IN cityname varchar(30))
-> BEGIN
-> INSERT INTO City VALUES (cityname);
-> END;
-> //
Query OK, 0 rows affected (0.00 sec)
Connector/C++ Client:
statement *stmt;
stmt -> execute ("CALL addCity('Tokyo, Japan')");
20
Exceptions, Warnings
Example:
Statement *stmt;
try {
..
throw SQLException ("Dummy Error ..");
..
SQLWarning *warn = stmt -> getWarnings();
if (warn != NULL) {
cout << "Warning: " << warn -> getMessage();
}
} catch (MethodNotImplementedException &e) { .. }
catch (InvalidArgumentException &e) { .. }
catch (SQLException &e) {
cout << "MySQL err: " << e.getErrorCode() << ", State: " << e.getSQLState()
<< "Error: " << e.what();
} catch (std::runtime_error &e) {.. }
21
Debug Traces using MySQL Client Library
Example:
Connection *con;
Statement *stmt;
ResultSet *rs;
22
Sample Debug Trace
<cli_read_query_result
>mysql_real_query
| enter: handle: 0x808a228
| query: Query = 'SELECT * FROM City'
| >mysql_send_query
| | enter: rpl_parse: 0 rpl_pivot: 1
| <mysql_send_query
| >cli_advanced_command
| | >net_write_command
| | | enter: length: 37
| | <net_write_command
| | >net_flush
| | | <vio_is_blocking
| | | >net_real_write
| | | | >vio_write
| | | | | enter: sd: 4 buf: 0x808fa38 size: 42
...
Voluntary context switches 25, Involuntary context switches 5
>TERMINATE
| safe: sf_malloc_count: 4
| safe: Memory that was not free'ed (16420 bytes):
| safe: Maximum memory usage: 65569 bytes (65k) 23
Debug Traces using Driver's Tracing Module
# MYSQLCPPCONN_TRACE_ENABLED=1 ./connectorc++client
24
Miscellaneous Notes
25
Connector/C++ Roadmap
26