test21.cxx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <iostream>
  2. #include <pqxx/connection>
  3. #include <pqxx/transaction>
  4. #include "test_helpers.hxx"
  5. using namespace pqxx;
  6. // Simple test program for libpqxx. Open a connection to database, start a
  7. // transaction, and perform a query inside it.
  8. namespace
  9. {
  10. void test_021()
  11. {
  12. connection conn;
  13. std::string const HostName{
  14. ((conn.hostname() == nullptr) ? "<local>" : conn.hostname())};
  15. conn.process_notice(
  16. std::string{} + "database=" + conn.dbname() +
  17. ", "
  18. "username=" +
  19. conn.username() +
  20. ", "
  21. "hostname=" +
  22. HostName +
  23. ", "
  24. "port=" +
  25. to_string(conn.port()) +
  26. ", "
  27. "backendpid=" +
  28. to_string(conn.backendpid()) + "\n");
  29. work tx{conn, "test_021"};
  30. // By now our connection should really have been created
  31. conn.process_notice("Printing details on actual connection\n");
  32. conn.process_notice(
  33. std::string{} + "database=" + conn.dbname() +
  34. ", "
  35. "username=" +
  36. conn.username() +
  37. ", "
  38. "hostname=" +
  39. HostName +
  40. ", "
  41. "port=" +
  42. to_string(conn.port()) +
  43. ", "
  44. "backendpid=" +
  45. to_string(conn.backendpid()) + "\n");
  46. std::string P;
  47. from_string(conn.port(), P);
  48. PQXX_CHECK_EQUAL(
  49. P, to_string(conn.port()), "Port string conversion is broken.");
  50. PQXX_CHECK_EQUAL(to_string(P), P, "Port string conversion is broken.");
  51. result R(tx.exec("SELECT * FROM pg_tables"));
  52. tx.process_notice(pqxx::internal::concat(
  53. to_string(std::size(R)), " result row in transaction ", tx.name(), "\n"));
  54. tx.commit();
  55. }
  56. PQXX_REGISTER_TEST(test_021);
  57. } // namespace