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

« back to all changes in this revision

Viewing changes to ext/cppconn/examples/resultset_types.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
* Basic example demonstrating how to check the type of a result set column
 
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
#ifdef _WIN32
 
44
#define L64(x) x##i64
 
45
#else
 
46
#define L64(x) x##LL
 
47
#endif
 
48
 
 
49
using namespace std;
 
50
 
 
51
int main(int argc, const char **argv)
 
52
{
 
53
        const string url(argc >= 2 ? argv[1] : EXAMPLE_HOST);
 
54
        const string user(argc >= 3 ? argv[2] : EXAMPLE_USER);
 
55
        const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS);
 
56
        const string database(argc >= 5 ? argv[4] : EXAMPLE_DB);
 
57
 
 
58
        /* Driver Manager */
 
59
        sql::Driver *driver;
 
60
 
 
61
        struct _test_data min, max;
 
62
        int c_int1;
 
63
        bool c_bool1 = true, c_bool2;
 
64
        /* TODO: long long is not C++, its C99 !!! */
 
65
        int64_t c_long1 = L64(9223372036854775807), c_long2;
 
66
        double c_double1 = -999.9999, c_double2;
 
67
 
 
68
        cout << boolalpha;
 
69
        cout << "1..1" << endl;
 
70
        cout << "# Connector/C++ result set.." << endl;
 
71
 
 
72
        try {
 
73
                /* Using the Driver to create a connection */
 
74
                driver = sql::mysql::get_driver_instance();
 
75
                std::auto_ptr< sql::Connection > con(driver->connect(url, user, pass));
 
76
                con->setSchema(database);
 
77
 
 
78
                std::auto_ptr< sql::Statement > stmt(con->createStatement());
 
79
                stmt->execute("DROP TABLE IF EXISTS test");
 
80
 
 
81
                /*
 
82
                Note that MySQL has its very own mapping from SQL type (e.g. BOOLEAN)
 
83
                specified in a SQL statement and type actually used. Check the MySQL
 
84
                manual - conversions are a common cause of false bug reports!
 
85
                Also, don't get confused by the precision of float/double columns.
 
86
                For precision math use DECIMAL!
 
87
                */
 
88
                stmt->execute("CREATE TABLE test(id INT, label CHAR(1), c_bool BOOLEAN, "
 
89
                        "c_long BIGINT, c_double DOUBLE, c_null INT DEFAULT NULL)");
 
90
                cout << "#\t Test table created" << endl;
 
91
 
 
92
                std::auto_ptr< sql::PreparedStatement> prep_stmt(
 
93
                        con->prepareStatement("INSERT INTO test(id, label, c_bool, c_long, "
 
94
                                " c_double) VALUES (?, ?, ?, ?, ?)"));
 
95
 
 
96
                /* Populate the test table with data */
 
97
                min = max = test_data[0];
 
98
                for (unsigned int i = 0; i < EXAMPLE_NUM_TEST_ROWS; i++) {
 
99
                        /* Remember min/max for further testing */
 
100
                        if (test_data[i].id < min.id) {
 
101
                                min = test_data[i];
 
102
                        }
 
103
                        if (test_data[i].id > max.id) {
 
104
                                max = test_data[i];
 
105
                        }
 
106
 
 
107
                        prep_stmt->setInt(1, test_data[i].id);
 
108
                        prep_stmt->setString(2, test_data[i].label);
 
109
                        prep_stmt->setBoolean(3, c_bool1);
 
110
                        prep_stmt->setInt64(4, c_long1);
 
111
                        prep_stmt->setDouble(5, c_double1);
 
112
                        prep_stmt->execute();
 
113
                }
 
114
                cout << "#\t Test table populated" << endl;
 
115
 
 
116
                std::auto_ptr< sql::ResultSet > res(stmt->executeQuery("SELECT id, label, c_bool, c_long, c_double, c_null FROM test ORDER BY id ASC"));
 
117
                while (res->next()) {
 
118
                        /* sql::ResultSet.rowsCount() returns size_t */
 
119
                        size_t row = res->getRow() - 1;
 
120
 
 
121
                        cout << "#\t\t Row " << res->getRow() << endl;
 
122
                        cout << "#\t\t\t id INT = " << res->getInt("id") << endl;
 
123
                        cout << "#\t\t\t id (as Integer) = " << res->getInt("id") << endl;
 
124
                        cout << "#\t\t\t id (as String) = " << res->getString("id") << endl;
 
125
                        cout << "#\t\t\t id (as Boolean) = " << res->getBoolean("id") << endl;
 
126
                        cout << "#\t\t\t id (as Long) = " << res->getInt64("id") << endl;
 
127
                        cout << "#\t\t\t id (as Double) = " << res->getDouble("id") << endl;
 
128
                        cout << "#" << endl;
 
129
                        if (test_data[row].id != res->getInt(1)) {
 
130
                                throw runtime_error("Wrong results for column id");
 
131
                        }
 
132
 
 
133
                        std::string c_string = res->getString(2);
 
134
                        cout << "#\t\t\t label CHAR(1) = " << c_string << endl;
 
135
                        cout << "#\t\t\t label (as Integer) = " << res->getInt(2) << endl;
 
136
                        cout << "#\t\t\t label (as String) = " << res->getString(2) << endl;
 
137
                        cout << "#\t\t\t label (as Boolean) = " << res->getBoolean(2) << endl;
 
138
                        cout << "#\t\t\t label (as Long) = " << res->getInt64(2) << endl;
 
139
                        cout << "#\t\t\t label (as Double) = " << res->getDouble(2) << endl;
 
140
                        cout << "#" << endl;
 
141
                        if (test_data[row].label != c_string) {
 
142
                                throw runtime_error("Wrong result for column label");
 
143
                        }
 
144
 
 
145
                        c_bool2 = res->getBoolean("c_bool");
 
146
                        cout << "#\t\t\t c_bool CHAR(1) = " << c_bool2 << endl;
 
147
                        cout << "#\t\t\t c_bool (as Integer) = " << res->getInt(3) << endl;
 
148
                        cout << "#\t\t\t c_bool (as String) = " << res->getString(3) << endl;
 
149
                        cout << "#\t\t\t c_bool (as Boolean) = " << res->getBoolean(3) << endl;
 
150
                        cout << "#\t\t\t c_bool (as Long) = " << res->getInt64(3) << endl;
 
151
                        cout << "#\t\t\t c_bool (as Double) = " << res->getDouble(3) << endl;
 
152
                        cout << "#" << endl;
 
153
                        if (c_bool1 != c_bool2) {
 
154
                                throw runtime_error("Wrong result for column c_bool");
 
155
                        }
 
156
 
 
157
                        c_long2 = res->getInt64("c_long");
 
158
                        cout << "#\t\t\t c_long BIGINT = " << c_long2 << endl;
 
159
                        cout << "#\t\t\t c_long (as Integer) = " << res->getInt("c_long") << endl;
 
160
                        cout << "#\t\t\t c_long (as String) = " << res->getString("c_long") << endl;
 
161
                        cout << "#\t\t\t c_long (as Boolean) = " << res->getBoolean("c_long") << endl;
 
162
                        cout << "#\t\t\t c_long (as Long) = " << res->getInt64("c_long") << endl;
 
163
                        cout << "#\t\t\t c_long (as Double) = " << res->getDouble("c_long") << endl;
 
164
                        cout << "#" << endl;
 
165
                        if (c_long1 != c_long2) {
 
166
                                throw runtime_error("Wrong result for column c_long");
 
167
                        }
 
168
 
 
169
                        c_double2 = res->getDouble("c_double");
 
170
                        cout << "#\t\t\t c_double DOUBLE = " << c_double2 << endl;
 
171
                        cout << "#\t\t\t c_double (as Integer) = " << res->getInt("c_double") << endl;
 
172
                        cout << "#\t\t\t c_double (as String) = " << res->getString("c_double") << endl;
 
173
                        cout << "#\t\t\t c_double (as Boolean) = " << res->getBoolean("c_double") << endl;
 
174
                        cout << "#\t\t\t c_double (as Long) = " << res->getInt64("c_double") << endl;
 
175
                        cout << "#\t\t\t c_double (as Double) = " << res->getDouble("c_double") << endl;
 
176
                        cout << "#\t\t\t c_double wasNull() = " << res->wasNull() << endl;
 
177
                        cout << "#" << endl;
 
178
                        if (c_double1 != c_double2) {
 
179
                                throw runtime_error("Wrong result for column c_double");
 
180
                        }
 
181
 
 
182
                        c_int1 = res->getInt("c_null");
 
183
                        cout << "#\t\t\t c_null INT DEFAULT NULL = " << c_int1;
 
184
                        cout << " (isNull = " << res->isNull("c_null") << ")" << endl;
 
185
                        cout << "#\t\t\t c_null (as Integer) = " << res->getInt("c_null") << endl;
 
186
                        cout << "#\t\t\t c_null (as String) = " << res->getString("c_null") << endl;
 
187
                        cout << "#\t\t\t c_null (as Boolean) = " << res->getBoolean("c_null") << endl;
 
188
                        cout << "#\t\t\t c_null (as Long) = " << res->getInt64("c_null") << endl;
 
189
                        cout << "#\t\t\t c_null (as Double) = " << res->getDouble("c_null") << endl;
 
190
                        cout << "#\t\t\t c_null wasNull() = " << res->wasNull() << endl;
 
191
                        cout << "#" << endl;
 
192
                        if (!res->isNull(6) || !res->wasNull()) {
 
193
                                throw runtime_error("isNull() or wasNull() has not reported NULL value of column c_null");
 
194
                        }
 
195
 
 
196
                }
 
197
 
 
198
                /* Clean up */
 
199
                stmt->execute("DROP TABLE IF EXISTS test");
 
200
 
 
201
                cout << "# done!" << endl;
 
202
 
 
203
        } catch (sql::SQLException &e) {
 
204
                /*
 
205
                The MySQL Connector/C++ throws three different exceptions:
 
206
 
 
207
                - sql::MethodNotImplementedException (derived from sql::SQLException)
 
208
                - sql::InvalidArgumentException (derived from sql::SQLException)
 
209
                - sql::SQLException (derived from std::runtime_error)
 
210
                */
 
211
                cout << "# ERR: SQLException in " << __FILE__;
 
212
                cout << "(" << EXAMPLE_FUNCTION << ") on line " << __LINE__ << endl;
 
213
                /* Use what(), getErrorCode() and getSQLState() */
 
214
                cout << "# ERR: " << e.what();
 
215
                cout << " (MySQL error code: " << e.getErrorCode();
 
216
                cout << ", SQLState: " << e.getSQLState() << " )" << endl;
 
217
                cout << "not ok 1 - examples/resultset_types.cpp" << endl;
 
218
 
 
219
                return EXIT_FAILURE;
 
220
        } catch (std::runtime_error &e) {
 
221
 
 
222
                cout << "# ERR: runtime_error in " << __FILE__;
 
223
                cout << "(" << EXAMPLE_FUNCTION << ") on line " << __LINE__ << endl;
 
224
                cout << "# ERR: " << e.what() << endl;
 
225
                cout << "not ok 1 - examples/resultset_types.cpp" << endl;
 
226
 
 
227
                return EXIT_FAILURE;
 
228
        }
 
229
 
 
230
        cout << "ok 1 - examples/resultset_types.cpp" << endl;
 
231
        return EXIT_SUCCESS;
 
232
}