forked from perl5-dbi/DBD-ODBC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathODBC.pm
2799 lines (2125 loc) · 104 KB
/
ODBC.pm
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (c) 1994,1995,1996,1998 Tim Bunce
# portions Copyright (c) 1997-2004 Jeff Urlwin
# portions Copyright (c) 1997 Thomas K. Wenrich
# portions Copyright (c) 2007-2014 Martin J. Evans
#
# You may distribute under the terms of either the GNU General Public
# License or the Artistic License, as specified in the Perl README file.
## no critic (ProhibitManyArgs ProhibitMultiplePackages)
require 5.008;
# NOTE: Don't forget to update the version reference in the POD below too.
# NOTE: If you create a developer release x.y_z ensure y is greater than
# the preceding y in the non developer release e.g., 1.24 should be followed
# by 1.25_1 and then released as 1.26.
# see discussion on dbi-users at
# http://www.nntp.perl.org/group/perl.dbi.dev/2010/07/msg6096.html and
# http://www.dagolden.com/index.php/369/version-numbers-should-be-boring/
$DBD::ODBC::VERSION = '1.56';
{
## no critic (ProhibitMagicNumbers ProhibitExplicitISA)
## no critic (ProhibitPackageVars)
package DBD::ODBC;
use DBI ();
use DynaLoader ();
use Exporter ();
@ISA = qw(Exporter DynaLoader);
# my $Revision = substr(q$Id$, 13,2);
require_version DBI 1.609;
bootstrap DBD::ODBC $VERSION;
$err = 0; # holds error code for DBI::err
$errstr = q{}; # holds error string for DBI::errstr
$sqlstate = "00000"; # starting state
$drh = undef; # holds driver handle once initialised
use constant {
# header fields in SQLGetDiagField:
SQL_DIAG_CURSOR_ROW_COUNT => -1249,
SQL_DIAG_DYNAMIC_FUNCTION => 7,
SQL_DIAG_DYNAMIC_FUNCTION_CODE => 12,
SQL_DIAG_NUMBER => 2,
SQL_DIAG_RETURNCODE => 1,
SQL_DIAG_ROW_COUNT => 3,
# record fields in SQLGetDiagField:
SQL_DIAG_CLASS_ORIGIN => 8,
SQL_DIAG_COLUMN_NUMBER => -1247,
SQL_DIAG_CONNECTION_NAME => 10,
SQL_DIAG_MESSAGE_TEXT => 6,
SQL_DIAG_NATIVE => 5,
SQL_DIAG_ROW_NUMBER => -1248,
SQL_DIAG_SERVER_NAME => 11,
SQL_DIAG_SQLSTATE => 4,
SQL_DIAG_SUBCLASS_ORIGIN => 9,
# TAF constants - these are just copies of Oracle constants
# events:
OCI_FO_END => 0x00000001,
OCI_FO_ABORT => 0x00000002,
OCI_FO_REAUTH => 0x00000004,
OCI_FO_BEGIN => 0x00000008,
OCI_FO_ERROR => 0x00000010,
# callback return codes:
OCI_FO_RETRY => 25410,
# types:
OCI_FO_NONE => 0x00000001,
OCI_FO_SESSION => 0x00000002,
OCI_FO_SELECT => 0x00000004,
OCI_FO_TXNAL => 0x00000008
};
our @EXPORT_DIAGS = qw(SQL_DIAG_CURSOR_ROW_COUNT SQL_DIAG_DYNAMIC_FUNCTION SQL_DIAG_DYNAMIC_FUNCTION_CODE SQL_DIAG_NUMBER SQL_DIAG_RETURNCODE SQL_DIAG_ROW_COUNT SQL_DIAG_CLASS_ORIGIN SQL_DIAG_COLUMN_NUMBER SQL_DIAG_CONNECTION_NAME SQL_DIAG_MESSAGE_TEXT SQL_DIAG_NATIVE SQL_DIAG_ROW_NUMBER SQL_DIAG_SERVER_NAME SQL_DIAG_SQLSTATE SQL_DIAG_SUBCLASS_ORIGIN);
our @EXPORT_TAF = qw(OCI_FO_END OCI_FO_ABORT OCI_FO_REAUTH OCI_FO_BEGIN OCI_FO_ERROR OCI_FO_RETRY OCI_FO_NONE OCI_FO_SESSION OCI_FO_SELECT OCI_FO_TXNAL);
our @EXPORT_OK = (@EXPORT_DIAGS, @EXPORT_TAF);
our %EXPORT_TAGS = (
diags => \@EXPORT_DIAGS,
taf => \@EXPORT_TAF);
sub parse_trace_flag {
my ($class, $name) = @_;
return 0x02_00_00_00 if $name eq 'odbcunicode';
return 0x04_00_00_00 if $name eq 'odbcconnection';
return DBI::parse_trace_flag($class, $name);
}
sub parse_trace_flags {
my ($class, $flags) = @_;
return DBI::parse_trace_flags($class, $flags);
}
my $methods_are_installed = 0;
sub driver{
return $drh if $drh;
my($class, $attr) = @_;
$class .= "::dr";
# not a 'my' since we use it above to prevent multiple drivers
$drh = DBI::_new_drh($class, {
'Name' => 'ODBC',
'Version' => $VERSION,
'Err' => \$DBD::ODBC::err,
'Errstr' => \$DBD::ODBC::errstr,
'State' => \$DBD::ODBC::sqlstate,
'Attribution' => 'DBD::ODBC by Jeff Urlwin, Tim Bunce and Martin J. Evans',
});
if (!$methods_are_installed) {
DBD::ODBC::st->install_method("odbc_lob_read");
DBD::ODBC::st->install_method("odbc_rows", { O=>0x00000000 });
DBD::ODBC::st->install_method("odbc_describe_param", { O=>0x00000000 });
# don't clear errors - IMA_KEEP_ERR = 0x00000004
DBD::ODBC::st->install_method("odbc_getdiagrec", { O=>0x00000004 });
DBD::ODBC::db->install_method("odbc_getdiagrec", { O=>0x00000004 });
DBD::ODBC::db->install_method("odbc_getdiagfield", { O=>0x00000004 });
DBD::ODBC::st->install_method("odbc_getdiagfield", { O=>0x00000004 });
$methods_are_installed++;
}
return $drh;
}
sub CLONE { undef $drh }
1;
}
{ package DBD::ODBC::dr; # ====== DRIVER ======
use strict;
use warnings;
## no critic (ProhibitBuiltinHomonyms)
sub connect {
my($drh, $dbname, $user, $auth, $attr)= @_;
#$user = q{} unless defined $user;
#$auth = q{} unless defined $auth;
# create a 'blank' dbh
my $this = DBI::_new_dbh($drh, {
'Name' => $dbname,
'USER' => $user,
'CURRENT_USER' => $user,
});
# Call ODBC _login func in Driver.xst file => dbd_db_login6
# and populate internal handle data.
# There are 3 versions (currently) if you have a recent DBI:
# dbd_db_login (oldest)
# dbd_db_login6 (with attribs hash & char * args) and
# dbd_db_login6_sv (as dbd_db_login6 with perl scalar args
DBD::ODBC::db::_login($this, $dbname, $user, $auth, $attr) or return;
return $this;
}
## use critic
sub data_sources {
my ($drh, $attr) = @_;
my $dsref = DBD::ODBC::dr::_data_sources( $drh, $attr );
if( defined( $dsref ) && ref( $dsref ) eq "ARRAY" ) {
return @$dsref;
}
return (); # Return empty array
}
}
{ package DBD::ODBC::db; # ====== DATABASE ======
use strict;
use warnings;
use constant SQL_DRIVER_HSTMT => 5;
use constant SQL_DRIVER_HLIB => 76;
use constant SQL_DRIVER_HDESC => 135;
sub parse_trace_flag {
my ($h, $name) = @_;
return DBD::ODBC->parse_trace_flag($name);
}
sub private_attribute_info {
return {
odbc_ignore_named_placeholders => undef, # sth and dbh
odbc_default_bind_type => undef, # sth and dbh
odbc_force_bind_type => undef, # sth and dbh
odbc_force_rebind => undef, # sth and dbh
odbc_async_exec => undef, # sth and dbh
odbc_exec_direct => undef,
odbc_describe_parameters => undef,
odbc_SQL_ROWSET_SIZE => undef,
odbc_SQL_DRIVER_ODBC_VER => undef,
odbc_cursortype => undef,
odbc_query_timeout => undef, # sth and dbh
odbc_has_unicode => undef,
odbc_out_connect_string => undef,
odbc_version => undef,
odbc_err_handler => undef,
odbc_putdata_start => undef, # sth and dbh
odbc_column_display_size => undef, # sth and dbh
odbc_utf8_on => undef, # sth and dbh
odbc_driver_complete => undef,
odbc_batch_size => undef,
odbc_array_operations => undef, # sth and dbh
odbc_taf_callback => undef,
odbc_trace => undef, # dbh
odbc_trace_file => undef, # dbh
};
}
sub prepare {
my($dbh, $statement, @attribs)= @_;
# create a 'blank' sth
my $sth = DBI::_new_sth($dbh, {
'Statement' => $statement,
});
# Call ODBC func in ODBC.xs file.
# (This will actually also call SQLPrepare for you.)
# and populate internal handle data.
DBD::ODBC::st::_prepare($sth, $statement, @attribs)
or return;
return $sth;
}
sub column_info {
my ($dbh, $catalog, $schema, $table, $column) = @_;
$catalog = q{} if (!$catalog);
$schema = q{} if (!$schema);
$table = q{} if (!$table);
$column = q{} if (!$column);
# create a "blank" statement handle
my $sth = DBI::_new_sth($dbh, { 'Statement' => "SQLColumns" });
_columns($dbh,$sth, $catalog, $schema, $table, $column)
or return;
return $sth;
}
sub columns {
my ($dbh, $catalog, $schema, $table, $column) = @_;
$catalog = q{} if (!$catalog);
$schema = q{} if (!$schema);
$table = q{} if (!$table);
$column = q{} if (!$column);
# create a "blank" statement handle
my $sth = DBI::_new_sth($dbh, { 'Statement' => "SQLColumns" });
_columns($dbh,$sth, $catalog, $schema, $table, $column)
or return;
return $sth;
}
sub table_info {
my ($dbh, $catalog, $schema, $table, $type) = @_;
if ($#_ == 1) {
my $attrs = $_[1];
$catalog = $attrs->{TABLE_CAT};
$schema = $attrs->{TABLE_SCHEM};
$table = $attrs->{TABLE_NAME};
$type = $attrs->{TABLE_TYPE};
}
# the following was causing a problem
# changing undef to '' makes a big difference to SQLTables
# as SQLTables has special cases for empty string calls
#$catalog = q{} if (!$catalog);
#$schema = q{} if (!$schema);
#$table = q{} if (!$table);
#$type = q{} if (!$type);
# create a "blank" statement handle
my $sth = DBI::_new_sth($dbh, { 'Statement' => "SQLTables" });
DBD::ODBC::st::_tables($dbh,$sth, $catalog, $schema, $table, $type)
or return;
return $sth;
}
sub primary_key_info {
my ($dbh, $catalog, $schema, $table ) = @_;
# create a "blank" statement handle
my $sth = DBI::_new_sth($dbh, { 'Statement' => "SQLPrimaryKeys" });
$catalog = q{} if (!$catalog);
$schema = q{} if (!$schema);
$table = q{} if (!$table);
DBD::ODBC::st::_primary_keys($dbh,$sth, $catalog, $schema, $table )
or return;
return $sth;
}
sub statistics_info {
my ($dbh, $catalog, $schema, $table, $unique, $quick ) = @_;
# create a "blank" statement handle
my $sth = DBI::_new_sth($dbh, { 'Statement' => "SQLStatistics" });
$catalog = q{} if (!$catalog);
$schema = q{} if (!$schema);
$table = q{} if (!$table);
$unique = 1 if (!$unique);
$quick = 1 if (!$quick);
DBD::ODBC::st::_statistics($dbh, $sth, $catalog, $schema, $table,
$unique, $quick)
or return;
return $sth;
}
sub foreign_key_info {
my ($dbh, $pkcatalog, $pkschema, $pktable, $fkcatalog, $fkschema, $fktable ) = @_;
# create a "blank" statement handle
my $sth = DBI::_new_sth($dbh, { 'Statement' => "SQLForeignKeys" });
$pkcatalog = q{} if (!$pkcatalog);
$pkschema = q{} if (!$pkschema);
$pktable = q{} if (!$pktable);
$fkcatalog = q{} if (!$fkcatalog);
$fkschema = q{} if (!$fkschema);
$fktable = q{} if (!$fktable);
_GetForeignKeys($dbh, $sth, $pkcatalog, $pkschema, $pktable, $fkcatalog, $fkschema, $fktable) or return;
return $sth;
}
sub ping {
my $dbh = shift;
# DBD::Gofer does the following (with a 0 instead of "0") but it I
# cannot make it set a warning.
#return $dbh->SUPER::set_err("0", "can't ping while not connected") # warning
# unless $dbh->SUPER::FETCH('Active');
#my $pe = $dbh->FETCH('PrintError');
#$dbh->STORE('PrintError', 0);
my $evalret = eval {
# create a "blank" statement handle
my $sth = DBI::_new_sth($dbh, { 'Statement' => "SQLTables_PING" })
or return 1;
my ($catalog, $schema, $table, $type);
$catalog = q{};
$schema = q{};
$table = 'NOXXTABLE';
$type = q{};
DBD::ODBC::st::_tables($dbh,$sth, $catalog, $schema, $table, $type)
or return 1;
$sth->finish;
return 0;
};
#$dbh->STORE('PrintError', $pe);
$dbh->set_err(undef,'',''); # clear any stored error from eval above
if ($evalret == 0) {
return 1;
} else {
return 0;
}
}
##### # saved, just for posterity.
##### sub oldping {
##### my $dbh = shift;
##### my $state = undef;
#####
##### # should never 'work' but if it does, that's okay!
##### # JLU incorporated patches from Jon Smirl 5/4/99
##### {
##### local $dbh->{RaiseError} = 0 if $dbh->{RaiseError};
##### # JLU added local PrintError handling for completeness.
##### # it shouldn't print, I think.
##### local $dbh->{PrintError} = 0 if $dbh->{PrintError};
##### my $sql = "select sysdate from dual1__NOT_FOUND__CANNOT";
##### my $sth = $dbh->prepare($sql);
##### # fixed "my" $state = below. Was causing problem with
##### # ping! Also, fetching fields as some drivers (Oracle 8)
##### # may not actually check the database for activity until
##### # the query is "described".
##### # Right now, Oracle8 is the only known version which
##### # does not actually check the server during prepare.
##### my $ok = $sth && $sth->execute();
#####
##### $state = $dbh->state;
##### $DBD::ODBC::err = 0;
##### $DBD::ODBC::errstr = "";
##### $DBD::ODBC::sqlstate = "00000";
##### return 1 if $ok;
##### }
##### return 1 if $state eq 'S0002'; # Base table not found
##### return 1 if $state eq '42S02'; # Base table not found.Solid EE v3.51
##### return 1 if $state eq 'S0022'; # Column not found
##### return 1 if $state eq '37000'; # statement could not be prepared (19991011, JLU)
##### # return 1 if $state eq 'S1000'; # General Error? ? 5/30/02, JLU. This is what Openlink is returning
##### # We assume that any other error means the database
##### # is no longer connected.
##### # Some special cases may need to be added to the code above.
##### return 0;
##### }
# New support for DBI which has the get_info command.
# leaving support for ->func(xxx, GetInfo) (below) for a period of time
# to support older applications which used this.
sub get_info {
my ($dbh, $item) = @_;
# Ignore some we cannot do
if ($item == SQL_DRIVER_HSTMT ||
$item == SQL_DRIVER_HLIB ||
$item == SQL_DRIVER_HDESC) {
return;
}
return _GetInfo($dbh, $item);
}
# new override of do method provided by Merijn Broeren
# this optimizes "do" to use SQLExecDirect for simple
# do statements without parameters.
## no critic (ProhibitBuiltinHomonyms)
sub do {
my($dbh, $statement, $attr, @params) = @_;
my $rows = 0;
## no critic (ProhibitMagicNumbers)
if( -1 == $#params ) {
$dbh->STORE(Statement => $statement);
# No parameters, use execute immediate
$rows = ExecDirect( $dbh, $statement );
if( 0 == $rows ) {
$rows = "0E0"; # 0 but true
} elsif( $rows < -1 ) {
undef $rows;
}
}
else
{
$rows = $dbh->SUPER::do( $statement, $attr, @params );
}
return $rows
}
## use critic
#
# can also be called as $dbh->func($sql, ExecDirect);
# if, for some reason, there are compatibility issues
# later with DBI's do.
#
sub ExecDirect {
my ($dbh, $sql) = @_;
return _ExecDirect($dbh, $sql);
}
# Call the ODBC function SQLGetInfo
# Args are:
# $dbh - the database handle
# $item: the requested item. For example, pass 6 for SQL_DRIVER_NAME
# See the ODBC documentation for more information about this call.
#
sub GetInfo {
my ($dbh, $item) = @_;
return get_info($dbh, $item);
}
# Call the ODBC function SQLStatistics
# Args are:
# See the ODBC documentation for more information about this call.
#
sub GetStatistics {
my ($dbh, $catalog, $schema, $table, $unique) = @_;
# create a "blank" statement handle
my $sth = DBI::_new_sth($dbh, { 'Statement' => "SQLStatistics" });
_GetStatistics($dbh, $sth, $catalog, $schema,
$table, $unique) or return;
return $sth;
}
# Call the ODBC function SQLForeignKeys
# Args are:
# See the ODBC documentation for more information about this call.
#
sub GetForeignKeys {
my ($dbh, $pk_catalog, $pk_schema, $pk_table,
$fk_catalog, $fk_schema, $fk_table) = @_;
# create a "blank" statement handle
my $sth = DBI::_new_sth($dbh, { 'Statement' => "SQLForeignKeys" });
_GetForeignKeys($dbh, $sth, $pk_catalog, $pk_schema, $pk_table,
$fk_catalog, $fk_schema, $fk_table) or return;
return $sth;
}
# Call the ODBC function SQLPrimaryKeys
# Args are:
# See the ODBC documentation for more information about this call.
#
sub GetPrimaryKeys {
my ($dbh, $catalog, $schema, $table) = @_;
# create a "blank" statement handle
my $sth = DBI::_new_sth($dbh, { 'Statement' => "SQLPrimaryKeys" });
_GetPrimaryKeys($dbh, $sth, $catalog, $schema, $table) or return;
return $sth;
}
# Call the ODBC function SQLSpecialColumns
# Args are:
# See the ODBC documentation for more information about this call.
#
sub GetSpecialColumns {
my ($dbh, $identifier, $catalog, $schema, $table, $scope, $nullable) = @_;
# create a "blank" statement handle
my $sth = DBI::_new_sth($dbh, { 'Statement' => "SQLSpecialColumns" });
_GetSpecialColumns($dbh, $sth, $identifier, $catalog, $schema,
$table, $scope, $nullable) or return;
return $sth;
}
# sub GetTypeInfo {
# my ($dbh, $sqltype) = @_;
# # create a "blank" statement handle
# my $sth = DBI::_new_sth($dbh, { 'Statement' => "SQLGetTypeInfo" });
# # print "SQL Type is $sqltype\n";
# _GetTypeInfo($dbh, $sth, $sqltype) or return;
# return $sth;
# }
sub type_info_all {
my ($dbh, $sqltype) = @_;
$sqltype = DBI::SQL_ALL_TYPES unless defined $sqltype;
my $sth = DBI::_new_sth($dbh, { 'Statement' => "SQLGetTypeInfo" });
_GetTypeInfo($dbh, $sth, $sqltype) or return;
my $info = $sth->fetchall_arrayref;
unshift @{$info}, {
map { ($sth->{NAME}->[$_] => $_) } 0..$sth->{NUM_OF_FIELDS}-1
};
return $info;
}
}
{ package DBD::ODBC::st; # ====== STATEMENT ======
use strict;
use warnings;
*parse_trace_flag = \&DBD::ODBC::db::parse_trace_flag;
sub private_attribute_info {
return {
odbc_ignore_named_placeholders => undef, # sth and dbh
odbc_default_bind_type => undef, # sth and dbh
odbc_force_bind_type => undef, # sth and dbh
odbc_force_rebind => undef, # sth and dbh
odbc_async_exec => undef, # sth and dbh
odbc_query_timeout => undef, # sth and dbh
odbc_putdata_start => undef, # sth and dbh
odbc_column_display_size => undef, # sth and dbh
odbc_utf8_on => undef, # sth and dbh
odbc_exec_direct => undef, # sth and dbh
odbc_describe_parameters => undef, # sth and dbh
odbc_batch_size => undef, # sth and dbh
odbc_array_operations => undef, # sth and dbh
};
}
sub ColAttributes { # maps to SQLColAttributes
my ($sth, $colno, $desctype) = @_;
my $tmp = _ColAttributes($sth, $colno, $desctype);
return $tmp;
}
sub cancel {
my $sth = shift;
my $tmp = _Cancel($sth);
return $tmp;
}
sub execute_for_fetch {
my ($sth, $fetch_tuple_sub, $tuple_status) = @_;
#print "execute_for_fetch\n";
my $row_count = 0;
my $tuple_count="0E0";
my $tuple_batch_status;
my $batch_size = $sth->FETCH('odbc_batch_size');
$sth->trace_msg("execute_for_fetch($fetch_tuple_sub, " .
($tuple_status ? $tuple_status : 'undef') .
") batch_size = $batch_size\n", 4);
# Use DBI's execute_for_fetch if ours is disabled
my $override = (defined($ENV{ODBC_DISABLE_ARRAY_OPERATIONS}) ?
$ENV{ODBC_DISABLE_ARRAY_OPERATIONS} : -1);
if ((($sth->FETCH('odbc_array_operations') == 0) && ($override != 0)) ||
$override == 1) {
$sth->trace_msg("array operations disabled\n", 4);
my $sth = shift;
return $sth->SUPER::execute_for_fetch(@_);
}
$tuple_batch_status = [ ]; # we always want this here
if (defined($tuple_status)) {
@$tuple_status = ();
}
my $finished;
while (1) {
my @tuple_batch;
for (my $i = 0; $i < $batch_size; $i++) {
$finished = $fetch_tuple_sub->();
push @tuple_batch, [ @{$finished || last} ];
}
$sth->trace_msg("Found " . scalar(@tuple_batch) . " rows\n", 4);
last unless @tuple_batch;
my $res = odbc_execute_for_fetch($sth,
\@tuple_batch,
scalar(@tuple_batch),
$tuple_batch_status);
$sth->trace_msg("odbc_execute_array returns " .
($res ? $res : 'undef') . "\n", 4);
#print "odbc_execute_array XS returned $res\n";
# count how many tuples were used
# basically they are all used unless marked UNUSED
if ($tuple_batch_status) {
foreach (@$tuple_batch_status) {
$tuple_count++ unless $_ == 7; # SQL_PARAM_UNUSED
next if ref($_);
$_ = -1; # we don't know individual row counts
}
if ($tuple_status) {
push @$tuple_status, @$tuple_batch_status
if defined($tuple_status);
}
}
if (!defined($res)) { # error
$row_count = undef;
last;
} else {
$row_count += $res;
}
last if !$finished;
}
if (!wantarray) {
return undef if !defined $row_count;
return $tuple_count;
}
return (defined $row_count ? $tuple_count : undef, $row_count);
}
}
1;
__END__
=head1 NAME
DBD::ODBC - ODBC Driver for DBI
=for html
<a href="https://travis-ci.org/perl5-dbi/DBD-ODBC"><img src="https://travis-ci.org/perl5-dbi/DBD-ODBC.svg?branch=master"></a>
<a href="http://badge.fury.io/pl/DBD-ODBC"><img src="https://badge.fury.io/pl/DBD-ODBC.svg" alt="CPAN version" height="18"></a>
=head1 VERSION
This documentation refers to DBD::ODBC version 1.56.
=head1 WARNING
This version of DBD::ODBC contains a significant fix to unicode when
inserting into CHAR/VARCHAR columns and it is a change in behaviour
from 1.45. The change B<only> applies to unicode builds of DBD::ODBC
(the default on Windows but you can build it for unicode on unix too)
and char/varchar columns and not nchar/nvarchar columns.
Prior to this release of DBD::ODBC when you are using the unicode
build of DBD::ODBC and inserted data into a CHAR/VARCHAR columns using
parameters DBD::ODBC did this:
1 if you set odbc_describe_parameters to 0, (thus preventing DBD::ODBC
from calling SQLDescribeParam) parameters for CHAR/VARCHAR columns
were bound as SQL_WVARCHAR or SQL_WLONGVARCHAR (depending on the
length of the parameter).
2 if you set odbc_force_bind_type then all parameters are bound as you
specified.
3 if you override the parameter type in the bind_param method, the
type you specified would be used.
4 if the driver does not support SQLDescribeParam or SQLDescribeParam
was called and failed then the bind type defaulted as in 1.
5 if none of the above (and I'd guess that is the normal case for most
people) then DBD::ODBC calls SQLDescribeParam to find the parameter
type. This usually returns SQL_CHAR or SQL_VARCHAR for CHAR/VARCHAR
columns unsurprisingly. The parameter was then bound as SQL_VARCHAR.
Items 1 to 4 still apply. 5 now has a different behaviour. In this
release, DBD::ODBC now looks at your bound data first before using the
type returned by SQLDescribeParam. If you data looks like unicode
(i.e., SvUTF8() is true) it now binds the parameter as SQL_WVARCHAR.
What might this might mean to you?
If you had Perl scalars that were bound to CHAR/VARCHAR columns in an
insert/update/delete and those scalars contained unicode, DBD::ODBC
would actually pass the individual octets in your scalar not
characters. For instance, if you had the Perl scalar "\x{20ac}" (the
Euro unicode character) and you bound it to a CHAR/VARCHAR, DBD::ODBC
would pass 0xe2, 0x82, 0xc2 as separate characters because those bytes
were Perl's UTF-8 encoding of a euro. These would probably be
interpreted by your database engine as 3 characters in its current
codepage. If you queried your database to find the length of the data
inserted you'd probably get back 3, not 1.
However, when DBD::ODBC read that column back in a select
statement, it would bind the column as SQL_WCHAR and you'd get back 3
characters with the utf8 flag on (what those characters were depends
on how your database or driver translates code page characters to wide
characters).
What should happen now is that if your bound parameters are unicode,
DBD::ODBC will bind them as wide characters (unicode) and your driver
or database will attempt to convert them into the code page it is
using. This means so long as your database can store the data you are
inserting, when you read it back you should get what you inserted.
=head1 SYNOPSIS
use DBI;
$dbh = DBI->connect('dbi:ODBC:DSN=mydsn', 'user', 'password');
See L<DBI> for more information.
=head1 DESCRIPTION
=head2 Change log and FAQs
Please note that the change log has been moved to
DBD::ODBC::Changes. To access this documentation, use
C<perldoc DBD::ODBC::Changes>.
The FAQs have also moved to DBD::ODBC::FAQ.pm. To access the FAQs use
C<perldoc DBD::ODBC::FAQ>.
=head2 Important note about the tests
DBD::ODBC is unlike most other DBDs in that it connects to literally
dozens of possible ODBC Drivers. It is practically impossible for me
to test every one and so some tests may fail with some ODBC Drivers.
This does not mean DBD::ODBC will not work with your ODBC Driver but
it is worth reporting any test failures on rt.cpan.org or to the
dbi-users mailing list.
=head2 DBI attribute handling
If a DBI defined attribute is not mentioned here it behaves as per the
DBI specification.
=head3 ReadOnly (boolean)
DBI documents the C<ReadOnly> attribute as being settable and
retrievable on connection and statement handles. In ODBC setting
ReadOnly to true causes the connection attribute C<SQL_ATTR_ACCESS_MODE>
to be set to C<SQL_MODE_READ_ONLY> and setting it to false will set the
access mode to C<SQL_MODE_READ_WRITE> (which is the default in ODBC).
B<Note:> There is no equivalent of setting ReadOnly on a statement
handle in ODBC.
B<Note:> See ODBC documentation on C<SQL_ATTR_ACCESS_MODE> as setting it
to C<SQL_MODE_READ_ONLY> does B<not> prevent your script from running
updates or deletes; it is simply a hint to the driver/database that
you won't being doing updates.
B<Note:> Since DBD::ODCB 1.44_3, if the driver does not support
setting C<SQL_ATTR_ACCESS_MODE> and returns SQL_SUCCESS_WITH_INFO and
"option value changed" a warning is issued (which you'll only see if
you have DBI > 1.628). In addition, any subsequent attempts to fetch
the ReadOnly attribute will return the value last set.
This attribute requires DBI version 1.55 or better.
=head2 Private attributes common to connection and statement handles
=head3 odbc_ignore_named_placeholders
Use this if you have special needs (such as Oracle triggers, etc)
where :new or :name mean something special and are not just place
holder names. You B<must> then use ? for binding parameters. Example:
$dbh->{odbc_ignore_named_placeholders} = 1;
$dbh->do("create trigger foo as if :new.x <> :old.x then ... etc");
Without this, DBD::ODBC will think :new and :old are placeholders for
binding and get confused.
=head3 odbc_default_bind_type
This value defaults to 0.
Older versions of DBD::ODBC assumed that the parameter binding type
was 12 (C<SQL_VARCHAR>). Newer versions always attempt to call
C<SQLDescribeParam> to find the parameter types but if
C<SQLDescribeParam> is unavailable DBD::ODBC falls back to a default
bind type. The internal default bind type is C<SQL_VARCHAR> (for
non-unicode build) and C<SQL_WVARCHAR> or C<SQL_VARCHAR> (for a
unicode build depending on whether the parameter is unicode or
not). If you set C<odbc_default_bind_type> to a value other than 0 you
override the internal default.
B<N.B> If you call the C<bind_param> method with a SQL type this
overrides everything else above.
=head3 odbc_force_bind_type
This value defaults to 0.
If set to anything other than 0 this will force bound parameters to be
bound as this type and C<SQLDescribeParam> will not be used; in other
words it implies L</odbc_describe_parameters> is set to false too.
Older versions of DBD::ODBC assumed the parameter binding type was 12
(C<SQL_VARCHAR>) and newer versions always attempt to call
C<SQLDescribeParam> to find the parameter types. If your driver
supports C<SQLDescribeParam> and it succeeds it may still fail to
describe the parameters accurately (MS SQL Server sometimes does this
with some SQL like I<select myfunc(?) where 1 = 1>). Setting
C<odbc_force_bind_type> to C<SQL_VARCHAR> will force DBD::ODBC to bind
all the parameters as C<SQL_VARCHAR> and ignore SQLDescribeParam.
Bear in mind that if you are inserting unicode data you probably want
to use C<SQL_WVARCHAR>/C<SQL_WCHAR>/C<SQL_WLONGVARCHAR> and not
C<SQL_VARCHAR>.
As this attribute was created to work around buggy ODBC Drivers which
support SQLDescribeParam but describe the parameters incorrectly you
are probably better specifying the bind type on the C<bind_param> call
on a per statement level rather than blindly setting
C<odbc_force_bind_type> across a whole connection.
B<N.B> If you call the C<bind_param> method with a SQL type this
overrides everything else above.
=head3 odbc_force_rebind
This is to handle special cases, especially when using multiple result sets.
Set this before execute to "force" DBD::ODBC to re-obtain the result set's
number of columns and column types for each execute. Especially useful for
calling stored procedures which may return different result sets each
execute. The only performance penalty is during execute(), but I didn't
want to incur that penalty for all circumstances. It is probably fairly
rare that this occurs. This attribute will be automatically set when
multiple result sets are triggered. Most people shouldn't have to worry
about this.
=head3 odbc_async_exec
Allow asynchronous execution of queries. This causes a spin-loop
(with a small "sleep") until the ODBC API being called is complete
(i.e., while the ODBC API returns C<SQL_STILL_EXECUTING>). This is
useful, however, if you want the error handling and asynchronous
messages (see the L</odbc_err_handler> and F<t/20SQLServer.t> for an
example of this).
=head3 odbc_query_timeout
This allows you to change the ODBC query timeout (the ODBC statement
attribute C<SQL_ATTR_QUERY_TIMEOUT>). ODBC defines the query time out as
the number of seconds to wait for a SQL statement to execute before
returning to the application. A value of 0 (the default) means there
is no time out. Do not confuse this with the ODBC attributes
C<SQL_ATTR_LOGIN_TIMEOUT> and C<SQL_ATTR_CONNECTION_TIMEOUT>. Add
{ odbc_query_timeout => 30 }
to your connect, set on the C<dbh> before creating a statement or
explicitly set it on your statement handle. The odbc_query_timeout on
a statement is inherited from the parent connection.
Note that internally DBD::ODBC only sets the query timeout if you set it
explicitly and the default of 0 (no time out) is implemented by the
ODBC driver and not DBD::ODBC.
Note that some ODBC drivers implement a maximum query timeout value
and will limit timeouts set above their maximum. You may see a
warning if your time out is capped by the driver but there is
currently no way to retrieve the capped value back from the driver.
Note that some drivers may not support this attribute.
See F<t/20SqlServer.t> for an example.
=head3 odbc_putdata_start
C<odbc_putdata_start> defines the size at which DBD::ODBC uses
C<SQLPutData> and C<SQLParamData> to send larger objects to the
database instead of simply binding them as normal with
C<SQLBindParameter>. It is mostly a placeholder for future changes
allowing chunks of data to be sent to the database and there is little
reason for anyone to change it currently.
The default for odbc_putdata_start is 32768 because this value was
hard-coded in DBD::ODBC until 1.16_1.
=head3 odbc_column_display_size
If you ODBC driver does not support the SQL_COLUMN_DISPLAY_SIZE and
SQL_COLUMN_LENGTH attributes to SQLColAtrributes then DBD::ODBC does
not know how big the column might be. odbc_column_display_size sets
the default value for the column size when retrieving column data
where the size cannot be determined.
The default for odbc_column_display_size is 2001 because this value was
hard-coded in DBD::ODBC until 1.17_3.
=head3 odbc_utf8_on
Set this flag to treat all strings returned from the ODBC driver
(except columns described as SQL_BINARY or SQL_TIMESTAMP and its
variations) as UTF-8 encoded. Some ODBC drivers (like Aster and maybe
PostgreSQL) return UTF-8 encoded data but do not support the SQLxxxW
unicode API. Enabling this flag will cause DBD::ODBC to treat driver
returned data as UTF-8 encoded and it will be marked as such in Perl.
Do not confuse this with DBD::ODBC's unicode support. The
C<odbc_utf8_on> attribute only applies to non-unicode enabled builds
of DBD::ODBC.
=head3 odbc_describe_parameters
Defaults to on. When set this allows DBD::ODBC to call SQLDescribeParam
(if the driver supports it) to retrieve information about any
parameters.
When off/false DBD::ODBC will not call SQLDescribeParam and defaults
to binding parameters as SQL_CHAR/SQL_WCHAR depending on the build
type and whether your data is unicode or not.
You do not have to disable odbc_describe_parameters just because your
driver does not support SQLDescribeParam as DBD::ODBC will work this
out at the start via SQLGetFunctions.
B<Note>: disabling odbc_describe_parameters when your driver does support
SQLDescribeParam may prevent DBD::ODBC binding parameters for some
column types properly.
You can also set this attribute in the attributes passed to the
prepare method.
This attribute was added so someone moving from freeTDS (a driver
which does not support SQLDescribeParam) to a driver which does
support SQLDescribeParam could do so without changing any Perl. The
situation was very specific since dates were being bound as dates when
SQLDescribeParam was called and chars without and the data format was
not a supported date format.
=head2 Private methods common to connection and statement handles
=head3 odbc_getdiagrec
@diags = $handle->odbc_getdiagrec($record_number);
Introduced in 1.34_3.
This is just a wrapper around the ODBC API SQLGetDiagRec. When a
method on a connection or statement handle fails if there are any ODBC
diagnostics you can use this method to retrieve them. Records start at
1 and there may be more than 1. It returns an array containing the
state, native and error message text or an empty array if the requested
diagnostic record does not exist. To get all diagnostics available
keep incrementing $record_number until odbc_getdiagrec returns an
empty array.
All of the state, native and message text are already passed to DBI
via its set_err method so this method does not really tell you
anything you cannot already get from DBI except when there is more
than one diagnostic.
You may find this useful in an error handler as you can get the ODBC
diagnostics as they are and not how DBD::ODBC was forced to fit them
into the DBI's system.
NOTE: calling this method does not clear DBI's error values as usually
happens.
=head3 odbc_getdiagfield
$diag = $handle->odbc_getdiagfield($record, $identifier);