File: multiquery.cpp

package info (click to toggle)
mysql%2B%2B 2.0.7-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 8,548 kB
  • ctags: 2,792
  • sloc: cpp: 35,566; sh: 8,517; xml: 2,769; perl: 752; makefile: 178
file content (218 lines) | stat: -rw-r--r-- 6,266 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/***********************************************************************
 multiquery.cpp - Example showing how to iterate over result sets upon
    execution of a query that returns more than one result set.  You can
	get multiple result sets when executing multiple separate SQL
	statments in a single query, or when dealing with the results of
	calling a stored procedure.

 Copyright (c) 1998 by Kevin Atkinson, (c) 1999, 2000 and 2001 by
 MySQL AB, (c) 2004, 2005 by Educational Technology Resources, Inc.,
 and (c) 2005 by Arnon Jalon.  Others may also hold copyrights on
 code in this file.  See the CREDITS file in the top directory of
 the distribution for details.

 This file is part of MySQL++.

 MySQL++ is free software; you can redistribute it and/or modify it
 under the terms of the GNU Lesser General Public License as published
 by the Free Software Foundation; either version 2.1 of the License, or
 (at your option) any later version.

 MySQL++ is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with MySQL++; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 USA
***********************************************************************/

#include "util.h"

#include <mysql++.h>

#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;
using namespace mysqlpp;


typedef vector<int> IntVectorType;


static void
print_header(IntVectorType& widths, Result& res)
{
	cout << "  |" << setfill(' ');
	for (size_t i = 0; i < res.names().size(); i++) {
		cout << " " << setw(widths.at(i)) << res.names(i) << " |";
	}
	cout << endl;
}


static void
print_row(IntVectorType& widths, Row& row)
{
	cout << "  |" << setfill(' ');
	for (size_t i = 0; i < row.size(); i++) {
		cout << " " << setw(widths.at(i)) << row.raw_data(i) << " |";
	}
	cout << endl;
}


static void
print_row_separator(IntVectorType& widths)
{
	cout << "  +" << setfill('-');
	for (size_t i = 0; i < widths.size(); i++) {
		cout << "-" << setw(widths.at(i)) << '-' << "-+";
	}
	cout << endl;
}


static void
print_result(Result& res, int index)
{
	// Show how many rows are in result, if any
	int num_results = res.size();
	if (res && (num_results > 0)) {
		cout << "Result set " << index << " has " << num_results <<
				" row" << (num_results == 1 ? "" : "s") << ':' << endl;
	}
	else {
		cout << "Result set " << index << " is empty." << endl;
		return;
	}

	// Figure out the widths of the result set's columns
	IntVectorType widths;
	int size = res.columns();
	for (int i = 0; i < size; i++) {
		mysql_type_info mti(res.fields(i));
		widths.push_back((res.names(i).size() > mti.max_length()) ?
				res.names(i).size() : mti.max_length());
	}

	// Print result set header
	print_row_separator(widths);
	print_header(widths, res);
	print_row_separator(widths);

	// Display the result set contents
	for (int i = 0; i < num_results; ++i) {
		Row row = res.fetch_row();
		print_row(widths, row);
	}

	// Print result set footer
	print_row_separator(widths);
}


static void
print_multiple_results(Query& query)
{
	try {
		// Execute query and print all result sets
		Result res = query.store();
		print_result(res, 0);
		for (int i = 1; query.more_results(); ++i) {
			res = query.store_next();
			print_result(res, i);
		}
	}
	catch (Exception& err) {
		// Something bad happened....
		cerr << "Multi-query failure: " << err.what() << endl;
		exit(1);
	}
}


int
main(int argc, char *argv[])
{
	Connection con;
	try {
		// Enable multi-queries.  Notice that we can set connection
		// options before the connection is established, which the
		// underlying MySQL C API does not allow.  In this particular
		// case, this is not a mere nicety: the multi-query option has
		// a side effect of setting one of the flags used when 
		// establishing the database server connection.  We could set it
		// directly, but then we couldn't use connect_to_db().
		con.set_option(Connection::opt_multi_statements, true);

		// Connect to database
		if (!connect_to_db(argc, argv, con)) {
			return 1;
		}
	}
	catch (const BadOption& err) {
		if (err.what_option() == Connection::opt_multi_statements) {
			cerr << "This example only works when MySQL++ is built "
					"against MySQL C API" << endl;
			cerr << "version 4.1.01 or later." << endl;
		}
		else {
			cerr << "Unexpected option failure: " << err.what() << endl;
		}
		return 1;
	}
	catch (const ConnectionFailed& err) {
		cerr << "Failed to connect to database server: " <<
				err.what() << endl;
		return 1;
	}
	catch (const Exception& er) {
		// Catch-all for any other MySQL++ exceptions
		cerr << "Error: " << er.what() << endl;
		return 1;
	}

	// Set up query with multiple queries.
	Query query = con.query();
	query << "DROP TABLE IF EXISTS test_table;" << endl <<
			"CREATE TABLE test_table(id INT);" << endl <<
			"INSERT INTO test_table VALUES(10);" << endl <<
			"UPDATE test_table SET id=20 WHERE id=10;" << endl <<
			"SELECT * FROM test_table;" << endl <<
			"DROP TABLE test_table" << endl;
	cout << "Multi-query: " << endl << query.preview() << endl;

	// Execute statement and display all result sets.
	print_multiple_results(query);

#if MYSQL_VERSION_ID >= 50000
	// If it's MySQL v5.0 or higher, also test stored procedures, which
	// return their results the same way multi-queries do.
	query.reset();
	query << "DROP PROCEDURE IF EXISTS get_stock;" << endl <<
			"CREATE PROCEDURE get_stock" << endl <<
			"( i_item varchar(20) )" << endl <<
			"BEGIN" << endl <<
			"SET i_item = concat('%', i_item, '%');" << endl <<
			"SELECT * FROM stock WHERE lower(item) like lower(i_item);" << endl <<
			"END" << endl <<
			";";
	cout << "Stored procedure query: " << endl << query.preview() << endl;

	// Create the stored procedure.
	print_multiple_results(query);

	// Call the stored procedure and display its results.
	query.reset();
	query << "CALL get_stock('relish')";
	cout << "Query: " << query.preview() << endl;
	print_multiple_results(query);
#endif

	return 0;
}