#include <libpq++.H>

#include <exception>
#include <iostream>

namespace Postgres {

class Exception : public std::exception {
   const char *reason;
public:
   Exception(const char *s) : reason(s) { }
   virtual const char* what() const { return reason; }
};

class Database : public PgDatabase {
public:
  Database(const char *s) : PgDatabase(s) {
    //if (Status() != CONNECTION_OK)
    if (ConnectionBad())
      throw Exception(ErrorMessage());
  }

  Exec(const char *s) {
    ExecStatusType stat= PgDatabase::Exec(s);
    if (stat != PGRES_COMMAND_OK && stat != PGRES_TUPLES_OK)
      throw Exception(ErrorMessage());
  }

};

} // namespace Postgres


using namespace Postgres;


int main()
{
  try {

    Database data("");

    //data.Exec("drop table foo");

    data.Exec("create table foo2 (a int4, b char(16), d float8)");

    data.PgDatabase::Exec("copy foo2 from stdin");
    data.PutLine("3\tHello World\t4.5\n");
    data.PutLine("4\tGoodbye World\t7.11\n");
    data.PutLine("\n");
    data.EndCopy();

  } catch (std::exception &e) {
    cerr << e.what() << endl;
  } catch (...) {
    cerr << "Unknown exception!\n";
  }

  return 0;
}
