summaryrefslogtreecommitdiffstats
path: root/src/interfaces/ecpg/test/preproc/outofscope.pgc
blob: ed607821f9533fccada4702b0923894358029172 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

exec sql include ../regression;

exec sql include pgtypes_numeric.h;

exec sql begin declare section;
exec sql include struct.h;
exec sql end declare section;

exec sql whenever sqlerror stop;

/* Functions for test 1 */

static void
get_var1(MYTYPE **myvar0, MYNULLTYPE **mynullvar0)
{
	exec sql begin declare section;
	MYTYPE		*myvar = malloc(sizeof(MYTYPE));
	MYNULLTYPE	*mynullvar = malloc(sizeof(MYNULLTYPE));
	exec sql end declare section;

	/* Test DECLARE ... SELECT ... INTO with pointers */

	exec sql declare mycur cursor for select * INTO :myvar :mynullvar from a1;

	if (sqlca.sqlcode != 0)
		exit(1);

	*myvar0 = myvar;
	*mynullvar0 = mynullvar;
}

static void
open_cur1(void)
{
	exec sql open mycur;
}

static void
get_record1(void)
{
	exec sql fetch mycur;
}

static void
close_cur1(void)
{
	exec sql close mycur;
}

int
main (void)
{
	MYTYPE		*myvar;
	MYNULLTYPE	*mynullvar;
	int loopcount;
	char msg[128];

	ECPGdebug(1, stderr);

	strcpy(msg, "connect");
	exec sql connect to REGRESSDB1;

	strcpy(msg, "set");
	exec sql set datestyle to iso;

	strcpy(msg, "create");
	exec sql create table a1(id serial primary key, t text, d1 numeric, d2 float8, c character(10));

	strcpy(msg, "insert");
	exec sql insert into a1(id, t, d1, d2, c) values (default, 'a', 1.0, 2, 'a');
	exec sql insert into a1(id, t, d1, d2, c) values (default, null, null, null, null);
	exec sql insert into a1(id, t, d1, d2, c) values (default, 'b', 2.0, 3, 'b');

	strcpy(msg, "commit");
	exec sql commit;

	/* Test out-of-scope DECLARE/OPEN/FETCH/CLOSE */

	get_var1(&myvar, &mynullvar);
	open_cur1();

	for (loopcount = 0; loopcount < 100; loopcount++)
	{
		memset(myvar, 0, sizeof(MYTYPE));
		get_record1();
		if (sqlca.sqlcode == ECPG_NOT_FOUND)
			break;
		printf("id=%d%s t='%s'%s d1=%f%s d2=%f%s c = '%s'%s\n",
			myvar->id, mynullvar->id ? " (NULL)" : "",
			myvar->t, mynullvar->t ? " (NULL)" : "",
			myvar->d1, mynullvar->d1 ? " (NULL)" : "",
			myvar->d2, mynullvar->d2 ? " (NULL)" : "",
			myvar->c, mynullvar->c ? " (NULL)" : "");
	}

	close_cur1();

	free(myvar);
	free(mynullvar);

	strcpy(msg, "drop");
	exec sql drop table a1;

	strcpy(msg, "commit");
	exec sql commit;

	strcpy(msg, "disconnect");
	exec sql disconnect;

	return 0;
}