Commit 36e8d4e7 authored by Michael Meskes's avatar Michael Meskes

Removed old test files

parent 1fa6be6f
This diff is collapsed.
/* dynamic SQL test program
*
* $PostgreSQL: pgsql/src/interfaces/ecpg/test/dyntest.pgc,v 1.12 2006/03/11 16:57:43 momjian Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
exec sql include sql3types;
exec sql include sqlca;
static void error(void)
{ printf("#%ld:%s\n",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exit(1);
}
int main(int argc,char **argv)
{ exec sql begin declare section;
int COUNT;
int INTVAR;
int INDEX;
int INDICATOR;
bool BOOLVAR;
int TYPE,LENGTH,OCTET_LENGTH,PRECISION,SCALE,NULLABLE,RETURNED_OCTET_LENGTH;
int DATETIME_INTERVAL_CODE;
char NAME[120];
char STRINGVAR[1024];
float FLOATVAR;
double DOUBLEVAR;
char QUERY[1024];
char DB[1024];
exec sql end declare section;
int done=0;
FILE *dbgs;
if ((dbgs = fopen("log", "w")) != NULL)
ECPGdebug(1, dbgs);
snprintf(QUERY,sizeof QUERY,"select * from %s",argc>1 && argv[1][0]?argv[1]:"pg_tables");
exec sql whenever sqlerror do error();
exec sql allocate descriptor MYDESC;
strncpy(DB,argc>2?argv[2]:"mm",sizeof DB);
exec sql connect to :DB;
exec sql prepare MYQUERY from :QUERY;
exec sql declare MYCURS cursor for MYQUERY;
exec sql open MYCURS;
while (1)
{ exec sql fetch in MYCURS into sql descriptor MYDESC;
if (sqlca.sqlcode) break;
exec sql get descriptor MYDESC :COUNT = count;
if (!done)
{ printf("%d Columns\n",COUNT);
for (INDEX=1;INDEX<=COUNT;++INDEX)
{ exec sql get descriptor MYDESC value :INDEX
:TYPE = type,
:LENGTH = length, :OCTET_LENGTH=octet_length,
:PRECISION = precision, :SCALE=scale,
:NULLABLE=nullable, :NAME=name,
:RETURNED_OCTET_LENGTH=returned_octet_length;
printf("%s ",NAME);
switch (TYPE)
{ case SQL3_BOOLEAN:
printf("bool ");
break;
case SQL3_NUMERIC:
printf("numeric(%d,%d) ",PRECISION,SCALE);
break;
case SQL3_DECIMAL:
printf("decimal(%d,%d) ",PRECISION,SCALE);
break;
case SQL3_INTEGER:
printf("integer ");
break;
case SQL3_SMALLINT:
printf("smallint ");
break;
case SQL3_FLOAT:
printf("float(%d,%d) ",PRECISION,SCALE);
break;
case SQL3_REAL:
printf("real ");
break;
case SQL3_DOUBLE_PRECISION:
printf("double precision ");
break;
case SQL3_DATE_TIME_TIMESTAMP:
exec sql get descriptor MYDESC value :INDEX
:DATETIME_INTERVAL_CODE=datetime_interval_code;
switch(DATETIME_INTERVAL_CODE)
{ case SQL3_DDT_DATE:
printf("date "); break;
case SQL3_DDT_TIME:
printf("time "); break;
case SQL3_DDT_TIMESTAMP:
printf("timestamp "); break;
case SQL3_DDT_TIME_WITH_TIME_ZONE:
printf("time with time zone "); break;
case SQL3_DDT_TIMESTAMP_WITH_TIME_ZONE:
printf("timestamp with time zone "); break;
}
break;
case SQL3_INTERVAL:
printf("interval ");
break;
case SQL3_CHARACTER:
if (LENGTH>0) printf("char(%d) ",LENGTH);
else printf("char(?) ");
break;
case SQL3_CHARACTER_VARYING:
if (LENGTH>0) printf("varchar(%d) ",LENGTH);
else printf("varchar() ");
break;
default:
if (TYPE<0) printf("<OID %d> ",-TYPE);
else printf("<SQL3 %d> ",TYPE);
break;
}
if (!NULLABLE) printf("not null ");
if (OCTET_LENGTH>0) printf("[%d bytes]",OCTET_LENGTH);
putchar('\n');
}
putchar('\n');
done=1;
}
for (INDEX=1;INDEX<=COUNT;++INDEX)
{ exec sql get descriptor MYDESC value :INDEX
:TYPE = type, :SCALE=scale, :PRECISION = precision,
:INDICATOR=indicator;
if (INDICATOR==-1) printf("NULL");
else switch (TYPE)
{ case SQL3_BOOLEAN:
exec sql get descriptor MYDESC value :INDEX :BOOLVAR=data;
printf(BOOLVAR?"true":"false");
break;
case SQL3_NUMERIC:
case SQL3_DECIMAL:
if (SCALE==0) /* we might even print leading zeros "%0*d" */
{ exec sql get descriptor MYDESC value :INDEX :INTVAR=data;
printf("%*d",PRECISION,INTVAR);
}
else
{ exec sql get descriptor MYDESC value :INDEX :FLOATVAR=data;
printf("%*.*f",PRECISION+1,SCALE,FLOATVAR);
}
break;
case SQL3_INTEGER:
case SQL3_SMALLINT:
exec sql get descriptor MYDESC value :INDEX :INTVAR=data;
printf("%d",INTVAR);
break;
case SQL3_FLOAT:
case SQL3_REAL:
exec sql get descriptor MYDESC value :INDEX :FLOATVAR=data;
printf("%f",FLOATVAR);
break;
case SQL3_DOUBLE_PRECISION:
exec sql get descriptor MYDESC value :INDEX :DOUBLEVAR=data;
printf("%f",DOUBLEVAR);
break;
case SQL3_DATE_TIME_TIMESTAMP:
case SQL3_INTERVAL:
case SQL3_CHARACTER:
case SQL3_CHARACTER_VARYING:
default:
exec sql get descriptor MYDESC value :INDEX :STRINGVAR=data;
printf("'%s'",STRINGVAR);
break;
}
putchar('|');
}
putchar('\n');
}
exec sql close MYCURS;
exec sql deallocate descriptor MYDESC;
if (dbgs != NULL)
fclose(dbgs);
return 0;
}
/* dynamic SQL test program
*
* $PostgreSQL: pgsql/src/interfaces/ecpg/test/dyntest2.pgc,v 1.7 2006/03/11 16:57:44 momjian Exp $
*/
#include <stdio.h>
#include <stdlib.h>
exec sql include sql3types;
exec sql include sqlca;
static void error(void)
{
printf("\n#%ld:%s\n",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exit(1);
}
int main(int argc,char **argv)
{
exec sql begin declare section;
int COUNT;
int INTVAR, BOOLVAR;
int INDEX;
int INDICATOR;
int TYPE,LENGTH,OCTET_LENGTH,PRECISION,SCALE,NULLABLE,RETURNED_OCTET_LENGTH;
int DATETIME_INTERVAL_CODE;
char NAME[120];
char STRINGVAR[1024];
float FLOATVAR;
double DOUBLEVAR;
char QUERY[1024];
exec sql end declare section;
int done=0;
FILE *dbgs;
exec sql var BOOLVAR is bool;
if ((dbgs = fopen("log", "w")) != NULL)
ECPGdebug(1, dbgs);
snprintf(QUERY,sizeof QUERY,"select * from %s",argc>1?argv[1]:"pg_tables");
exec sql whenever sqlerror do error();
exec sql allocate descriptor MYDESC;
exec sql connect to mm;
exec sql prepare MYQUERY from :QUERY;
exec sql declare MYCURS cursor for MYQUERY;
exec sql open MYCURS;
while (1)
{ exec sql fetch in MYCURS into sql descriptor MYDESC;
if (sqlca.sqlcode) break;
exec sql get descriptor MYDESC :COUNT = count;
if (!done)
{ printf("Count %d\n",COUNT);
done=1;
}
for (INDEX=1;INDEX<=COUNT;++INDEX)
{ exec sql get descriptor MYDESC value :INDEX
:TYPE = type,
:LENGTH = length, :OCTET_LENGTH=octet_length,
:RETURNED_OCTET_LENGTH=returned_octet_length,
:PRECISION = precision, :SCALE=scale,
:NULLABLE=nullable, :NAME=name,
:INDICATOR=indicator;
printf("%2d\t%s (type: %d length: %d precision: %d scale: %d\n"
"\toctet_length: %d returned_octet_length: %d nullable: %d)\n\t= "
,INDEX,NAME,TYPE,LENGTH,PRECISION,SCALE
,OCTET_LENGTH,RETURNED_OCTET_LENGTH,NULLABLE);
if (INDICATOR==-1) printf("NULL\n");
else switch (TYPE)
{
case SQL3_BOOLEAN:
exec sql get descriptor MYDESC value :INDEX :BOOLVAR=data;
printf("%s\n",BOOLVAR ? "true":"false");
break;
case SQL3_NUMERIC:
case SQL3_DECIMAL:
if (SCALE==0)
{ exec sql get descriptor MYDESC value :INDEX :INTVAR=data;
printf("%d\n",INTVAR);
}
else
{ exec sql get descriptor MYDESC value :INDEX :FLOATVAR=data;
printf("%.*f\n",SCALE,FLOATVAR);
}
break;
case SQL3_INTEGER:
case SQL3_SMALLINT:
exec sql get descriptor MYDESC value :INDEX :INTVAR=data;
printf("%d\n",INTVAR);
break;
case SQL3_FLOAT:
case SQL3_REAL:
exec sql get descriptor MYDESC value :INDEX :FLOATVAR=data;
printf("%.*f\n",PRECISION,FLOATVAR);
break;
case SQL3_DOUBLE_PRECISION:
exec sql get descriptor MYDESC value :INDEX :DOUBLEVAR=data;
printf("%.*f\n",PRECISION,DOUBLEVAR);
break;
case SQL3_DATE_TIME_TIMESTAMP:
exec sql get descriptor MYDESC value :INDEX
:DATETIME_INTERVAL_CODE=datetime_interval_code,
:STRINGVAR=data;
printf("%d \"%s\"\n",DATETIME_INTERVAL_CODE,STRINGVAR);
break;
case SQL3_INTERVAL:
exec sql get descriptor MYDESC value :INDEX :STRINGVAR=data;
printf("\"%s\"\n",STRINGVAR);
break;
case SQL3_CHARACTER:
case SQL3_CHARACTER_VARYING:
exec sql get descriptor MYDESC value :INDEX :STRINGVAR=data;
printf("\"%s\"\n",STRINGVAR);
break;
default:
exec sql get descriptor MYDESC value :INDEX :STRINGVAR=data;
printf("<\"%s\">\n",STRINGVAR);
break;
}
}
}
exec sql close MYCURS;
exec sql deallocate descriptor MYDESC;
if (dbgs != NULL)
fclose(dbgs);
return 0;
}
/* $PostgreSQL: pgsql/src/interfaces/ecpg/test/num_test.pgc,v 1.11 2006/03/11 04:38:40 momjian Exp $ */
#include <stdio.h>
#include <pgtypes_numeric.h>
#include <decimal.h>
int
main(void)
{
char *text="error\n";
numeric *value1, *value2, *res;
exec sql begin declare section;
numeric(14,7) des = {0, 0, 0, 0, 0, NULL, NULL} ;
exec sql end declare section;
double d;
FILE *dbgs;
if ((dbgs = fopen("log", "w")) != NULL)
ECPGdebug(1, dbgs);
exec sql whenever sqlerror do sqlprint();
exec sql connect to mm;
exec sql create table test (text char(5), num numeric(14,7));
value1 = PGTYPESnumeric_new();
PGTYPESnumeric_from_int(1407, value1);
text = PGTYPESnumeric_to_asc(value1, -1);
printf("long = %s\n", text);
value1 = PGTYPESnumeric_from_asc("2369.7", NULL);
value2 = PGTYPESnumeric_from_asc("10.0", NULL);
res = PGTYPESnumeric_new();
PGTYPESnumeric_add(value1, value2, res);
text = PGTYPESnumeric_to_asc(res, -1);
printf("add = %s\n", text);
PGTYPESnumeric_sub(res, value2, res);
text = PGTYPESnumeric_to_asc(res, -1);
printf("sub = %s\n", text);
PGTYPESnumeric_copy(res, &des);
exec sql insert into test (text, num) values ('test', :des);
value2 = PGTYPESnumeric_from_asc("2369.7", NULL);
PGTYPESnumeric_mul(value1, value2, res);
exec sql select num into :des from test where text = 'test';
PGTYPESnumeric_mul(res, &des, res);
text = PGTYPESnumeric_to_asc(res, -1);
printf("mul = %s\n", text);
value2 = PGTYPESnumeric_from_asc("10000", NULL);
PGTYPESnumeric_div(res, value2, res);
text = PGTYPESnumeric_to_asc(res, -1);
PGTYPESnumeric_to_double(res, &d);
printf("div = %s %e\n", text, d);
exec sql rollback;
exec sql disconnect;
if (dbgs != NULL)
fclose(dbgs);
return (0);
}
/* $PostgreSQL: pgsql/src/interfaces/ecpg/test/perftest.pgc,v 1.11 2006/03/11 04:38:40 momjian Exp $ */
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
exec sql include sqlca;
exec sql whenever sqlerror sqlprint;
exec sql whenever not found sqlprint;
static void
print_result(long sec, long usec, char *text)
{
if (usec < 0)
{
sec--;
usec+=1000000;
}
printf("%ld seconds and %ld microseconds for test %s\n", sec, usec, text);
exec sql vacuum;
sleep(1);
}
int
main (void)
{
exec sql begin declare section;
long i;
exec sql end declare section;
struct timeval tvs, tve;
exec sql connect to mm;
exec sql create table perftest1(number int4, ascii char(16));
exec sql create unique index number1 on perftest1(number);
exec sql create table perftest2(number int4, next_number int4);
exec sql create unique index number2 on perftest2(number);
exec sql commit;
exec sql set autocommit to on;
exec sql begin transaction;
gettimeofday(&tvs, NULL);
for (i = 0;i < 1407; i++)
{
exec sql begin declare section;
char text[16];
exec sql end declare section;
sprintf(text, "%ld", i);
exec sql insert into perftest1(number, ascii) values (:i, :text);
exec sql insert into perftest2(number, next_number) values (:i, :i+1);
}
exec sql commit;
gettimeofday(&tve, NULL);
print_result(tve.tv_sec - tvs.tv_sec, tve.tv_usec - tvs.tv_usec, "insert");
exec sql begin transaction;
gettimeofday(&tvs, NULL);
for (i = 0;i < 1407; i++)
{
exec sql begin declare section;
char text[16];
exec sql end declare section;
exec sql select ascii into :text from perftest1 where number = :i;
}
exec sql commit;
gettimeofday(&tve, NULL);
print_result(tve.tv_sec - tvs.tv_sec, tve.tv_usec - tvs.tv_usec, "selection&projection");
exec sql begin transaction;
gettimeofday(&tvs, NULL);
for (i = 0;i < 1407; i++)
{
exec sql begin declare section;
char text[16];
exec sql end declare section;
exec sql select perftest1.ascii into :text from perftest1, perftest2 where perftest1.number = perftest2.number and perftest2.number = :i;
}
exec sql commit;
gettimeofday(&tve, NULL);
print_result(tve.tv_sec - tvs.tv_sec, tve.tv_usec - tvs.tv_usec, "join");
exec sql begin transaction;
gettimeofday(&tvs, NULL);
exec sql update perftest2 set next_number = next_number + 1;
exec sql commit;
gettimeofday(&tve, NULL);
print_result(tve.tv_sec - tvs.tv_sec, tve.tv_usec - tvs.tv_usec, "update");
exec sql begin transaction;
gettimeofday(&tvs, NULL);
exec sql delete from perftest2;
exec sql commit;
gettimeofday(&tve, NULL);
print_result(tve.tv_sec - tvs.tv_sec, tve.tv_usec - tvs.tv_usec, "delete");
exec sql set autocommit = off;
exec sql drop index number2;
exec sql drop table perftest2;
exec sql drop index number1;
exec sql drop table perftest1;
exec sql commit;
exec sql disconnect;
return (0);
}
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
/* just a test comment */ exec sql whenever sqlerror do PrintAndStop(msg);
exec sql whenever sqlwarning do warn();
static void PrintAndStop(char *msg)
{
fprintf(stderr, "Error in statement '%s':\n", msg);
sqlprint();
exit(-1);
}
static void warn(void)
{
fprintf(stderr, "Warning: At least one column was truncated\n");
}
/* comment */
exec sql define AMOUNT 6;
exec sql define NAMELEN 8;
exec sql type intarray is int[AMOUNT];
typedef int intarray[AMOUNT];
int
main(void)
{
exec sql begin declare section;
exec sql ifdef NAMELEN;
typedef char string[NAMELEN];
intarray amount;
int increment=100;
char name[AMOUNT][NAMELEN];
char letter[AMOUNT][1];
struct name_letter_struct
{
char name[NAMELEN];
int amount;
char letter;
} name_letter[AMOUNT];
#if 0
int not_used;
#endif
exec sql endif;
struct ind_struct
{
short a;
short b;
short c;
} ind[AMOUNT];
char command[128];
char *connection="pm";
int how_many;
char *user="postgres";
exec sql end declare section;
exec sql var name is string[AMOUNT];
char msg[128];
FILE *dbgs;
int i,j;
if ((dbgs = fopen("log", "w")) != NULL)
ECPGdebug(1, dbgs);
strcpy(msg, "connect");
exec sql connect to mm as main;
strcpy(msg, "connect");
exec sql connect to pm user :user;
strcpy(msg, "create");
exec sql at main create table "Test" (name char(NAMELEN), amount int, letter char(1));
exec sql create table "Test" (name char(NAMELEN), amount int, letter char(1));
strcpy(msg, "commit");
exec sql at main commit;
exec sql commit;
strcpy(msg, "set connection");
exec sql set connection to main;
strcpy(msg, "execute insert 1");
sprintf(command, "insert into \"Test\" (name, amount, letter) values ('db: ''mm''', 1, 'f')");
exec sql execute immediate :command;
printf("New tuple got OID = %ld\n", sqlca.sqlerrd[1]);
sprintf(command, "insert into \"Test\" (name, amount, letter) values ('db: ''mm''', 2, 't')");
exec sql execute immediate :command;
strcpy(msg, "execute insert 2");
sprintf(command, "insert into \"Test\" (name, amount, letter) values ('db: ''pm''', 1, 'f')");
exec sql at pm execute immediate :command;
strcpy(msg, "execute insert 3");
sprintf(command, "insert into \"Test\" (name, amount, letter) select name, amount+10, letter from \"Test\"");
exec sql execute immediate :command;
printf("Inserted %ld tuples via execute immediate\n", sqlca.sqlerrd[2]);
strcpy(msg, "execute insert 4");
sprintf(command, "insert into \"Test\" (name, amount, letter) select name, amount+?, letter from \"Test\"");
exec sql prepare I from :command;
exec sql at pm execute I using :increment;
printf("Inserted %ld tuples via prepared execute\n", sqlca.sqlerrd[2]);
strcpy(msg, "commit");
exec sql commit;
/* Start automatic transactioning for connection pm. */
exec sql at pm set autocommit to on;
exec sql at pm begin transaction;
strcpy(msg, "select");
exec sql select * into :name, :amount, :letter from "Test";
printf("Database: mm\n");
for (i=0, how_many=j=sqlca.sqlerrd[2]; i<j; i++)
{
exec sql begin declare section;
char n[8], l = letter[i][0];
int a = amount[i];
exec sql end declare section;
strncpy(n, name[i], NAMELEN);
printf("name[%d]=%8.8s\tamount[%d]=%d\tletter[%d]=%c\n", i, n, i, a, i, l);
amount[i]+=1000;
strcpy(msg, "insert");
exec sql at pm insert into "Test" (name, amount, letter) values (:n, :amount[i], :l);
}
strcpy(msg, "commit");
exec sql at pm commit;
sprintf (command, "select * from \"Test\"");
exec sql prepare F from :command;
exec sql declare CUR cursor for F;
strcpy(msg, "open");
exec sql open CUR;
strcpy(msg, "fetch");
exec sql fetch :how_many in CUR into :name, :amount, :letter;
printf("Database: mm\n");
for (i=0, j=sqlca.sqlerrd[2]; i<j; i++)
{
exec sql begin declare section;
char n[8], l = letter[i][0];
int a = amount[i];
exec sql end declare section;
strncpy(n, name[i], 8);
printf("name[%d]=%8.8s\tamount[%d]=%d\tletter[%d]=%c\n", i, n, i, a, i, l);
}
exec sql close CUR;
strcpy(msg, "select");
exec sql at :connection select name, amount, letter into :name, :amount, :letter from "Test";
printf("Database: %s\n", connection);
for (i=0, j=sqlca.sqlerrd[2]; i<j; i++)
printf("name[%d]=%8.8s\tamount[%d]=%d\tletter[%d]=%c\n", i, name[i], i, amount[i],i, letter[i][0]);
strcpy(msg, "commit");
exec sql commit;
strcpy(msg, "select");
exec sql at pm select name, amount, letter into :name_letter:ind from "Test";
printf("Database: pm\n");
for (i=0, j=sqlca.sqlerrd[2]; i<j; i++)
printf("name[%d]=%8.8s\tamount[%d]=%d\tletter[%d]=%c\n", i, name_letter[i].name, i, name_letter[i].amount,i, name_letter[i].letter);
name_letter[4].amount=1407;
strcpy(msg, "insert");
exec sql insert into "Test" (name, amount, letter) values (:name_letter[4]);
strcpy(msg, "select");
exec sql select name, amount, letter into :name_letter[2] from "Test" where amount = 1407;
printf("Database: mm\n");
printf("name[2]=%8.8s\tamount[2]=%d\tletter[2]=%c\n", name_letter[2].name, name_letter[2].amount, name_letter[2].letter);
/* Start automatic transactioning for connection main. */
exec sql set autocommit to on;
strcpy(msg, "drop");
exec sql drop table "Test";
exec sql at pm drop table "Test";
strcpy(msg, "disconnect");
exec sql disconnect main;
exec sql disconnect pm;
if (dbgs != NULL)
fclose(dbgs);
return (0);
}
#include <stdlib.h>
#include <string.h>
exec sql include header_test;
exec sql type c is char reference;
typedef char* c;
exec sql type ind is union { int integer; short smallint; };
typedef union { int integer; short smallint; } ind;
#define BUFFERSIZ 8
exec sql type str is varchar[BUFFERSIZ];
exec sql declare cur cursor for
select name, born, age, married, children from meskes;
int
main (void)
{
exec sql struct birthinfo { long born; short age; };
exec sql begin declare section;
struct personal_struct { str name;
struct birthinfo birth;
} personal, *p;
struct personal_indicator { int ind_name;
struct birthinfo ind_birth;
} ind_personal, *i;
ind ind_children;
char *query="select name, born, age, married, children from meskes where name = :var1";
exec sql end declare section;
exec sql char *married = NULL;
exec sql float ind_married;
exec sql ind children;
exec sql var ind_married is long;
char msg[128];
FILE *dbgs;
if ((dbgs = fopen("log", "w")) != NULL)
ECPGdebug(1, dbgs);
strcpy(msg, "connect");
exec sql connect to tcp:postgresql://127.0.0.1:5432/mm;
strcpy(msg, "create");
exec sql create table meskes(name char(8), born integer, age smallint, married date, children integer);
strcpy(msg, "insert");
exec sql insert into meskes(name, married, children) values ('Petra', '19900404', 3);
exec sql insert into meskes(name, born, age, married, children) values ('Michael', 19660117, 35, '19900404', 3);
exec sql insert into meskes(name, born, age) values ('Carsten', 19910103,10);
exec sql insert into meskes(name, born, age) values ('Marc', 19930907, 8);
exec sql insert into meskes(name, born, age) values ('Chris', 19970923, 4);
strcpy(msg, "commit");
exec sql commit;
strcpy(msg, "open");
exec sql open cur;
exec sql whenever not found do break;
p=&personal;
i=&ind_personal;
memset(i, 0, sizeof(ind_personal));
while (1) {
strcpy(msg, "fetch");
exec sql fetch cur into :p:i, :married:ind_married, :children.integer:ind_children.smallint;
printf("%8.8s", personal.name.arr);
if (i->ind_birth.born >= 0)
printf(", born %ld", personal.birth.born);
if (i->ind_birth.age >= 0)
printf(", age = %d", personal.birth.age);
if ((long)ind_married >= 0)
printf(", married %s", married);
if (ind_children.smallint >= 0)
printf(", children = %d", children.integer);
putchar('\n');
free(married);
married = NULL;
}
strcpy(msg, "close");
exec sql close cur;
/* and now a same query with prepare */
exec sql prepare MM from :query;
exec sql declare prep cursor for MM;
strcpy(msg, "open");
exec sql open prep using 'Petra';
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.integer:ind_children.smallint;
printf("%8.8s", personal.name.arr);
if (ind_personal.ind_birth.born >= 0)
printf(", born %ld", personal.birth.born);
if (ind_personal.ind_birth.age >= 0)
printf(", age = %d", personal.birth.age);
if ((long)ind_married >= 0)
printf(", married %s", married);
if (ind_children.smallint >= 0)
printf(", children = %d", children.integer);
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;
if (dbgs != NULL)
fclose(dbgs);
return (0);
}
/****************************************************************************/
/* Test comment */
/*--------------------------------------------------------------------------*/
exec sql include header_test;
exec sql type str is varchar[10];
#include <stdlib.h>
#include <string.h>
int
main (void)
{
exec sql begin declare section;
typedef struct { long born; short age; } birthinfo;
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, movevalue = 2;
int ind_children;
str *married = NULL;
char *wifesname="Petra";
char *query="select * from meskes where name = ?";
exec sql end declare section;
exec sql declare cur cursor for
select name, born, age, married, children from meskes;
char msg[128];
FILE *dbgs;
if ((dbgs = fopen("log", "w")) != NULL)
ECPGdebug(1, dbgs);
strcpy(msg, "connect");
exec sql connect to unix:postgresql://localhost:5432/mm;
strcpy(msg, "create");
exec sql create table meskes(name char(8), born integer, age smallint, married date, children integer);
strcpy(msg, "insert");
exec sql insert into meskes(name, married, children) values (:wifesname, '19900404', 3);
exec sql insert into meskes(name, born, age, married, children) values ('Michael', 19660117, 35, '19900404', 3);
exec sql insert into meskes(name, born, age) values ('Carsten', 19910103, 10);
exec sql insert into meskes(name, born, age) values ('Marc', 19930907, 8);
exec sql insert into meskes(name, born, age) values ('Chris', 19970923, 4);
strcpy(msg, "commit");
exec sql commit;
strcpy(msg, "open");
exec sql open cur;
strcpy(msg, "move");
exec sql move :movevalue in cur;
exec sql whenever not found do break;
while (1) {
strcpy(msg, "fetch");
exec sql fetch from cur 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 %ld", 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;
/* and now a query with prepare */
exec sql prepare MM from :query;
exec sql declare prep cursor for MM;
strcpy(msg, "open");
exec sql open prep using :wifesname;
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 %ld", 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;
if (dbgs != NULL)
fclose(dbgs);
return (0);
}
#include <locale.h>
#include <string.h>
#include <stdlib.h>
exec sql whenever sqlerror sqlprint;
exec sql include sqlca;
EXEC SQL type errtype is enum
{
OK = 0,
ERR = 1,
WARN = 2
};
int
main (void)
{
EXEC SQL BEGIN DECLARE SECTION;
struct
{
errtype e :2;
int code :14;
} error = {1, 147};
int i = 1;
int *did = &i;
int a[10] = {9,8,7,6,5,4,3,2,1,0};
char text[25] = "klmnopqrst";
char *t = (char *)malloc(10);
double f;
bool b = true;
varchar database[3];
EXEC SQL END DECLARE SECTION;
FILE *dbgs;
strcpy(t, "0123456789");
setlocale(LC_ALL, "de_DE");
if ((dbgs = fopen("log", "w")) != NULL)
ECPGdebug(1, dbgs);
strcpy(database.arr, "mm");
EXEC SQL CONNECT TO :database;
EXEC SQL SET AUTOCOMMIT = ON;
EXEC SQL BEGIN WORK;
EXEC SQL CREATE TABLE test (f float, i int, a int[10], text char(10), b bool, t int, err int);
EXEC SQL INSERT INTO test(f,i,a,text,b,t,err) VALUES(404.90,3,'{0,1,2,3,4,5,6,7,8,9}','abcdefghij','f',0,0);
EXEC SQL INSERT INTO test(f,i,a,text,b,t,err) VALUES(140787.0,2,:a,:text,'t',2,14);
EXEC SQL IFDEF BIT_FIELD_IS_NOT_ACCESSIBLE;
EXEC SQL INSERT INTO test(f,i,a,text,b,t,err) VALUES(14.07,:did,:a,:t,:b,:error);
EXEC SQL ELSE;
EXEC SQL INSERT INTO test(f,i,a,text,b,t,err) VALUES(14.07,:did,:a,:t,:b,1,147);
error.code=0;
EXEC SQL ENDIF;
EXEC SQL COMMIT;
EXEC SQL BEGIN WORK;
EXEC SQL SELECT f,text,b
INTO :f,:text,:b
FROM test
WHERE i = 1;
printf("Found f=%f text=%10.10s b=%d\n", f, text, b);
f=140787;
EXEC SQL SELECT a,text
INTO :a,:t
FROM test
WHERE f = :f;
for (i = 0; i < 10; i++)
printf("Found a[%d] = %d\n", i, a[i]);
printf("Found text=%10.10s\n", t);
EXEC SQL SELECT a
INTO :text
FROM test
WHERE f = :f;
printf("Found text=%s\n", text);
EXEC SQL DROP TABLE test;
EXEC SQL COMMIT;
EXEC SQL DISCONNECT;
if (dbgs != NULL)
fclose(dbgs);
return (0);
}
#include <stdio.h>
#include <stdlib.h>
EXEC SQL typedef long mmInteger;
EXEC SQL typedef char mmChar;
EXEC SQL typedef short mmSmallInt;
EXEC SQL BEGIN DECLARE SECTION;
struct TBempl
{
mmInteger idnum;
mmChar name[21];
mmSmallInt accs;
mmChar byte[20];
};
EXEC SQL END DECLARE SECTION;
int
main (void)
{
EXEC SQL BEGIN DECLARE SECTION;
struct TBempl empl;
char *data = "\\001\\155\\000\\212";
union
{
mmSmallInt accs;
char t[2];
} a;
EXEC SQL END DECLARE SECTION;
FILE *dbgs;
int i;
if ((dbgs = fopen ("log", "w")) != NULL)
ECPGdebug (1, dbgs);
empl.idnum = 1;
EXEC SQL connect to mm;
if (sqlca.sqlcode)
{
printf ("connect error = %ld\n", sqlca.sqlcode);
exit (sqlca.sqlcode);
}
EXEC SQL create table empl
(idnum integer, name char (20), accs smallint, byte bytea);
if (sqlca.sqlcode)
{
printf ("create error = %ld\n", sqlca.sqlcode);
exit (sqlca.sqlcode);
}
EXEC SQL insert into empl values (1, 'first user', 320,:data);
if (sqlca.sqlcode)
{
printf ("insert error = %ld\n", sqlca.sqlcode);
exit (sqlca.sqlcode);
}
EXEC SQL select name, accs, byte
into :empl.name, :empl.accs, :empl.byte
from empl
where idnum =:empl.idnum;
if (sqlca.sqlcode)
{
printf ("select error = %ld\n", sqlca.sqlcode);
exit (sqlca.sqlcode);
}
printf ("name=%s, accs=%d byte=%s\n", empl.name, empl.accs, empl.byte);
EXEC SQL DECLARE C CURSOR FOR select name, accs, byte from empl where idnum =:empl.idnum;
EXEC SQL OPEN C;
EXEC SQL FETCH C INTO:empl.name,:empl.accs,:empl.byte;
if (sqlca.sqlcode)
{
printf ("fetch error = %ld\n", sqlca.sqlcode);
exit (sqlca.sqlcode);
}
printf ("name=%s, accs=%d byte=%s\n", empl.name, empl.accs, empl.byte);
memset(empl.name, 0, 21L);
memset(empl.byte, '#', 20L);
EXEC SQL DECLARE B BINARY CURSOR FOR select name, accs, byte from empl where idnum =:empl.idnum;
EXEC SQL OPEN B;
EXEC SQL FETCH B INTO :empl.name,:a.accs,:empl.byte;
if (sqlca.sqlcode)
{
printf ("fetch error = %ld\n", sqlca.sqlcode);
exit (sqlca.sqlcode);
}
EXEC SQL CLOSE B;
i=a.t[0];
a.t[0]=a.t[1];
a.t[1]=i;
printf ("name=%s, accs=%d byte=", empl.name, a.accs);
for (i=0; i<20; i++)
{
if (empl.byte[i] == '#')
break;
printf("(%o)", (unsigned char)empl.byte[i]);
}
printf("\n");
EXEC SQL disconnect;
fclose (dbgs);
exit (0);
}
// $PostgreSQL: pgsql/src/interfaces/ecpg/test/test_code100.pgc,v 1.3 2003/11/29 22:41:18 pgsql Exp $
exec sql include sqlca;
#include <stdio.h>
int main(int argc, char **argv)
{ exec sql begin declare section;
int index;
exec sql end declare section;
// ECPGdebug(1,stdout);
exec sql connect to mm;
if (sqlca.sqlcode) printf("%ld:%s\n",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql create table test (
"index" numeric(3) primary key,
"payload" int4 NOT NULL);
if (sqlca.sqlcode) printf("%ld:%s\n",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql commit work;
if (sqlca.sqlcode) printf("%ld:%s\n",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
for (index=0;index<10;++index)
{ exec sql insert into test
(payload, index)
values (0, :index);
if (sqlca.sqlcode) printf("%ld:%s\n",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
}
exec sql commit work;
if (sqlca.sqlcode) printf("%ld:%s\n",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql update test
set payload=payload+1 where index=-1;
if (sqlca.sqlcode!=100) printf("%ld:%s\n",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql delete from test where index=-1;
if (sqlca.sqlcode!=100) printf("%ld:%s\n",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql insert into test (select * from test where index=-1);
if (sqlca.sqlcode!=100) printf("%ld:%s\n",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql drop table test;
if (sqlca.sqlcode) printf("%ld:%s\n",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql commit work;
if (sqlca.sqlcode) printf("%ld:%s\n",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql disconnect;
if (sqlca.sqlcode) printf("%ld:%s\n",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
return 0;
}
EXEC SQL WHENEVER SQLERROR SQLPRINT;
int
main(void)
{
EXEC SQL BEGIN DECLARE SECTION;
char *stmt1 = "INSERT INTO test1 VALUES (?, ?)";
char *stmt2 = "SELECT * from test1 where a = ? and b = ?";
char *stmt3 = "SELECT * from test1 where a = ?";
int val1 = 1;
char val2[] = "one", val2output[] = "AAA";
int val1output = 2, val2i = 0;
int val2null = -1;
EXEC SQL END DECLARE SECTION;
FILE *dbgs;
if ((dbgs = fopen("log", "w")) != NULL)
ECPGdebug(1, dbgs);
EXEC SQL ALLOCATE DESCRIPTOR indesc;
EXEC SQL ALLOCATE DESCRIPTOR outdesc;
EXEC SQL SET DESCRIPTOR indesc VALUE 1 DATA = :val1;
EXEC SQL SET DESCRIPTOR indesc VALUE 2 INDICATOR = :val2i, DATA = :val2;
EXEC SQL CONNECT TO mm;
EXEC SQL CREATE TABLE test1 (a int, b text);
EXEC SQL PREPARE foo1 FROM :stmt1;
EXEC SQL PREPARE foo2 FROM :stmt2;
EXEC SQL PREPARE foo3 FROM :stmt3;
EXEC SQL EXECUTE foo1 USING DESCRIPTOR indesc;
EXEC SQL SET DESCRIPTOR indesc VALUE 1 DATA = 2;
EXEC SQL SET DESCRIPTOR indesc VALUE 2 INDICATOR = :val2null, DATA = :val2;
EXEC SQL EXECUTE foo1 USING DESCRIPTOR indesc;
EXEC SQL SET DESCRIPTOR indesc VALUE 1 DATA = :val1;
EXEC SQL SET DESCRIPTOR indesc VALUE 2 INDICATOR = :val2i, DATA = :val2;
EXEC SQL EXECUTE foo2 USING DESCRIPTOR indesc INTO DESCRIPTOR outdesc;
EXEC SQL GET DESCRIPTOR outdesc VALUE 1 :val2output = DATA;
printf("output = %s\n", val2output);
EXEC SQL DECLARE c1 CURSOR FOR foo2;
EXEC SQL OPEN c1 USING DESCRIPTOR indesc;
EXEC SQL FETCH next FROM c1 INTO :val1output, :val2output;
printf("val1=%d val2=%s\n", val1output, val2output);
EXEC SQL CLOSE c1;
EXEC SQL SET DESCRIPTOR indesc COUNT = 1;
EXEC SQL SET DESCRIPTOR indesc VALUE 1 DATA = 2;
EXEC SQL DECLARE c2 CURSOR FOR foo3;
EXEC SQL OPEN c2 USING DESCRIPTOR indesc;
EXEC SQL FETCH next FROM c2 INTO :val1output, :val2output :val2i;
printf("val1=%d val2=%s\n", val1output, val2i ? "null" : val2output);
EXEC SQL CLOSE c2;
EXEC SQL SELECT * INTO :val1output, :val2output :val2i FROM test1 where a = 2;
printf("val1=%d val2=%s\n", val1output, val2i ? "null" : val2output);
EXEC SQL DROP TABLE test1;
EXEC SQL DISCONNECT;
EXEC SQL DEALLOCATE DESCRIPTOR indesc;
EXEC SQL DEALLOCATE DESCRIPTOR outdesc;
if (dbgs != NULL)
fclose(dbgs);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[]) {
FILE *dbgs;
if ((dbgs = fopen("log", "w")) != NULL)
ECPGdebug(1, dbgs);
EXEC SQL CONNECT TO mm;
EXEC SQL SET AUTOCOMMIT TO ON;
EXEC SQL WHENEVER SQLWARNING SQLPRINT;
EXEC SQL WHENEVER SQLERROR SQLPRINT;
EXEC SQL CREATE TABLE My_Table ( Item1 int, Item2 text );
EXEC SQL CREATE FUNCTION My_Table_Check() RETURNS trigger
AS $test$
BEGIN
RAISE NOTICE 'TG_NAME=%, TG WHEN=%', TG_NAME, TG_WHEN;
RETURN NEW;
END; $test$
LANGUAGE plpgsql;
EXEC SQL CREATE TRIGGER My_Table_Check_Trigger
BEFORE INSERT
ON My_Table
FOR EACH ROW
EXECUTE PROCEDURE My_Table_Check();
EXEC SQL INSERT INTO My_Table VALUES (1234, 'Some random text');
EXEC SQL INSERT INTO My_Table VALUES (5678, 'The Quick Brown');
EXEC SQL DROP TRIGGER My_Table_Check_Trigger ON My_Table;
EXEC SQL DROP FUNCTION My_Table_Check();
EXEC SQL DROP TABLE My_Table;
EXEC SQL DISCONNECT ALL;
if (dbgs != NULL)
fclose(dbgs);
return 0;
}
#include "sqltypes.h"
static void openit(void);
int main(void)
{
$int i = 14;
$decimal j, m, n;
FILE *dbgs;
if ((dbgs = fopen("log", "w")) != NULL)
ECPGdebug(1, dbgs);
$connect to mm;
$create table test(i int primary key, j int);
rsetnull(CDECIMALTYPE, (char *)&j);
$insert into test (i, j) values (7, :j);
$commit;
$insert into test (i, j) values (7, 2);
printf("INSERT: %ld=%s\n", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
if (sqlca.sqlcode != 0) $rollback;
$insert into test (i, j) values (:i, 1);
$commit;
$select i from test where j=(select j from test);
printf("SELECT: %ld=%s\n", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
if (sqlca.sqlcode != 0) $rollback;
$declare c cursor for select * from test where i <= :i;
openit();
deccvint(0, &j);
while (1)
{
$fetch forward c into :i, :j;
if (sqlca.sqlcode == 100) break;
else if (sqlca.sqlcode != 0) printf ("Error: %ld\n", sqlca.sqlcode);
if (risnull(CDECIMALTYPE, (char *)&j))
printf("%d NULL\n", i);
else
{
int a;
dectoint(&j, &a);
printf("%d %d\n", i, a);
}
}
deccvint(7, &j);
deccvint(14, &m);
decadd(&j, &m, &n);
$delete from test where i=:n;
printf("DELETE: %ld\n", sqlca.sqlcode);
$select 1 from test where i=14;
printf("Exists: %ld\n", sqlca.sqlcode);
$select 1 from test where i=147;
printf("Does not exist: %ld\n", sqlca.sqlcode);
$commit;
$drop table test;
$commit;
$close database;
return 0;
}
static void openit(void)
{
$open c;
}
#include <stdio.h>
#include <stdlib.h>
#include "sqltypes.h"
EXEC SQL include sqlca.h;
/* Check SQLCODE, and produce a "standard error" if it's wrong! */
static void sql_check(char *fn, char *caller, int ignore)
{
char errorstring[255];
if (SQLCODE == ignore)
return;
else
{
if (SQLCODE != 0)
{
sprintf(errorstring, "**SQL error %ld doing '%s' in function '%s'. [%s]",
SQLCODE, caller, fn, sqlca.sqlerrm.sqlerrmc);
fprintf(stderr, "%s", errorstring);
printf("%s\n", errorstring);
/* attempt a ROLLBACK */
EXEC SQL rollback;
if (SQLCODE == 0)
{
sprintf(errorstring, "Rollback successful.\n");
} else {
sprintf(errorstring, "Rollback failed with code %ld.\n", SQLCODE);
}
fprintf(stderr, "%s", errorstring);
printf("%s\n", errorstring);
exit(1);
}
}
}
int main(void)
{
EXEC SQL BEGIN DECLARE SECTION;
int c;
timestamp d;
timestamp maxd;
char dbname[30];
EXEC SQL END DECLARE SECTION;
EXEC SQL whenever sqlerror sqlprint;
strcpy(dbname, "mm");
EXEC SQL connect to :dbname;
sql_check("main", "connect", 0);
EXEC SQL create table history (customerid integer, timestamp timestamp without time zone, action_taken char(5), narrative varchar(100));
sql_check("main", "create", 0);
EXEC SQL insert into history
(customerid, timestamp, action_taken, narrative)
values(1, now(), 'test', 'test');
sql_check("main", "insert", 0);
EXEC SQL select max(timestamp)
into :maxd
from history;
sql_check("main", "select max", 100);
if (risnull(CDTIMETYPE, (char *) &maxd))
{
printf("Nothing on the history table\n\n");
exit(0);
}
EXEC SQL select customerid, timestamp
into :c, :d
from history
where timestamp = :maxd
limit 1;
sql_check("main", "select", 0);
printf("Read in customer %d\n", c);
/* Adding 1 to d adds 1 second. So:
60 1 minute
3600 1 hour
86400 1 day */
d=d+86400;
c++;
EXEC SQL insert into history
(customerid, timestamp, action_taken, narrative)
values(:c, :d, 'test', 'test');
sql_check("main", "update", 0);
EXEC SQL commit;
EXEC SQL drop table history;
sql_check("main", "drop", 0);
EXEC SQL commit;
EXEC SQL disconnect;
sql_check("main", "disconnect", 0);
printf("All OK!\n");
exit(0);
/*
Table "public.history"
Column | Type | Modifiers
--------------+-----------------------------+-----------
customerid | integer | not null
timestamp | timestamp without time zone | not null
action_taken | character(5) | not null
narrative | character varying(100) |
*/
}
exec sql include sqlca;
int fa(void) { return 2; }
int fb(int x) { return x; }
int fc(const char *x) { return *x; }
int fd(const char *x,int i) { return (*x)*i; }
enum e { ENUM0, ENUM1 };
int fe(enum e x) { return (int)x; }
struct sa { int member; };
void sqlmeldung(char *meldung, short trans)
{
}
exec sql define NONO 0;
#define YES 1
#ifdef _cplusplus
namespace N
{ static const int i=2;
};
#endif
int main(void)
{ struct sa x,*y=&x;
exec sql begin declare section;
int a=(int)2;
int b=2+2;
int b2=(14*7);
int d=x.member;
int g=fb(2);
int i=3^1;
int j=1?1:2;
int e=y->member;
int c=10>>2;
bool h=2||1;
long iay /* = 1L */ ;
long long iax /* = 40000000000LL */ ;
exec sql end declare section;
int f=fa();
#ifdef _cplusplus
exec sql begin declare section;
int k=N::i; /* compile error */
exec sql end declare section;
#endif
exec sql whenever sqlerror do fa();
exec sql select now();
exec sql whenever sqlerror do fb(20);
exec sql select now();
exec sql whenever sqlerror do fc("50");
exec sql select now();
exec sql whenever sqlerror do fd("50",1);
exec sql select now();
exec sql whenever sqlerror do fe(ENUM0);
exec sql select now();
exec sql whenever sqlerror do sqlmeldung(NULL, NONO);
exec sql select now();
return 0;
}
// $PostgreSQL: pgsql/src/interfaces/ecpg/test/test_notice.pgc,v 1.4 2003/11/29 22:41:18 pgsql Exp $
exec sql include sqlca;
#include <stdio.h>
static void printwarning(void)
{
if (sqlca.sqlwarn[0]) printf("sqlca.sqlwarn: %c",sqlca.sqlwarn[0]);
else return;
if (sqlca.sqlwarn[1]) putchar('1');
if (sqlca.sqlwarn[2]) putchar('2');
putchar('\n');
}
int main(int argc, char **argv)
{
exec sql begin declare section;
int payload;
exec sql end declare section;
FILE *dbgs;
/* actually this will print 'sql error' if a warning occurs */
exec sql whenever sqlwarning do printwarning();
if ((dbgs = fopen("log", "w")) != NULL)
ECPGdebug(1, dbgs);
exec sql connect to mm;
if (sqlca.sqlcode) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql create table test (
"index" numeric(3) primary key,
"payload" int4 NOT NULL);
if (sqlca.sqlcode) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql commit work;
if (sqlca.sqlcode) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql begin work;
if (sqlca.sqlcode) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql begin work;
if (sqlca.sqlcode!=ECPG_WARNING_IN_TRANSACTION) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql commit;
if (sqlca.sqlcode) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql commit;
if (sqlca.sqlcode!=ECPG_WARNING_NO_TRANSACTION) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql rollback;
if (sqlca.sqlcode!=ECPG_WARNING_NO_TRANSACTION) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
sqlca.sqlcode=0;
exec sql declare x cursor for select * from test;
if (sqlca.sqlcode) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql open x;
if (sqlca.sqlcode) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql open x;
if (sqlca.sqlcode!=ECPG_WARNING_PORTAL_EXISTS) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql close x;
if (sqlca.sqlcode) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql close x;
if (sqlca.sqlcode!=ECPG_WARNING_UNKNOWN_PORTAL) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql update test set nonexistent=2;
if (sqlca.sqlcode!=ECPG_PGSQL) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql select payload into :payload from test where index=1;
if (sqlca.sqlcode!=ECPG_WARNING_QUERY_IGNORED) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql rollback;
if (sqlca.sqlcode) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
// this will raise a warning
exec sql drop table test;
if (sqlca.sqlcode) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql commit work;
if (sqlca.sqlcode) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
exec sql disconnect;
if (sqlca.sqlcode) printf("%d %ld:%s\n",__LINE__,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
if (dbgs != NULL)
fclose(dbgs);
return 0;
}
/*
* Thread test program
* by Philip Yarra & Lee Kindness.
*/
/* #define ECPGDEBUG */
#include <pthread.h>
#include <stdlib.h>
void *test_thread(void *arg);
EXEC SQL BEGIN DECLARE SECTION;
char *l_dbname;
EXEC SQL END DECLARE SECTION;
int nthreads = 2;
int iterations = 10;
int main(int argc, char *argv[])
{
#ifdef ECPGDEBUG
char debugfilename[] = "thread_test.log";
FILE *debugfile;
#endif
pthread_t *threads;
int n;
EXEC SQL BEGIN DECLARE SECTION;
int l_rows;
EXEC SQL END DECLARE SECTION;
/* parse command line arguments */
if( (argc < 2) || (argc > 4) )
{
fprintf(stderr, "Usage: %s dbname [threads] [iterations_per_thread]\n", argv[0]);
return( 1 );
}
l_dbname = argv[1];
if( argc >= 3 )
nthreads = atoi(argv[2]);
if( argc == 4 )
iterations = atoi(argv[3]);
/* open ECPG debug log? */
#ifdef ECPGDEBUG
debugfile = fopen(debugfilename, "w");
if( debugfile != NULL )
ECPGdebug(1, debugfile);
else
fprintf(stderr, "Cannot open ECPG debug log: %s\n", debugfilename);
#endif
/* setup test_thread table */
EXEC SQL CONNECT TO :l_dbname;
EXEC SQL DROP TABLE test_thread; /* DROP might fail */
EXEC SQL COMMIT;
EXEC SQL CREATE TABLE
test_thread(tstamp TIMESTAMP NOT NULL DEFAULT CAST(timeofday() AS TIMESTAMP),
thread TEXT NOT NULL,
iteration INTEGER NOT NULL,
PRIMARY KEY(thread, iteration));
EXEC SQL COMMIT;
EXEC SQL DISCONNECT;
/* create, and start, threads */
threads = calloc(nthreads, sizeof(pthread_t));
if( threads == NULL )
{
fprintf(stderr, "Cannot alloc memory\n");
return( 1 );
}
for( n = 0; n < nthreads; n++ )
{
pthread_create(&threads[n], NULL, test_thread, (void *) (n + 1));
}
/* wait for thread completion */
for( n = 0; n < nthreads; n++ )
{
pthread_join(threads[n], NULL);
}
free(threads);
/* and check results */
EXEC SQL CONNECT TO :l_dbname;
EXEC SQL SELECT COUNT(*) INTO :l_rows FROM test_thread;
EXEC SQL COMMIT;
EXEC SQL DISCONNECT;
if( l_rows == (nthreads * iterations) )
printf("\nSuccess.\n");
else
printf("\nERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
/* close ECPG debug log? */
#ifdef ECPGDEBUG
if( debugfile != NULL )
{
ECPGdebug(0, debugfile);
fclose(debugfile);
}
#endif
return( 0 );
}
void *test_thread(void *arg)
{
long threadnum = (long)arg;
EXEC SQL BEGIN DECLARE SECTION;
int l_i;
char l_connection[128];
EXEC SQL END DECLARE SECTION;
/* build up connection name, and connect to database */
snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
EXEC SQL WHENEVER sqlerror sqlprint;
EXEC SQL CONNECT TO :l_dbname AS :l_connection;
if( sqlca.sqlcode != 0 )
{
printf("%s: ERROR: cannot connect to database!\n", l_connection);
return( NULL );
}
EXEC SQL AT :l_connection BEGIN;
/* insert into test_thread table */
for( l_i = 1; l_i <= iterations; l_i++ )
{
printf("%s: inserting %d\n", l_connection, l_i);
EXEC SQL AT :l_connection INSERT INTO test_thread(thread, iteration) VALUES(:l_connection, :l_i);
if( sqlca.sqlcode == 0 )
printf("%s: insert done\n", l_connection);
else
printf("%s: ERROR: insert failed!\n", l_connection);
}
/* all done */
EXEC SQL AT :l_connection COMMIT;
EXEC SQL DISCONNECT :l_connection;
printf("%s: done!\n", l_connection);
return( NULL );
}
/*
* Thread test program
* by Lee Kindness.
*/
/* #define ECPGDEBUG */
#include <pthread.h>
#include <stdlib.h>
void *test_thread(void *arg);
EXEC SQL BEGIN DECLARE SECTION;
char *l_dbname;
EXEC SQL END DECLARE SECTION;
int nthreads = 2;
int iterations = 10;
int main(int argc, char *argv[])
{
#ifdef ECPGDEBUG
char debugfilename[] = "thread_test_implicit.log";
FILE *debugfile;
#endif
pthread_t *threads;
int n;
EXEC SQL BEGIN DECLARE SECTION;
int l_rows;
EXEC SQL END DECLARE SECTION;
/* parse command line arguments */
if( (argc < 2) || (argc > 4) )
{
fprintf(stderr, "Usage: %s dbname [threads] [iterations_per_thread]\n", argv[0]);
return( 1 );
}
l_dbname = argv[1];
if( argc >= 3 )
nthreads = atoi(argv[2]);
if( argc == 4 )
iterations = atoi(argv[3]);
/* open ECPG debug log? */
#ifdef ECPGDEBUG
debugfile = fopen(debugfilename, "w");
if( debugfile != NULL )
ECPGdebug(1, debugfile);
else
fprintf(stderr, "Cannot open ECPG debug log: %s\n", debugfilename);
#endif
/* setup test_thread table */
EXEC SQL CONNECT TO:l_dbname;
EXEC SQL DROP TABLE test_thread; /* DROP might fail */
EXEC SQL COMMIT;
EXEC SQL CREATE TABLE
test_thread(tstamp TIMESTAMP NOT NULL DEFAULT CAST(timeofday() AS TIMESTAMP),
thread TEXT NOT NULL,
iteration INTEGER NOT NULL,
PRIMARY KEY(thread, iteration));
EXEC SQL COMMIT;
EXEC SQL DISCONNECT;
/* create, and start, threads */
threads = calloc(nthreads, sizeof(pthread_t));
if( threads == NULL )
{
fprintf(stderr, "Cannot alloc memory\n");
return( 1 );
}
for( n = 0; n < nthreads; n++ )
{
pthread_create(&threads[n], NULL, test_thread, (void *) (n + 1));
}
/* wait for thread completion */
for( n = 0; n < nthreads; n++ )
{
pthread_join(threads[n], NULL);
}
free(threads);
/* and check results */
EXEC SQL CONNECT TO :l_dbname;
EXEC SQL SELECT COUNT(*) INTO :l_rows FROM test_thread;
EXEC SQL COMMIT;
EXEC SQL DISCONNECT;
if( l_rows == (nthreads * iterations) )
printf("\nSuccess.\n");
else
printf("\nERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
/* close ECPG debug log? */
#ifdef ECPGDEBUG
if( debugfile != NULL )
{
ECPGdebug(0, debugfile);
fclose(debugfile);
}
#endif
return( 0 );
}
void *test_thread(void *arg)
{
long threadnum = (long)arg;
EXEC SQL BEGIN DECLARE SECTION;
int l_i;
char l_connection[128];
EXEC SQL END DECLARE SECTION;
/* build up connection name, and connect to database */
snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
EXEC SQL WHENEVER sqlerror sqlprint;
EXEC SQL CONNECT TO :l_dbname AS :l_connection;
if( sqlca.sqlcode != 0 )
{
printf("%s: ERROR: cannot connect to database!\n", l_connection);
return( NULL );
}
EXEC SQL BEGIN;
/* insert into test_thread table */
for( l_i = 1; l_i <= iterations; l_i++ )
{
printf("%s: inserting %d\n", l_connection, l_i);
EXEC SQL INSERT INTO test_thread(thread, iteration) VALUES(:l_connection, :l_i);
if( sqlca.sqlcode == 0 )
printf("%s: insert done\n", l_connection);
else
printf("%s: ERROR: insert failed!\n", l_connection);
}
/* all done */
EXEC SQL COMMIT;
EXEC SQL DISCONNECT :l_connection;
printf("%s: done!\n", l_connection);
return( NULL );
}
#include <stdio.h>
exec sql include sqlca;
#include <stdlib.h>
int main(void)
{
exec sql begin declare section;
char **cpp=0;
int *ipointer=0;
exec sql end declare section;
int i;
if (getenv("SQLOPT")) ECPGdebug(1,stderr);
exec sql whenever sqlerror do sqlprint();
exec sql connect to postgres;
exec sql allocate descriptor mydesc;
exec sql select tablename into descriptor mydesc from pg_tables;
exec sql get descriptor mydesc value 1 :cpp=DATA, :ipointer=INDICATOR;
printf("Result ");
for (i=0;i<sqlca.sqlerrd[2];++i)
{ if (ipointer[i]) printf("NULL, ");
else printf("'%s', ",cpp[i]);
}
ECPGfree_auto_mem();
printf("\n");
exec sql deallocate descriptor mydesc;
exec sql disconnect;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment