#!/bin/sh

db_engine=MySQL
db_options="-P 3306 -u www"

WD=`pwd`/data-Wisconsin
db_name=wisconsin
log=run.log
> $log

case $db_engine
in
  MySQL)
    do_query="java jms.jmysql $db_options"
    START_OF_QUERY="select now();" # ""
    END_OF_QUERY="select now();" # ""
    MIN=min
    char16="char(16)"
    ;;
  PostgreSQL)
    do_query=pql
    START_OF_QUERY="select timeofday();"
    END_OF_QUERY="select timeofday();"
    MIN=int4min
    char16="char16"
    ;;
esac

into_table="into table temp"
into_table=""

drop_table="drop table temp;"
drop_table=""

echo =============== destroying old $db_name database... =================
java jms.jmsadmin $db_options -f drop $db_name >> $log 2>&1

echo =============== creating new $db_name database... =================
java jms.jmsadmin -f $db_options create $db_name >> $log 2>&1

if test $? -ne 0
then
  echo Createdb $db_name failed
  exit 1
fi

time $do_query $db_name -e << END_OF_CREATE >> $log 2>&1

create table onek (
	unique1 int4,
	unique2 int4,
	two int4,
	four int4,
	ten int4,
	twenty int4,
	hundred int4,
	thousand int4,
	twothousand int4,
	fivethous int4,
	tenthous int4,
	odd int4,
	even int4,
	stringu1 $char16,
	stringu2 $char16,
	string4 $char16,
	unique (unique1),
	unique (unique2),
	key (hundred));

create table tenk1 (
	unique1 int4,
	unique2 int4,
	two int4,
	four int4,
	ten int4,
	twenty int4,
	hundred int4,
	thousand int4,
	twothousand int4,
	fivethous int4,
	tenthous int4,
	odd int4,
	even int4,
	stringu1 $char16,
	stringu2 $char16,
	string4 $char16,
	unique (unique1),
	unique (unique2),
	index (hundred));

create table tenk2 (
	unique1 int4,
	unique2 int4,
	two int4,
	four int4,
	ten int4,
	twenty int4,
	hundred int4,
	thousand int4,
	twothousand int4,
	fivethous int4,
	tenthous int4,
	odd int4,
	even int4,
	stringu1 $char16,
	stringu2 $char16,
	string4 $char16,
	unique (unique1),
	unique (unique2),
	index (hundred));

load data infile '$WD/onek.data' into table onek;
load data infile '$WD/tenk.data' into table tenk1;
load data infile '$WD/tenk.data' into table tenk2;

# Hack until 'select * into table' works
# select * into table Bprime from tenk1 t where t.unique2 < 1000;

create table Bprime (
	unique1 int4,
	unique2 int4,
	two int4,
	four int4,
	ten int4,
	twenty int4,
	hundred int4,
	thousand int4,
	twothousand int4,
	fivethous int4,
	tenthous int4,
	odd int4,
	even int4,
	stringu1 $char16,
	stringu2 $char16,
	string4 $char16,
	unique (unique1),
	unique (unique2),
	index (hundred));

load data infile '$WD/tenk.data' into table Bprime;
delete from Bprime where Bprime.unique2 >= 1000;
END_OF_CREATE

if test $? -ne 0
then
  echo Initial database load failed
  exit 1
fi
  
echo =============== running benchmark... =================

time $do_query $db_name -e << END_OF_QUERYS >> $log 2>&1
$START_OF_QUERY
select * $into_table from tenk1 where (unique2 > 301) and (unique2 < 402);
$drop_table
select * $into_table from tenk1 where (unique2 > 301) and (unique2 < 402);
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select * $into_table from tenk1 where (unique1 > 647) and (unique1 < 1648);
$drop_table
select * $into_table from tenk1 where (unique1 > 647) and (unique1 < 1648);
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select * $into_table from tenk1 where (unique2 > 301) and (unique2 < 402);
$drop_table
select * $into_table from tenk1 where (unique2 > 301) and (unique2 < 402);
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select * $into_table from tenk1 where (unique1 > 647) and (unique1 < 1648);
$drop_table
select * $into_table from tenk1 where (unique1 > 647) and (unique1 < 1648);
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select * $into_table from tenk1 where (unique2 > 301) and (unique2 < 402);
$drop_table
select * $into_table from tenk1 where (unique2 > 301) and (unique2 < 402);
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select * $into_table from tenk1 where (unique2 > 647) and (unique2 < 1648);
$drop_table
select * $into_table from tenk1 where (unique2 > 647) and (unique2 < 1648);
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select * from tenk1 where unique2 = 2001;
select * from tenk1 where unique2 = 2001;
$END_OF_QUERY
$START_OF_QUERY
select * from tenk1 where (unique2 > 301) and (unique2 < 402);
select * from tenk1 where (unique2 > 301) and (unique2 < 402);
$END_OF_QUERY
$START_OF_QUERY
select t1.*, t2.unique1 AS t2unique1, t2.unique2 AS t2unique2, t2.two AS t2two, t2.four AS t2four, t2.ten AS t2ten, t2.twenty AS t2twenty, t2.hundred AS t2hundred, t2.thousand AS t2thousand, t2.twothousand AS t2twothousand, t2.fivethous AS t2fivethous, t2.tenthous AS t2tenthous, t2.odd AS t2odd, t2.even AS t2even, t2.stringu1 AS t2stringu1, t2.stringu2 AS t2stringu2, t2.string4 AS t2string4 $into_table from tenk1 t1, tenk1 t2 where (t1.unique2 = t2.unique2) and (t2.unique2 < 1000);
$drop_table
select t1.*, t2.unique1 AS t2unique1, t2.unique2 AS t2unique2, t2.two AS t2two, t2.four AS t2four, t2.ten AS t2ten, t2.twenty AS t2twenty, t2.hundred AS t2hundred, t2.thousand AS t2thousand, t2.twothousand AS t2twothousand, t2.fivethous AS t2fivethous, t2.tenthous AS t2tenthous, t2.odd AS t2odd, t2.even AS t2even, t2.stringu1 AS t2stringu1, t2.stringu2 AS t2stringu2, t2.string4 AS t2string4 $into_table from tenk1 t1, tenk1 t2 where (t1.unique2 = t2.unique2) and (t2.unique2 < 1000);
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select t.*,B.unique1 AS Bunique1,B.unique2 AS Bunique2,B.two AS Btwo,B.four AS Bfour,B.ten AS Bten,B.twenty AS Btwenty,B.hundred AS Bhundred,B.thousand AS Bthousand,B.twothousand AS Btwothousand,B.fivethous AS Bfivethous,B.tenthous AS Btenthous,B.odd AS Bodd,B.even AS Beven,B.stringu1 AS Bstringu1,B.stringu2 AS Bstringu2,B.string4 AS Bstring4 $into_table from tenk1 t, Bprime B where t.unique2 = B.unique2;
$drop_table
select t.*,B.unique1 AS Bunique1,B.unique2 AS Bunique2,B.two AS Btwo,B.four AS Bfour,B.ten AS Bten,B.twenty AS Btwenty,B.hundred AS Bhundred,B.thousand AS Bthousand,B.twothousand AS Btwothousand,B.fivethous AS Bfivethous,B.tenthous AS Btenthous,B.odd AS Bodd,B.even AS Beven,B.stringu1 AS Bstringu1,B.stringu2 AS Bstringu2,B.string4 AS Bstring4 $into_table from tenk1 t, Bprime B where t.unique2 = B.unique2;
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select t1.*,o.unique1 AS ounique1,o.unique2 AS ounique2,o.two AS otwo,o.four AS ofour,o.ten AS oten,o.twenty AS otwenty,o.hundred AS ohundred,o.thousand AS othousand,o.twothousand AS otwothousand,o.fivethous AS ofivethous,o.tenthous AS otenthous,o.odd AS oodd, o.even AS oeven,o.stringu1 AS ostringu1,o.stringu2 AS ostringu2,o.string4 AS ostring4 $into_table from onek o, tenk1 t1, tenk1 t2 where (o.unique2 = t1.unique2) and (t1.unique2 = t2.unique2) and (t1.unique2 < 1000) and (t2.unique2 < 1000);
$drop_table
select t1.*,o.unique1 AS ounique1,o.unique2 AS ounique2,o.two AS otwo,o.four AS ofour,o.ten AS oten,o.twenty AS otwenty,o.hundred AS ohundred,o.thousand AS othousand,o.twothousand AS otwothousand,o.fivethous AS ofivethous,o.tenthous AS otenthous,o.odd AS oodd, o.even AS oeven,o.stringu1 AS ostringu1,o.stringu2 AS ostringu2,o.string4 AS ostring4 $into_table from onek o, tenk1 t1, tenk1 t2 where (o.unique2 = t1.unique2) and (t1.unique2 = t2.unique2) and (t1.unique2 < 1000) and (t2.unique2 < 1000);
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select t1.*,t2.unique1 AS t2unique1,t2.unique2 AS t2unique2,t2.two AS t2two, t2.four AS t2four,t2.ten AS t2ten,t2.twenty AS t2twenty,t2.hundred AS t2hundred,t2.thousand AS t2thousand,t2.twothousand AS t2twothousand, t2.fivethous AS t2fivethous,t2.tenthous AS t2tenthous,t2.odd AS t2odd, t2.even AS t2even,t2.stringu1 AS t2stringu1,t2.stringu2 AS t2stringu2, t2.string4 AS t2string4 $into_table from tenk1 t1, tenk2 t2 where (t1.unique2 = t2.unique2) and (t2.unique2 < 1000);
$drop_table
select t1.*,t2.unique1 AS t2unique1,t2.unique2 AS t2unique2,t2.two AS t2two, t2.four AS t2four,t2.ten AS t2ten,t2.twenty AS t2twenty,t2.hundred AS t2hundred,t2.thousand AS t2thousand,t2.twothousand AS t2twothousand, t2.fivethous AS t2fivethous,t2.tenthous AS t2tenthous,t2.odd AS t2odd, t2.even AS t2even,t2.stringu1 AS t2stringu1,t2.stringu2 AS t2stringu2, t2.string4 AS t2string4 $into_table from tenk1 t1, tenk2 t2 where (t1.unique2 = t2.unique2) and (t2.unique2 < 1000);
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select t.*,B.unique1 AS Bunique1,B.unique2 AS Bunique2,B.two AS Btwo,B.four AS Bfour,B.ten AS Bten,B.twenty AS Btwenty,B.hundred AS Bhundred,B.thousand AS Bthousand,B.twothousand AS Btwothousand,B.fivethous AS Bfivethous,B.tenthous AS Btenthous,B.odd AS Bodd,B.even AS Beven,B.stringu1 AS Bstringu1,B.stringu2 AS Bstringu2,B.string4 AS Bstring4 $into_table from tenk1 t, Bprime B where t.unique2 = B.unique2;
$drop_table
select t.*,B.unique1 AS Bunique1,B.unique2 AS Bunique2,B.two AS Btwo,B.four AS Bfour,B.ten AS Bten,B.twenty AS Btwenty,B.hundred AS Bhundred,B.thousand AS Bthousand,B.twothousand AS Btwothousand,B.fivethous AS Bfivethous,B.tenthous AS Btenthous,B.odd AS Bodd,B.even AS Beven,B.stringu1 AS Bstringu1,B.stringu2 AS Bstringu2,B.string4 AS Bstring4 $into_table from tenk1 t, Bprime B where t.unique2 = B.unique2;
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select t1.*,o.unique1 AS ounique1,o.unique2 AS ounique2,o.two AS otwo,o.four AS ofour,o.ten AS oten,o.twenty AS otwenty,o.hundred AS ohundred,o.thousand AS othousand,o.twothousand AS otwothousand,o.fivethous AS ofivethous,o.tenthous AS otenthous,o.odd AS oodd, o.even AS oeven,o.stringu1 AS ostringu1,o.stringu2 AS ostringu2,o.string4 AS ostring4 $into_table from onek o, tenk1 t1, tenk1 t2 where (o.unique2 = t1.unique2) and (t1.unique2 = t2.unique2) and (t1.unique2 < 1000) and (t2.unique2 < 1000);
$drop_table
select t1.*,o.unique1 AS ounique1,o.unique2 AS ounique2,o.two AS otwo,o.four AS ofour,o.ten AS oten,o.twenty AS otwenty,o.hundred AS ohundred,o.thousand AS othousand,o.twothousand AS otwothousand,o.fivethous AS ofivethous,o.tenthous AS otenthous,o.odd AS oodd, o.even AS oeven,o.stringu1 AS ostringu1,o.stringu2 AS ostringu2,o.string4 AS ostring4 $into_table from onek o, tenk1 t1, tenk1 t2 where (o.unique2 = t1.unique2) and (t1.unique2 = t2.unique2) and (t1.unique2 < 1000) and (t2.unique2 < 1000);
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select t1.*, t2.unique1 AS t2unique1, t2.unique2 AS t2unique2, t2.two AS t2two, t2.four AS t2four, t2.ten AS t2ten, t2.twenty AS t2twenty, t2.hundred AS t2hundred, t2.thousand AS t2thousand,t2.twothousand AS t2twothousand, t2.fivethous AS t2fivethous, t2.tenthous AS t2tenthous, t2.odd AS t2odd, t2.even AS t2even, t2.stringu1 AS t2stringu1, t2.stringu2 AS t2stringu2, t2.string4 AS t2string4 $into_table from tenk1 t1, tenk2 t2 where (t1.unique1 = t2.unique1) and (t2.unique1 < 1000);
$drop_table
select t1.*, t2.unique1 AS t2unique1, t2.unique2 AS t2unique2, t2.two AS t2two, t2.four AS t2four, t2.ten AS t2ten, t2.twenty AS t2twenty, t2.hundred AS t2hundred, t2.thousand AS t2thousand,t2.twothousand AS t2twothousand, t2.fivethous AS t2fivethous, t2.tenthous AS t2tenthous, t2.odd AS t2odd, t2.even AS t2even, t2.stringu1 AS t2stringu1, t2.stringu2 AS t2stringu2, t2.string4 AS t2string4 $into_table from tenk1 t1, tenk2 t2 where (t1.unique1 = t2.unique1) and (t2.unique1 < 1000);
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select t.*, B.unique1 AS Bunique1,B.unique2 AS Bunique2,B.two AS Btwo,B.four AS Bfour,B.ten AS Bten, B.twenty AS Btwenty, B.hundred AS Bhundred,B.thousand AS Bthousand,B.twothousand AS Btwothousand, B.fivethous AS Bfivethous,B.tenthous AS Btenthous,B.odd AS Bodd, B.even AS Beven,B.stringu1 AS Bstringu1,B.stringu2 AS Bstringu2,B.string4 AS Bstring4 $into_table  from tenk1 t, Bprime B where t.unique1 = B.unique1;
$drop_table
select t.*, B.unique1 AS Bunique1,B.unique2 AS Bunique2,B.two AS Btwo,B.four AS Bfour,B.ten AS Bten, B.twenty AS Btwenty, B.hundred AS Bhundred,B.thousand AS Bthousand,B.twothousand AS Btwothousand, B.fivethous AS Bfivethous,B.tenthous AS Btenthous,B.odd AS Bodd, B.even AS Beven,B.stringu1 AS Bstringu1,B.stringu2 AS Bstringu2,B.string4 AS Bstring4 $into_table  from tenk1 t, Bprime B where t.unique1 = B.unique1;
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select t1.*, o.unique1 AS ounique1,o.unique2 AS ounique2,o.two AS otwo,o.four AS ofour,o.ten AS oten,o.twenty AS otwenty,o.hundred AS  ohundred,o.thousand AS othousand,o.twothousand AS otwothousand,o.fivethous AS ofivethous,o.tenthous AS otenthous,o.odd AS oodd, o.even AS oeven,o.stringu1 AS ostringu1,o.stringu2 AS ostringu2,o.string4 AS ostring4 $into_table from onek o, tenk1 t1, tenk2 t2 where (o.unique1 = t1.unique1) and (t1.unique1 = t2.unique1) and (t1.unique1 < 1000) and (t2.unique1 < 1000);
$drop_table
select t1.*, o.unique1 AS ounique1,o.unique2 AS ounique2,o.two AS otwo,o.four AS ofour,o.ten AS oten,o.twenty AS otwenty,o.hundred AS  ohundred,o.thousand AS othousand,o.twothousand AS otwothousand,o.fivethous AS ofivethous,o.tenthous AS otenthous,o.odd AS oodd, o.even AS oeven,o.stringu1 AS ostringu1,o.stringu2 AS ostringu2,o.string4 AS ostring4 $into_table from onek o, tenk1 t1, tenk2 t2 where (o.unique1 = t1.unique1) and (t1.unique1 = t2.unique1) and (t1.unique1 < 1000) and (t2.unique1 < 1000);
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select two, four, ten, twenty, hundred, string4 $into_table from tenk1;
$drop_table
select two, four, ten, twenty, hundred, string4 $into_table from tenk1;
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select * $into_table from onek;
$drop_table
select * $into_table from onek;
$drop_table
$END_OF_QUERY
$START_OF_QUERY
select $MIN(unique2) as x $into_table from tenk1;
$drop_table
select $MIN(unique2) as x $into_table from tenk1;
$drop_table
$END_OF_QUERY
$START_OF_QUERY
$END_OF_QUERY
$START_OF_QUERY
$END_OF_QUERY
$START_OF_QUERY
select $MIN(unique2) as x $into_table from tenk1;
$drop_table
select $MIN(unique2) as x $into_table from tenk1;
$drop_table
$END_OF_QUERY
$START_OF_QUERY
$END_OF_QUERY
$START_OF_QUERY
$END_OF_QUERY
$START_OF_QUERY
# Changed 1000 to 10001, 74 to 74001, 1999 to 19991, 60 to 60001
insert into tenk1 (unique1, unique2, two, four, ten, twenty, hundred, thousand, twothousand, fivethous, tenthous, odd, even,stringu1,stringu2, string4) values (10001, 74001, 0, 2, 0, 10, 50, 688, 1950, 4950, 9950, 1, 100, "ron may choi","jae kwang choi", "u. c. berkeley");
insert into tenk1 (unique1, unique2, two, four, ten, twenty, hundred, thousand, twothousand, fivethous, tenthous, odd, even, stringu1, stringu2, string4) values (19991, 60001, 0, 2, 0, 10, 50, 688, 1950, 4950, 9950, 1, 100, "ron may choi", "jae kwang choi", "u. c. berkeley");
$END_OF_QUERY
$START_OF_QUERY
delete from tenk1 where tenk1.unique2 = 877;
delete from tenk1 where tenk1.unique2 = 876;
$END_OF_QUERY
$START_OF_QUERY
update tenk1 set unique2 = 10001 where tenk1.unique2 =1491;
update tenk1 set unique2 = 10023 where tenk1.unique2 =1480;
$END_OF_QUERY
$START_OF_QUERY
# changed keys to be unique
insert into tenk1 (unique1, unique2, two, four, ten, twenty, hundred, thousand, twothousand, fivethous, tenthous, odd, even, stringu1, stringu2, string4) values (10002, 70002, 0, 2, 0, 10, 50, 688, 1950, 4950, 9950, 1, 100, "ron may choi", "jae kwang choi", "u. c. berkeley");
insert into tenk1 (unique1, unique2, two, four, ten, twenty, hundred, thousand, twothousand, fivethous, tenthous, odd, even, stringu1, stringu2, string4) values (50002, 40002, 0, 2, 0, 10, 50, 688, 1950, 4950, 9950, 1, 100, "ron may choi", "jae kwang choi", "u. c. berkeley");
$END_OF_QUERY
$START_OF_QUERY
delete from tenk1 where tenk1.unique2 = 10001;
delete from tenk1 where tenk1.unique2 = 900;
$END_OF_QUERY
$START_OF_QUERY
update tenk1 set unique2 = 10088 where tenk1.unique2 =187;
update tenk1 set unique2 = 10003 where tenk1.unique2 =2000;
$END_OF_QUERY
$START_OF_QUERY
update tenk1 set unique2 = 10020 where tenk1.unique2 =1974;
# Fix unique 160 - 16001
update tenk1 set unique2 = 16001 where tenk1.unique2 =1140;
$END_OF_QUERY
END_OF_QUERYS

exit 0
