2000-03-30 13:41:46 +02:00
|
|
|
/****************************************************************************/
|
|
|
|
/* Test comment */
|
|
|
|
/*--------------------------------------------------------------------------*/
|
1999-03-07 09:01:18 +01:00
|
|
|
exec sql include header_test;
|
|
|
|
|
|
|
|
exec sql type str is varchar[10];
|
|
|
|
|
|
|
|
int
|
|
|
|
main ()
|
|
|
|
{
|
|
|
|
typedef struct { long born; short age; } birthinfo;
|
|
|
|
exec sql type birthinfo is struct { long born; short age; };
|
|
|
|
exec sql begin declare section;
|
|
|
|
struct personal_struct { str name;
|
|
|
|
birthinfo birth;
|
|
|
|
} personal;
|
|
|
|
struct personal_indicator { int ind_name;
|
|
|
|
birthinfo ind_birth;
|
|
|
|
} ind_personal;
|
|
|
|
int *ind_married = NULL;
|
|
|
|
int children;
|
|
|
|
int ind_children;
|
|
|
|
str *married = NULL;
|
1999-06-29 11:25:25 +02:00
|
|
|
char *wifesname="Petra";
|
2000-01-27 20:01:35 +01:00
|
|
|
char *query="select * from meskes where name = ?";
|
1999-03-07 09:01:18 +01:00
|
|
|
exec sql end declare section;
|
|
|
|
|
|
|
|
exec sql declare cur cursor for
|
|
|
|
select name, born, age, married, children from meskes;
|
|
|
|
|
|
|
|
char msg[128], command[128];
|
|
|
|
FILE *dbgs;
|
|
|
|
|
|
|
|
if ((dbgs = fopen("log", "w")) != NULL)
|
|
|
|
ECPGdebug(1, dbgs);
|
|
|
|
|
|
|
|
strcpy(msg, "connect");
|
1999-06-29 11:25:25 +02:00
|
|
|
exec sql connect to unix:postgresql://localhost:5432/mm;
|
1999-03-07 09:01:18 +01:00
|
|
|
|
|
|
|
strcpy(msg, "create");
|
|
|
|
exec sql create table meskes(name char(8), born integer, age smallint, married date, children integer);
|
|
|
|
|
|
|
|
strcpy(msg, "insert");
|
1999-06-29 11:25:25 +02:00
|
|
|
exec sql insert into meskes(name, married, children) values (:wifesname, '19900404', 3);
|
2000-02-22 20:57:12 +01:00
|
|
|
exec sql insert into meskes(name, born, age, married, children) values ('Michael', 19660117, 34, '19900404', 3);
|
|
|
|
exec sql insert into meskes(name, born, age) values ('Carsten', 19910103, 9);
|
2000-01-07 09:01:18 +01:00
|
|
|
exec sql insert into meskes(name, born, age) values ('Marc', 19930907, 6);
|
|
|
|
exec sql insert into meskes(name, born, age) values ('Chris', 19970923, 2);
|
1999-03-07 09:01:18 +01:00
|
|
|
|
|
|
|
strcpy(msg, "commit");
|
|
|
|
exec sql commit;
|
|
|
|
|
|
|
|
strcpy(msg, "open");
|
|
|
|
exec sql open cur;
|
|
|
|
|
|
|
|
exec sql whenever not found do break;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
strcpy(msg, "fetch");
|
2000-01-27 20:01:35 +01:00
|
|
|
exec sql fetch from cur into :personal:ind_personal, :married:ind_married, :children:ind_children;
|
1999-03-07 09:01:18 +01:00
|
|
|
printf("%8.8s", personal.name.arr);
|
|
|
|
if (ind_personal.ind_birth.born >= 0)
|
|
|
|
printf(", born %d", personal.birth.born);
|
|
|
|
if (ind_personal.ind_birth.age >= 0)
|
|
|
|
printf(", age = %d", personal.birth.age);
|
|
|
|
if (ind_married >= 0)
|
|
|
|
printf(", married %10.10s", married->arr);
|
|
|
|
if (ind_children >= 0)
|
|
|
|
printf(", children = %d", children);
|
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
free(married);
|
|
|
|
married = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(msg, "close");
|
|
|
|
exec sql close cur;
|
|
|
|
|
2000-01-18 14:03:49 +01:00
|
|
|
/* and now a query with prepare */
|
2000-01-27 20:01:35 +01:00
|
|
|
exec sql prepare MM from :query;
|
1999-03-07 09:01:18 +01:00
|
|
|
exec sql declare prep cursor for MM;
|
|
|
|
|
|
|
|
strcpy(msg, "open");
|
1999-06-29 11:25:25 +02:00
|
|
|
exec sql open prep using :wifesname;
|
1999-03-07 09:01:18 +01:00
|
|
|
|
|
|
|
exec sql whenever not found do break;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
strcpy(msg, "fetch");
|
|
|
|
exec sql fetch in prep into :personal:ind_personal, :married:ind_married, :children:ind_children;
|
|
|
|
printf("%8.8s", personal.name.arr);
|
|
|
|
if (ind_personal.ind_birth.born >= 0)
|
|
|
|
printf(", born %d", personal.birth.born);
|
|
|
|
if (ind_personal.ind_birth.age >= 0)
|
|
|
|
printf(", age = %d", personal.birth.age);
|
|
|
|
if (ind_married >= 0)
|
|
|
|
printf(", married %10.10s", married->arr);
|
|
|
|
if (ind_children >= 0)
|
|
|
|
printf(", children = %d", children);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
free(married);
|
|
|
|
|
|
|
|
strcpy(msg, "close");
|
|
|
|
exec sql close prep;
|
|
|
|
|
|
|
|
strcpy(msg, "drop");
|
|
|
|
exec sql drop table meskes;
|
|
|
|
|
|
|
|
strcpy(msg, "commit");
|
|
|
|
exec sql commit;
|
|
|
|
|
|
|
|
strcpy(msg, "disconnect");
|
|
|
|
exec sql disconnect;
|
1999-09-17 20:28:15 +02:00
|
|
|
|
1999-03-07 09:01:18 +01:00
|
|
|
if (dbgs != NULL)
|
|
|
|
fclose(dbgs);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|