#!/bin/sh


numInsert=10000
numInsertClob=1000

echo 1..9 # Number of tests to be executed.

CEGO=../src/cego
DBXML=db/cegodb.xml

$srcdir/mkdb > expimp.stdout

cat <<EOF >db/prepExportReady.sql
drop if exists table t1;
create table t1 ( a int, b string(30), c datetime default sysdate);

drop if exists table t2;
create table t2 ( a int, b clob);

@
create procedure checkInsert ( numIns in int ) 
begin 

   var actIns int;

   :actIns = 0;

   while :actIns < :numIns
   begin
	insert into t1 (a, b) values ( :actIns , randstr(20) );	
	:actIns = :actIns + 1;
   end;


end;
@

@
create procedure checkClob ( numIns in int ) 
begin 

   var actIns int;
   var clobString string(100);

   :actIns = 0;

   while :actIns < :numIns
   begin
        if randint(2) = 1
        then
	   insert into t2 (a, b) values ( :actIns , randstr(100) );
        else
           insert into t2 (a, b) values ( :actIns , null );
        end;
	:actIns = :actIns + 1;
   end;
end;
@


select count(*) from t1;
call checkInsert($numInsert);
select count(*) from t1;

select count(*) from t2;
call checkClob($numInsertClob);
select * from t2;
select count(*) from t2;
EOF

cat <<EOF >db/verifyImport.sql
select count(*) from t1;
select * from t2;
EOF

$CEGO --mode=batch --batchfile=db/prepExportReady.sql --dbxml=$DBXML --user=lemke/lemke --poolsize=1000 --tableset=TS1 > expimp.stdout
if [ $? -eq 0 ]
then
    echo "ok 1 - Database generated with $numInsert inserts"
else
    echo "not ok 1 - Database generated"
    exit 1
fi

$CEGO --mode=xmlexport --tableset=TS1 --dbxml=$DBXML --user=lemke/lemke --expfile=db/ts1.xml >> expimp.stdout
if [ $? -eq 0 ]
then
    echo "ok 2 - XML Export in $USEDTIME seconds"
else
    echo "not ok 2 - XML Export"
    exit 1
fi

rm -rf db/*.sys
rm -rf db/*.temp
rm -rf db/*.dat
rm -rf db/*.log
rm -rf arch/*.dbf

$CEGO --mode=create --dbxml=$DBXML --tableset=TS1
if [ $? -eq 0 ]
then
    echo "ok 3 - Database create"
else
    echo "not ok 3 - Database create"
    exit 1
fi

$CEGO --mode=xmlimport --tableset=TS1 --dbxml=$DBXML --user=lemke/lemke  --impfile=db/ts1.xml >> expimp.stdout
if [ $? -eq 0 ]
then
    echo "ok 4 - XML Import in $USEDTIME seconds"
else
    echo "not ok 4 - XML Import"
    exit 1
fi

$CEGO --mode=batch --batchfile=db/verifyImport.sql --dbxml=$DBXML --user=lemke/lemke --poolsize=1000 --tableset=TS1 >> expimp.stdout
if [ $? -eq 0 ]
then
    echo "ok 5 - Verify import"
else
    echo "not ok 5 - Verify import"
    exit 1
fi


$CEGO --mode=binexport --tableset=TS1 --dbxml=$DBXML --user=lemke/lemke  --expfile=db/ts1.bin >> expimp.stdout
if [ $? -eq 0 ]
then
    echo "ok 6 - Binary Export in $USEDTIME seconds"
else
    echo "not ok 6 - Binary Export"
    exit 1
fi

rm -rf db/*.sys
rm -rf db/*.temp
rm -rf db/*.dat
rm -rf db/*.log
rm -rf arch/*.dbf

$CEGO --mode=create --dbxml=$DBXML --tableset=TS1
if [ $? -eq 0 ]
then
    echo "ok 7 - Database create"
else
    echo "not ok 7 - Database create"
    exit 1
fi

$CEGO --mode=binimport --tableset=TS1 --dbxml=$DBXML --user=lemke/lemke  --impfile=db/ts1.bin >> expimp.stdout
if [ $? -eq 0 ]
then
    echo "ok 8 - Binary Import in $USEDTIME seconds"
else
    echo "not ok 8 - Binary Import"
    exit 1
fi

$CEGO --mode=batch --batchfile=db/verifyImport.sql --dbxml=$DBXML --user=lemke/lemke --poolsize=1000 --tableset=TS1 >> expimp.stdout
if [ $? -eq 0 ]
then
    echo "ok 9 - Verify import"
else
    echo "not ok 9 - Verify import"
    exit 1
fi
