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
|
/***********************************************************************
dbinfo.cpp - Example showing how to request information about the
database schema, such as table names, column types, etc.
Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
(c) 2004-2009 by Educational Technology Resources, Inc. Others may
also hold copyrights on code in this file. See the CREDITS.txt 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 "cmdline.h"
#include "printdata.h"
#include <mysql++.h>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Insert a bar into the stream with the given query string centered
static void
separator(ostream& os, string qstr)
{
string sep("========================================"
"========================================");
if (qstr.size()) {
string::size_type start = (sep.size() - qstr.size()) / 2;
sep.replace(start - 1, 1, 1, ' ');
sep.replace(start, qstr.size(), qstr);
sep.replace(start + qstr.size(), 1, 1, ' ');
os << "\n\n";
}
os << sep << endl;
}
// Print out the MySQL server version
static void
show_mysql_version(mysqlpp::Connection& con)
{
separator(cout, "");
cout << "MySQL version: " << con.client_version();
}
// Print out the names of all the databases managed by the server
static void
show_databases(mysqlpp::Connection& con)
{
mysqlpp::Query query = con.query("show databases");
separator(cout, query.str());
mysqlpp::StoreQueryResult res = query.store();
cout << "Databases found: " << res.size();
cout.setf(ios::left);
mysqlpp::StoreQueryResult::iterator rit;
for (rit = res.begin(); rit != res.end(); ++rit) {
cout << "\n\t" << (*rit)[0];
}
}
// Print information about each of the tables we found
static void
show_table_info(mysqlpp::Connection& con, const vector<string>& tables)
{
vector<string>::const_iterator it;
for (it = tables.begin(); it != tables.end(); ++it) {
mysqlpp::Query query = con.query();
query << "describe " << *it;
separator(cout, query.str());
mysqlpp::StoreQueryResult res = query.store();
size_t columns = res.num_fields();
vector<size_t> widths;
for (size_t i = 0; i < columns; ++i) {
string s = res.field_name(int(i));
if (s.compare("field") == 0) {
widths.push_back(22);
}
else if (s.compare("type") == 0) {
widths.push_back(20);
}
else if (s.compare("null") == 0) {
widths.push_back(4);
}
else if (s.compare("key") == 0) {
widths.push_back(3);
}
else if (s.compare("extra") == 0) {
widths.push_back(0);
}
else {
widths.push_back(15);
}
if (widths[i]) {
cout << '|' << setw(widths[i]) <<
res.field_name(int(i)) << '|';
}
}
cout << endl;
mysqlpp::StoreQueryResult::iterator rit;
for (rit = res.begin(); rit != res.end(); ++rit) {
for (unsigned int i = 0; i < columns; ++i) {
if (widths[i]) {
cout << ' ' << setw(widths[i]) <<
(*rit)[i].c_str() << ' ';
}
}
cout << endl;
}
}
}
// Print out the names of all tables in the sample database, and
// return the list of tables.
static void
show_tables(mysqlpp::Connection& con)
{
mysqlpp::Query query = con.query("show tables");
separator(cout, query.str());
mysqlpp::StoreQueryResult res = query.store();
cout << "Tables found: " << res.size();
cout.setf(ios::left);
vector<string> tables;
mysqlpp::StoreQueryResult::iterator rit;
for (rit = res.begin(); rit != res.end(); ++rit) {
string tbl((*rit)[0]);
cout << "\n\t" << tbl;
tables.push_back(tbl);
}
show_table_info(con, tables);
}
// Call all the above functions in sequence
int
main(int argc, char* argv[])
{
// Get database access parameters from command line
mysqlpp::examples::CommandLine cmdline(argc, argv);
if (!cmdline) {
return 1;
}
try {
// Connect to server, then dump a bunch of stuff we find on it
mysqlpp::Connection con(mysqlpp::examples::db_name,
cmdline.server(), cmdline.user(), cmdline.pass());
show_mysql_version(con);
show_databases(con);
show_tables(con);
}
catch (const mysqlpp::BadQuery& er) {
// Handle any query errors
cerr << "Query error: " << er.what() << endl;
return -1;
}
catch (const mysqlpp::Exception& er) {
// Catch-all for any other MySQL++ exceptions
cerr << "Error: " << er.what() << endl;
return -1;
}
return 0;
}
|