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
* Handling binary data using getString()
32
/* Standard C++ includes */
38
/* Public interface of the MySQL Connector/C++ */
39
#include <driver/mysql_public_iface.h>
40
/* Connection parameter and sample data */
45
int main(int argc, const char **argv)
47
static const string url(argc >= 2 ? argv[1] : EXAMPLE_HOST);
48
static const string user(argc >= 3 ? argv[2] : EXAMPLE_USER);
49
static const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS);
50
static const string database(argc >= 5 ? argv[4] : EXAMPLE_DB);
55
/* sql::ResultSet.rowsCount() returns size_t */
58
struct _test_data min, max;
61
cout << "1..1" << endl;
62
cout << "# Connector/C++ result set.." << endl;
65
/* Using the Driver to create a connection */
66
driver = sql::mysql::get_driver_instance();
67
std::auto_ptr< sql::Connection > con(driver->connect(url, user, pass));
69
con->setSchema(database);
71
/* Creating a "simple" statement - "simple" = not a prepared statement */
72
std::auto_ptr< sql::Statement > stmt(con->createStatement());
73
stmt->execute("DROP TABLE IF EXISTS test");
74
stmt->execute("CREATE TABLE test(id INT, label CHAR(1), col_binary BINARY(4), col_varbinary VARBINARY(10))");
75
cout << "#\t Test table created" << endl;
77
/* Populate the test table with data */
78
min = max = test_data[0];
79
for (i = 0; i < EXAMPLE_NUM_TEST_ROWS; i++) {
80
/* Remember mnin/max values for further testing */
81
if (test_data[i].id < min.id) {
84
if (test_data[i].id > max.id) {
89
KLUDGE: You should take measures against SQL injections!
92
sql << "INSERT INTO test(id, label, col_binary, col_varbinary) VALUES (";
93
sql << test_data[i].id << ", '" << test_data[i].label << "', ";
94
sql << "\"a\\0b\", '" << i * 5 << "\\0abc')";
95
stmt->execute(sql.str());
97
cout << "#\t Test table populated" << endl;
99
cout << "#\t Testing sql::Statement based resultset" << endl;
101
std::auto_ptr< sql::ResultSet > res(stmt->executeQuery("SELECT * FROM test ORDER BY id ASC"));
103
while (res->next()) {
104
cout << "#\t\t Row " << row << ", getRow() " << res->getRow();
105
cout << " id = " << res->getInt("id");
106
cout << ", label = '" << res->getString("label") << "'";
107
/* cout might "hide" \0 and other special characters from the output */
108
cout << ", col_binary = '" << res->getString("col_binary") << "'";
109
cout << ", col_varbinary = '" << res->getString("col_varbinary") << "'" << endl;
111
/* fixed length column - length = size of the column! */
112
if (res->getString("col_binary").length() != 4) {
113
throw runtime_error("BINARY(n) should return std::string of length n regardless how long the value stored in the column is.");
116
if (res->getString("col_binary").compare(0, 1, "a")) {
117
throw runtime_error("First sign from BINARY(n) seems wrong");
120
if (res->getString("col_binary").compare(2, 1, "b")) {
121
throw runtime_error("Third sign from BINARY(n) seems wrong");
124
if (res->getString("col_varbinary").length() != 5 && res->getString("col_varbinary").length() != 6) {
125
throw runtime_error("VARBINARY(n) should return std::string of length n which holds the length of the actual column value.");
127
sql::ResultSetMetaData * meta = res->getMetaData();
128
cout << "#\t\t COLUMN_SIZE = " << meta->getColumnDisplaySize(3) << endl;
134
/* stmt->execute("DROP TABLE IF EXISTS test");*/
135
cout << "# done!" << endl;
137
} catch (sql::SQLException &e) {
139
The MySQL Connector/C++ throws three different exceptions:
141
- sql::MethodNotImplementedException (derived from sql::SQLException)
142
- sql::InvalidArgumentException (derived from sql::SQLException)
143
- sql::SQLException (derived from std::runtime_error)
145
cout << "# ERR: SQLException in " << __FILE__;
146
cout << "(" << EXAMPLE_FUNCTION << ") on line " << __LINE__ << endl;
147
// Use what(), getErrorCode() and getSQLState()
148
cout << "# ERR: " << e.what();
149
cout << " (MySQL error code: " << e.getErrorCode();
150
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
151
cout << "not ok 1 - examples/resultset_binary.cpp" << endl;
154
} catch (std::runtime_error &e) {
156
cout << "# ERR: runtime_error in " << __FILE__;
157
cout << "(" << EXAMPLE_FUNCTION << ") on line " << __LINE__ << endl;
158
cout << "# ERR: " << e.what() << endl;
159
cout << "not ok 1 - examples/resultset_binary.cpp" << endl;
164
cout << "ok 1 - examples/resultset_binary.cpp" << endl;