File: rt_39841.t

package info (click to toggle)
libdbd-odbc-perl 1.37-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,272 kB
  • sloc: perl: 7,932; ansic: 5,991; makefile: 33; sql: 8
file content (316 lines) | stat: -rw-r--r-- 8,565 bytes parent folder | download | duplicates (2)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/perl -w -I./t
# $Id: rt_39841.t 12710 2009-04-20 15:21:32Z mjevans $
#
# Test fix for rt 39841 - problem with SQLDecribeParam in MS SQL Server
#
use Test::More;
use strict;
use Data::Dumper;
$| = 1;

my $has_test_nowarnings = 1;
eval "require Test::NoWarnings";
$has_test_nowarnings = undef if $@;
my $tests = 28;
$tests += 1 if $has_test_nowarnings;
plan tests => $tests;

# can't seem to get the imports right this way
use DBI qw(:sql_types);
#1
use_ok('ODBCTEST');

my $dbh;

BEGIN {
   if (!defined $ENV{DBI_DSN}) {
      plan skip_all => "DBI_DSN is undefined";
   }
}

END {
    if ($dbh) {
        eval {
            local $dbh->{PrintWarn} = 0;
            local $dbh->{PrintError} = 0;
            $dbh->do(q/drop table PERL_DBD_rt_39841a/);
            $dbh->do(q/drop table PERL_DBD_rt_39841b/);
        };
    }
    Test::NoWarnings::had_no_warnings()
          if ($has_test_nowarnings);
}

$dbh = DBI->connect();
unless($dbh) {
   BAIL_OUT("Unable to connect to the database $DBI::errstr\nTests skipped.\n");
   exit 0;
}
$dbh->{RaiseError} = 1;
my $dbms_name = $dbh->get_info(17);
#2
ok($dbms_name, "got DBMS name: $dbms_name");
my $dbms_version = $dbh->get_info(18);
#3
ok($dbms_version, "got DBMS version: $dbms_version");
my $driver_name = DBI::neat($dbh->get_info(6));

my ($ev, $sth);

SKIP: {
    skip "not SQL Server", 25 if $dbms_name !~ /Microsoft SQL Server/;
    skip "not SQL Server ODBC or native client driver", 25
        if ($driver_name !~ /SQLSRV32.DLL/oi) &&
            ($driver_name !~ /sqlncli10.dll/oi) &&
                ($driver_name !~ /SQLNCLI>DLL/oi);

    my $major_version = $dbms_version;

    eval {
        local $dbh->{PrintWarn} = 0;
        local $dbh->{PrintError} = 0;
        $dbh->do('drop table PERL_DBD_39841a');
        $dbh->do('drop table PERL_DBD_39841b');
    };

    test_1($dbh);       # 16 tests
    test_2($dbh);       # 9 tests
};

#
# A bug in the SQL Server OBDC driver causes SQLDescribeParam to
# report the parameter as an integer of column_size 10 instead of
# a varchar of column size 10. Thus when you execute with 'bbbbbb'
# SQL Server will complain that an unsupported conversion has occurred.
# We can work around this by specifically telling DBD::ODBC to bind
# as a VARCHAR.
# The bug is due to SQL Server rearranging the SQL above to:
# select a1 from PERL_DBD_38941a where 1 = 2
# and it should have run
# select b2 from PERL_DBD_38941b where 1 = 2
#
sub test_1
{
    $dbh = shift;
    my $sth;

    eval {
        $dbh->do('create table PERL_DBD_39841a (a1 integer, a2 varchar(20))');
        $dbh->do('create table PERL_DBD_39841b (b1 double precision, b2 varchar(8))');
    };
    $ev = $@;
    #1
    ok(!$ev, 'create test tables');

  SKIP: {
        skip "Failed to create test table", 10 if ($ev);
        eval {
            $dbh->do(q/insert into PERL_DBD_39841a values(1, 'aaaaaaaaaa')/);
            $dbh->do(q/insert into PERL_DBD_39841b values(1, 'bbbbbbbb')/);
        };
        $ev = $@;
        #2
        ok(!$ev, "populate tables");

        eval {
            $sth = $dbh->prepare(q/select b1, ( select a2 from PERL_DBD_39841a where a1 = b1 ) from PERL_DBD_39841b where b2 = ?/);
        };
        $ev = $@;
        #3
        ok(!$ev, 'prepare select');

      SKIP: {			# 13
	  skip 'cannot prepare SQL for test', 13 if $ev;
	  eval {
	      local $sth->{PrintError} = 0;
	      $sth->execute('bbbbbb');
	  };
	  my $ev = $@;

        SKIP: {			# 5
	    if ($ev) {
                diag($dbh->errstr);
		diag($dbh->state);
		if ($dbh->state eq '22018') {
                    diag("\nNOTE: Your SQL Server ODBC driver has a bug which can describe parameters\n");
                    diag("in SQL using sub selects incorrectly. In this case a VARCHAR(8) parameter\n");
                    diag("is described as an INTEGER\n\n");
		    skip 'test_1 execute failed - bug in SQL Server ODBC Driver', 5;
		} else {
		    skip 'test_1 execute failed with unexpected error', 5;
		}
            }
	    #1
            pass('test_1 execute');
	    #2
	    is($sth->{NUM_OF_PARAMS}, 1, 'correct number of parameters');
	    #diag(Dumper($sth->{ParamTypes}));
	    #3
	    is($sth->{NUM_OF_FIELDS}, 2, 'fields in result-set');
	    my $count;
	    eval {
		while($sth->fetchrow_array) {
		    $count = 0 if !defined($count);
		    $count++};
	    };
	    #4
	    ok(!$ev, "fetchrow_array");
	    #5
	    ok(!defined($count), "no rows returned");
	  };

	SKIP: {			# 8
	    skip "no bug found", 8 if !$ev;
	    skip "unexpected error this test is not checking for", 8
		if ($dbh->state ne '22018');
	    diag("Checking you can work around bug in SQL Server ODBC Driver");
	    eval {
		$sth->bind_param(1, 'bbbbbb', SQL_VARCHAR);
		$sth->execute;
	    };
	    $ev = $@;
	    if ($ev) {
		diag("No you cannot");
		skip "Cannot work around bug", 4;
	    } else {
		diag("Yes you can");
		#1
		is($sth->{NUM_OF_PARAMS}, 1, 'correct number of parameters');
		#2
		is($sth->{NUM_OF_FIELDS}, 2, 'fields in result-set');
		#diag(Dumper($sth->{ParamTypes}));
		my $pv = $sth->{ParamValues};
		#3
		ok(defined($pv), "Parameter values");
	      SKIP: {
          	  skip "no parameter values", 3 if !$pv;
		  #1
          	  is(ref($pv), 'HASH', 'parameter value hash');
		  #2
          	  ok(exists($pv->{1}), 'parameter 1 exists');
          	  SKIP: {
		      skip "no p1", 1 if !exists($pv->{1});
		      #1
		      is($pv->{1}, 'bbbbbb', 'parameter has right value');
          	  };
		};
		my $count;
		eval {
		    while($sth->fetchrow_array) {
			$count = 0 if !defined($count);
			$count++};
		};
		#4
		ok(!$ev, "fetchrow_array");
		#5
		ok(!defined($count), "no rows returned");
	    }
	  };
	};
    }
    eval {
        local $dbh->{PrintWarn} = 0;
        local $dbh->{PrintError} = 0;
        $dbh->do('drop table PERL_DBD_39841a');
        $dbh->do('drop table PERL_DBD_39841b');
    };
}

#
# Here SQL Server gets confused and rearranges the SQL to find out about
# PERL_DBD_39841a.a2 when it should have returned information about
# PERL_DBD_39841b.b2. This used to lead to DBD::ODBC binding p1 as
# 'bbbbbbbbbbbbbbbbbbbb' but specifying a column size of 10 - hence
# data truncation error.
#
sub test_2
{
    $dbh = shift;
    my $sth;

    eval {
	local $dbh->{PrintError} = 1;
        $dbh->do('create table PERL_DBD_39841a (a1 integer, a2 varchar(10))');
        $dbh->do('create table PERL_DBD_39841b (b1 varchar(10), b2 varchar(20))');
    };
    $ev = $@;
    #1
    ok(!$ev, 'create test tables');

  SKIP: {                       # 8
        skip "Failed to create test table", 8 if ($ev);
        eval {
            $dbh->do(q/insert into PERL_DBD_39841a values(1, 'aaaaaaaaaa')/);
            $dbh->do(q/insert into PERL_DBD_39841b values('aaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbb')/);
        };
        $ev = $@;
        #1 1
        ok(!$ev, "populate tables");

        eval {
            $sth = $dbh->prepare(q/select b1, ( select a2 from PERL_DBD_39841a where a2 = b1 ) from PERL_DBD_39841b where b2 = ?/);
        };
        $ev = $@;
        #1 2
        ok(!$ev, 'prepare select');

      SKIP: {			# 6
	  skip 'cannot prepare SQL for test', 6 if $ev;
	  eval {
	      local $sth->{PrintError} = 0;
	      $sth->execute('bbbbbbbbbbbbbbbbbbbb');
	  };
	  my $ev = $@;

        SKIP: {			# 5 + 1
	    if ($ev) {
                diag($dbh->errstr);
		diag($dbh->state);
		if ($dbh->state eq '22001') {
		    diag("Bug 39841 is back in some unexpected way");
		    diag("Please report this via rt");
		    #1
		    fail('test_1 execute');
		    skip 'Bug 39841 is back', 5;
		} else {
		    diag("Unexpected error - please report this via rt");
		    fail('test_1 execute');
		    #1
		    skip 'unexpected error', 5;
		}
            } else {
		#1
		pass('test_1 execute');
	    }
	    #2
	    is($sth->{NUM_OF_PARAMS}, 1, 'correct number of parameters');
	    #diag(Dumper($sth->{ParamTypes}));
	    #3
	    is($sth->{NUM_OF_FIELDS}, 2, 'fields in result-set');
	    my $count;
	    eval {
		while($sth->fetchrow_array) {
		    $count = 0 if !defined($count);
		    $count++};
	    };
	    #4
	    ok(!$ev, "fetchrow_array");
	    #5
	    ok(defined($count), "rows returned");
	  SKIP: {               # 1
	      skip "no rows returned", 1 if !defined($count);
	      # 6
	      is($count, 1, 'correct number of rows returned');
	    };
	  };
	};
    }
    eval {
        local $dbh->{PrintWarn} = 0;
        local $dbh->{PrintError} = 0;
        $dbh->do('drop table PERL_DBD_39841a');
        $dbh->do('drop table PERL_DBD_39841b');
    };
}