##########################################################################
#
# Package: sql_select_pkg.pl
#
# Purpose: Provides a set of routines to select data from Sybase tables
#
# Member routines:
#   @record = SelectSql ($db, $query, $msg); -- return a single record resulting
#             from a $query to the already openned $db object. $msg is error 
#             message. Null record returned if no data found.
#   @record = FirstRecordSql ($db, $query, $msg); -- return first record 
#             resulting from $query to the already openned $db object. $msg is 
#             error message.
#   @record = NextRecordSql ($db, $msg); -- return next record 
#             resulting from open query in the $db object. $msg is 
#             error message. The FirstRecordSql routine must used to start the
#             query. Null record is returned after last record.
#
# Error handling:
#  Exits with $msg if there is an error in results from the query. Exits with
#  DB message if query has bad syntax or cannot be executed.
#
# Calls:
#    PrintMsg,  which must be declared prior to this module.
#
# Modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 04/26/02 45738  J.Baum    first version
# 
#########################################################################
#
##---------------------------------------------------------------------
#---  used only to return one record from a table 
sub SelectSql
{
    my ($db, $query, $msg) = @_;
    die if $db->dbcmd($query) == Sybase::DBlib::FAIL;
    die if $db->dbsqlexec     == Sybase::DBlib::FAIL;
    if ($db->dbresults        == Sybase::DBlib::FAIL) {
      PrintMsg ("E",$msg);
      exit( 1); # EXIT FAILURE
    }
    $db->dbnextrow;
}
#---------------------------------------------------------------------
#---  used to setup a query for records to be used with NextRecordSQL
sub FirstRecordSql
{
    my ($db, $query, $msg) = @_;
    my $status;

    die if $db->dbcmd($query) == Sybase::DBlib::FAIL;
    die if $db->dbsqlexec     == Sybase::DBlib::FAIL;
    $status = $db->dbresults;
    if ( $status              == Sybase::DBlib::FAIL) {
      PrintMsg ("E",$msg);
      exit( 1);  # EXIT FAILURE
    } elsif ($status          == Sybase::DBlib::NO_MORE_RESULTS) {
      return;
    } else {
      return ($db->dbnextrow);
    }
}
#---------------------------------------------------------------------
#---  used to return next record from a table 
sub NextRecordSql
{
    my ($db, $msg) = @_;
    my $status;

    my @record = $db->dbnextrow;
    if ( (scalar @record) > 0) {
       return @record;
    } else {
      $status = $db->dbresults;
      if ($status      == Sybase::DBlib::FAIL) {
         PrintMsg ("E",$msg);
         exit( 1);  # EXIT FAILURE
      } elsif ($status == Sybase::DBlib::NO_MORE_RESULTS) {
        return;
      } else {
        return ($db->dbnextrow);
      }
    }
}
1;
