mirror of
https://github.com/postgres/postgres.git
synced 2025-07-05 07:21:24 +03:00
From: Michael Meskes <meskes@topsystem.de>
Tue Apr 28 14:48:41 CEST 1998 - Put operator "->" back into parser. Note that :foo->bar means the C term, but :foo ->bar means the operator "->". Tue Apr 28 15:49:07 CEST 1998 - Added exec sql disconnect command. - Allow varchar in C to be written in uppercase too. - Added whenever option "do break;" Wed Apr 29 09:17:53 CEST 1998 - Corrected parsing of C comments. - Also allow C++ style comments. - Make sure not found is only checked after commands that could return it. - Added error codes, see ecpgerror.h for details. - Added "exec sql <TransactionStmt> release" as disconnect statement for compatibility issues. Thu Apr 30 10:42:10 CEST 1998 - Added a -t option to disable automatic transaction start. - Added sqlerrd[] to sqlca struct. - Give back number of tuples affect in sqlca.sqlerrd[2]. Thu Apr 30 13:36:02 CEST 1998 - Make the return code different in case of different errors. Wed May 6 11:42:48 CEST 1998 - Free memory if possible - Some bugfixes for bugs I found while changing the memory allocation code - Now able to fill complete array with one call (see test1.pgc for an example) - Set version to 2.3.0 - Set library version to 2.1
This commit is contained in:
@ -1,14 +1,18 @@
|
||||
all: test2 perftest
|
||||
all: test1 test2 perftest
|
||||
|
||||
LDFLAGS=-g -I ../include -I ../../libpq -L../lib -lecpg -L../../libpq -lpq -lcrypt --static
|
||||
|
||||
test1: test1.c
|
||||
test1.c: test1.pgc
|
||||
../preproc/ecpg $?
|
||||
|
||||
test2: test2.c
|
||||
gcc -g -I ../include -I ../../libpq -o test2 test2.c -L../lib -lecpg -L../../libpq -lpq -lcrypt --static
|
||||
test2.c: test2.pgc
|
||||
../preproc/ecpg test2.pgc
|
||||
../preproc/ecpg $?
|
||||
|
||||
perftest: perftest.c
|
||||
gcc -g -I ../include -I ../../libpq -o perftest perftest.c -L../lib -lecpg -L../../libpq -lpq -lcrypt --static
|
||||
perftest.c: perftest.pgc
|
||||
../preproc/ecpg perftest.pgc
|
||||
perftest.c:perftest.pgc
|
||||
../preproc/ecpg $?
|
||||
|
||||
clean:
|
||||
/bin/rm test2 test2.c perftest perftest.c log
|
||||
/bin/rm test1 test2 perftest *.c log
|
||||
|
@ -1,77 +0,0 @@
|
||||
/* These two include files are added by the preprocessor */
|
||||
#include <ecpgtype.h>
|
||||
#include <ecpglib.h>
|
||||
/* exec sql begin declare section */
|
||||
|
||||
/* VARSIZE */ struct varchar_uid
|
||||
{
|
||||
int len;
|
||||
char arr[200];
|
||||
} uid;
|
||||
struct varchar_name
|
||||
{
|
||||
int len;
|
||||
char arr[200];
|
||||
} name;
|
||||
short value;
|
||||
|
||||
/* exec sql end declare section */
|
||||
|
||||
|
||||
#include "sqlca.h"
|
||||
|
||||
#define DBCP(x,y) strcpy(x.arr,y);x.len = strlen(x.arr)
|
||||
#define LENFIX(x) x.len=strlen(x.arr)
|
||||
#define STRFIX(x) x.arr[x.len]='\0'
|
||||
#define SQLCODE sqlca.sqlcode
|
||||
|
||||
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()
|
||||
{
|
||||
strcpy(uid.arr, "test/test");
|
||||
LENFIX(uid);
|
||||
|
||||
ECPGconnect("kom");
|
||||
if (SQLCODE)
|
||||
db_error("connect");
|
||||
|
||||
strcpy(name.arr, "opt1");
|
||||
LENFIX(name);
|
||||
|
||||
ECPGdo(__LINE__, "declare cur cursor for select name , value from pace_test ", ECPGt_EOIT, ECPGt_EORT);
|
||||
if (SQLCODE)
|
||||
db_error("declare");
|
||||
|
||||
|
||||
if (SQLCODE)
|
||||
db_error("open");
|
||||
|
||||
while (1)
|
||||
{
|
||||
ECPGdo(__LINE__, "fetch in cur ", ECPGt_EOIT, ECPGt_varchar, &name, 200, 0, sizeof(struct varchar_name), ECPGt_short, &value, 0, 0, sizeof(short), ECPGt_EORT);
|
||||
if (SQLCODE)
|
||||
break;
|
||||
STRFIX(name);
|
||||
printf("%s\t%d\n", name.arr, value);
|
||||
}
|
||||
|
||||
if (SQLCODE < 0)
|
||||
db_error("fetch");
|
||||
|
||||
ECPGdo(__LINE__, "close cur ", ECPGt_EOIT, ECPGt_EORT);
|
||||
if (SQLCODE)
|
||||
db_error("close");
|
||||
ECPGcommit(__LINE__);
|
||||
if (SQLCODE)
|
||||
db_error("commit");
|
||||
|
||||
return (0);
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
exec sql include sqlca;
|
||||
|
||||
exec sql whenever not found do set_not_found();
|
||||
exec sql whenever not found do break;
|
||||
exec sql whenever sqlerror sqlprint;
|
||||
|
@ -121,5 +121,7 @@ exec sql end declare section;
|
||||
|
||||
exec sql commit;
|
||||
|
||||
exec sql disconnect;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
@ -1,72 +0,0 @@
|
||||
exec sql begin declare section;
|
||||
VARCHAR uid[200 /* VARSIZE */ ];
|
||||
varchar name[200];
|
||||
short value;
|
||||
exec sql end declare section;
|
||||
|
||||
exec sql include sqlca;
|
||||
|
||||
#define DBCP(x,y) strcpy(x.arr,y);x.len = strlen(x.arr)
|
||||
#define LENFIX(x) x.len=strlen(x.arr)
|
||||
#define STRFIX(x) x.arr[x.len]='\0'
|
||||
#define SQLCODE sqlca.sqlcode
|
||||
|
||||
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()
|
||||
{
|
||||
strcpy(uid.arr, "test/test");
|
||||
LENFIX(uid);
|
||||
|
||||
exec sql connect 'kom';
|
||||
|
||||
if (SQLCODE)
|
||||
db_error("connect");
|
||||
|
||||
strcpy(name.arr, "opt1");
|
||||
LENFIX(name);
|
||||
|
||||
exec sql declare cur cursor for
|
||||
select name,
|
||||
value from pace_test;
|
||||
|
||||
if (SQLCODE)
|
||||
db_error("declare");
|
||||
|
||||
exec sql open cur;
|
||||
|
||||
if (SQLCODE)
|
||||
db_error("open");
|
||||
|
||||
while (1)
|
||||
{
|
||||
exec sql fetch in cur into:name,
|
||||
: value;
|
||||
|
||||
if (SQLCODE)
|
||||
break;
|
||||
STRFIX(name);
|
||||
printf("%s\t%d\n", name.arr, value);
|
||||
}
|
||||
|
||||
if (SQLCODE < 0)
|
||||
db_error("fetch");
|
||||
|
||||
exec sql close cur;
|
||||
|
||||
if (SQLCODE)
|
||||
db_error("close");
|
||||
exec sql commit;
|
||||
|
||||
if (SQLCODE)
|
||||
db_error("commit");
|
||||
|
||||
return (0);
|
||||
}
|
63
src/interfaces/ecpg/test/test1.pgc
Normal file
63
src/interfaces/ecpg/test/test1.pgc
Normal file
@ -0,0 +1,63 @@
|
||||
#include <stdio.h>
|
||||
|
||||
exec sql whenever sqlerror sqlprint;
|
||||
|
||||
exec sql include sqlca;
|
||||
|
||||
int
|
||||
main ()
|
||||
{
|
||||
exec sql begin declare section;
|
||||
int amount[5];
|
||||
char name[5][8];
|
||||
exec sql end declare section;
|
||||
char msg[128], command[128];
|
||||
FILE *dbgs;
|
||||
int i,j;
|
||||
|
||||
if ((dbgs = fopen("log", "w")) != NULL)
|
||||
ECPGdebug(1, dbgs);
|
||||
|
||||
strcpy(msg, "connect");
|
||||
exec sql connect mm;
|
||||
|
||||
strcpy(msg, "create");
|
||||
exec sql create table test(name char(8), amount int);
|
||||
|
||||
strcpy(msg, "execute insert 1");
|
||||
sprintf(command, "insert into test(name, amount) values ('foobar', 1)");
|
||||
exec sql execute immediate :command;
|
||||
|
||||
strcpy(msg, "excute insert 2");
|
||||
sprintf(command, "insert into test(name, amount) select name, amount+1 from test");
|
||||
exec sql execute immediate :command;
|
||||
|
||||
strcpy(msg, "excute insert 3");
|
||||
sprintf(command, "insert into test(name, amount) select name, amount+10 from test");
|
||||
exec sql execute immediate :command;
|
||||
|
||||
printf("Inserted %d tuples via execute immediate\n", sqlca.sqlerrd[2]);
|
||||
|
||||
strcpy(msg, "commit");
|
||||
exec sql commit;
|
||||
|
||||
strcpy(msg, "select");
|
||||
exec sql select name, amount into :name, :amount from test;
|
||||
|
||||
for (i=0, j=sqlca.sqlerrd[2]; i<j; i++)
|
||||
printf("name[%d]=%8.8s, amount[%d]=%d\n", i, name[i], i, amount[i]);
|
||||
|
||||
strcpy(msg, "drop");
|
||||
exec sql drop table test;
|
||||
|
||||
strcpy(msg, "commit");
|
||||
exec sql commit;
|
||||
|
||||
strcpy(msg, "disconnect");
|
||||
exec sql disconnect;
|
||||
|
||||
if (dbgs != NULL)
|
||||
fclose(dbgs);
|
||||
|
||||
return (0);
|
||||
}
|
@ -2,13 +2,6 @@
|
||||
|
||||
exec sql include header_test;
|
||||
|
||||
static int not_found = 0;
|
||||
static void
|
||||
set_not_found(void)
|
||||
{
|
||||
not_found = 1;
|
||||
}
|
||||
|
||||
int
|
||||
main ()
|
||||
{
|
||||
@ -43,10 +36,7 @@ exec sql end declare section;
|
||||
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);
|
||||
|
||||
sprintf(command, "insert into meskes(name, born, age) values ('Chris', 19970923, 0)");
|
||||
strcpy(msg, "execute");
|
||||
exec sql execute immediate :command;
|
||||
exec sql insert into meskes(name, born, age) values ('Chris', 19970923, 0);
|
||||
|
||||
strcpy(msg, "commit");
|
||||
exec sql commit;
|
||||
@ -58,11 +48,10 @@ exec sql end declare section;
|
||||
strcpy(msg, "open");
|
||||
exec sql open cur;
|
||||
|
||||
while (not_found == 0) {
|
||||
while (1) {
|
||||
strcpy(msg, "fetch");
|
||||
exec sql fetch cur into :personal:ind_personal, :married:ind_married;
|
||||
if (not_found == 0)
|
||||
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);
|
||||
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");
|
||||
@ -74,6 +63,9 @@ exec sql end declare section;
|
||||
strcpy(msg, "commit");
|
||||
exec sql commit;
|
||||
|
||||
strcpy(msg, "disconnect");
|
||||
|
||||
exec sql disconnect;
|
||||
if (dbgs != NULL)
|
||||
fclose(dbgs);
|
||||
|
||||
|
Reference in New Issue
Block a user