Skip to content

Commit dfef4ba

Browse files
Bogdan DegtyariovDaniel Horecki
authored andcommitted
Updated documentation for the legacy C++ JDBC4 API
1 parent 7066122 commit dfef4ba

File tree

5 files changed

+340
-60
lines changed

5 files changed

+340
-60
lines changed

doc/building.txt

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ Prerequisites
66
To build Connector/C++, the following tools and libraries are required:
77

88
- A C++ compiler that supports C++11. In case of gcc it must be at least
9-
version 4.8. These compilers have been tested: gcc 4.8.4, clang 7.3.0,
10-
MS Visual Studio 2015.
9+
version 4.8. These compilers have been tested: gcc 4.8.4, 4.8.5, 4.9.2, 5.4.0,
10+
6.3.0, 6.3.1, 7.1.1, 7.2.0, 7.2.1, clang 9.0.0, MS Visual Studio 2015.
1111

12-
- CMake 2.8.11 or later.
12+
- CMake 2.8.12 or later.
1313

1414
- OpenSSL version 1.0.x if connector is built with OpenSSL.
1515

1616
@note: On some platforms Boost libraries are needed to build the connector.
17-
Boost version 1.55 or higher is required (for MS Visual Studio
18-
version 1.59 or higher).
17+
Boost version 1.59.
1918

2019
Build configuration
2120
-------------------
@@ -44,6 +43,27 @@ By default a shared library is built. To build a static library, use the option:
4443

4544
- `-DBUILD_STATIC=yes`
4645

46+
Building of the legacy C++ JDBC4 library
47+
----------------------------------------
48+
Connector/C++ supports the legacy C++ JDBC4 API, which is placed inside a
49+
separate library `libmysqlcppconn` or `mysqlcppconn` depending on the Operating
50+
System.
51+
52+
In order to enable building of the legacy C++ JDBC4 library the Connector/C++
53+
needs to be built with the following options:
54+
55+
- `WITH_JDBC` to enable the legacy C++ JDBC4 API. Usage of this option in cmake
56+
command looks like this: `-DWITH_JDBC=yes`. By default this option is not
57+
enabled and the legacty C++ JDBC4 functionality is not built.
58+
59+
- `MYSQL_DIR` to allow using server libraries such as libmysqlclient if it is
60+
not installed at the default location.
61+
The command line should look like: `-DMYSQL_DIR=<path_to_mysql_server_dir>`
62+
63+
- `WITH_BOOST` to point to boost location if it is different from the default.
64+
The command line should look like: `-DMYSQL_DIR=<path_to_mysql_server_dir>`
65+
66+
4767
Configuring SSL libraries
4868
-------------------------
4969
Connector/C++ can be built with different SSL libraries.
@@ -84,10 +104,18 @@ It is also possible to omit the `--config CCC` option in which case the default
84104
After a successful build, the build location should contain
85105
the Connector/C++ shared library:
86106

87-
- `libmysqlcppconn8.so.1.0` on Unix
88-
- `libmysqlcppconn8.1.0.dylib` on OS X
107+
- `libmysqlcppconn8.so.1` on Unix
108+
- `libmysqlcppconn8.1.dylib` on OS X
89109
- `CCC/mysqlcppconn8-vsXX.dll` on Windows, where `-vs12` or `-vs14` prefix
90110
depends on the MSVC version used to build the connector
111+
112+
If Connector/C++ is built to support the legacy C++ JDBC4 API (WITH_JDBC option
113+
enabled) the build library directory will have another shared library:
114+
115+
- `libmysqlcppconn.so.7` on Unix
116+
- `libmysqlcppconn.7.dylib` on OS X
117+
- `CCC/mysqlcppconn-7-vsXX.dll` on Windows, where `-vs12` or `-vs14` prefix
118+
depends on the MSVC version used to build the connector
91119

92120
On Unix and OS X also appropriate symbolic links are created. On Windows the
93121
import library for the DLL is created at `CCC/mysqlcppconn8.lib` (the `CCC/`
@@ -96,8 +124,15 @@ subdirectory is named the same as the build configuration used).
96124
In the case of a static library build, the output library names are:
97125

98126
- `libmysqlcppconn8-static.a` on Unix and OS X
99-
- `CCC/mysqlcppconn8-static.lib` on Windows
127+
- `CCC/mysqlcppconn8-static.lib` and `CCC/mysqlcppconn8-static-mt.lib`
128+
on Windows
129+
130+
If Connector/C++ is built to support the legacy C++ JDBC4 API (WITH_JDBC option
131+
enabled) the build library directory will have another static library:
100132

133+
- `libmysqlcppconn-static.a` on Unix and OS X
134+
- `CCC/mysqlcppconn-static.lib` and `CCC/mysqlcppconn-static-mt.lib`
135+
on Windows
101136

102137
### Testing
103138

@@ -130,8 +165,8 @@ $ cmake -DWITH_CONCPP=<Connector/C++ install location> <source location>/testapp
130165
$ cmake --build . --config CCC
131166
~~~~~~~
132167

133-
This should create the `devapi_test` and `xapi_test` executables in the `run/`
134-
subdirectory of the build location.
168+
This should create the `devapi_test`, `xapi_test` and `jdbc_test` executables
169+
in the `run/` subdirectory of the build location.
135170

136171
@note
137172
On Windows, if the connector was built with the static runtime by specifying
@@ -142,6 +177,9 @@ subdirectory of the build location.
142177
The 32/64-bit Windows `cmake` generator (`"Visual Studio 14 2015 Win64"` vs
143178
`"Visual Studio 14 2015"`) used to build test applications must match
144179
the one used to build the connector.
180+
181+
@note in order to build `jdbc_test` the Connector/C++ has to be built
182+
with WITH_JDBC, MYSQL_DIR and WITH_BOOST options.
145183

146184
Before running test programs, ensure that a MySQL Server instance is running
147185
with X Plugin loaded into it. The easiest way of arranging this is to use
@@ -167,24 +205,17 @@ the standard 33060 one.
167205
Now one can start one of the test programs and see the output similar to the
168206
one presented below. The `devapi_test` program accepts port number as the first
169207
argument, with the default value 33060. Thus, if the server was started
170-
as described above, run the test program as follows:
208+
as described above, run the test program with the following URL as a parameter:
171209

172210
~~~~~~~
173-
$ run/devapi_test 13009
211+
$ run/devapi_test mysqlx://[email protected]:13009
174212
~~~~~~~
175213

176214
The program uses the `root` user account without any password and assumes
177215
that there is a `test` schema in the server (these assumptions hold for
178216
a server started using `mysql-test-run.pl`). Different user credentials can
179217
be passed as the second and the third argument of `devapi_test` invocation.
180218

181-
For the `xapi_test` program, connection details can be given in form of an URL,
182-
as shown below:
183-
184-
~~~~~~~
185-
$ run/xapi_test mysqlx://[email protected]:13009
186-
~~~~~~~
187-
188219
If everything goes well, `devapi_test` should produce output similar to this:
189220

190221
~~~~~~~
@@ -222,6 +253,20 @@ doc#1: {"_id": "A0ABC08DAABAD1110C120800273BD115", "age": 2, "name": "bar", "toy
222253
Done!
223254
~~~~~~~
224255

256+
For the `xapi_test` program, connection details can be given in form of an URL,
257+
as shown below:
258+
259+
~~~~~~~
260+
$ run/xapi_test mysqlx://[email protected]:13009
261+
~~~~~~~
262+
263+
For the `jdbc_test` program the command line would look as this:
264+
265+
~~~~~~~
266+
$ run/jdbc_test tcp://127.0.0.1:13009 root
267+
~~~~~~~
268+
269+
225270
Windows Notes
226271
-------------
227272
Connector/C++ uses C++11 language and for that reason it is not possible
@@ -256,7 +301,7 @@ the compiler and the linker invocations. Another option is to set the required
256301
deployment target and then the compiler defaults are changed accordingly.
257302
To define the deployment target, set the environment variable
258303
`MACOSX_DEPLOYMENT_TARGET` to the requested OS X version. Binary distributions
259-
of Connector/C++ are built with `MACOSX_DEPLOYMENT_TARGET` set to `10.9`.
304+
of Connector/C++ are built with `MACOSX_DEPLOYMENT_TARGET` set to `10.12`.
260305

261306
Building Connector/C++ with `gcc` or its `libstdc++` runtime has not been
262307
tested and there is no support in the build system for using an alternative

doc/devapi_ref.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ See @ref usage for instructions on how to build the sample code.
4747
Code which uses X DevAPI should include the `mysql_devapi.h` header. The API
4848
is declared within the `mysqlx` namespace:
4949

50-
@skip #include <mysql_devapi.h>
50+
@skip #include <mysqlx/xdevapi.h>
5151
@until using namespace
5252

5353
To create an @link mysqlx::Session `Session` @endlink object, specify DNS name

doc/index.txt

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,34 @@ MySQL Connector/C++ Documentation {#mainpage}
33

44
MySQL Connector/C++ is a library for applications written in C or C++ that want
55
to communicate with MySQL database servers. Version 8.0 of Connector/C++
6-
implements new APIs which, on top of traditional SQL, give access
7-
to MySQL implementing a document store. For code written in C++ this is the new
8-
[X DevAPI] (@ref devapi), which is the common MySQL API to access
9-
MySQL Document Store, also implemented by other MySQL connectors. Code written
10-
in plain C can use the [XAPI] (@ref xapi) -- a new plain C API which offers
11-
functionality similar to X DevAPI.
6+
implements three different APIs which can be used by applications...
127

8+
- The new [X DevAPI] (@ref devapi) for applications written in C++.
9+
- The new [XAPI] (@ref xapi) for applications written in plain C.
10+
- The [legacy JDBC based API]
11+
(https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-reference.html),
12+
also implemented in version 1.1 of the connector.
13+
14+
The new APIs give access to MySQL implementing a [document store]
15+
(https://dev.mysql.com/doc/refman/8.0/en/document-store.html). Consequently,
16+
code written against these APIs can work only with a MySQL Server with the
17+
X Plugin enabled in it. This is supported by MySQL Server version 8 or 5.7.12+.
18+
Apart from accessing the document store, the new APIs allow executing
19+
traditional SQL queries as well.
20+
21+
Application written against the JDBC based API of Connector/C++ 1.1 can be also
22+
compiled with Connector/C++ 8.0 which is backward compatible with the earlier
23+
version. Such code does not require the X Plugin and can communicate with older
24+
version of the MySQL Server.
25+
26+
The API to be used is chosen by including appropriate set of headers, as
27+
explained in @ref usage.
28+
1329
More information:
1430

1531
- [Connector/C++ X DevAPI Reference] (@ref devapi_ref)
1632
- [Connector/C++ XAPI Reference] (@ref xapi_ref)
33+
- [Connector/C++ legacy C++ API based on JDBC4 Reference] (@ref jdbc_ref)
1734
- [How to build code that uses Connector/C++](@ref usage)
1835
- [Building Connector/C++] (@ref building)
1936
- [Indexing Document Collections] (@ref indexing)

doc/jdbc_ref.txt

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
Connector/C++ 8.0 legacy C++ API based on JDBC4 Reference {#jdbc_ref}
2+
=======================================
3+
4+
Connector/C++ 8.0 supports the legacy C++ API based on JDBC4 specification.
5+
6+
This allows an easy migration to Connector/C++ version 8.0 for applications
7+
that use Connector/C++ version 1.1.
8+
9+
More detailed information about the legacy C++ API can be found in the
10+
[X MySQL Connector/C++ Developer Guide]
11+
(https://dev.mysql.com/doc/connector-cpp/en/)
12+
of MySQL online manual.
13+
14+
Source distributions of Connector/C++ include an examples directory
15+
containing usage examples that explain how to use the following classes:
16+
17+
- `Connection`
18+
19+
- `Driver`
20+
21+
- `PreparedStatement`
22+
23+
- `ResultSet`
24+
25+
- `ResultSetMetaData`
26+
27+
- `Statement`
28+
29+
The examples cover:
30+
31+
- Using the Driver class to connect to MySQL
32+
33+
- Creating tables, inserting rows, fetching rows using (simple) statements
34+
35+
- Creating tables, inserting rows, fetching rows using prepared statements
36+
37+
- Hints for working around prepared statement limitations
38+
39+
- Accessing result set metadata
40+
41+
### Sample code which uses Connector/C++ with legacy C++ API ###
42+
43+
The following code demonstrates how to connect to MySQL Server using the
44+
legacy C++ API.
45+
46+
@note
47+
The connection is established over the legacy protocol and therefore
48+
X Plugin is not needed and the server does not need to support X Protocol.
49+
50+
After the connection is established the code executes a simple SQL statement
51+
and reads the result from the server. The source file can be found in
52+
testapp/jdbc_test.cc in the source distribution of Connector/C++ 8.0.
53+
See @ref usage for instructions on how to build the sample code.
54+
55+
Each program which is using the legacy C++ API has to do the following
56+
includes:
57+
58+
@dontinclude jdbc_test.cc
59+
@skip #include <jdbc/mysql_connection.h>
60+
@until #include <jdbc/mysql_driver.h>
61+
62+
These headers are needed for using the `ResultSet` and `Statement` classes:
63+
64+
@skip #include <jdbc/cppconn/resultset.h>
65+
@until #include <jdbc/cppconn/statement.h>
66+
67+
Define the credentials for connecting, process command line params and
68+
other housekeeping stuff:
69+
70+
@skip #define DEFAULT_URI "tcp://127.0.0.1"
71+
@until try {
72+
73+
To establish a connection to MySQL Server, retrieve an instance of
74+
`sql::Connection` from a `sql::mysql::MySQL_Driver` object.
75+
A `sql::mysql::MySQL_Driver` object is returned by
76+
`sql::mysql::get_mysql_driver_instance()`:
77+
78+
@skip sql::Driver
79+
@until con(driver->connect(url, user, pass));
80+
81+
Make sure that you free con, the `sql::Connection` object, as soon as you do
82+
not need it any more. But do not explicitly free driver, the connector object.
83+
Connector/C++ takes care of freeing that.
84+
85+
@note `get_mysql_driver_instance()` calls `get_driver_instance()`, which is
86+
not thread-safe. Either avoid invoking these methods from within multiple
87+
threads at once, or surround the calls with a mutex to prevent simultaneous
88+
execution in multiple threads.
89+
90+
These methods can be used to check the connection state or reconnect:
91+
92+
- `sql::Connection::isValid()` checks whether the connection is alive
93+
94+
- `sql::Connection::reconnect()` reconnects if the connection has gone down
95+
96+
The `sql::Connection` is used to set the schema and create a statement:
97+
98+
@skip con->setSchema(database);
99+
@until boost::scoped_ptr< sql::Statement > stmt(con->createStatement());
100+
101+
To run simple queries, you can use the `sql::Statement::execute()`,
102+
`sql::Statement::executeQuery()`, and `sql::Statement::executeUpdate()` methods.
103+
Use the method `sql::Statement::execute()` if your query does not return
104+
a result set or if your query returns more than one result set:
105+
106+
@skip boost::scoped_ptr< sql::ResultSet >
107+
@until res(stmt->executeQuery("SELECT 'Welcome to Connector/C++' AS _message"));
108+
109+
@note The Statement and ResultSet objects are temporary, but they are
110+
dynamically allocated. Therefore we use boost::scoped_ptr<> to free memory
111+
when they are not needed anymore.
112+
113+
The API for fetching result sets is identical for (simple) statements and
114+
prepared statements. If your query returns one result set, use
115+
`sql::Statement::executeQuery()` or `sql::PreparedStatement::executeQuery()`
116+
to run your query. Both methods return `sql::ResultSet` objects.
117+
By default, Connector/C++ buffers all result sets on the client to support
118+
cursors.
119+
120+
The code below walks through the entire result set row by row using
121+
`sql::ResultSet::next()` method, which returns `true` if the row was sucessfully
122+
read. Otherwise it returns `false`, which means we reached the end of the
123+
result set and there are no more rows to read.
124+
125+
The actual data is obtained trhough the getXxxx() functions such as
126+
`getInt(), getString()`, etc. The columns can be indexed by numbers in the
127+
order they are given inside the result set starting from 1.
128+
Alternatively the column name can be used as a string index.
129+
130+
@skip while
131+
@until }
132+
133+
Error handling is done through the standard try/catch blocks:
134+
135+
@skip catch
136+
@until }
137+
138+
Here is the complete C++ code of the test sample for the legacy C++ API:
139+
140+
@include jdbc_test.cc
141+
142+
<!--
143+
Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
144+
145+
This program is free software; you can redistribute it and/or modify
146+
it under the terms of the GNU General Public License, version 2.0, as
147+
published by the Free Software Foundation.
148+
149+
This program is also distributed with certain software (including
150+
but not limited to OpenSSL) that is licensed under separate terms,
151+
as designated in a particular file or component or in included license
152+
documentation. The authors of MySQL hereby grant you an
153+
additional permission to link the program and your derivative works
154+
with the separately licensed software that they have included with
155+
MySQL.
156+
157+
Without limiting anything contained in the foregoing, this file,
158+
which is part of MySQL Connector/C++, is also subject to the
159+
Universal FOSS Exception, version 1.0, a copy of which can be found at
160+
http://oss.oracle.com/licenses/universal-foss-exception.
161+
162+
This program is distributed in the hope that it will be useful, but
163+
WITHOUT ANY WARRANTY; without even the implied warranty of
164+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
165+
See the GNU General Public License, version 2.0, for more details.
166+
167+
You should have received a copy of the GNU General Public License
168+
along with this program; if not, write to the Free Software Foundation, Inc.,
169+
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
170+
-->

0 commit comments

Comments
 (0)