| 16 | | // |
|---|
| 17 | | // Usage: test008 [connect-string] [table] |
|---|
| 18 | | // |
|---|
| 19 | | // Where connect-string is a set of connection options in Postgresql's |
|---|
| 20 | | // PQconnectdb() format, eg. "dbname=template1" to select from a database |
|---|
| 21 | | // called template1, or "host=foo.bar.net user=smith" to connect to a backend |
|---|
| 22 | | // running on host foo.bar.net, logging in as user smith. |
|---|
| 23 | | // |
|---|
| 24 | | // The default table name is "pqxxevents" as used by other test programs. |
|---|
| 25 | | // PostgreSQL currently implements pg_tables as a view, which cannot be read by |
|---|
| 26 | | // using the COPY command. Otherwise, pg_tables would have made a better |
|---|
| 27 | | // default value here. |
|---|
| 28 | | int main(int argc, char *argv[]) |
|---|
| | 18 | |
|---|
| | 19 | namespace |
|---|
| 35 | | string Table = "pqxxevents"; |
|---|
| 36 | | if (argc > 2) Table = argv[2]; |
|---|
| 37 | | |
|---|
| 38 | | // Begin a transaction acting on our current connection |
|---|
| 39 | | work T(C, "test8"); |
|---|
| 40 | | |
|---|
| 41 | | vector<string> R, First; |
|---|
| 42 | | |
|---|
| 43 | | // Set up a tablereader stream to read data from table pg_tables |
|---|
| 44 | | tablereader Stream(T, Table); |
|---|
| 45 | | |
|---|
| 46 | | // Read results into string vectors and print them |
|---|
| 47 | | for (int n=0; (Stream >> R); ++n) |
|---|
| 48 | | { |
|---|
| 49 | | // Keep the first row for later consistency check |
|---|
| 50 | | if (n == 0) First = R; |
|---|
| 51 | | |
|---|
| 52 | | cout << n << ":\t" << separated_list("\t",R.begin(),R.end()) << endl; |
|---|
| 53 | | R.clear(); |
|---|
| 54 | | } |
|---|
| 55 | | |
|---|
| 56 | | Stream.complete(); |
|---|
| 57 | | |
|---|
| 58 | | // Verify the contents we got for the first row |
|---|
| 59 | | if (!First.empty()) |
|---|
| 60 | | { |
|---|
| 61 | | tablereader Verify(T, Table); |
|---|
| 62 | | string Line; |
|---|
| 63 | | |
|---|
| 64 | | if (!Verify.get_raw_line(Line)) |
|---|
| 65 | | throw logic_error("tablereader got rows the first time around, " |
|---|
| 66 | | "but none the second time!"); |
|---|
| 67 | | |
|---|
| 68 | | cout << "First tuple was: " << endl << Line << endl; |
|---|
| 69 | | |
|---|
| 70 | | Verify.tokenize(Line, R); |
|---|
| 71 | | if (R != First) |
|---|
| 72 | | throw logic_error("Got different results re-parsing first tuple!"); |
|---|
| 73 | | } |
|---|
| 74 | | } |
|---|
| 75 | | catch (const sql_error &e) |
|---|
| 76 | | { |
|---|
| 77 | | // If we're interested in the text of a failed query, we can write separate |
|---|
| 78 | | // exception handling code for this type of exception |
|---|
| 79 | | cerr << "SQL error: " << e.what() << endl |
|---|
| 80 | | << "Query was: '" << e.query() << "'" << endl; |
|---|
| 81 | | return 1; |
|---|
| 82 | | } |
|---|
| 83 | | catch (const exception &e) |
|---|
| 84 | | { |
|---|
| 85 | | // All exceptions thrown by libpqxx are derived from std::exception |
|---|
| 86 | | cerr << "Exception: " << e.what() << endl; |
|---|
| 87 | | return 2; |
|---|
| 88 | | } |
|---|
| 89 | | catch (...) |
|---|
| 90 | | { |
|---|
| 91 | | // This is really unexpected (see above) |
|---|
| 92 | | cerr << "Unhandled exception" << endl; |
|---|
| 93 | | return 100; |
|---|
| | 36 | cout << n << ":\t" << separated_list("\t",R.begin(),R.end()) << endl; |
|---|
| | 37 | R.clear(); |
|---|
| 96 | | return 0; |
|---|
| | 40 | Stream.complete(); |
|---|
| | 41 | |
|---|
| | 42 | // Verify the contents we got for the first row |
|---|
| | 43 | if (!First.empty()) |
|---|
| | 44 | { |
|---|
| | 45 | tablereader Verify(T, Table); |
|---|
| | 46 | string Line; |
|---|
| | 47 | |
|---|
| | 48 | const bool outcome(Verify.get_raw_line(Line)); |
|---|
| | 49 | PQXX_CHECK( |
|---|
| | 50 | outcome, |
|---|
| | 51 | "tablereader got rows the first time around, but not the second time."); |
|---|
| | 52 | |
|---|
| | 53 | cout << "First tuple was: " << endl << Line << endl; |
|---|
| | 54 | |
|---|
| | 55 | Verify.tokenize(Line, R); |
|---|
| | 56 | PQXX_CHECK_EQUAL(R, First, "Got different results re-parsing first tuple."); |
|---|
| | 57 | } |
|---|