##########################################################################
#
# routine: db_osp_data_source.pl
#
# Purpose: Look up a dataset name in the otfr_special_processing relation.
#          Return the value of the osp_data_source field.
#
# Input:   $DSET - name of exposure to look up
#          ENV.ARCH_SERVER - name of DB server
#          ENV.ARCH_DB - name of database
#
# Return:  osp_data_source, if an entry exists for this dataset
#          "" if no entry found
#
# Errors:  Will fail if unable to connect to database
#          Will fail if database query fails to execute
#
# Calls:   PrintMsg
#
# 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
# 
#########################################################################
sub DB_osp_data_source{
    my ($DSET) = @_;                            # ARGUMENTS from @_
    my ($db, $query, $count);  # LOCAL VARS
    my ($osp_data_source);

    require 'do_dbi_pkg.pl';

    # set exit value as if failed
    $osp_data_source = "";

    $server = $ENV{"ARCH_SERVER"};
    $database = lc($ENV{"ARCH_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

    $query = <<"EOQ";
SELECT osp_data_source FROM otfr_special_processing
WHERE osp_data_set_name = '$DSET' AND osp_status = 'ACTIVE'
EOQ

    $count = 0;
    $sth = DoDBIexecute( $db, $query);
    while ( ( $tmp ) = DoDBIfetch( $db, $query, $sth) ) {
        $osp_data_source = $tmp;
        PrintMsg("I","found $DSET,$osp_data_source in otfr_special_processing");
        $count++;
    }
    PrintMsg("D","$DSET: osp_data_source count = $count");

    DoDBIclose($db);
    $osp_data_source;   # return value
}
1;
