#!/usr/local/bin/perl-5.8.2

BEGIN { $ENV{'LANG'} = 'C'; };

use strict;
use DBI;
use DBD::ODBC;
use Getopt::Std;
use Carp;

sub usage {
	use File::Basename;
	die "INCORRECT USAGE OF PROGRAM \nperl ".basename($0)." -U USERNAME -S SERVERNAME -P PASSWORD -D DATABASE -Q QUERY\n";
}
use vars qw ($opt_U $opt_P $opt_S $opt_D $opt_d $opt_q $opt_Q);
usage() unless getopts('U:P:S:D:dq:Q:');
usage() unless $opt_U and $opt_P and $opt_S and $opt_D;
$opt_q = $opt_Q if $opt_Q and ! $opt_q;
$opt_q = "exec sp__id" unless $opt_q;

my($Driver)="Sybase";
$Driver="ODBC" if is_nt();
print "Using dbi:$Driver:server=$opt_S\n";

DBI->trace(8) if $opt_d;


# connect to database
my $dbh = DBI->connect("dbi:$Driver:server=$opt_S", $opt_U, $opt_P) 
		or die $DBI::errstr;

my $sth;
my $dat;

$dbh->do("use $opt_D");
unless ($sth = $dbh -> prepare ($opt_q)) { 
	print("SQL command $opt_q prepare failed ");
	$dbh->disconnect;
	exit;
}

# excute the query
unless ( $sth -> execute ) { 
	print("SQL command $opt_q execute failed ");
	$dbh->disconnect;
	exit;
}
	 
while ( $dat = $sth -> fetchrow_arrayref ) {
	print "SUCCESS $opt_S ".join(" ",@$dat),"\n";
}

$sth ->finish;
$dbh->disconnect;
exit;

sub is_nt {
	return 1 if $^O =~ /MSWin32/;
	return 0;
}
