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

« back to all changes in this revision

Viewing changes to ext/cppconn/examples/connection_meta_schemaobj.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 connect and simple queries
 
29
*
 
30
*/
 
31
 
 
32
 
 
33
/* Standard C++ includes */
 
34
#include <stdlib.h>
 
35
#include <iostream>
 
36
#include <iomanip>
 
37
#include <sstream>
 
38
#include <stdexcept>
 
39
#include <list>
 
40
 
 
41
/* Public interface of the MySQL Connector/C++ */
 
42
#include <driver/mysql_public_iface.h>
 
43
/* Connection parameter and sample data */
 
44
#include "examples.h"
 
45
 
 
46
using namespace std;
 
47
 
 
48
/**
 
49
* Usage example for Driver, Connection, (simple) Statement, ResultSet
 
50
*/
 
51
int main(int argc, const char **argv)
 
52
{
 
53
 
 
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);
 
58
 
 
59
        static std::list< sql::SQLString > table_types;
 
60
        table_types.push_back("TABLE");
 
61
 
 
62
        unsigned int column, row;
 
63
        string ddl;
 
64
 
 
65
        cout << boolalpha;
 
66
        cout << "1..1" << endl;
 
67
        cout << "# Connector/C++ connection meta data example.." << endl;
 
68
        cout << "#" << endl;
 
69
 
 
70
 
 
71
        try {
 
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;
 
77
 
 
78
                std::auto_ptr< sql::Connection > con(driver->connect(url, user, pass));
 
79
                sql::DatabaseMetaData * con_meta = con->getMetaData();
 
80
 
 
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");
 
84
                }
 
85
                cout << "# CDBC (API) minor version = " << con_meta->getCDBCMinorVersion() << endl;
 
86
 
 
87
                {
 
88
                        /*
 
89
                        Which schemata/databases exist?
 
90
                        NOTE Connector C++ defines catalog = n/a, schema = MySQL database
 
91
                        */
 
92
                        cout << "# List of available schemata/databases: ";
 
93
                        std::auto_ptr< sql::ResultSet > res(con_meta->getSchemas());
 
94
 
 
95
                        /* just for fun... of course you can scroll and fetch in whatever order you want */
 
96
                        res->afterLast();
 
97
                        while (res->previous()) {
 
98
                                cout << res->getString("TABLE_SCHEM");
 
99
                                if (!res->isFirst()) {
 
100
                                        cout << ", ";
 
101
                                }
 
102
                        }
 
103
                        cout << endl;
 
104
                        if (!res->isBeforeFirst() || res->isFirst())
 
105
                                throw runtime_error("Cursor should be positioned before first row");
 
106
                }
 
107
 
 
108
                {
 
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()) {
 
115
                                        cout << ", ";
 
116
                                }
 
117
                        }
 
118
                        cout << endl;
 
119
                        if (!res->isAfterLast() || res->isLast())
 
120
                                throw runtime_error("Cursor should be positioned after last row");
 
121
                }
 
122
 
 
123
 
 
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))");
 
130
 
 
131
                /* "" = empty string requests all types of objects */
 
132
                std::auto_ptr< sql::ResultSet > res(con_meta->getSchemaObjects(con->getCatalog(), con->getSchema(), ""));
 
133
                row = 1;
 
134
                sql::ResultSetMetaData * res_meta = res->getMetaData();
 
135
                while (res->next()) {
 
136
                        if (row++ > 2)
 
137
                                break;
 
138
 
 
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");
 
142
                        }
 
143
                        if (res->getString("OBJECT_TYPE").length() == 0) {
 
144
                                throw runtime_error("OBJECT_TYPE must not be empty");
 
145
                        }
 
146
 
 
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");
 
151
                        }
 
152
 
 
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");
 
157
                        }
 
158
 
 
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");
 
163
                        }
 
164
 
 
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");
 
171
                        }
 
172
 
 
173
                }
 
174
                cout << "#" << endl;
 
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 */
 
179
                res->previous();
 
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;
 
185
                cout << "#" << endl;
 
186
 
 
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()) {
 
192
                                cout << ", ";
 
193
                        }
 
194
                }
 
195
                cout << ")" << endl;
 
196
 
 
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");
 
203
                }
 
204
 
 
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;
 
219
 
 
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();
 
222
 
 
223
                cout << "#" << endl;
 
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()) {
 
229
                        cout << "#\t ";
 
230
                        for (column = 1; column < res_meta_tables->getColumnCount(); column++) {
 
231
                                cout << "'" << res_tables->getString(column) << "' ";
 
232
                        }
 
233
                        cout <<  endl;
 
234
                }
 
235
                cout << "#" << endl;
 
236
 
 
237
                res_tables->first();
 
238
                std::auto_ptr< sql::ResultSet > res_columns(con_meta->getColumns(con->getCatalog(), database, "test1", "%"));
 
239
 
 
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!");
 
244
 
 
245
                while (res_columns->next()) {
 
246
                        cout << "#\t Column name = '" << res_columns->getString("COLUMN_NAME") << "'" << endl;
 
247
                }
 
248
                cout << "#" << endl;
 
249
 
 
250
                cout << "# done!" << endl;
 
251
                cout << "ok 1 - examples/connection_meta_schemaobj.cpp" << endl;
 
252
 
 
253
        } catch (sql::SQLException &e) {
 
254
                /*
 
255
                The MySQL Connector/C++ throws three different exceptions:
 
256
 
 
257
                - sql::MethodNotImplementedException (derived from sql::SQLException)
 
258
                - sql::InvalidArgumentException (derived from sql::SQLException)
 
259
                - sql::SQLException (derived from std::runtime_error)
 
260
                */
 
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;
 
268
 
 
269
                return EXIT_FAILURE;
 
270
        } catch (std::runtime_error &e) {
 
271
 
 
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;
 
276
 
 
277
                return EXIT_FAILURE;
 
278
        }
 
279
 
 
280
        return EXIT_SUCCESS;
 
281
}