1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#!/usr/bin/perl -I./t -w
# $Id: 09multi.t 550 2004-10-31 15:29:32Z jurl $
use Test::More;
$| = 1;
use_ok('strict');
use_ok('DBI');
use_ok('ODBCTEST');
my $tests;
# to help ActiveState's build process along by behaving (somewhat) if a dsn is not provided
BEGIN {
$tests = 7;
if (!defined $ENV{DBI_DSN}) {
plan skip_all => "DBI_DSN is undefined";
} else {
plan tests => $tests;
}
}
# $ENV{'ODBCINI'}="/export/cmn/etc/odbc.ini" ;
#my($connectString) = "dbi:ODBC:DSN=TESTDB;Database=xxxxx;uid=usrxxxxx;pwd=xxxxx" ;
my $dbh=DBI->connect();
unless($dbh) {
BAILOUT("Unable to connect to the database $DBI::errstr\nTests skipped.\n");
exit 0;
}
$dbh->{RaiseError} = 1;
$dbh->{PrintError} = 0;
$dbh->{LongReadLen} = 10000;
SKIP:
{
skip "Multiple statements not supported using " . $dbh->get_info(17) . " (SQL_MULT_RESULT_SETS)", $tests-3 unless ($dbh->get_info(36) eq "Y");
my($sqlStr) ;
my @test_colnames = sort(keys(%ODBCTEST::TestFieldInfo));
$sqlStr = "select $test_colnames[0] FROM $ODBCTEST::table_name
select $test_colnames[0] from $ODBCTEST::table_name" ;
#$sqlStr = "select emp_id from employee where emp_id = 2
# select emp_id, emp_name, address1, address2 from employee where emp_id = 2" ;
my $result_sets = 0;
my $sth;
eval {
$sth = $dbh->prepare($sqlStr);
$sth->execute;
};
if ($@) {
skip("Multiple statements not supported using " . $dbh->get_info(17) . "\n", $tests-3);
}
my @row;
my $cnt = 0;
$result_sets = 0;
do {
# print join(":", @{$sth->{NAME}}), "\n";
while ( my $ref = $sth->fetch ) {
# print join(":", @$ref), "\n";
}
$result_sets++;
} while ( $sth->{odbc_more_results} ) ;
is($result_sets, 2, "count number of result sets");
my $sql;
my @expected_result_cols;
# lets get some dummy data for testing.
ODBCTEST::tab_insert($dbh);
$sql = "select $test_colnames[0] from $ODBCTEST::table_name order by $test_colnames[0]
select $test_colnames[0],$test_colnames[1] from $ODBCTEST::table_name order by $test_colnames[0]";
@expected_result_cols = (1, 2);
ok(RunMultiTest($sql, \@expected_result_cols), "Multiple result sets with different column counts (less then more)");
$sql = "select $test_colnames[0],$test_colnames[1] from $ODBCTEST::table_name order by $test_colnames[0]
select $test_colnames[0] from $ODBCTEST::table_name order by $test_colnames[0]";
@expected_result_cols = (2, 1);
ok(RunMultiTest($sql, \@expected_result_cols), "Multiple result sets with different column counts (more then less)");
$sql = "select " . join(", ", grep {/COL_[ABC]/} @test_colnames) . " from $ODBCTEST::table_name order by $test_colnames[0]
select $test_colnames[0] from $ODBCTEST::table_name order by $test_colnames[0]";
@expected_result_cols = ($#test_colnames, 1);
ok(RunMultiTest($sql, \@expected_result_cols), "Multiple result sets with multiple cols, then second result set with one col");
# clean up the dummy data.
ODBCTEST::tab_delete($dbh);
};
$dbh->disconnect();
sub RunMultiTest {
my $sql = shift;
my $ref_expected_result_cols = shift;
my @expected_result_cols = @$ref_expected_result_cols;
my $test_pass = 1;
my $result_sets = 0;
$sth = $dbh->prepare($sql);
$sth->execute;
do {
# $#expected_result_cols is the array of number of result cols
# and the count/array size represents the number of result sets...
if ($result_sets > $#expected_result_cols) {
print "Number of result sets not correct in test $result_sets is more than the expected $#expected_result_cols.\n";
$test_pass = 0;
} else {
if ($sth->{NUM_OF_FIELDS} != $expected_result_cols[$result_sets]) {
print "Num of fields not correct in result set $result_sets. Expected $expected_result_cols[$result_sets], found $sth->{NUM_OF_FIELDS}\n";
$test_pass = 0;
}
}
# print join(", ", @{$sth->{NAME}}), "\n";
my $i = 0;
while ( my $ref = $sth->fetchrow_arrayref ) {
# if ($] > 5.005) {
# no warnings;
# print join(":", @$ref), "\n";
#}
my $row = $ODBCTEST::tab_insert_values[$i];
my $j;
for ($j = 0; $j < $sth->{NUM_OF_FIELDS}; $j++) {
if ($row->[$j] ne $ref->[$j]) {
print "Data mismatch, result set $result_sets, row $i, col $j ($row->[$j] != $ref->[$j])\n";
$test_pass = 0;
}
}
$i++;
}
$result_sets++;
} while ( $sth->{odbc_more_results} ) ;
if ($result_sets <= $#expected_result_cols) {
print "Number of result sets not correct in test (fewer than expected)\n";
$test_pass = 0;
}
$test_pass;
}
exit(0);
print $DBI::errstr;
print $ODBCTEST::tab_insert_values[0];
print sort(keys(%ODBCTEST::TestFieldInfo));
|