blib/lib/DBD/mysql/AutoTypes.pm

filter: Statementstmt 75.0% 9 / 12 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 75.0% 3 / 4 Podpod n/a totaltotal 41.3% 12 / 29 CC 6 SCAR 33.4
CC
6
Cov
41%
_mysql_fix
42.0
BEGIN
1.0
BEGIN
1.0
CRAP 28.3
↑ Summary
1 package DBD::mysql::AutoTypes;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 our @ISA = qw( Exporter );
6 our @EXPORT = qw( mysql_auto_types );
7
8 our $VERSION = "1.0";
9 our $DBD_mysql_VERSION = 2.9002;
10
11 1 ▶use Regexp::Common qw ( number );
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 0 ▶sub _mysql_fix {
Subroutine _mysql_fix: not called CC=6 SCAR=37.4
14 0 my ($sth, $attr, @bind) = @_;
15 0 my $n = 1;
16 $sth->bind_param( $n, $_,
17 $attr->{TYPES} && $attr->{TYPES}[$n-1] ||
18 /^$RE{num}{int}$/ ? DBI::SQL_INTEGER :
19 /^$RE{num}{real}$/ ? DBI::SQL_DOUBLE :
20 DBI::SQL_VARCHAR
21 0 ▶ ), $n++ foreach (@bind);
Branch: 2 branches
BranchTrueFalseTotal
$attr->{'TYPES'} && $attr->{'TYPES'}[$n - 1] || /^$DBD::mysql::AutoTypes::RE{'num'}{'int'}$/ ? : 0 0 0
/^$DBD::mysql::AutoTypes::RE{'num'}{'real'}$/ ? : 0 0 0
Condition0%0/6
$attr->{"TYPES"} and $attr->{"TYPES"}[$n - 1] or /^$DBD::mysql::AutoTypes::RE{"num"}{"int"}$/
l!l&&r!l&&!r
000
$attr->{"TYPES"} and $attr->{"TYPES"}[$n - 1]
!ll&&!rl&&r
000
MC/DC0%0/3
$attr->{"TYPES"} and $attr->{"TYPES"}[$n - 1] or /^$DBD::mysql::AutoTypes::RE{"num"}{"int"}$/
$attr->{"TYPES"} $attr->{"TYPES"}[$n - 1] /^$DBD::mysql::AutoTypes::RE{"num"}{"int"}$/
Truth table0%0/5
A and B or C
A= $attr->{"TYPES"}B= $attr->{"TYPES"}[$n - 1]C= /^$DBD::mysql::AutoTypes::RE{"num"}{"int"}$/
ABCresult
11X1
0X11
1011
0X00
1000
22 }
23
24 our $FIXES = {
25
26 'selectall_arrayref' => { pkg => '_', code => sub {
27 my ($dbh, $stmt, $attr, @bind) = @_;
28 my $sth = ( ref $stmt ) ? $stmt : $dbh->prepare( $stmt, $attr ) or return;
29 _mysql_fix( $sth, $attr, @bind);
30 $sth->execute() || return;
31 my $slice = $attr->{Slice};
32 if (!$slice and $slice=$attr->{Columns}) {
33 if (ref $slice eq 'ARRAY') {
34 $slice = [ @{$attr->{Columns}} ];
35 for (@$slice) { $_-- }
36 }
37 }
38 return $sth->fetchall_arrayref($slice, $attr->{MaxRows});
39 }},
40
41 'selectall_hashref' => { pkg => '_', code => sub {
42 my ($dbh, $stmt, $key_field, $attr, @bind) = @_;
43 my $sth = (ref $stmt) ? $stmt : $dbh->prepare($stmt, $attr) or return;
44 _mysql_fix( $sth, $attr, @bind);
45 $sth->execute(@bind) || return;
46 return $sth->fetchall_hashref($key_field);
47 }},
48
49 'selectcol_arrayref' => { pkg => '_', code => sub {
50 my ($dbh, $stmt, $attr, @bind) = @_;
51 my $sth = (ref $stmt) ? $stmt : $dbh->prepare($stmt, $attr) or return;
52 _mysql_fix( $sth, $attr, @bind);
53 $sth->execute(@bind) || return;
54 my @columns = ($attr->{Columns}) ? @{$attr->{Columns}} : (1);
55 my @values = (undef) x @columns;
56 my $idx = 0;
57 for (@columns) {
58 $sth->bind_col($_, \$values[$idx++]) || return;
59 }
60 my @col;
61 if (my $max = $attr->{MaxRows}) {
62 push @col, @values while @col<$max && $sth->fetch;
63 } else {
64 push @col, @values while $sth->fetch;
65 }
66 return \@col;
67 }},
68
69 'do' => { pkg => 'mysql', code => sub {
70 my($dbh, $statement, $attr, @bind) = @_;
71 my $sth = $dbh->prepare($statement, $attr) or return undef;
72 _mysql_fix( $sth, $attr, @bind );
73 $sth->execute(@bind) or return undef;
74 my $rows = $sth->rows;
75 ($rows == 0) ? "0E0" : $rows;
76 }},
77
78 '_do_selectrow' => { pkg => 'mysql', code => sub {
79 my ($method, $dbh, $stmt, $attr, @bind) = @_;
80 my $sth = ((ref $stmt) ? $stmt : $dbh->prepare($stmt, $attr)) or return;
81 _mysql_fix( $sth, $attr, @bind );
82 $sth->execute(@bind) or return;
83 my $row = $sth->$method() and $sth->finish;
84 return $row;
85 }},
86
87 'selectrow_array' => { pkg => 'mysql', code => sub {
88 my $row = DBD::mysql::db::_do_selectrow('fetchrow_arrayref', @_) or return;
89 return $row->[0] unless wantarray;
90 return @$row;
91 }},
92
93 'selectrow_arrayref' => { pkg => 'mysql', code => sub {
94 return DBD::mysql::db::_do_selectrow('fetchrow_arrayref', @_);
95 }},
96
97 'selectrow_hashref' => { pkg => '_', code => sub {
98 return DBD::mysql::db::_do_selectrow('fetchrow_hashref', @_);
99 }},
100
101 };
102
103 sub mysql_auto_types {
104 return if $DBD::mysql::VERSION < $DBD_mysql_VERSION;
105
106 while (my ($meth, $params) = each %$FIXES) {
107 no warnings;
108 if ($params->{pkg} eq '_') {
109 $DBD::_::db::{$meth} = $params->{code};
110 } elsif ($params->{pkg} eq 'mysql') {
111 $DBD::mysql::db::{$meth} = $params->{code};
112 }
113 }
114 }
115
116 'Grishace';
117
118 =head1 NAME
119
120 DBD::mysql::AutoTypes -- automatically assign parameters' sql type to support old DBD::mysql functionality
121
122 =head1 SYNOPSIS
123
124 use DBI;
125 use DBD::mysql::AutoTypes;
126
127 my $dbh = DBI->connect('DBI:mysql:...', '...', '...') and mysql_auto_types();
128
129 =head1 DESCRIPTION
130
131 Since version 2.9002 DBD::mysql requires explicit sql type for query parameters.
132 You should change the tonnes of $dbh->selectall_arrayref() to the ugly
133 "prepare - bind - execute - fetch" pipeline.
134
135 This module is provided to solve the problem.
136
137 You have to change only two lines of code (use the module, and apply fixup after accuring database connection).
138
139 =head1 DEPENDENCIES
140
141 =over 3
142
143 =item *
144
145 B<DBI>
146
147 =item *
148
149 B<DBD::mysql>
150
151 =item *
152
153 B<Regexp::Common>
154
155 =back
156
157 =head1 BUGS
158
159 May be...
160
161 =head1 SEE ALSO
162
163 =over 4
164
165 =item *
166
167 B<DBI> -- Perl DataBase Interface (L<http://search.cpan.org/~timb/DBI/>)
168
169 =item *
170
171 B<DBD::mysql> -- MySQL (L<http://www.mysql.com>) driver (L<http://search.cpan.org/~rudy/DBD-mysql/>) and
172 B<DBD::mysql ChangeLog> -- look for the version 2.9002 changes (L<http://search.cpan.org/src/RUDY/DBD-mysql-2.9002/ChangeLog>),
173 that break old behaviour
174
175 =item *
176
177 B<Regexp::Common> -- determine is data number or string
178
179 =back
180
181 =head1 AUTHOR
182
183 Greg "Grishace" Belenky <greg@webzavod.ru>
184
185 =head1 COPYRIGHT
186
187 Copyright (C) 2004 Greg "Grishace" Belenky
188 Portions of code cut'n'pasted from the DBI module
189
190 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
191
192 =cut
↑ Summary