• Marc G. Fournier's avatar
    · 186aeb1d
    Marc G. Fournier authored
    From: Dr. Michael Meskes <meskes@online-club.de>
    
    So this should finally get cursors working. There was an ugly bug in it.
    186aeb1d
test2.pgc 1.98 KB
#include <stdio.h>

exec sql include header_test;

int
main ()
{
exec sql begin declare section;
	struct personal_struct	{	varchar name[8];
					struct birth_struct {	long born;
								short age;
					} birth;
				} personal;
	struct personal_indicator {	short ind_name;
					struct birth_indicator {	short ind_born;
									int ind_age;
					} ind_birth;
				  } ind_personal;
	long ind_married;
	char married[9];
exec sql end declare section;
	char msg[128], command[128];
	FILE *dbgs;

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

	strcpy(msg, "connect");
	exec sql connect to tcp:postgresql://localhost:5432/mm;

	strcpy(msg, "create");
	exec sql create table meskes(name char(8), born integer, age smallint, married char(8));

	strcpy(msg, "insert");
	exec sql insert into meskes(name, born, age, married) values ('Petra', 19661202, 31, '19900404');
	exec sql insert into meskes(name, born, age, married) values ('Michael', 19660117, 32, '19900404');
	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, "commit");
	exec sql commit;

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

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

	exec sql whenever not found do break;

	while (1) {
		strcpy(msg, "fetch");
		exec sql fetch cur into :personal:ind_personal, :married:ind_married;
                printf ("%8.8s was born %d (age = %d) %s%s\n", personal.name.arr, personal.birth.born, personal.birth.age, ind_married ? "" : "and married ", ind_married ? "" : married);
	}

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

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

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

	strcpy(msg, "disconnect"); 

	exec sql disconnect;
	if (dbgs != NULL)
                fclose(dbgs);

	return (0);
}