aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/libpq++/pgconnection.cc
blob: 777a12e8e7ac24346bcac12a9f895fed1060748e (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*-------------------------------------------------------------------------
 *
 *   FILE
 *	pgconnection.cc
 *
 *   DESCRIPTION
 *      implementation of the PGconnection class.
 *   PGconnection encapsulates a frontend to backend connection
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
 *    $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pgconnection.cc,v 1.1.1.1 1996/07/09 06:22:18 scrappy Exp $
 *
 *-------------------------------------------------------------------------
 */

#include "libpq++.H"

// default constructor
// checks environment variable for database name
PGconnection::PGconnection()
{
  char* name;
  PGenv* newenv;

  conn = NULL;
  result = NULL;
  errorMessage[0] = '\0';

  newenv = new PGenv(); // use reasonable defaults for the environment
  if (!(name = getenv(ENV_DEFAULT_DBASE)))
    return;
  connect(newenv, name);
}

// constructor -- for given environment and database name
PGconnection::PGconnection(PGenv* env, char* dbName)
{
  conn = NULL;
  result = NULL;
  errorMessage[0] = '\0';
  connect(env, dbName);
}

// destructor - closes down the connection and cleanup
PGconnection::~PGconnection()
{
  if (result)	PQclear(result);
  if (conn)	PQfinish(conn);
}

// PGconnection::connect
// establish a connection to a backend
ConnStatusType
PGconnection::connect(PGenv* newenv, char* dbName)
{
#if 0
    FILE *debug;
    debug = fopen("/tmp/trace.out","w");
    PQtrace(conn, debug);
#endif

    env = newenv;
    fe_setauthsvc(env->pgauth, errorMessage); 
    conn = PQsetdb(env->pghost, env->pgport, env->pgoption, env->pgtty, dbName);
    if(strlen(errorMessage))
      return CONNECTION_BAD;
    else
      return status();
}

// PGconnection::status -- return connection or result status
ConnStatusType
PGconnection::status()
{
    return PQstatus(conn);
}

// PGconnection::exec  -- send a query to the backend
ExecStatusType
PGconnection::exec(char* query)
{
  if (result)
    PQclear(result); 

  result = PQexec(conn, query);
  if (result)
      return PQresultStatus(result);
  else {
      strcpy(errorMessage, PQerrorMessage(conn));
      return PGRES_FATAL_ERROR;
  }
}