#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;
}

