##########################################################################
#
# routine: db_podnames.pl
#
# Purpose: Look up the podfile names in the podnames relation for
#          a given 8-character ipppssoo value.
#          Return lists of the podfile and ipppssoot names.
#
# Input:   $IPPPSSOO - 8-character partial exposure name to look up
#          ENV.OPUS_SERVER - name of database server
#          ENV.OPUS_DB - name of database containing podnames relation
#
# Return:  @podfiles - list of podfiles containing data for the exposure
#          @ipppssoots - list of ipppssoots corresponding to each podfile
#          Both lists will be empty if an error occurred or the ipppssoot was not found
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 09/03/00 43165  MSwam     first version
# 03/25/10 64274  MSwam     Replace ST_DBlib with DBI
# 06/30/10 64432  MSwam     Use single quotes for SQLServer
# 09/18/12 72255  Sherbert  get rid of DSQUERY
# 
##########################################################################
sub DB_podnames {
    my ($IPPPSSOO, $podfiles, $ipppssoots) = @_;  # ARGUMENTS from @_
    my ($db, $query, $wild, $count);    # LOCAL VARS

    # initialize as failed
    @$podfiles = ();
    @$ipppssoots = ();

    require 'do_dbi_pkg.pl';
    $server = $ENV{"OPUS_SERVER"};
    $database = lc($ENV{"OPUS_DB"});

    my $db = STScI::DBI->connect( "dbi:Sybase:server=$server");
    if (!defined($db)) {
        PrintMsg("E","Cannot connect to server: $DBI::errstr");
        return;
    }
    DoDBI($db,"use $database"); # open database

    #
    # append the wildcard character to the 8-character exposure name
    #
    if (length($IPPPSSOO) == 0) { 
       PrintMsg("E","ERROR: DB_podnames given blank exposure name\n");
       DoDBIclose($db);
       return;
    }

    $wild = $IPPPSSOO . '%';
    $query = <<"EOQ";
SELECT ipppssoot,podname FROM podnames 
WHERE ipppssoot like '$wild'
EOQ

    $count = 0;
    $sth = DoDBIexecute( $db, $query);
    while ( ( $ipppssoot, $podfile) = DoDBIfetch( $db, $query, $sth) ) {
        PrintMsg("D","found $ipppssoot,$podfile");
        push(@$podfiles, $podfile);
        push(@$ipppssoots, $ipppssoot);
        $count++;
    }
    PrintMsg("D","$IPPPSSOO podnames: count = $count");
    DoDBIclose($db);
}
1;
