#include <stdio.h>

exec sql include sqlca;

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

exec sql whenever not found do set_not_found();
exec sql whenever sqlerror sqlprint;

static int not_found = 0;
static void
set_not_found(void)
{
	not_found = 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;
	char msg[128];
	FILE *dbgs;

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

	strcpy(msg, "connect");
	exec sql connect 'mm';

	strcpy(msg, "create");
	exec sql create table meskes(name char8, born int4, age int2);

	strcpy(msg, "insert");
	exec sql insert into meskes(name, born, age) values ('Petra', 19661202, 31);
	exec sql insert into meskes(name, born, age) values ('Michael', 19660117, 32);
	exec sql insert into meskes(name, born, age) values ('Carsten', 19910103, 7);
	exec sql insert into meskes(name, born, age) values ('Marc', 19930907, 4);
	exec sql insert into meskes(name, born, age) values ('Chris', 19970923, 0);

	strcpy(msg, "declare");
	exec sql declare cur cursor for 
		select name, born, age from meskes;

	exec sql open cur;

	while (not_found == 0) {
		strcpy(msg, "fetch");
		exec sql fetch cur into :personal;
		if (not_found == 0)
			printf ("%8.8s was born %d (age = %d)\n", personal.name.arr, personal.birth.born, personal.birth.age);
	}

	strcpy(msg, "close");
	exec sql close cur;

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

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

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

	return (0);
}
