aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/libpq++/examples/testlibpq5.cc
blob: 086a598cc7c3fe98092526603e85636d944d171e (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
/*
* testlibpq5.cc
*	Test the C++ version of LIBPQ, the POSTGRES frontend library.
*	tests the binary cursor interface
*
*
*
populate a database by doing the following (use testlibpq5.sql):

CREATE TABLE test1 (i int4, d float4, p polygon);

INSERT INTO test1 values (1, 3.567, '(3.0, 4.0, 1.0, 2.0)'::polygon);

INSERT INTO test1 values (2, 89.05, '(4.0, 3.0, 2.0, 1.0)'::polygon);

the expected output is:

tuple 0: got
i = (4 bytes) 1,
d = (4 bytes) 3.567000,
p = (4 bytes) 2 points		   boundbox = (hi=3.000000/4.000000, lo = 1.000000,2.000000)
tuple 1: got
i = (4 bytes) 2,
d = (4 bytes) 89.050003,
p = (4 bytes) 2 points		   boundbox = (hi=4.000000/3.000000, lo = 2.000000,1.000000)

*
*/
#include <iostream.h>
#include "libpq++.h"
#include <stdlib.h>
extern "C"
{
#include "postgres.h"		// for Postgres types
#include "utils/geo_decls.h" // for the POLYGON type
}

int main()
{
	// Begin, by connecting to the backend using hardwired constants
	// and a test database created by the user prior to the invokation
	// of this test program.  Connect using cursor interface.
	const char* dbName = "dbname=template1"; // change this to the name of your test database
	PgCursor data(dbName, "mycursor");

	// check to see that the backend connection was successfully made
	if ( data.ConnectionBad() )
	{
		cerr << "Connection to database '" << dbName << "' failed." << endl
		<< data.ErrorMessage();
		exit(1);
	}

	// Declare a binary cursor for all the tuples in database 'test1'
	if ( !data.Declare("select * from test1", 1) )
	{
		cerr << "DECLARE CURSOR command failed" << endl;
		exit(1);
	}

	// fetch all instances from the current cursor
	if ( !data.Fetch() )
	{
		cerr << "FETCH ALL command didn't return tuples properly" << endl;
		exit(1);
	}

	// Find the field numbers for the columns 'i', 'd', and 'p'
	int i_fnum = data.FieldNum("i");
	int d_fnum = data.FieldNum("d");
	int p_fnum = data.FieldNum("p");

	/*
	  for (i=0;i<3;i++) {
		  printf("type[%d] = %d, size[%d] = %d\n",
			 i, data.FieldType(i),
			 i, data.FieldSize(i));
	  }
	*/

	// Print out the information about the extracted tuple
	for (int i = 0; i < data.Tuples(); i++)
	{
		// we hard-wire this to the 3 fields we know about
		int* ival = (int*)data.GetValue(i, i_fnum);
		float* dval = (float*)data.GetValue(i, d_fnum);
		int plen = data.GetLength(i, p_fnum);

		// Allocate correct memory space for the Polygon struct and copy
		// the extracted data into it.
		// plen doesn't include the length field so need to increment by VARHDSZ
		POLYGON* pval = (POLYGON*) malloc(plen + VARHDRSZ);
		pval->size = plen;
		memmove((char*)&pval->npts, data.GetValue(i, p_fnum), plen);

		// Display Polygon Information
		cout << "tuple " << i << ": got" << endl
		<< " i = (" << data.GetLength(i, i_fnum) << " bytes) " << *ival << "," << endl
		<< " d = (" << data.GetLength(i, d_fnum) << " bytes) " << *dval << "," << endl
		<< " p = (" << data.GetLength(i, d_fnum) << " bytes) " << pval->npts << " points"
		<< "\tboundbox = (hi=" << pval->boundbox.high.x << "/" << pval->boundbox.high.y << ","
		<< "lo = " << pval->boundbox.low.x << "," << pval->boundbox.low.y << ")" << endl;

		// Deallocate memory allocated for the Polygon structure
		free(pval);
	}
	return 0;
}