~ubuntu-branches/ubuntu/quantal/mysql-workbench/quantal

« back to all changes in this revision

Viewing changes to ext/cppconn/examples/resultset_binary.cpp

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2012-03-01 21:57:30 UTC
  • Revision ID: package-import@ubuntu.com-20120301215730-o7y8av8y38n162ro
Tags: upstream-5.2.38+dfsg
ImportĀ upstreamĀ versionĀ 5.2.38+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
 
3
 
 
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>.
 
10
 
 
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.
 
14
 
 
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
 
18
for more details.
 
19
 
 
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
 
23
*/
 
24
 
 
25
 
 
26
 
 
27
/* *
 
28
* Handling binary data using getString()
 
29
*
 
30
*/
 
31
 
 
32
/* Standard C++ includes */
 
33
#include <stdlib.h>
 
34
#include <iostream>
 
35
#include <sstream>
 
36
#include <stdexcept>
 
37
 
 
38
/* Public interface of the MySQL Connector/C++ */
 
39
#include <driver/mysql_public_iface.h>
 
40
/* Connection parameter and sample data */
 
41
#include "examples.h"
 
42
 
 
43
using namespace std;
 
44
 
 
45
int main(int argc, const char **argv)
 
46
{
 
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);
 
51
 
 
52
        /* Driver Manager */
 
53
        sql::Driver *driver;
 
54
 
 
55
        /* sql::ResultSet.rowsCount() returns size_t */
 
56
        stringstream sql;
 
57
        int i;
 
58
        struct _test_data min, max;
 
59
 
 
60
        cout << boolalpha;
 
61
        cout << "1..1" << endl;
 
62
        cout << "# Connector/C++ result set.." << endl;
 
63
 
 
64
        try {
 
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));
 
68
 
 
69
                con->setSchema(database);
 
70
 
 
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;
 
76
 
 
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) {
 
82
                                min = test_data[i];
 
83
                        }
 
84
                        if (test_data[i].id > max.id) {
 
85
                                max = test_data[i];
 
86
                        }
 
87
 
 
88
                        /*
 
89
                        KLUDGE: You should take measures against SQL injections!
 
90
                        */
 
91
                        sql.str("");
 
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());
 
96
                }
 
97
                cout << "#\t Test table populated" << endl;
 
98
 
 
99
                cout << "#\t Testing sql::Statement based resultset" << endl;
 
100
                {
 
101
                        std::auto_ptr< sql::ResultSet > res(stmt->executeQuery("SELECT * FROM test ORDER BY id ASC"));
 
102
                        int row = 0;
 
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;
 
110
 
 
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.");
 
114
                                }
 
115
 
 
116
                                if (res->getString("col_binary").compare(0, 1, "a")) {
 
117
                                        throw runtime_error("First sign from BINARY(n) seems wrong");
 
118
                                }
 
119
 
 
120
                                if (res->getString("col_binary").compare(2, 1, "b")) {
 
121
                                        throw runtime_error("Third sign from BINARY(n) seems wrong");
 
122
                                }
 
123
 
 
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.");
 
126
                                }
 
127
                                sql::ResultSetMetaData * meta = res->getMetaData();
 
128
                                cout << "#\t\t COLUMN_SIZE = " << meta->getColumnDisplaySize(3) << endl;
 
129
                                row++;
 
130
                        }
 
131
                }
 
132
 
 
133
                /* Clean up */
 
134
                /* stmt->execute("DROP TABLE IF EXISTS test");*/
 
135
                cout << "# done!" << endl;
 
136
 
 
137
        } catch (sql::SQLException &e) {
 
138
                /*
 
139
                The MySQL Connector/C++ throws three different exceptions:
 
140
 
 
141
                - sql::MethodNotImplementedException (derived from sql::SQLException)
 
142
                - sql::InvalidArgumentException (derived from sql::SQLException)
 
143
                - sql::SQLException (derived from std::runtime_error)
 
144
                */
 
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;
 
152
 
 
153
                return EXIT_FAILURE;
 
154
        } catch (std::runtime_error &e) {
 
155
 
 
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;
 
160
 
 
161
                return EXIT_FAILURE;
 
162
        }
 
163
 
 
164
        cout << "ok 1 - examples/resultset_binary.cpp" << endl;
 
165
        return EXIT_SUCCESS;
 
166
}