2
Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
4
The MySQL Connector/C++ is licensed under the terms of the GPLv2
5
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
6
MySQL Connectors. There are special exceptions to the terms and
7
conditions of the GPLv2 as it is applied to this software, see the
8
FLOSS License Exception
9
<http://www.mysql.com/about/legal/licensing/foss-exception.html>.
11
This program is free software; you can redistribute it and/or modify
12
it under the terms of the GNU General Public License as published
13
by the Free Software Foundation; version 2 of the License.
15
This program is distributed in the hope that it will be useful, but
16
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20
You should have received a copy of the GNU General Public License along
21
with this program; if not, write to the Free Software Foundation, Inc.,
22
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28
* Basic example demonstrating connect and simple queries
33
/* Standard C++ includes */
41
/* Public interface of the MySQL Connector/C++ */
42
#include <driver/mysql_public_iface.h>
43
/* Connection parameter and sample data */
49
* Usage example for Driver, Connection, (simple) Statement, ResultSet
51
int main(int argc, const char **argv)
54
string url(argc >= 2 ? argv[1] : EXAMPLE_HOST);
55
const string user(argc >= 3 ? argv[2] : EXAMPLE_USER);
56
const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS);
57
const string database(argc >= 5 ? argv[4] : EXAMPLE_DB);
59
static std::list< sql::SQLString > table_types;
60
table_types.push_back("TABLE");
62
unsigned int column, row;
66
cout << "1..1" << endl;
67
cout << "# Connector/C++ connection meta data example.." << endl;
72
/* Using the Driver to create a connection */
73
sql::Driver * driver = sql::mysql::get_driver_instance();
74
cout << "# " << driver->getName() << ", version ";
75
cout << driver->getMajorVersion() << "." << driver->getMinorVersion();
76
cout << "." << driver->getPatchVersion() << endl;
78
std::auto_ptr< sql::Connection > con(driver->connect(url, user, pass));
79
sql::DatabaseMetaData * con_meta = con->getMetaData();
81
cout << "# CDBC (API) major version = " << con_meta->getCDBCMajorVersion() << endl;
82
if (con_meta->getCDBCMajorVersion() <= 0) {
83
throw runtime_error("API major version must not be 0");
85
cout << "# CDBC (API) minor version = " << con_meta->getCDBCMinorVersion() << endl;
89
Which schemata/databases exist?
90
NOTE Connector C++ defines catalog = n/a, schema = MySQL database
92
cout << "# List of available schemata/databases: ";
93
std::auto_ptr< sql::ResultSet > res(con_meta->getSchemas());
95
/* just for fun... of course you can scroll and fetch in whatever order you want */
97
while (res->previous()) {
98
cout << res->getString("TABLE_SCHEM");
99
if (!res->isFirst()) {
104
if (!res->isBeforeFirst() || res->isFirst())
105
throw runtime_error("Cursor should be positioned before first row");
109
/* What object types does getSchemaObjects support? */
110
cout << "# Supported Object types: ";
111
std::auto_ptr< sql::ResultSet > res(con_meta->getSchemaObjectTypes());
112
while (res->next()) {
113
cout << res->getString(1);
114
if (!res->isLast()) {
119
if (!res->isAfterLast() || res->isLast())
120
throw runtime_error("Cursor should be positioned after last row");
124
std::auto_ptr< sql::Statement > stmt(con->createStatement());
125
stmt->execute("USE " + database);
126
stmt->execute("DROP TABLE IF EXISTS test1");
127
stmt->execute("CREATE TABLE test1(id INT, label CHAR(1))");
128
stmt->execute("DROP TABLE IF EXISTS test2");
129
stmt->execute("CREATE TABLE test2(id INT, label CHAR(1))");
131
/* "" = empty string requests all types of objects */
132
std::auto_ptr< sql::ResultSet > res(con_meta->getSchemaObjects(con->getCatalog(), con->getSchema(), ""));
134
sql::ResultSetMetaData * res_meta = res->getMetaData();
135
while (res->next()) {
139
cout << "#\t Object " << res->getRow() << " (" << res->getString("OBJECT_TYPE") << ")" << endl;
140
if (res->getString(1) != res->getString("OBJECT_TYPE")) {
141
throw runtime_error("Fetch using numeric/non-numeric index failed for OBJECT_TYPE");
143
if (res->getString("OBJECT_TYPE").length() == 0) {
144
throw runtime_error("OBJECT_TYPE must not be empty");
147
cout << "#\t Catalog = " << res->getString("CATALOG");
148
cout << " (empty: " << (res->getString("CATALOG").length() == 0) << ")" << endl;
149
if (res->getString(2) != res->getString("CATALOG")) {
150
throw runtime_error("Fetch using numeric/non-numeric index failed for CATALOG");
153
cout << "#\t Schema = " << res->getString("SCHEMA");
154
cout << " (empty: " << (res->getString("SCHEMA").length() == 0) << ")" << endl;
155
if (res->getString(3) != res->getString("SCHEMA")) {
156
throw runtime_error("Fetch using numeric/non-numeric index failed for SCHEMA");
159
cout << "#\t Name = " << res->getString("NAME");
160
cout << " (empty: " << (res->getString("NAME").length() == 0) << ")" << endl;
161
if (res->getString(4) != res->getString("NAME")) {
162
throw runtime_error("Fetch using numeric/non-numeric index failed for NAME");
165
ddl = res->getString("DDL");
166
cout << "#\t DDL = " << setw(40);
167
cout << ddl.substr(0, ddl.find_first_of("\n", 1) - 1) << "..." << endl;
168
cout << "#\t DDL is empty: " << ddl.empty() << endl;
169
if (res->getString(5) != res->getString("DDL")) {
170
throw runtime_error("Fetch using numeric/non-numeric index failed for DDL");
175
cout << "#\t\t Using different getter methods at the example of the DDL column" << endl;
176
cout << "#\t\t Column DDL is of type " << res_meta->getColumnTypeName(5);
177
cout << " / Code: " << res_meta->getColumnType(5) << endl;
178
/* scroll back to last row in set */
180
cout << "#\t\t DDL (as String) = " << setw(30) << ddl.substr(0, ddl.find_first_of("\n", 1) - 1) << "..." << endl;
181
cout << "#\t\t DDL (as Boolean) = " << res->getBoolean("DDL") << "/" << res->getBoolean(5) << endl;
182
cout << "#\t\t DDL (as Double) = " << res->getDouble("DDL") << "/" << res->getDouble(5) << endl;
183
cout << "#\t\t DDL (as Int) = " << res->getInt("DDL") << "/" << res->getInt(5) << endl;
184
cout << "#\t\t DDL (as Long) = " << res->getInt64("DDL") << "/" << res->getInt64(5) << endl;
187
cout << "#\t\t Meta data on the result set at the example of the DDL column" << endl;
188
cout << "#\t\t Column count = " << res_meta->getColumnCount() << " (Columns: ";
189
for (column = 1; column <= res_meta->getColumnCount(); column++) {
190
cout << res_meta->getColumnName(column);
191
if (column < res_meta->getColumnCount()) {
197
cout << "#\t\t getCatalogName() = " << res_meta->getCatalogName(5) << endl;
198
/* cout << "#\t\t getColumnDisplaySize() = " << res_meta->getDisplaySize("DDL") << endl; */
199
cout << "#\t\t getColumnLabel() = " << res_meta->getColumnLabel(5) << endl;
200
cout << "#\t\t getColumnName() = " << res_meta->getColumnName(5) << endl;
201
if (res_meta->getColumnName(5) != res_meta->getColumnLabel(5)) {
202
throw runtime_error("column names differ for column DDL");
205
cout << "#\t\t getColumnType() = " << res_meta->getColumnType(5) << endl;
206
cout << "#\t\t getColumnTypeName() " << res_meta->getColumnTypeName(5) << endl;
207
/* cout << "#\t\t getPrecision() = " << res_meta->getPrecision(5) << endl; */
208
cout << "#\t\t getSchemaName() = " << res_meta->getSchemaName(5) << endl;
209
cout << "#\t\t getTableName() = " << res_meta->getTableName(5) << endl;
210
cout << "#\t\t isAutoIncrement() = " << res_meta->isAutoIncrement(5) << endl;
211
cout << "#\t\t isCaseSensitive() = " << res_meta->isCaseSensitive(5) << endl;
212
cout << "#\t\t isCurrency() = " << res_meta->isCurrency(5) << endl;
213
cout << "#\t\t isDefinitelyWritable() = " << res_meta->isDefinitelyWritable(5) << endl;
214
cout << "#\t\t isNullable() = " << res_meta->isNullable(5) << endl;
215
cout << "#\t\t isReadOnly() = " << res_meta->isReadOnly(5) << endl;
216
cout << "#\t\t isSearchable() = " << res_meta->isSearchable(5) << endl;
217
cout << "#\t\t isSigned() = " << res_meta->isSigned(5) << endl;
218
cout << "#\t\t isWritable() = " << res_meta->isWritable(5) << endl;
220
std::auto_ptr< sql::ResultSet > res_tables(con_meta->getTables(con->getCatalog(), database, "t%", table_types));
221
sql::ResultSetMetaData * res_meta_tables = res_tables->getMetaData();
224
cout << "# Tables with names like 't%':" << endl;;
225
cout << "#\t rowsCount() = " << res_tables->rowsCount() << endl;
226
cout << "#\t getColumnCount() = " << res_meta_tables->getColumnCount() << endl;
227
cout << "#\t" << endl;
228
while (res_tables->next()) {
230
for (column = 1; column < res_meta_tables->getColumnCount(); column++) {
231
cout << "'" << res_tables->getString(column) << "' ";
238
std::auto_ptr< sql::ResultSet > res_columns(con_meta->getColumns(con->getCatalog(), database, "test1", "%"));
240
cout << "#" << "Columns in the table 'test1'..." << endl;
241
cout << "#\t rowsCount() = " << res_columns->rowsCount() << endl;
242
if (res_columns->rowsCount() != 2)
243
throw runtime_error("Table test1 has two columns, no more!");
245
while (res_columns->next()) {
246
cout << "#\t Column name = '" << res_columns->getString("COLUMN_NAME") << "'" << endl;
250
cout << "# done!" << endl;
251
cout << "ok 1 - examples/connection_meta_schemaobj.cpp" << endl;
253
} catch (sql::SQLException &e) {
255
The MySQL Connector/C++ throws three different exceptions:
257
- sql::MethodNotImplementedException (derived from sql::SQLException)
258
- sql::InvalidArgumentException (derived from sql::SQLException)
259
- sql::SQLException (derived from std::runtime_error)
261
cout << "# ERR: SQLException in " << __FILE__;
262
cout << "(" << EXAMPLE_FUNCTION << ") on line " << __LINE__ << endl;
263
/* Use what() (derived from std::runtime_error) */
264
cout << "# ERR: " << e.what();
265
cout << " (MySQL error code: " << e.getErrorCode();
266
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
267
cout << "not ok 1 - examples/connection_meta_schemaobj.cpp" << endl;
270
} catch (std::runtime_error &e) {
272
cout << "# ERR: runtime_error in " << __FILE__;
273
cout << "(" << EXAMPLE_FUNCTION << ") on line " << __LINE__ << endl;
274
cout << "# ERR: " << e.what() << endl;
275
cout << "not ok 1 - examples/connection_meta_schemaobj.cpp" << endl;