aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/libpq++/examples/testlibpq4.cc
blob: 9d5ca3ec769ac3674370a0d7cadc2fe2293cf5a2 (plain)
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
/*
 * testlibpq4.cc
 * 	Test the C++ version of LIBPQ, the POSTGRES frontend library.
 * tests the copy in features
 *
 */
#include <stdio.h>
#include "libpq++.H"

#define DEBUG printf("Got here %d\n", __LINE__);
main()
{
  char* dbName;
  int nFields;
  int i,j;

  /* begin, by creating the parameter environment for a backend
     connection. When no parameters are given then the system will
     try to use reasonable defaults by looking up environment variables 
     or, failing that, using hardwired constants */
  PGenv env;
  PGdatabase* data;

  dbName = getenv("USER"); /* change this to the name of your test database */

  /* make a connection to the database */
  data = new PGdatabase(&env, dbName);

  /* check to see that the backend connection was successfully made */
  if (data->status() == CONNECTION_BAD) {
    fprintf(stderr,"Connection to database '%s' failed.\n", dbName);
    fprintf(stderr,"%s",data->errormessage());
    delete data;
    exit(1);
  }

  /* start a transaction block */
  if(data->exec("BEGIN") != PGRES_COMMAND_OK) {
    fprintf(stderr,"BEGIN command failed\n");
    delete data;
    exit(1);
  }

  if (data->exec("CREATE TABLE foo (a int4, b char16, d float8)") != 
      PGRES_COMMAND_OK) {
      fprintf(stderr,"CREATE TABLE foo command failed\n");
      delete data;
      exit(1);
  }

  if (data->exec("COPY foo FROM STDIN") != PGRES_COMMAND_OK) {
      fprintf(stderr,"COPY foo FROM STDIN\n");
      delete data;
      exit(1);      
  }

  data->putline("3\thello world\t4.5\n");
  data->putline("4\tgoodbye word\t7.11\n");
  data->putline(".\n");
  data->endcopy();
  data->exec("SELECT * FROM foo");
  data->printtuples(stdout,1,"|",1,0);
  data->exec("DROP TABLE foo");
  // end the transaction 
  data->exec("END");

  // close the connection to the database and cleanup 
  delete data;
}