package			# hide this package from CPAN indexer
	Win32::ODBC;

use strict;

use DBI;

# once we've been loaded we don't want perl to load the real Win32::ODBC
#$INC{'Win32/ODBC.pm'} = $INC{'Win32/DBIODBC.pm'} || 1;

#my $db = new Win32::ODBC("DSN=$self->{'DSN'};UID=$self->{'UID'};PWD=$self->{'PWD'};");

#EMU --- my $db = new Win32::ODBC("DSN=$DSN;UID=$login;PWD=$password;");
sub new
{
	shift;
	my $connect_line= shift;
	
	my $self= {};
		
	
	my @params = split(/;/,$connect_line);
	my %dsn_params;
	foreach (@params)
	{
		my ($key,$value) = split(/\=/);
 		$dsn_params{$key} = $value;
 	}

	#---- DBI CONNECT
	# Special case for SQL Server where the SERVER can be specified in the DSN
	if (exists($dsn_params{SERVER}))
	{
		$self->{'DBI_DBNAME'} = $connect_line;
		$self->{'DBI_DBH'}=DBI->connect("DBI:ODBC:" . $self->{'DBI_DBNAME'});
	}
	else
	{	
		$self->{'DBI_DBNAME'}=$dsn_params{'DSN'};
		$self->{'DBI_USER'}=$dsn_params{'UID'};
		$self->{'DBI_PASSWORD'}=$dsn_params{'PWD'};
		$self->{'DBI_DBH'}=DBI->connect($self->{'DBI_DBNAME'},
		$self->{'DBI_USER'},
		$self->{'DBI_PASSWORD'},
			'ODBC');
	}
        	
	die "Error($DBI::err) : $DBI::errstr : Connect string is $connect_line\n" if ! $self->{'DBI_DBH'}; 

        
	#---- RETURN 
	
	bless $self;
}


#EMU --- $db->Sql('SELECT * FROM DUAL');
sub Sql
{
 	my $self= shift;
 	my $SQL_statment=shift;

 #	print " SQL : $SQL_statment \n";
	
	$self->{'DBI_SQL_STATMENT'}=$SQL_statment;
	
	my $dbh=$self->{'DBI_DBH'};

 #	print " DBH : $dbh \n";
	
	my $sth=$dbh->prepare("$SQL_statment");
	
 #	print " STH : $sth \n";
	
	$self->{'DBI_STH'}=$sth;
	
	if (!$DBI::err)
	{
		$sth->execute();
		if (!$DBI::err)
		{
			$self->{'DBI_NAME'} = $sth->{NAME};
			return 0;
		}
	}

	#--- GET ERROR MESSAGES
	$self->{DBI_ERR}=$DBI::err;
	$self->{DBI_ERRSTR}=$DBI::errstr;
	return 1;
}
 

#EMU --- $db->FetchRow())
sub FetchRow
{ 
 	my $self= shift;
 	
 	my $sth=$self->{'DBI_STH'};
 	if ($sth)
	{
	 	my $hash = $sth->fetchrow_hashref;
	 	$self->{'DBI_ROW_HASH'} = $hash;

	 	if (defined($hash))
	 	{
	 		foreach (keys %{$hash})
	 		{
	 			$hash->{$_} =~ s/\s+$//;
	 		}
			#-- the row of result is not nul
			#-- return somthing nothing will be return else
			1;
	 	} 	
	}
} 
 
#EMU --- %record = $db->DataHash;
sub DataHash
{ 
 	my $self= shift;
 	
 	return %{$self->{'DBI_ROW_HASH'}};

} 

sub Data
{ 
 	my $self= shift;
 	my $col_name = shift;
 	
 	return $self->{'DBI_ROW_HASH'}->{$col_name};
} 

sub RowCount
{ 
 	my $self= shift;
 	
 	return $self->{'DBI_STH'}->rows;
} 

#EMU --- $db->Error()
sub Error
{ 
 	my $self= shift;
 	 	
 	return ($self->{'DBI_ERR'},$self->{'DBI_ERRSTR'}); 	
}

#EMU --- $db->Close()
sub Close
{ 
 	my $self= shift;
 	 	
 	$self->{'DBI_DBH'}->disconnect;
}

# Destroy the object
sub DESTROY
{
	my $self = shift;
	
	$self = undef;
} 
 
1;

__END__

=head1 NAME

Win32::DBIODBC - Win32::ODBC emulation layer for the DBI

=head1 SYNOPSIS

  use Win32::DBIODBC;     # instead of use Win32::ODBC

=head1 DESCRIPTION

This is a I<very> basic I<very> alpha quality Win32::ODBC emulation
for the DBI. To use it just replace

	use Win32::ODBC;

in your scripts with

	use Win32::DBIODBC;

or, while experimenting, you can pre-load this module without changing your
scripts by doing

	perl -MWin32::DBIODBC your_script_name

=head1 TO DO

Error handling is virtually non-existant.

=head1 AUTHOR

Tom Horen <tho@melexis.com>

=cut
