#****************************************************************************
#  ##   ##         #####   #####  ##     **      NoSQL RDBMS - datatype     *
#  ###  ##        ####### ####### ##     **        $Revision: 2.1 $			*
#  #### ##        ###     ##   ## ##     ************************************
#  #######  ####  #####   ##   ## ##     **      Carlo Strozzi (c) 1998     *
#  ####### ######   ##### ## # ## ##     ************************************
#  ## #### ##  ##     ### ##  ### ##     **           Written by            *
#  ##  ### ###### ####### ######  ###### **          Carlo Strozzi          *
#  ##   ##  ####   #####   #### # ###### **     e-mail: carlos@linux.it     *
#****************************************************************************
#   NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.                          *
#   This program comes with ABSOLUTELY NO WARRANTY; for details             *
#   refer to the GNU General Public License.                                *
#****************************************************************************
#
#  Displays the type of data in each column of the input table.
#
#  Data types are :
#
#  S		String
#  I		Integer
#  F.1		Float (precision 1 decimal digit)
#  F.5		Float (precision 5 decimal digits)
#  D  		Date 
#  T  		Time 
#  C  		Currency
#
#
#  Refer to the documentation of the 'addtypes' operator for more
#  information on supported data types.
#
#  This NoSQL operator reads the output of the 'addtypes' operator from
#  STDIN and writes a table with the datatype information to STDOUT.
#
########################################################################

########################################################################
# BEGIN block
########################################################################

BEGIN { NULL = ""; FS = OFS = "\t"; }

########################################################################
# Main loop
########################################################################

NR == 1 { split( $0, c_names ) }

NR == 2 \
{
  split( $0, c_types )

  for ( i in c_types )
  {
	c_widths[i] = c_types[i]
	sub( /[A-Za-z].*$/, NULL, c_widths[i] )
	sub( /^[0-9]+/, NULL, c_types[i] )
	if ( c_types[i] ~ /[Ii]/ )
	{
	  c_types[i] = "I" ; c_desc[i] = "integer"
	}
	else if ( c_types[i] ~ /[Dd]/ )
	{
	  c_types[i] = "D" ; c_desc[i] = "date"
	}
	else if ( c_types[i] ~ /[Tt]/ )
	{
	  c_types[i] = "T" ; c_desc[i] = "time"
	}
	else if ( c_types[i] ~ /[Cc]/ )
	{
	  c_types[i] = "C" ; c_desc[i] = "currency"
	}
	else if ( c_types[i] ~ /[Ff]/ )
	{
	  split( c_types[i], a, "." )
	  c_types[i] = "F." a[2]
	  c_desc[i] = "float " a[2]
	}
	else
	{
	  c_types[i] = "S" ; c_desc[i] = "string"
	}
  }

  print "Field" OFS "Width" OFS "Type" OFS "Description"
  print "-----" OFS "-----" OFS "----" OFS "-----------"

  for ( i = 1; c_names[i] != NULL; i++ )
  {
	if ( c_widths[i] == NULL ) c_widths[i] = length( c_names[i] )
	print c_names[i] OFS c_widths[i] OFS c_types[i] OFS c_desc[i]
  }

  exit
}

