#!/bin/sh
#
# A one-shot test battery using the 'countrysongs' table.
#

# Usage:     battery2 [options]
# Options:      -wordindex  create and use a word index
#		-combinedwordindex  create and use a combinedword index
#               -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=countrysongs
NOINDEX="-noindex"
ORIGINALFILE=originals/countrysongs
INDEXTYPE=word

for i in $*
do
  if [ $i = "-wordindex" ]; then
    NOINDEX=""
    INDEXTYPE=word
  elif [ $i = "-combinedwordindex" ]; then
    NOINDEX=""
    INDEXTYPE=combinedword
  elif [ $i = "-hier" ]; then
    TABLE=hiertest/countrysongs
  elif [ $i = "-tabdelim" ]; then
    ORIGINALFILE=originals/countrysongs_tab
  elif [ $i = "-wsdelim" ]; then
    ORIGINALFILE=originals/countrysongs_mixws
  else
    echo "$i - unrecognized option - quitting."
    exit
  fi
done



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

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


echo "*************************************************************"
echo "0. drop old tables that may be lying around.. and set up a fresh sequences table"
$SHSQL2 "drop table $TABLE"  2>/dev/null

echo "*************************************************************"
echo "1. create table"
$SHSQL2 "create table $TABLE ( id, title, origin )" 2>&1

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

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

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

if [ "$NOINDEX" = "" ]; then
  echo "*************************************************************"
  echo "8. create word indexes.."
  $SHSQL2 "create index (type=$INDEXTYPE) on $TABLE (title, origin)" 2>&1
fi


echo "*************************************************************"
echo "****  retrievals  *******************************************"
echo "*************************************************************"
echo "10. "
$SHSQL "select id, title from $TABLE where title contains 'beer' order by title, id" 2>&1

echo "*************************************************************"
echo "11. "
$SHSQL "select id, title from $TABLE where title contains 'bed' order by title, id" 2>&1

echo "*************************************************************"
echo "12. "
$SHSQL "select _matchscore, title, origin from $TABLE where title contains 'johnny paycheck' 
	or origin contains 'johnny paycheck' order by _matchscore, id" 2>&1

echo "*************************************************************"
echo "13. "
$SHSQL "select _matchscore, title, origin from $TABLE where title contains '\"johnny paycheck\"' 
	or origin contains '\"johnny paycheck\"' order by _matchscore, id" 2>&1


echo "*************************************************************"
echo "14. "
$SHSQL "select title, _matchscore from countrysongs where title contains 'mama hammer' order by _matchscore" 2>&1

echo "*************************************************************"
echo "****  updates *******************************************"
echo "*************************************************************"
echo "20. mismatched quotes - should give error"
$SHSQL2 "insert into countrysongs ( id, title, origin )
		values ( 328, 'I Love My Truck, It's Parked Outside', null )" 2>&1
echo "*************************************************************"
echo "21. quote is now escaped with a preceding backslash"
$SHSQL2 "insert into countrysongs ( id, title, origin )
		values ( 328, 'I Love My Truck, It\'s Parked Outside', null )" 2>&1
echo "*************************************************************"
echo "21. "
$SHSQL "select id, title from $TABLE where title contains 'truck' order by title, id" 2>&1

echo "*************************************************************"
echo "22. this should find 1 row.."
$SHSQL "select id, title from $TABLE where title contains 'ketchum' or origin contains 'ketchum' order by title, id" 2>&1 

echo "*************************************************************"
echo "23. "
$SHSQL2 "update countrysongs set origin = null where id = 28" 2>&1

echo "*************************************************************"
echo "24. this should find 0 rows.."
$SHSQL "select id, title from $TABLE where title contains 'ketchum' or origin contains 'ketchum' order by title, id" 2>&1 

echo "Finished." >&2
