Post on 07-Apr-2018
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 1/23
Not so quiet onthe C++ front
Learn about the latestMySQL Connector
Andrey HristovSUN Microsystems<andrey.hristov at sun com>
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 2/23
:~$ whoami
Born in Bulgaria, living in Germany
MySQL Connectors Software Developer
Author of the Event Scheduler in MySQL 5.1
Connector/C++Connector/OO.org
PHP drivers for MySQL
Strong in and around PHP
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 3/23
Who is the Connector/C++?
Your new driver!
Driver of choice for MySQL Workbench
Heart of MySQL Connector/OO.org
Dual licensing:
GPL with FLOSS exception
OEM / commercial license
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 4/23
Architecture
Client Application
C++ API
MySQL Connector/C++
C API
MySQL Connector/C
C/S Protocol
MySQL Server
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 5/23
The API
Follows JDBC 4.0
Implements about 80% of it
No templates
No subclassing
Straightforward to learn
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 6/23
WYSISWYG
Driver
PreparedStatement
Connection
Statement DatabaseMetaData
ResultSet
ResultSetMetaData
ParameterMetaData
SQLException SQLWarning
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 7/23
Ooops, not my talk ?!
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 8/23
A 4-minute walk-through: Download
Almost all MySQL Server platformsExcept QNX & OpenSCO
Binary and source downloadsTAR.GZ, ZIPhttp://launchpad.net/mysql-connector-cpp
Windows MSI Installer
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 9/23
In 4 minutes: Installation
BinaryUnpack to a location of your choice
Sourcecmake -G “Unix Makefiles” .make && make install
Windows? Click “Next”..
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 10/23
In 4 minutes: Tooling
Linux command lineg++ -o hello examples/standalone_example.cpp
- I/usr/local/include/ -lmysqlcppconn
IDE tutorials in the manualNetBeansVisualStudio
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 11/23
In 4 minutes: Thank you for trying!
Driver* driver =sql::mysql::get_mysql_driver_instance();
Connection* conn =driver->connect(URL, USER, PASS);
Statement* stmt =conn->createStatement();
ResultSet* rset =stmt->executeQuery("SELECT 'Thank you for trying!'");
while (rset->next())cout << rset->getString(1) << std::endl;
Examples contained in the source distribution:Driver, ConnectionStatement, Prepared StatementMetadata, Exceptions, Debug, ...
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 12/23
24 seconds for the hackers
> time`VERSION=mysql-connector-1.0.5-betawget mysql.he.net/Downloads/Connector-C+/$VERSION.tar.gz;tar zxvf $VERSION.tar.gz ;cd $VERSION ;cmake . ;make ;sudo make install ;g++ -o hello examples/standalone_example.cpp -I
/usr/local/include/ -l mysqlcppconn ;./hello tcp://localhost user password schema`
Connector/C++ standalone program example...
... running 'SELECT 'Welcome to Connector/C++'
... MySQL replies: Welcome to Connector/C++
... say it again, MySQL
....MySQL replies: Welcome to Connector/C++
... find more at http://www.mysql.com
real 0m24.588s
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 13/23
Hello world – in full
#include <iostream>#include <mysql_driver.h>#include <cppconn/connection.h>
#include <cppconn/statement.h>#include <cppconn/resultset.h>
#define URL "tcp://127.0.0.1:3306/test"#define USER "root"#define PASS "root"
using sql;int main(int argc, const char **argv) {
Driver * driver = mysql::get_mysql_driver_instance();Connection * conn = driver->connect(URL, USER, PASS));Statement * stmt = conn->createStatement();
ResultSet * rset = stmt->executeQuery("SELECT 'WORLD'");while (rset->next()) {std::cout << rset->getString(1) << std::endl;
}
delete rset;delete stmt;
delete conn;}
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 14/23
Want to get connected?
Most C-API options supported
Connection properties
Unix sockets, TCP/IP, Pipe
Character set results
Default statement resultset typeSSL
Compression
Multi Statement, Multi Result
...
setClientOption(), getClientOption()
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 15/23
Connection properties
#include <map>#include <mysql_driver.h>#include <cppconn/connection.h>
using sql;
int main(int argc, const char **argv) {
Driver * driver = mysql::get_mysql_driver_instance();Connection * conn;
std::map<std::string, ConnectPropertyVal> props;std::string url("tcp://127.0.0.1:3306/test");{ConnectPropertyVal tmp;tmp.str.val = url.c_str();tmp.str.len = url.length();
connection_properties[std::string("hostName")] = tmp;}/* … more properties … */
conn = driver->connect(props);
delete conn;
}
P d S
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 16/23
Prepared Statements
[...]#include <cppconn/prepared_statement.h>[...]
using sql;int main(int argc, const char **argv) {
Driver * driver = mysql::get_mysql_driver_instance();Connection * conn = driver->connect(URL, USER, PASS));
PreparedStatement * pstmt =
conn->prepareStatement("SELECT '101' AS col");
ResultSet * rset = pstmt->executeQuery();while (rset->next()) {std::cout << rset->getString(1) << std::endl;std::cout << rset->getString("col") << std::endl;std::cout << rset->getInt("col") << std::endl;
std::cout << rset->getInt64("col") << std::endl;}
[...]}
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 17/23
ere oes er romDid you see the JDBC difference?In some places where JDBC uses int, we use uint,because it makes sense – Java has no uint.
C99 types are used : int64_t, uint64_t, size_t – for Windows we have typedefs
getLong() is known as getInt64()
getUInt() and getUInt64() are new
getDouble() returns long double
long double differs on different platforms
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 18/23
More on JDBC compliance
Connector/J returns Database as CAT(ALOG) and NULLfor SCHEMA
Connector/C++ returns “def” as CAT(ALOG) and thedatabase for SCHEMA
Less getXXX() methods in ResultSet
Date, Time, Datetime should be read with getString()
We document all differences
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 19/23
Result set buffering
Default: always fetch all results into a client buffer
Pro: Server can release resources early
Con: Client eats memory
Prepared Statements are buffered
Statement
unbuffered - TYPE_FORWARD_ONLY
buffered - TYPE_SCROLL_INSENSITIVE
stmt->setResultSetType(ResultSetType::TYPE_FORWARD_ONLY)->executeQuery(...);
int option = ResultSet::TYPE_FORWARD_ONLY;conn->setClientOption(
"defaultStatementResultType",(static_cast<void *> (&option))
);
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 20/23
Portability?
Built and tested on:
Win32 & Win64
Linux – RedHat, SuSE, Ubuntu (x86, x86_64, ia64)
MacOS 10.4, 10.5 (ppc32, ppc64, x86, x86_64)
Solaris 8, 9, 10 (sparc32, sparc64, x86, x86_64)
OpenSolaris
FreeBSD 6 & 7
HPUX 11.x
AIX 5.2/5.3 (ppc32, ppc64)i5/OS
But... is C++ portable?
Consider building it from source!
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 21/23
Supported compilers
Microsoft Visual Studio
2003, 2005, 2008
Any recent GCC on Linux
this excludes 2.95 :)
Sun Studio 12
Every other compiler we use for building binaries
Check README
Versions matter!
8/3/2019 Not So Quiet on the C++ Front
http://slidepdf.com/reader/full/not-so-quiet-on-the-c-front 22/23
ResourcesProduct pagehttp://www.mysql.com/products/connector/
Downloadshttp://dev.mysql.com/downloads/connector/cpp/1.0.html
Documentationhttp://dev.mysql.com/doc/refman/6.0/en/connector-cpp.html
Nice article on using MySQL Connector/C++http://dev.mysql.com/tech-resources/articles/mysql-connector-cpp.html
Projects wiki page on MySQL Forgehttp://forge.mysql.com/wiki/Connector_C%2B%2B
Launchpad source code repositoryhttps://launchpad.net/mysql-connector-cpp
MySQL Connector/C++ Community Forumhttp://forums.mysql.com/list.php?167
Q ti ?