#! /usr/local/bin/perl

# This sample client is mainly a test for calling ODBC functions
# via the network bridge.  

require 5.004;
use strict;

require RPC::PlClient;
use UnixODBC qw (:all);
use UnixODBC::BridgeServer;

my $Port = 9999;
my $MaxFieldLength = 65535;

#
# Edit for the Host Address, DSN, Login, and Table of the
# remote DBMS.
#
my $HostAddress = '127.0.0.1';
my $DSN = 'Information';
my $TableName = 'email';
my $UserName = 'mysql';
my $PassWord = 'mysql';

# SQL Query Text
my $query = "select \* from $TableName\;";

# Uncomment if you want the Driver Manager to log the ODBC 
# function calls.  Also uncomment the call to dm_log_open, 
# below.
# my $ODBCLogFile = '/tmp/bridge.log';

my $client = 
    eval { RPC::PlClient->new('peeraddr' => $HostAddress,
			  'peerport' => $Port,
			  'application' => 'RPC::PlServer',
			  'version' => $UnixODBC::VERSION,
			  'user' => 'kiesling',
			  'password' => 'password') }
    or print "Failed to make first connection: $@\n";

my $c = $client -> ClientObject ('BridgeAPI', 'new');

# Uncomment to log ODBC function calls from the Driver Manager.
# $c -> dm_log_open ('UnixODBC Bridge', $ODBCLogFile);

my $evh = 0;  # Environment Handle
my $cnh = 0;  # Connection Handle
my $sth = 0;  # Statement Handle

# Return values for sql_get_diag_rec 
my ($r, $sqlstate, $native, $text, $textlen);

# Rows and columns in result set
my ($nrows, $ncols);

$evh =  $c -> sql_alloc_handle ($SQL_HANDLE_ENV, $SQL_NULL_HANDLE);
if (defined $evh) { 
    $r = $c -> 
	sql_set_env_attr ($evh, $SQL_ATTR_ODBC_VERSION, $SQL_OV_ODBC2, 0);
} else {
    ($r, $sqlstate, $native, $text, $textlen) = 
        $c -> sql_get_diag_rec ($SQL_HANDLE_ENV, $evh, 1, 255);
    print "\nsql_alloc_handle: $r, $text, $textlen\n";
    exit 1;
}

$cnh = $c -> sql_alloc_handle ($SQL_HANDLE_DBC, $evh);

$r = $c -> sql_connect ($cnh, $DSN, length($DSN),
			$UserName, length($UserName), 
			$PassWord, length($PassWord), 0);
if ($r != 0) {
    ($r, $sqlstate, $native, $text, $textlen) = 
        $c -> sql_get_diag_rec ($SQL_HANDLE_DBC, $cnh, 1, 255);
    print "\nconnect: $r, $text, $textlen\n";
}

$sth = $c -> sql_alloc_handle ($SQL_HANDLE_STMT, $cnh);
if (! defined $sth) {
    ($r, $sqlstate, $native, $text, $textlen) = 
        $c -> sql_get_diag_rec ($SQL_HANDLE_DBC, $cnh, 1, 255);
    print "\nsql_alloc_handle sth: $r, $text, $textlen\n";
}

my $query = "select \* from $TableName\;";

$r = $c -> sql_exec_direct ($sth, $query, length ($query));
if ($r != 0) {
    ($r, $sqlstate, $native, $text, $textlen) = 
        $c -> sql_get_diag_rec ($SQL_HANDLE_STMT, $sth, 1, 255);
    print "\nsql_prepare: $r, $text, $textlen\n";
}

($r, $ncols) = $c -> sql_num_result_columns ($sth);
if ($r != 0) {
    ($r, $sqlstate, $native, $text, $textlen) = 
        $c -> sql_get_diag_rec ($SQL_HANDLE_STMT, $sth, 1, 255);
    print "\nsql_num_result_columns: $r, $text, $textlen\n";
}

($r, $nrows) = $c -> sql_row_count ($sth);
if ($r != 0) {
    ($r, $sqlstate, $native, $text, $textlen) = 
        $c -> sql_get_diag_rec ($SQL_HANDLE_STMT, $sth, 1, 255);
    print "\nsql_num_result_columns: $r, $text, $textlen\n";
}
print "\n$nrows rows, $ncols columns\n";


while (1) {
    $r = $c -> sql_fetch ($sth);
    last if $r == $SQL_NO_DATA;
    foreach my $colno (1..$ncols) {
	($r, $text, $textlen) = 
	    $c -> sql_get_data ($sth, $colno, $SQL_C_CHAR, $MaxFieldLength);
	print "$text ";
    }
    print "\n";
}

$r = $c -> sql_free_handle ($SQL_HANDLE_STMT, $sth);
if ($r != 0) {
    ($r, $sqlstate, $native, $text, $textlen) = 
        $c -> sql_get_diag_rec ($SQL_HANDLE_DBC, $cnh, 1, 255);
    print "\nfree_handle sth: $r, $text, $textlen\n";
}

$r = $c -> sql_disconnect ($cnh);
if ($r != 0) {
    ($r, $sqlstate, $native, $text, $textlen) = 
        $c -> sql_get_diag_rec ($SQL_HANDLE_DBC, $cnh, 1, 255);
    print "\nconnect: $r, $text, $textlen\n";
}

$r = $c -> sql_free_connect ($cnh);
if ($r != 0) {
    ($r, $sqlstate, $native, $text, $textlen) = 
        $c -> sql_get_diag_rec ($SQL_HANDLE_ENV, $evh, 1, 255);
    print "\nfree_connect: $r, $text, $textlen\n";
}

$r = $c -> sql_free_handle ($SQL_HANDLE_ENV, $evh);
if ($r != 0) {
    ($r, $sqlstate, $native, $text, $textlen) = 
        $c -> sql_get_diag_rec ($SQL_HANDLE_ENV, $evh, 1, 255);
    print "\nfree_connect: $r, $text, $textlen\n";
}

dm_log_close;


