aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/ecpg/test/test2.pgc
blob: 37533515c3d86566480e183ffb28555d505b7580 (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
#include <stdio.h>

exec sql include sqlca;

#define       SQLCODE    sqlca.sqlcode

extern void ECPGdebug(int n, FILE *dbgs);

void
db_error (char *msg)
{
	sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml] = '\0';
	printf ("%s: db error %s\n", msg, sqlca.sqlerrm.sqlerrmc);
	exit (1);
}

int
main ()
{
exec sql begin declare section;
	struct personal_struct	{	varchar name[8];
					struct birth_struct {	long born;
								short age;
							    } birth;
				} personal;
exec sql end declare section;
	FILE *dbgs;

	if ((dbgs = fopen("log", "w")) != NULL)
		ECPGdebug(1, dbgs);

	exec sql connect 'mm';
	if (SQLCODE)
		db_error ("connect");

	exec sql declare cur cursor for 
		select name, born, age from meskes;
	if (SQLCODE) db_error ("declare");

	exec sql open cur;
	if (SQLCODE)
		db_error ("open");

	while (1) {
		exec sql fetch in cur into :personal;
		if (SQLCODE)
			break;
		printf ("%8.8s was born %d (age = %d)\n", personal.name.arr, personal.birth.born, personal.birth.age);
	}

	if (SQLCODE < 0)
		db_error ("fetch");

	exec sql close cur;
	if (SQLCODE) db_error ("close");
	exec sql commit;
	if (SQLCODE) db_error ("commit");

	if (dbgs != NULL)
		fclose(dbgs);

	return (0);
}