File: xblk.t

package info (click to toggle)
libdbd-sybase-perl 1.24-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: ansic: 5,629; perl: 2,216; makefile: 4
file content (550 lines) | stat: -rw-r--r-- 12,134 bytes parent folder | download
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# -*-Perl-*-
# $Id: xblk.t,v 1.11 2005/11/04 18:35:54 mpeppler Exp $
#
#
# Small BLK test script for DBD::Sybase

use lib 't';
use _test;
use strict;

use Test::More tests => 62;

BEGIN {
  use_ok('DBI');
  use_ok('DBD::Sybase');
}

use vars qw($Pwd $Uid $Srv $Db);

( $Uid, $Pwd, $Srv, $Db ) = _test::get_info();

sub cslib_cb {
  my ( $layer, $origin, $severity, $number, $errmsg, $osmsg, $usermsg ) = @_;

  print "cslib_cb: $layer $origin $severity $number $errmsg\n";
  print "cslib_cb: User Message: $usermsg\n";

  if ( $number == 36 ) {
    return 1;
  }
  return 0;
}

$SIG{__WARN__} = sub { print @_; };

DBD::Sybase::set_cslib_cb( \&cslib_cb );

#DBI->trace(5);

my $charset = get_charset( $Srv, $Uid, $Pwd );

my $dbh = DBI->connect(
  "dbi:Sybase:$Srv;database=$Db;charset=$charset;bulkLogin=1",
  $Uid, $Pwd,
  {
    PrintError => 1,
    AutoCommit => 1,
  }
);

#			syb_err_handler => sub { local $^W = 0;
#						 print "@_\n";
#						 return 0}});

ok( defined($dbh), 'Connect' );

if ( !$dbh ) {
  warn
"No connection - did you set the user, password and server name correctly in PWD?\n";
  for ( 4 .. 62 ) {
    ok(1);
  }
  exit(0);
}

SKIP: {
  skip 'No BLK library available.', 59 unless $dbh->{syb_has_blk};

  my $rc = $dbh->do(
"create table #tmp(x numeric(9,0) identity, a1 varchar(20), i int null, n numeric(6,2), d datetime, s smalldatetime, mn money, mn1 smallmoney, b varbinary(8), img image null)"
  );

  ok( defined($rc), 'Create table' );

  test1($dbh);
  test2($dbh);
  test3($dbh);
  test4($dbh);
  test5($dbh);
  test6($dbh);
  test7($dbh);
  test8($dbh);
}

sub test1 {
  my $dbh = shift;

  $dbh->begin_work;

  #  DBI->trace(4);

  my $sth = $dbh->prepare(
    "insert #tmp values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
    {
      syb_bcp_attribs => {
        identity_flag   => 0,
        identity_column => 1
      }
    }
  );
  ok( defined($sth), 'Prepare #1' );

  my @data = (
    [
      undef,         "one",     123,   123.4,      'Oct 11 2001 11:00',
      'Oct 11 2001', 23.456789, 44.23, 'deadbeef', 'x' x 1000
    ],
    [
      undef,               "two",         -1,             123.456,
      'Oct 12 2001 11:23', 'Oct 11 2001', 44444444444.34, 44353.44,
      '0a0a0a0a',          'a' x 100
    ],
    [
      undef,               "three",       undef,       1234.78,
      'Oct 11 2001 11:00', 'Oct 11 2001', 343434.3333, 34.23,
      '20202020',          'z' x 100
    ]
  );

  my $rc;
  my $i = 1;
  foreach (@data) {
    $_->[8] = pack( 'H*', $_->[8] );
    $rc = $sth->execute(@$_);
    ok( defined($rc), "Send row $i - test 1" );
    ++$i;
  }

  $rc = $dbh->commit();
  ok( $rc, 'Commit test 1' );
  my $rows = $sth->rows();
  ok( $rows == 3, 'Rows test 1' );

  $sth->finish;

  #  DBI->trace(0);
}

sub test2 {
  my $dbh = shift;

  # Now test conversion failures. None of these rows should get loaded.

  $dbh->begin_work;

  my $sth = $dbh->prepare(
    "insert #tmp values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
    {
      syb_bcp_attribs => {
        identity_flag   => 0,
        identity_column => 1
      }
    }
  );
  ok( defined($sth), 'prepare #2' );

  my @data = (
    [
      undef,         "one b",   123,   123.4,      'feb 29 2001 11:00',
      'Oct 11 2001', 23.456789, 44.23, 'deadbeef', 'x' x 100
    ],
    [
      undef,               "two b",       123456789123456, 123.456,
      'Oct 12 2001 11:23', 'Oct 11 2001', 44444444444.34,  44353.44,
      '0a0a0a0a',          'a' x 100
    ],
    [
      undef,               "three b",     undef,       123456.78,
      'Oct 11 2001 11:00', 'Oct 11 2001', 343434.3333, 34.23,
      '20202020',          'z' x 100
    ],
    [
      undef,         "four b",    undef, 126.78, 'Oct 11 2001 11:00',
      'Oct 11 2001', 343434.3333, "34343434343434343434.23", '21212121',
      'z' x 100
    ],
  );

  my $i = 1;
  my $rc;
  foreach (@data) {
    $_->[8] = pack( 'H*', $_->[8] );
    $rc = $sth->execute(@$_);
    ok( !defined($rc), "Execute row $i, test 2" );
    ++$i;
  }
  $rc = $dbh->commit;
  ok( $rc, 'Commit test 2' );

  my $rows = $sth->rows;
  ok( $rows == 0, 'Rows, test 2' );

  $sth->finish;
}

# Test explicit identity value inserts.
sub test3 {
  my $dbh = shift;

  $dbh->begin_work;

  my $sth = $dbh->prepare(
    "insert #tmp values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
    {
      syb_bcp_attribs => {
        identity_flag   => 1,
        identity_column => 0
      }
    }
  );
  ok( defined($sth), 'Prepare #3' );

  my @data = (
    [
      10,           "one",       123,   123.4,      'Nov 1 2001 12:00',
      'Nov 1 2001', 343434.3333, 34.23, 'deadbeef', 'z' x 100
    ],
    [
      11, "two", -1, 123.456,
      '11/1/2001 12:00',
      '11/1/2001 11:21',
      343434.3333, 34.23, '25252525', 'z' x 100
    ],
    [
      12,                 "three",      undef,       123,
      'Nov 1 2001 12:00', 'Nov 1 2001', 343434.3333, 34.23,
      '43434343',         'z' x 100
    ]
  );
  my $i = 1;
  my $rc;
  foreach (@data) {
    $_->[8] = pack( 'H*', $_->[8] );
    $rc = $sth->execute(@$_);
    ok( defined($rc), "Execute row $i, test 3" );
    ++$i;
  }

  $rc = $dbh->commit;
  ok( $rc, 'Commit, test 3' );

  my $rows = $sth->rows;
  ok( $rows == 3, 'Rows, test 3' );

  $sth->finish;
}

# Test for prepare failures
sub test4 {
  my $dbh = shift;

  $dbh->begin_work;

  my $sth = $dbh->prepare(
    "insrt #tmp values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
    {
      syb_bcp_attribs => {
        identity_flag   => 1,
        identity_column => 0
      }
    }
  );
  ok( !defined($sth), 'Prepare #4' );
  print $dbh->errstr, "\n";

  #  DBI->trace(5);
  my $sth1 = $dbh->prepare(
    "select * from #tmp where foo = ?",
    {
      syb_bcp_attribs => {
        identity_flag   => 1,
        identity_column => 0
      }
    }
  );
  ok( !defined($sth1), 'Prepare #5' );
  my $sth2 = $dbh->prepare(
    "select * from #tmp",
    {
      syb_bcp_attribs => {
        identity_flag   => 1,
        identity_column => 0
      }
    }
  );
  ok( !defined($sth2), 'Prepare #6' );
  print $dbh->errstr, "\n";
}

# Test for missing commit/finish.
sub test5 {
  my $dbh = shift;

  $dbh->begin_work;

  my $sth = $dbh->prepare(
    "insert #tmp values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
    {
      syb_bcp_attribs => {
        identity_flag   => 0,
        identity_column => 1
      }
    }
  );
  ok( defined($sth), 'Prepare test 5' );

  my @data = (
    [
      undef,               "test5 one",   123,       123.4,
      'Oct 11 2001 11:00', 'Oct 11 2001', 23.456789, 44.23,
      'deadbeef',          'x' x 1000
    ],
    [
      undef,               "test5 two",   -1,             123.456,
      'Oct 12 2001 11:23', 'Oct 11 2001', 44444444444.34, 44353.44,
      '0a0a0a0a',          'a' x 100
    ],
    [
      undef,               "test5 three", undef,       1234.78,
      'Oct 11 2001 11:00', 'Oct 11 2001', 343434.3333, 34.23,
      '20202020',          'z' x 100
    ]
  );

  my $rc;
  my $i = 1;
  foreach (@data) {
    $_->[8] = pack( 'H*', $_->[8] );
    $rc = $sth->execute(@$_);
    ok( defined($rc), "Send row $i - test 5" );
    ++$i;
  }

  local $^W = 0;
  $sth->finish;
}

# Test for rollback.
sub test6 {
  my $dbh = shift;

  $dbh->begin_work;

  my $sth = $dbh->prepare(
    "insert #tmp values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
    {
      syb_bcp_attribs => {
        identity_flag   => 0,
        identity_column => 1
      }
    }
  );
  ok( defined($sth), 'Prepare test 6' );

  my @data = (
    [
      undef,               "test6 one",   123,       123.4,
      'Oct 11 2001 11:00', 'Oct 11 2001', 23.456789, 44.23,
      'deadbeef',          'x' x 1000
    ],
    [
      undef,               "test6 two",   -1,             123.456,
      'Oct 12 2001 11:23', 'Oct 11 2001', 44444444444.34, 44353.44,
      '0a0a0a0a',          'a' x 100
    ],
    [
      undef,               "test6 three", undef,       1234.78,
      'Oct 11 2001 11:00', 'Oct 11 2001', 343434.3333, 34.23,
      '20202020',          'z' x 100
    ]
  );

  my $rc;
  my $i = 1;
  foreach (@data) {
    $_->[8] = pack( 'H*', $_->[8] );
    $rc = $sth->execute(@$_);
    ok( defined($rc), "Send row $i - test 6" );
    ++$i;
  }

  $rc = $dbh->rollback;
  ok( $rc, 'test 6 rollback' );
  $rc = $sth->finish;
  ok( $rc, 'test 6 finish' );
  $sth = undef;

  $dbh->begin_work;
  my $sth2 = $dbh->prepare("select count(*) from #tmp where a1 like 'test6 %'");
  ok( defined($sth2), 'test 6 prepare select' );
  $rc = $sth2->execute;
  ok( $rc, 'test 6 execute select' );
  my $row = $sth2->fetch;
  ok( $row && $row->[0] == 0, 'test 6 row value' );
  $sth2->finish;
  $sth2 = undef;
  $dbh->commit;

  $dbh->begin_work;

  $sth = $dbh->prepare(
    "insert #tmp values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
    {
      syb_bcp_attribs => {
        identity_flag   => 0,
        identity_column => 1
      }
    }
  );
  ok( defined($sth), 'Prepare test 6 (2)' );

  foreach (@data) {
    $_->[8] = pack( 'H*', $_->[8] );
    $rc = $sth->execute(@$_);
    ok( defined($rc), "Send row $i - test 6" );
    ++$i;
  }
  $rc = $dbh->commit;
  ok( $rc, 'test 6 commit' );
  foreach (@data) {
    $_->[8] = pack( 'H*', $_->[8] );
    $rc = $sth->execute(@$_);
    ok( defined($rc), "Send row $i - test 6" );
    ++$i;
  }

  $rc = $dbh->rollback;
  ok( $rc, 'test 6 rollback' );
  $rc = $sth->finish;
  ok( $rc, 'test 6 finish' );
  $sth = undef;

  #  DBI->trace(0);
}

sub test7 {
  my $dbh = shift;

  $dbh->{AutoCommit} = 1;

  # Test some of the data in the #tmp table.

  my $sth = $dbh->prepare("select count(*), sum(i), sum(n) from #tmp");
  ok( defined($sth), 'prepare test 7' );
  my $rc = $sth->execute;
  ok( $rc, 'execute test 7' );
  my ( $c, $i, $n );
  while ( my $row = $sth->fetch ) {
    ( $c, $i, $n ) = @$row;
    print "@$row\n";
  }
  ok( $c == 9,       'Row count' );
  ok( $i == 366,     'Sum(i)' );
  ok( $n == 3333.11, 'Sum(n)' );
}

# Turn autocommit off, update some data, then try to run
# a bcp operation.
# This tests to make sure that the AutoCommit/CHAINED mode flip/flop
# happens correctly
sub test8 {
  my $dbh = shift;

  #DBI->trace(4);
  $dbh->begin_work;

  my $sth = $dbh->prepare("update #tmp set i = 20 where i = 123");
  ok( defined($dbh), 'Prepare update test 8' );
  my $rc = $sth->execute;
  ok( $rc, 'Execute update test 8' );
  $sth = undef;

  $sth = $dbh->prepare(
    "insert #tmp values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
    {
      syb_bcp_attribs => {
        identity_flag   => 0,
        identity_column => 1
      }
    }
  );
  ok( defined($sth), 'Prepare test 8' );

  my @data = (
    [
      undef,         "one",     123,   123.4,      'Oct 11 2001 11:00',
      'Oct 11 2001', 23.456789, 44.23, 'deadbeef', 'x' x 1000
    ],
    [
      undef,               "two",         -1,             123.456,
      'Oct 12 2001 11:23', 'Oct 11 2001', 44444444444.34, 44353.44,
      '0a0a0a0a',          'a' x 100
    ],
    [
      undef,               "three",       undef,       1234.78,
      'Oct 11 2001 11:00', 'Oct 11 2001', 343434.3333, 34.23,
      '20202020',          'z' x 100
    ]
  );

  my $i = 1;
  foreach (@data) {
    $_->[8] = pack( 'H*', $_->[8] );
    $rc = $sth->execute(@$_);
    ok( defined($rc), "Send row $i - test 8" );
    ++$i;
  }

  $rc = $dbh->commit();
  ok( $rc, 'Commit test 8' );
  my $rows = $sth->rows();
  ok( $rows == 3, 'Rows test 8' );

  #  $sth->finish;
  $sth = undef;
}

sub get_charset {
  my $srv = shift;
  my $uid = shift;
  my $pwd = shift;

  my $dbh = DBI->connect( "dbi:Sybase:$srv", $uid, $pwd );
  die "Can't connect to $srv" unless $dbh;

  my $sth = $dbh->prepare("sp_configure 'default character set id'");
  $sth->execute;
  my $id;
  while ( my $r = $sth->fetch ) {
    $id = $r->[4];
  }
  $sth->finish;
  if ( !$id ) {
    warn "Can't find charset id - using iso_1";
    return 'iso_1';
  }

  $sth = $dbh->prepare("select name from master..syscharsets where id = $id");
  $sth->execute;
  my $charset;
  while ( my $r = $sth->fetch ) {
    $charset = $r->[0];
  }
  if ( !defined($charset) ) {
    warn "Can't find charset name - using iso_1";
    return 'iso_1';
  }

  return $charset;
}