#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#.IDENTIFICATION  syb_dbi.pl
#.LANGUAGE	  SybPerl script
#
#.PURPOSE	  Database Interface to Sybase.
#
#		  &dbi_connect( $user, $pswd, $server, $database );
#		  &dbi_dosql( "... SQL commands ...");
#		  %row = &dbi_nextrow;
#		  &dbi_disconnect;
#		  &dbi_rowcount( $rows );
#		  &dbi_dateformat( $format );
#
#.AUTHOR	  Bo Frese Rasmussen [ST-ECF]   <bfrasmus@eso.org>
#
#.VERSION	1.0	22/12-1994	Creation
#------------------------------------------------------------------------------	
package WDB_DatabaseInterface;
require	"sybperl.pl";

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#.PURPOSE	Connects to a database.
#
#.REMARKS	This function must be called before any of the other functions
#		in this package.
#		It logs in to the given database server and connects to a
#		database.
#
#.RETURNS	Dies on error !
#------------------------------------------------------------------------------
sub main'dbi_connect
{
    local(  $user, $pswd, $server, $database ) = @_;
    $dbproc = &main'dblogin( $user, $pswd, $server );
    $dbproc != -1 || die "Can't connect to server $server ...\n";
    &main'dbuse( $database ) || die "Can't connect to database $database ...\n";
}

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#.PURPOSE	Prepares and executes an SQL statement, with error check etc.
#
#.REMARKS	Dies on error !
#
#.RETURNS	Return value of &dbresults 
#------------------------------------------------------------------------------
sub main'dbi_dosql 
{
    local($sql) = @_;

    &main'dbcmd( $sql ) || die "Error in dbcmd.\n" ;
    &main'dbsqlexec || die "Error in dbsqlexec.\n" ;
    &main'dbresults;
}
 
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#.PURPOSE	Gets the next row from a previous select (dbi_dosql).
#
#.REMARKS	After a dbi_dosql("select ... ") call, this function can be 
#		called repeatetly to retrieve all rows of the query.
#
#		Example :
#			&dbi_dosql("select * from mytab");
#			while( %row = &dbi_nextrow  ) {
#			    print $row{'columnname'};
#			    ...
#			}
#
#.RETURNS	An associative array (keyed on the column name) of formatted 
#		data, based on the datatype of the corresponding columns.
#------------------------------------------------------------------------------
sub main'dbi_nextrow
{
    %row = &main'dbnextrow($dbproc, 1);
    return %row;
}

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#.PURPOSE	Disconnects the current database connection
#
#.REMARKS	After this function is called there are no current database 
#		connection => no other function from this package can be 
#		called before a new dbi_connect call.
#
#.RETURNS	nothing.
#------------------------------------------------------------------------------
sub main'dbi_disconnect
{
    &main'dbclose($dbproc);
}

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#.PURPOSE	Sets the maximim number of rows returned from a query.
#
#.REMARKS	Causes SQL server to stop processing the query ( select, 
#		insert update, delete) after the specified number of rows 
#		are affected.
#		
#		PORTING NOTE: This function is added for efficiency only. 
#		When used with WDB it can safely be ignored ( leave an
#		empty function body : {} )
#
#.RETURNS	nothing.
#------------------------------------------------------------------------------
sub main'dbi_rowcount
{
    local ($rowcount) = @_;
    &main'dbi_dosql( "set rowcount $rowcount");
}

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#.PURPOSE	Sets the default output format for dates.
#
#.REMARKS	Sets the order of the date parts month/day/year for entering
#		datetime data. Valid arguments are mdy, dmy, ymd, ydm, myd, dym.
#
#.RETURNS	nothing.
#------------------------------------------------------------------------------
sub main'dbi_dateformat
{
    local ($dateformat) = @_;
    &main'dbi_dosql( "set dateformat $dateformat");
}

1;
