EXEC SQL INCLUDE ../regression;
EXEC SQL WHENEVER SQLERROR SQLPRINT;

int
main(void)
{
	EXEC SQL BEGIN DECLARE SECTION;
	char *stmt1 = "INSERT INTO test1 VALUES ($1, $2)";
	char *stmt2 = "SELECT * from test1 where a = $1 and b = $2";
	char *stmt3 = "SELECT * from test1 where :var = a";

	int val1 = 1;
	char val2[4] = "one", val2output[] = "AAA";
	int val1output = 2, val2i = 0;
	int val2null = -1;
	int ind1, ind2;
	EXEC SQL END DECLARE SECTION;

	ECPGdebug(1, stderr);

	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 REGRESSDB1;

	EXEC SQL CREATE TABLE test1 (a int, b text);
	EXEC SQL PREPARE foo1 FROM :stmt1;
	EXEC SQL PREPARE "Foo-1" 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 = 3;
	EXEC SQL SET DESCRIPTOR indesc VALUE 2 INDICATOR = :val1, DATA = 'this is a long test';

	EXEC SQL EXECUTE "Foo-1" USING DESCRIPTOR indesc;

	EXEC SQL DEALLOCATE "Foo-1";

	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:ind1, :val2output:ind2;
	printf("val1=%d (ind1: %d) val2=%s (ind2: %d)\n",
		val1output, ind1, val2output, ind2);

	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 = 3;
	printf("val1=%d val2=%c%c%c%c warn=%c truncate=%d\n", val1output, val2output[0], val2output[1], val2output[2], val2output[3], sqlca.sqlwarn[0], val2i);

	EXEC SQL DROP TABLE test1;
	EXEC SQL DEALLOCATE ALL;
	EXEC SQL DISCONNECT;

	EXEC SQL DEALLOCATE DESCRIPTOR indesc;
	EXEC SQL DEALLOCATE DESCRIPTOR outdesc;

	return 0;
}
