exec sql include header_test;

exec sql include sqlca;

/* comment */
exec sql define AMOUNT 4;

exec sql type intarray is int[AMOUNT];
exec sql type string is char(8); 

typedef int intarray[AMOUNT];

int
main ()
{
exec sql begin declare section;
        intarray amount;
	int increment=100;
        char name[AMOUNT][8];
	char letter[AMOUNT][1];
	char command[128];
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;

	strcpy(msg, "create");
	exec sql at main create table "Test" (name char(8), amount int, letter char(1));
	exec sql create table "Test" (name char(8), 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 = %d\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 %d 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 %d tuples via prepared execute\n", sqlca.sqlerrd[2]);

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

	/* Stop automatic transactioning for connection pm. */
	exec sql at pm set autocommit to off;

        strcpy(msg, "select");
        exec sql select name, amount, letter into :name, :amount, :letter from "Test";

        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]);

        exec sql at pm select * into :name, :amount, :letter from "Test";

        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, "drop");
	exec sql drop table "Test";
	exec sql at pm drop table "Test";

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

	strcpy(msg, "disconnect"); 
        exec sql disconnect all;
        
	if (dbgs != NULL)
                fclose(dbgs);

	return (0);
}
