#
#	@(#)sql.pl	1.1	6/24/92
#

sub sql {
    local($db,$sql,$sep)=@_;			# local copy parameters

    $sep = '~' unless $sep;			# provide default for sep

    @res = ();					# clear result array

    &dbcmd($db,$sql);				# pass sql to server
    &dbsqlexec($db);				# execute sql

    while(&dbresults($db) != $NO_MORE_RESULTS) {	# copy all results
	while (@data = &dbnextrow($db1)) {
	    push(@res,join($sep,@data));
	}
    }

    @res;					# return the result array
}


# Message and error handlers.

sub sql_message_handler
{
    local ($db, $message, $state, $severity, $text, $server, $procedure, $line)
	= @_;

    if ($severity > 0)
    {
	print ("Sybase message ", $message, ", Severity ", $severity,
	       ", state ", $state);
	print ("\nServer `", $server, "'") if defined ($server);
	print ("\nProcedure `", $procedure, "'") if defined ($procedure);
	print ("\nLine ", $line) if defined ($line);
	print ("\n    ", $text, "\n\n");

# &dbstrcpy returns the command buffer.

	local ($lineno) = 1;	# 
	foreach $row (split (/\n/, &dbstrcpy ($db)))
	{
	    print (sprintf ("%5d", $lineno ++), "> ", $row, "\n");
	}
    }
    elsif ($message == 0)
    {
	print ($text, "\n");
    }
    
    0;
}

sub sql_error_handler {
    # Check the error code to see if we should report this.
    if ($_[2] != &SYBESMSG) {
	local ($db, $severity, $error, $os_error, $error_msg, $os_error_msg)
	    = @_;
	print ("Sybase error: ", $error_msg, "\n");
	print ("OS Error: ", $os_error_msg, "\n") if defined ($os_error_msg);
    }

    &INT_CANCEL;
}


if( defined(&dbmsghandle))	# Is this a modern version of sybperl? ;-)
{
    &dbmsghandle ("sql_message_handler"); # Some user defined error handlers
    &dberrhandle ("sql_error_handler");
}


1;

