##########################################################################
#
# routine: runsqltransaction.pl
#
# purpose: Runs a SQL database query that checks status and messages.  
#          Meant for multiple insert statements in a transaction where rollback
#          code uses a select statement to indicate where any rollback occurs.
#          When an insertion fails the record count for any previous insertions
#          must be zeroed because of the rollback. This logic depends on the
#          coding of the query to rollback all insertions after an error. The
#          generations of rollback messages by select statements is optional.
#
# Input:   db - database object
#          query - the text of the query
#
# Returns: count of the number of records affected by the query
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 01/13/04 49555  J.Baum    first version - based on runsql.pl
# 
##########################################################################
sub RunSqlTransaction
{
    my ( $db, $query ) = @_;
    my $rec_count;
    my $count =0;

    #
    # initialize database
    use ST_DBlib;

    die if $db->dbcmd($query) == Sybase::DBlib::FAIL;
    die if $db->dbsqlexec     == Sybase::DBlib::FAIL;
    $dbresult = $db->dbresults;
    while ( $dbresult != Sybase::DBlib::NO_MORE_RESULTS ) {
        if ($dbresult == Sybase::DBlib::FAIL) {$count = 0;} 
        else {
	   $rec_count = $db->DBCOUNT;
	   @record = $db->dbnextrow; # report any message from the query
	   if (scalar @record > 0) {
	      PrintMsg("I","DB msg: $record[0]");
	   }
	}
        if ($rec_count>0){$count += $rec_count};
        $dbresult = $db->dbresults;
    }
    $count;
}
1;
