#! /etc/perl

require 5.004;
use strict;

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

my $client = 
    eval { RPC::PlClient->new('peeraddr' => '127.0.0.1',
			  'peerport' => 9999,
			  'application' => 'RPC::PlServer',
			  'version' => $UnixODBC::VERSION,
			  'user' => 'kiesling',
			  '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, 'Gutenberg Catalog', 
			length('Gutenberg Catalog'),
			'root', length('root'), '', 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 files;';

$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 = $c -> sql_end_tran ($SQL_HANDLE_STMT, $sth, 0);
if ($r != 0) {
    ($r, $sqlstate, $native, $text, $textlen) = 
        $c -> sql_get_diag_rec ($SQL_HANDLE_STMT, $sth, 1, 255);
    print "\nsql_end_tran: $r, $text, $textlen\n";
}

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


#my $name = '';
#my $namelength = 0;
#my $type = 0;
#my $size = 0;
#my $decimal_digits = 0;
#my $nullable = 0;
#($r, $name, $namelength, $type, $size, $decimal_digits, $nullable) 
#    = $c -> sql_describe_col ($sth, 1, 255);
#print "$name $namelength $type $size $decimal_digits $nullable\n";
#if ($r != 0) {
#    ($r, $sqlstate, $native, $text, $textlen) = 
#        $c -> sql_get_diag_rec ($SQL_HANDLE_STMT, $sth, 1, 255);
#    print "\nsql_execute: $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";

#
#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;


