#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#.IDENTIFICATION  isql_dbi.pl
#.LANGUAGE	  Isqlperl script
#
#.PURPOSE	  Database Interface to Informix.
#
#		  &dbi_connect( $user, $pswd, $server, $database );
#		  &dbi_dosql( "... SQL commands ...");
#		  %row = &dbi_nextrow;
#		  &dbi_disconnect;
#		  &dbi_rowcount( $rows );
#		  &dbi_dateformat( $format );
#
#.AUTHOR	  Informix version Curtis Wong <curtis@crl.com>
#.AUTHOR	  Bo Frese Rasmussen [ST-ECF]   <bfrasmus@eso.org>
#
#.VERSION	1.0	22/12-1994	Creation
#			16/01-1995	Informix version -ckw
#			10/02-1995	make change suggested by Bo
#------------------------------------------------------------------------------	
#require	"sybperl.pl";
# Stubs to help develop isqlfdf
sub dbnextrow { return 1 ; }
sub dbresults { return 1 ; }
sub dbsqlexec { return 1 ; }
sub dbcmd { return 1 ; }
sub dbuse {}
sub dblogin {}
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#.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 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";
print DEBUG "database <$database> isql_code <$isql_code>\n" if $debug;
&isql_execute("database $database") || die $isql_code;


}

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

#    &main'dbcmd( $sql ) || die "Error in dbcmd.\n" ;
#    &main'dbsqlexec || die "Error in dbsqlexec.\n" ;
#    &main'dbresults;
print DEBUG "dbi_dosql < $sql >\n" if $debug;
$cid = &isql_open( $sql ) || die "Error in isql_open. ( $sql )  $isql_code\n";

}
 
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#.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.
#               Return the empty list if the fetch failed.
#------------------------------------------------------------------------------
sub dbi_nextrow
{
#    %row = &main'dbnextrow($dbproc, 1);
#    return %row;
local(@titles,@trow,$i) ;

     if ( @trow    = &isql_fetch($cid) ) { #found a row of data
     	@titles = &isql_titles($cid) ;
#     	print DEBUG "dbi_nextrow  cid $cid $isql_code  \n" if $debug;
#    	for ($i = 0; $i < @titles; ++$i) {
#      	 	printf DEBUG  "%15s: %s\n", $titles[$i], $trow[$i] if debug ;
#    	  	}
#    	print DEBUG  "\n" if debug ;
     	for ( $i = 0 ; $i < @titles; ++$i) {
     		$row{$titles[$i]} = $trow[$i] ;
     	  	}
      }else {
	%row = () ;     
      }
     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 dbi_disconnect
{
#    &main'dbclose($dbproc);
&isql_shutdown() || die "Error in isql_shutdown.  $isql_code\n";
}

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#		Stubbed out in Isqlperl version - ckw
#.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 dbi_rowcount
{
#    local ($rowcount) = @_;
#    &main'dbi_dosql( "set rowcount $rowcount");
}

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#		Use informix env variable
#		Stubbed out in Isqlperl version - ckw
#.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 dbi_dateformat
{
#    local ($dateformat) = @_;
#    &main'dbi_dosql( "set dateformat $dateformat");
}

#
#
#
if ( ! $ENV{'SQLEXEC'}){
	if ( !$SQLEXEC ){
	 $ENV{'SQLEXEC'}="/usr/informix/lib/sqlturbo" ;
	}
	 else {
	 $ENV{'SQLEXEC'}=$SQLEXEC ;
	}
	
}

1;
