#!/bin/sh
#
# Extracts a requested key from an rdbtable along with cursor info.
#

RCS_ID='$Id$'

my_name=$(basename $0)

while [ $# -ge 1 ] ; do
  case $1 in
    -h*) cat <<_EOH_

        NoSQL operator: ${my_name}

Usage:  ${my_name}  [options] Key Value [rdbtable]

Options:
    -h    Print this help info.
    -n    Strip header from output.

Extracts from an rdbtable the record with the key field 'Key' that has
the value 'Value'. The output record is prepended with another six
columns: 'rN, 'oF', 'fKey', 'lKey', 'nKey' and 'pKey'. Their content is :

  rN   = Row No. of the record being requested.
  oF   = Total No. of records in table.

  fKey = Value of the Key field of the first record in table
  lKey =               "               last         "
  nKey =               "               next         "
  pKey =               "               previous     "

where 'next' and 'previous' are relative to the row number of the record
being requested.

The pointers 'wrap-around', i.e. if the current Key points to record 8 of
a table with 8 records, nKey will contain the Key value of the 1st record.
Conversely, if the current Key points to the 1st record, pKey will point to
the last one. If the input table contains only one record, all pointers
will be the same. If the input table contains no records, then the output
table will be empty too.

This operator reads an rdbtable from a file and prints the results to
STDOUT. If no rdbtable is specified on the command line, then the input
table is read from STDIN.


$RCS_ID

            ----------------------
NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.
This program comes with ABSOLUTELY NO WARRANTY; for details
refer to the GNU General Public License.

You should have received a copy of the GNU General Public License
along with this program;  if not, write to the Free Software
Foundation, Inc., 59 Temple Place Suite 330, Boston, MA 02111-1307
USA.
            ----------------------

_EOH_
        exit 0
        ;;
	-p)    shift; prefix=$1; shift ;;
	-n)    shift; no_hdr=1         ;;
    *)
	  if [ -z "${k_name}" ]
	  then
		k_name=$1; shift
	  else
		break
	  fi
	;;
  esac
done

case ${1} in
  -*|"")
    echo "Usage: ${my_name} [options] key value [rdbtable]" >&2
    exit 1
    ;;
esac

case ${2} in
  -*)
    echo "Usage: ${my_name} [options] key value [rdbtable]" >&2
    exit 2
    ;;
esac

awk 'BEGIN { NULL=""; FS="\t"; OFS=FS;
     k_name="'"${k_name}"'"; k_value="'"${1}"'";}
# Table comments.
r==0 && $0 ~ /^ *#/ { next }
# Column names and positions.
r==0 { 
  while( ++p <= NF ) {
	# Make sure we pick the first occurrence of duplicated column
	# names (it may happen after a join).
	if( P[$p] == NULL ) {
	  P[$p]=p
	  N[p]=$p
	}
  }	
  if( !'${no_hdr:-0}' )
	print "rN", "oF", "f" k_name, "l" k_name, "n" k_name, "p"  k_name, $0
  r++; next;
}
# Column definitions.
r==1 {
  NR=0
  if( !'${no_hdr:-0}' ) {
	out_rec = "6N" OFS "6N"
    p=0
    while( ++p <= NF )
	  if( D[N[p]] == NULL ) D[N[p]] = $p
    for( i=1; i<=4; i++) out_rec = out_rec OFS D[k_name]
    print out_rec, $0
  }
  r++; next;
}
{ 
  cursor[NR] = $P[k_name]
  if ( cursor[NR] == k_value ) {
	needle = $0
	c_pointer = NR
	prev_row = NR - 1
	next_row = NR + 1
  }
}
END {
  # Handle empty table, or record not found.
  if( NR == 0 || c_pointer == NULL ) exit
  if ( prev_row == 0 ) prev_row = NR
  if ( next_row > NR )  next_row = 1
  print c_pointer, NR, cursor[1], cursor[NR], cursor[next_row],
	cursor[prev_row], needle
}' ${2}

exit $?

