#!/bin/sh
#
# A one-shot test battery using the 'auctionitems' table.
# Some of the queries are nonsensical for real-life purposes, but needed for test purposes.
#
# Usage:     battery1 [options]
# Options:	-index	create and use indexes 
#		-hier	target table will be in a hierarchy
#		-tabdelim working table will have tab-delimited fields
#		-wsdelim working table will have fields delimited by mixed whitespace
# Note: -tabdelim and -wsdelim should work with or without 'dbdelim' config attribute set to 'tab'

TABLE=auctionitems
NOINDEX="-noindex"
ORIGINALFILE=originals/auctionitems

for i in $*
do
  if [ $i = "-index" ]; then
    NOINDEX=""
  elif [ $i = "-hier" ]; then
    TABLE=hiertest/auctionitems
  elif [ $i = "-tabdelim" ]; then
    ORIGINALFILE=originals/auctionitems_tab
  elif [ $i = "-wsdelim" ]; then
    ORIGINALFILE=originals/auctionitems_mixws
  else
    echo "$i - unrecognized option - quitting."
    exit
  fi
done


SHSQL="shsql -echo -n $NOINDEX"
SHSQL2="shsql -echo $NOINDEX"

echo "Starting test.. battery1 $*" >&2
echo "Starting test.. battery1 $*" 


echo "*************************************************************"
echo "0. drop old tables that may be lying around.. and set up tables"
$SHSQL2 "drop table $TABLE"  2>/dev/null
$SHSQL2 "drop table sellers"  2>/dev/null
$SHSQL2 "drop table _sequences"    2>/dev/null
$SHSQL2 "select * into _sequences table from originals/_sequences" 2>&1


echo "*************************************************************"
echo "1. create table"
$SHSQL2 "CREATE TABLE $TABLE 
	(id, item_title, current_price, nbids, high_bidder, seller, status, _locktime, _lockowner)" 2>&1

echo "*************************************************************"
echo "2. tabdef(1) "
tabdef $TABLE 2>&1

echo "*************************************************************"
echo "3. populate the table by selecting into from original copy.."
$SHSQL2 "select * into $TABLE table from $ORIGINALFILE" 2>&1

echo "*************************************************************"
echo "4. record count"
$SHSQL "select count(*) from $TABLE" 2>&1

echo "*************************************************************"
echo "5. drop the table"
$SHSQL "DROP TABLE $TABLE"  2>&1

echo "*************************************************************"
echo "6. create the table again by doing a direct 'select into'.."
$SHSQL2 "SELECT * INTO $TABLE TABLE FROM $ORIGINALFILE" 2>&1

echo "*************************************************************"
echo "7. test ALTER, and use it to add _locktime and _lockowner columns.."
$SHSQL2 "ALTER TABLE $TABLE ADD throwaway " 2>&1
$SHSQL2 "alter table $TABLE add _locktime " 2>&1
$SHSQL2 "alter table $TABLE add _lockowner " 2>&1
$SHSQL2 "alter table $TABLE DROP COLUMN throwaway " 2>&1

# note: build the indexes whether or not we're using -noindex to maintain cpu 
# time consistency for comparison..
echo "*************************************************************"
echo "8. create indexes.."
$SHSQL "create index (later) on $TABLE ( current_price order=num, nbids order=num, high_bidder, seller )" 2>&1
$SHSQL "CREATE INDEX (TYPE=WORD) ON $TABLE ( item_title )" 2>&1


echo "*************************************************************"
echo "****  retrievals  *******************************************"
echo "*************************************************************"
echo "10. basic select"
$SHSQL "select * from $TABLE where nbids > 8 order by id" 2>&1

echo "*************************************************************"
echo "11. basic select"
$SHSQL "select * from $TABLE where nbids >= 8 order by id" 2>&1

echo "*************************************************************"
echo "12. select with OR"
$SHSQL "select * from $TABLE where nbids > 8 OR current_price > 100.00 order by id" 2>&1

echo "*************************************************************"
echo "13. select  - test null v/v inrange numeric comparison"
$SHSQL "select * from $TABLE where nbids inrange 3,4 order by id" 2>&1

echo "*************************************************************"
echo "14. select  - test null v/v outrange numeric comparison"
$SHSQL "select * from $TABLE where nbids outrange 2,5 and current_price > 50.00 order by id" 2>&1

echo "*************************************************************"
echo "15. select - group by, no where clause"
$SHSQL "select seller, count(*) from $TABLE group by seller" 2>&1

echo "*************************************************************"
echo "16. select - group by w/ where clause"
$SHSQL "SELECT seller, COUNT(*) FROM $TABLE GROUP BY seller WHERE nbids > 5" 2>&1

echo "*************************************************************"
echo "17. select - isnot null"
$SHSQL "select * from $TABLE where seller = 's08' and nbids isnot null order by id" 2>&1

echo "*************************************************************"
echo "18. select - contains"
$SHSQL "select *, _matchscore from $TABLE 
	where item_title contains 'ital naples' order by _matchscore, id" 2>&1

echo "*************************************************************"
echo "19. select - contains w/ OR"
$SHSQL "select *, _matchscore from $TABLE 
	where item_title contains 'nude woman' 
		OR item_title contains 'nude female' order by _matchscore, id " 2>&1

echo "*************************************************************"
echo "20. select - count() on a field with some null"
$SHSQL "select count(nbids) from $TABLE"

echo "*************************************************************"
echo "21. select - min() and max() on nbids field "
$SHSQL "select min(nbids), max(nbids) from $TABLE"

echo "*************************************************************"
echo "22. select - min() and max() on nbids field w/ group by "
$SHSQL "select seller, MIN(nbids), MAX(nbids) FROM $TABLE GROUP BY seller"

echo "*************************************************************"
echo "23. select - avg() and sum() on nbids field w/ group by "
$SHSQL "select seller, avg(current_price), sum(current_price) from $TABLE group by seller"

echo "*************************************************************"
echo "24. select - IN w/ degenerate commalist"
$SHSQL "select * from auctionitems where high_bidder in ',,liz,,martha,kelly,,'"

echo "*************************************************************"
echo "*** updates section *****************************************"
echo "*************************************************************"
echo
echo "30. create sequence"
$SHSQL2 "create sequence on $TABLE" 2>&1
$SHSQL "select _sequence from $TABLE" 2>&1

echo "*************************************************************"
echo "31. set sequence number to an initial value of 38000"
$SHSQL2 "update _sequences set value = 38000 where tablename = '$TABLE' " 2>&1

echo "*************************************************************"
echo "32. get next sequence number"
ID=`shsql "select _sequence from $TABLE"` 
echo "got $ID"

echo "*************************************************************"
echo "33. insert a new row - certain fields, fieldnames given"
$SHSQL2 "insert into $TABLE (id, item_title, current_price, seller)
	values ($ID, 'Painting, Broadway Boogie-Woogie, Study by Gio Vivian', 40.00, 's19' )" 2>&1
$SHSQL "select * from $TABLE where id = $ID"


echo "*************************************************************"
echo "34. insert a new row - all fields, no fieldnames given"
ID=`shsql "select _sequence from $TABLE"` 
echo "got $ID"
$SHSQL2 "insert into $TABLE 
	values ( $ID, 'Painting, Blueberry hill, Maine, F Stanley', 35.00, 
	null, null, 's19', null, null, null )"   2>&1
$SHSQL "select * from $TABLE where id = $ID"

echo "*************************************************************"
echo "35. use 'update .. orinsert' to insert another new row"
ID=`shsql "select _sequence from $TABLE"` 
echo "got $ID"
shsql "update $TABLE ORINSERT
	set id = $ID, item_title = 'city',
		seller = 's19', current_price = 35.00
	where id = $ID"  2>&1
$SHSQL "select * from $TABLE where id = $ID"  2>&1
HOLDID=$ID

echo "*************************************************************"
echo "36. insert 2 new rows having weird characters"
# note: dollar signs ($) are escaped here to prevent shell evaluation
ID=`shsql "select _sequence from $TABLE"` 
echo "got $ID"
$SHSQL2 "insert into $TABLE (id, item_title, current_price, seller)
	values ($ID, '\$discount\$ painting / art supplies', 10.00, 's19' )"  2>&1
ID=`shsql "select _sequence from $TABLE"` 
echo "got $ID"
$SHSQL2 "insert into $TABLE (id, item_title, current_price, seller)
	values ($ID, '@discount@ 
	painting / art supplies', 10.00, 's19' )" 2>&1

echo "*************************************************************"
echo "37. retrieve the weird character entries by name"
# note: never use indexing on this one, because item_title has a word index that ignores punct chars..
$SHSQL -noindex "select * from $TABLE where item_title inlike '\$*,@*' order by id"  2>&1

ID=$HOLDID
echo "*************************************************************"
echo "38. update the 'city' record, each time one character longer, to test upd cutover"
$SHSQL2 "update $TABLE set item_title = 'city scene1' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene12' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene123' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene1234' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene12345' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene123456' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene1234567' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene12345678' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene123456789' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene123456789A' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene123456789AB' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene123456789ABC' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene123456789ABCD' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene123456789ABCDE' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene123456789ABCDEF' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene123456789ABCDEFG' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene123456789ABCDEFGH' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene123456789ABCDEFGHI' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene123456789ABCDEFGHIJ' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene123456789ABCDEFGHIJK' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene123456789ABCDEFGHIJKL' where id = $ID" 2>&1
$SHSQL2 "select item_title from $TABLE where id = $ID"
$SHSQL2 "update $TABLE set item_title = 'city scene123456789ABCDEFGHIJKLM' where id = $ID" 2>&1
$SHSQL  "select item_title from $TABLE where id = $ID"
$SHSQL "select * from $TABLE where seller = 's19'"

echo "*************************************************************"
echo "39. use 'update .. orinsert' to update the row we just added above.."
$SHSQL2 "update $TABLE ORINSERT
		set id = $ID, item_title = 'city scene, kingston, jamaica, 1908',
		seller = 's19', current_price = 35.00
		where id = $ID"  2>&1
$SHSQL "select * from $TABLE where id = $ID"  2>&1

echo "*************************************************************"
echo "40. update all records for seller 's19'"
$SHSQL2 "update $TABLE set status = 'P' where seller = 's19'" 2>&1
$SHSQL "select * from $TABLE where seller = 's19' order by id"  2>&1



echo "*************************************************************"
echo "***  Record locking  ****************************************"
echo "*************************************************************"
# Note: we don't display SELECT .. FOR UPDATE that have timestamps present, 
# because timestamps will differ across test runs and mess up the diffs..
# Also, note that row results from SELECT .. FOR UPDATE show the _lock* fields 
# as they were BEFORE the lock was set.

echo "50. select for update (set record lock) - single record"
$SHSQL -id steve "select * from $TABLE where id = 37044   FOR UPDATE"  2>&1

echo "*************************************************************"
echo "51. another user attempts to set record lock for same record - fails "
$SHSQL -id fred "select * from $TABLE where id = 37044   FOR UPDATE"  2>&1

echo "*************************************************************"
echo "52. original user updates record - clears lock"
$SHSQL2 -id steve "update $TABLE 
		set nbids = 1, current_price = 3.00, high_bidder = 'steve' 
		where id = 37044" 2>&1

echo "*************************************************************"
echo "53. other user can now gain record lock - success"
$SHSQL -id fred "select * from $TABLE where id = 37044   FOR UPDATE"  2>&1

echo "*************************************************************"
echo "54. locking user eventually updates even tho the lock has timed out.. - success,
since no other user has locked record in the meantime"
# fake the lock time for test purposes..
$SHSQL2 -id fred "update $TABLE set _locktime = '00000' where id = 37044" 2>&1
$SHSQL2 -id fred "update $TABLE 
		set nbids = 2, current_price = 5.00, high_bidder = 'fred' 
		where id = 37044" 2>&1

echo "*************************************************************"
echo "55. lock has timed out and another user wants the lock.. - success"
# fake the lock time for test purposes.. 
$SHSQL2 -id fred "update $TABLE set _lockowner = 'fred', _locktime = '00000' where id = 37044" 2>&1
$SHSQL -id steve "select * from $TABLE where id = 37044   FOR UPDATE"  2>&1


echo "*************************************************************"
echo "56. multi-row record lock..
Note that record 37044 was not locked because lock already held by steve.."
$SHSQL2 -id s22 "select * from $TABLE where seller = 's22'  FOR UPDATE"  > /dev/null 2>&1
$SHSQL "select id, _lockowner from $TABLE where seller = 's22' order by id" 2>&1

echo "*************************************************************"
echo "57. update all the locked records.. 37044 will not be updated and there will 
be a 'record is locked' msg.. "
$SHSQL2 -id s22 "update $TABLE set status = 'H' where seller = 's22'" 2>&1
$SHSQL "select id, status, _lockowner from $TABLE where seller = 's22' order by id" 2>&1

echo "*************************************************************"
echo "58. lock the same record set again, this time delete the records.. 37044 will 
not be deleted and there will be a 'record is locked' msg.. "
$SHSQL2 -id s22 "select * from $TABLE where seller = 's22'  FOR UPDATE"  > /dev/null 2>&1
$SHSQL2 -id s22 "delete from $TABLE where seller = 's22'"  2>&1
$SHSQL "select id, status, _lockowner from $TABLE where seller = 's22' order by id" 2>&1

echo "*************************************************************"
echo "59. fake it so the lock on 37044 is timed out, and try the lock/delete again.. should do it this time.."
$SHSQL2 -id steve "update $TABLE set _locktime = '00000' where id = 37044" 2>&1
$SHSQL2 -id s22 "select * from $TABLE where seller = 's22'  FOR UPDATE"  > /dev/null 2>&1
$SHSQL2 -id s22 "delete from $TABLE where seller = 's22'"  2>&1
$SHSQL "select id, status, _lockowner from $TABLE where seller = 's22' order by id" 2>&1

echo "*************************************************************"
echo "****  joins  ************************************************"
echo "*************************************************************"
echo "70. join test.. first make a file with seller info.."
# s34 intentionally omitted..
$SHSQL2 "create table sellers ( id, fullname )" 2>&1
$SHSQL2 "insert into sellers ( id, fullname ) values ( 's01', 'Marcus Sales Co.' )" 2>&1
$SHSQL2 "INSERT INTO sellers ( id, fullname ) VALUES ( 's04', 'Worthington\'s' )" 2>&1
$SHSQL2 "insert into sellers ( id, fullname ) values ( 's08', 'Class Act II' )" 2>&1
$SHSQL2 "insert into sellers ( id, fullname ) values ( 's17', 'John M. Havlicharcz and Sons' )" 2>&1
$SHSQL2 "insert into sellers ( id, fullname ) values ( 's22', 'Arts and Farces Inc.' )" 2>&1
$SHSQL2 "insert into sellers ( id, fullname ) values ( 's19', 'Bleeker and Blum Auctioneers, Tremont, Maine' )" 2>&1
$SHSQL "select * from sellers order by fullname"

echo "*************************************************************"
echo "71. retrieve the records we should be seeing later.."
$SHSQL "select * from $TABLE where current_price >= 50.00 and nbids > 0 order by current_price num desc, id"

echo "*************************************************************"
echo "72. inner join - note the order of tables in the JOIN clause for proper duplicate key handling.."
$SHSQL "select a.item_title, s.fullname, a.current_price
	from sellers (as s) join $TABLE (as a) on s.id = a.seller 
	where a.current_price >= 50.00 and a.nbids > 0
	order by a.current_price num desc, a.id" 2>&1

echo "*************************************************************"
echo "73. left join, w/ syntax variation"
$SHSQL "SELECT a.item_title, s.fullname, a.current_price
	FROM $TABLE = a LEFTDL JOIN sellers = s 
	ON a.seller = s.id 
	WHERE a.current_price >= 50.00 AND a.nbids > 0
	ORDER BY a.current_price num desc, a.id" 2>&1

echo "*************************************************************"
echo "74. right join, note order of tables in the JOIN clause for proper duplicate key handling"
$SHSQL "SELECT a.item_title, s.fullname, a.current_price
	FROM sellers = s RIGHT JOIN $TABLE = a 
	ON a.seller = s.id 
	WHERE a.current_price >= 50.00 AND a.nbids > 0
	ORDER BY a.current_price num desc, a.id" 2>&1



echo "*************************************************************"
echo "*****   misc   **********************************************"
echo "*************************************************************"
# note: requires -ttprefix so that temp tables can be shared across unix processes..
echo "80. select into temp table.."
$SHSQL2 -ttprefix battery1 "select * into \$tmp1 from $TABLE where seller = 's19' order by id" 2>&1
$SHSQL -ttprefix battery1 "select * from \$tmp1"

echo "*************************************************************"
# note: requires -ttprefix so that temp tables can be shared across unix processes..
echo "81. select from two tables.. "
$SHSQL -ttprefix battery1 "select * from auctionitems and \$tmp1 where seller = 's19' and item_title like 'paint*'"

echo "*************************************************************"
echo "82. test DATAEDIT .."
dataedit $TABLE -s -c "sleep 2" 2>&1

echo "*************************************************************"
echo "83. run MAINTAIN to remove blank lines from table, and rebuild indexes.."
$SHSQL2 "maintain $TABLE" 2>&1

echo "Finished." >&2
