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
|
#include "cmdline.h"
#include "printdata.h"
#include <iostream>
#include <iomanip>
using namespace std;
int
main(int argc, char *argv[])
{
// Get database access parameters from command line
mysqlpp::examples::CommandLine cmdline(argc, argv);
if (!cmdline) {
return 1;
}
try {
// Establish the connection to the database server.
mysqlpp::Connection con(mysqlpp::examples::db_name,
cmdline.server(), cmdline.user(), cmdline.pass());
// Get contents of main example table
mysqlpp::Query query = con.query("select * from stock");
mysqlpp::StoreQueryResult res = query.store();
// Show info about each field in that table
char widths[] = { 12, 22, 46 };
cout.setf(ios::left);
cout << setw(widths[0]) << "Field" <<
setw(widths[1]) << "SQL Type" <<
setw(widths[2]) << "Equivalent C++ Type" <<
endl;
for (size_t i = 0; i < sizeof(widths) / sizeof(widths[0]); ++i) {
cout << string(widths[i] - 1, '=') << ' ';
}
cout << endl;
for (size_t i = 0; i < res.field_names()->size(); i++) {
// Suppress C++ type name outputs when run under dtest,
// as they're system-specific.
const char* cname = res.field_type(int(i)).name();
mysqlpp::FieldTypes::value_type ft = res.field_type(int(i));
ostringstream os;
os << ft.sql_name() << " (" << ft.id() << ')';
cout << setw(widths[0]) << res.field_name(int(i)).c_str() <<
setw(widths[1]) << os.str() <<
setw(widths[2]) << cname <<
endl;
}
cout << endl;
// Simple type check
if (res.field_type(0) == typeid(string)) {
cout << "SQL type of 'item' field most closely resembles "
"the C++ string type." << endl;
}
// Tricky type check: the 'if' path shouldn't happen because the
// description field has the NULL attribute. We need to dig a
// little deeper if we want to ignore this in our type checks.
if (res.field_type(5) == typeid(string)) {
cout << "Should not happen! Type check failure." << endl;
}
else if (res.field_type(5) == typeid(mysqlpp::sql_blob_null)) {
cout << "SQL type of 'description' field resembles "
"a nullable variant of the C++ string type." << endl;
}
else {
cout << "Weird: fifth field's type is now " <<
res.field_type(5).name() << endl;
cout << "Did something recently change in resetdb?" << endl;
}
}
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;
}
|