#!/usr/local/bin/perl
#=======================================================================
#
#  NAME
#
#  ingest_info.pl
#
#  DESCRIPTION
#
#  Subroutines to get information from the ingest specific tables in 
#  the keyword database.
#
#  OPTIONS
#
#   None
#
#  HISTORY
#
#   02/15/2004 49680 L Gardner Initial Release
#   09/08/2006 56560 L Gardner Fix sij stis association delete.
#   09/19/2006 56505 L Gardner Use new keyword db fieldnames.
#
#=======================================================================

#-----------------------------------------------------------------------
#
# Subroutine GetTableType:
#   Load in the ingest_tables table from the keyword database.
#   One row is returned for each table and we get the selector value.
#   If the selector is MEMBEXP it means that only exposure members
#   of an association should populate the table.  This will be used
#   below in determining table cleanup.
#
#-----------------------------------------------------------------------
sub GetTableType
{
   my ( $dbh, $mission, $instr ) = @_;

   my $query = "select igt_tablename, igt_selector from ingest_tables " .
               "where igt_mission = '$mission' and igt_instrument = '$instr' ";

   my %tableType = GetDbHash( $dbh, $query );

   # Here are some overrides for selector
   $tableType{oms_data}       = 'MEMBEXP' if $instr eq 'OMS';
   $tableType{fuse_exposures} = 'MEMBEXP' if $instr eq 'FUSE';

   return %tableType;
}

#-----------------------------------------------------------------------
#
# Subroutine GetKeyFields:
#   Load in the ingest_keyfields table from the keyword database.
#   Get the key fields that will be used in constructing the where clause.
#   Store them in a hash to be used later.
#
#-----------------------------------------------------------------------
sub GetKeyFields
{
   my ( $dbh, $mission, $instr, $acWhere ) = @_;

   my %keyFields = ();

   my $query = "select igk_tablename, igk_fieldname from ingest_keyfields " .
               "where igk_mission = '$mission' and igk_instrument = '$instr' " .
               "and igk_archive_class $acWhere";
   
   my @dat = GetDbValues( $dbh, $query );
   foreach my $row ( @dat ) {
      my ( $tableName, $fieldName ) = @$row;
      $keyFields{$tableName}{$fieldName} = 1;
   }
   return %keyFields;
}
1;
