#! /usr/local/bin/perl

require 5.004;
use strict;

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

my $DSN = 'Gutenberg cube1';
my $UserName = 'postgres';
my $Password = 'postgres';

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

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

$c -> dm_log_open ('UnixODBC Bridge', '/tmp/bridge.log');

my $evh = 0;
my $cnh = 0;
my $sth = 0;
my ($r, $sqlstate, $native, $text, $textlen);

my $nrows = 0;
my $ncols = 0;

$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));
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";
}

$r = $c -> sql_tables ($sth, '', 0, '', 0, '', 0, '', 0);
if ($r != 0) {
    ($r, $sqlstate, $native, $text, $textlen) = 
        $c -> sql_get_diag_rec ($SQL_HANDLE_STMT, $sth, 1, 255);
    print "\nsql_tables: $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";
}
print "\n$ncols columns\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_row_count: $r, $text, $textlen\n";
}
print "$nrows rows\n";

while (1) {
    $r = $c -> sql_fetch ($sth);
    last if $r == $SQL_NO_DATA;
    ($r, $text, $textlen) = $c -> sql_get_data ($sth, 1, $SQL_C_CHAR, 255);
    print "$text ";
    ($r, $text, $textlen) = $c -> sql_get_data ($sth, 2, $SQL_C_CHAR, 255);
    print "$text ";
    ($r, $text, $textlen) = $c -> sql_get_data ($sth, 3, $SQL_C_CHAR, 255);
    print "$text ";
    ($r, $text, $textlen) = $c -> sql_get_data ($sth, 4, $SQL_C_CHAR, 255);
    print "$text ";
    ($r, $text, $textlen) = $c -> sql_get_data ($sth, 5, $SQL_C_CHAR, 255);
    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;
