aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/libpq++/libpq++.H
blob: 898a146aa314e4789f3c23d81cb16a1e82c9b632 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*-------------------------------------------------------------------------
 *
 * libpq++.H
 *    
 *
 *   DESCRIPTION
 *	C++ client interface to Postgres
 *   used for building front-end applications
 *
 *   NOTES
 *      Currently under construction.
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 *-------------------------------------------------------------------------
 */

#ifndef LIBPQXX_H
#define LIBPQXX_H

#include <stdio.h>
#include <strings.h>
#include <string>

extern "C" {
#include "config.h"
#include "postgres.h"
#include "libpq-fe.h"
}

static char rcsid[] = "$Id: libpq++.H,v 1.4 1999/05/23 01:03:58 momjian Exp $";


// ****************************************************************
//
// PgConnection - a connection made to a postgres backend
//
// ****************************************************************
class PgConnection {
protected:
  PGconn* pgConn;	// Connection Structures
  PGresult* pgResult;	// Query Result
  int pgCloseConnection; // Flag indicating whether the connection should be closed
  ConnStatusType Connect(const char* conninfo);
  string IntToString(int);
  PgConnection();
  
public:
  PgConnection(const char* conninfo); // use reasonable and environment defaults 
  ~PgConnection(); // close connection and clean up
  
  ConnStatusType Status();
  int ConnectionBad();
  const char* ErrorMessage();
  
  // returns the database name of the connection
  const char* DBName(); 
  
  ExecStatusType Exec(const char* query);  // send a query to the backend
  int ExecCommandOk(const char* query);    // send a command and check if it's
  int ExecTuplesOk(const char* query);     // send a command and check if tuple
  PGnotify* Notifies();
};

// ****************************************************************
//
// PgDatabase - a class for accessing databases
//
// ****************************************************************
class PgDatabase : public PgConnection {
protected:
  PgDatabase() : PgConnection() {}	// Do not connect

public:
  // connect to the database with conninfo
  PgDatabase(const char *conninfo) : PgConnection(conninfo) {};
  ~PgDatabase() {}; // close connection and clean up
  // query result access
  int Tuples();
  int Fields();
  const char* FieldName(int field_num);
  int FieldNum(const char *field_name);
  Oid FieldType(int field_num);
  Oid FieldType(const char *field_name);
  short FieldSize(int field_num);
  short FieldSize(const char *field_name);
  const char* GetValue(int tup_num, int field_num);
  const char* GetValue(int tup_num, const char *field_name);
  int GetLength(int tup_num, int field_num);
  int GetLength(int tup_num, const char* field_name);
  void DisplayTuples(FILE *out = 0, int fillAlign = 1, 
        const char* fieldSep = "|",int printHeader = 1, int quiet = 0) ;
  void PrintTuples(FILE *out = 0, int printAttName = 1, 
        int terseOutput = 0, int width = 0) ;

  // copy command related access
  int GetLine(char* string, int length);
  void PutLine(const char* string);
  const char *OidStatus();
  int EndCopy();
};



// ****************************************************************
//
// PGLargeObject - a class for accessing Large Object in a database
//
// ****************************************************************
class PgLargeObject : public PgConnection {
public:
  PgLargeObject(const char* conninfo = 0);   // use reasonable defaults and create large object
  PgLargeObject(Oid lobjId, const char* conninfo = 0); // use reasonable defaults and open large object
  ~PgLargeObject(); // close connection and clean up

  void Create();
  void Open();
  void Close();
  int Read(char* buf, int len);
  int Write(const char* buf, int len);
  int Lseek(int offset, int whence);
  int Tell();
  int Unlink();
  Oid LOid();
  Oid Import(const char* filename);
  int Export(const char* filename);
  string Status();
};


// ****************************************************************
//
// PgTransaction - a class for running transactions against databases
//
// ****************************************************************
class PgTransaction : public PgDatabase {
protected:
  ExecStatusType BeginTransaction();
  ExecStatusType EndTransaction();
  PgTransaction() : PgDatabase() {}	// Do not connect

public:
  PgTransaction(const char* conninfo);  // use reasonable & environment defaults
  // connect to the database with given environment and database name
  PgTransaction(const PgConnection&);
  virtual ~PgTransaction();     // close connection and clean up

};


// ****************************************************************
//
// PgCursor - a class for querying databases using a cursor
//
// ****************************************************************
class PgCursor : public PgTransaction {
protected:
  int Fetch(const string& num, const string& dir);
  string pgCursor;
  PgCursor() : PgTransaction() {}	// Do not connect

public:
  PgCursor(const char* dbName, const char* cursor);     // use reasonable & environment defaults
  // connect to the database with given environment and database name
  PgCursor(const PgConnection&, const char* cursor);
  virtual ~PgCursor();  // close connection and clean up

  // Commands associated with cursor interface
  int Declare(const string& query, int binary = 0);     // Declare a cursor with given name
  int Fetch(const char* dir = "FORWARD");               // Fetch ALL tuples in given direction
  int Fetch(unsigned num, const char* dir = "FORWARD"); // Fetch specified amount of tuples
  int Close();  // Close the cursor

  // Accessors to the cursor name
  const char* Cursor();
  void Cursor(const string& cursor);
};



// buffer size
#define BUFSIZE 1024

#endif /* LIBPQXX_H */