#****************************************************************************
#  ##   ##         #####   #####  ##     **       NoSQL RDBMS - column      *
#  ###  ##        ####### ####### ##     **        $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.                                *
#****************************************************************************
#
#  Extracts specific columns from a table.
#
#  Selects columns by name (and order) and outputs a table with
#  these columns. Can effectively select, order, delete, or duplicate
#  columns. If no columns are specified, then nothing is printed. If a
#  column name does not match any of the columns in table, a new column
#  with that name is inserted in that location.
#
#  This NoSQL operator reads a table from STDIN and writes a
#  table to STDOUT.
#
########################################################################

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

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

  # Exit if no columns were specified.

  j = split( __nosql_args, command_cols, " " )
  if ( j > 0 ) command_cols[0] = j
  else exit
}

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

NR == 1 \
{
  # Load the column position array.
  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 }
  }

  # Build the list of columns, allowing for the insertion of new ones.
  for ( i = 1; i <= command_cols[0]; i++ )
  {
	out_rec = out_rec OFS command_cols[i]

	# Is it a new column ?
	if ( P[ command_cols[i] ] == NULL ) P[ command_cols[i] ] = NF+1
  }

  # Remove leading extra OFS from out_rec, then print header and dashline.
  sub( /^\t/, "", out_rec ); print out_rec
  gsub( /[^\t]/, "-", out_rec ); print out_rec

  next
}

# Dashline
NR == 2 { next }

{
  out_rec = $P[ command_cols[1] ]
  for ( i = 2; i <= command_cols[0]; i++ )
	out_rec = out_rec OFS $P[ command_cols[i] ]

  print out_rec
}

