DBSchema/DBD/mysql.pm

filter: Statementstmt 27.3% 23 / 84 Branchbran 7.8% 3 / 38 Conditioncond 11.1% 2 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 45.4% 5 / 11 Podpod 100% 6 / 6 totaltotal 23.4% 39 / 166 SCAR 61.3
CC
29
Cov
20%
alter_column_callback
132.0
columns
90.0
_show_index
42.0
CRAP 459.6
« DBSchema/Column.pm ↑ Summary DBSchema.pm »
1 package DBIx::DBSchema::DBD::mysql;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use vars qw($VERSION @ISA %typemap);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use DBIx::DBSchema::DBD;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 $VERSION = '0.09';
8 2 @ISA = qw(DBIx::DBSchema::DBD);
9  
10 2 %typemap = (
11 'TIMESTAMP' => 'DATETIME',
12 'SERIAL' => 'INTEGER',
13 'BIGSERIAL' => 'BIGINT',
14 'BOOL' => 'TINYINT',
15 'LONG VARBINARY' => 'LONGBLOB',
16 'TEXT' => 'LONGTEXT',
17 );
18  
19 =head1 NAME
20  
21 DBIx::DBSchema::DBD::mysql - MySQL native driver for DBIx::DBSchema
22  
23 =head1 SYNOPSIS
24  
25 use DBI;
26 use DBIx::DBSchema;
27  
28 $dbh = DBI->connect('dbi:mysql:database', 'user', 'pass');
29 $schema = new_native DBIx::DBSchema $dbh;
30  
31 =head1 DESCRIPTION
32  
33 This module implements a MySQL-native driver for DBIx::DBSchema.
34  
35 =cut
36 2 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 sub columns {
39 0 ▶ my($proto, $dbh, $table ) = @_;
Subroutine columns: not called CC=9 SCAR=45.0
Pod documented
40 0 my $oldkhv=$dbh->{FetchHashKeyName};
41 0 $dbh->{FetchHashKeyName}="NAME";
42 0 ▶ my $sth = $dbh->prepare("SHOW COLUMNS FROM $table") or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("SHOW COLUMNS FROM $table") or die $dbh->errstr 0 0 0
43 0 ▶ $sth->execute or die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 0 0
44 my @r = map {
45 #warn Dumper($_);
46 $_->{'Type'} =~ /^(\w+)\(?([^)]+)?\)?( \d+)?$/
47 0 ▶ or die "Illegal type: ". $_->{'Type'}. "\n";
Branch: 1 branch
BranchTrueFalseTotal
$_->{"Type"} =~ /^(\w+)\(?([^)]+)?\)?( \d+)?$/ or die 'Illegal type: ' . $_->{"Type"} . "\n" 0 0 0
48 0 my($type, $length) = ($1, $2);
49  
50 0 my $default = $_->{'Default'};
51 0 ▶ if ( defined($default) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) { } 0 0 0
52 0 ▶ $default = \"''" if $default eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $default eq "" 0 0 0
53 0 ▶ $default = \0 if $default eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if $default eq 0 0 0 0
54 0 ▶ $default = \'NOW()' if uc($default) eq 'CURRENT_TIMESTAMP';
Branch: 1 branch
BranchTrueFalseTotal
if uc $default eq "CURRENT_TIMESTAMP" 0 0 0
55 } else {
56 0 $default = '';
57 }
58  
59 [
60 $_->{'Field'},
61 $type,
62 ( $_->{'Null'} =~ /^YES$/i ? 'NULL' : '' ),
63 $length,
64 $default,
65 0 ▶ $_->{'Extra'}
Branch: 1 branch
BranchTrueFalseTotal
$_->{'Null'} =~ /^YES$/i ? : 0 0 0
66 ]
67 0 } @{ $sth->fetchall_arrayref( {} ) };
68 0 $dbh->{FetchHashKeyName}=$oldkhv;
69 0 @r;
70 }
71  
72 #sub primary_key {
73 # my($proto, $dbh, $table ) = @_;
74 # my $primary_key = '';
75 # my $sth = $dbh->prepare("SHOW INDEX FROM $table")
76 # or die $dbh->errstr;
77 # $sth->execute or die $sth->errstr;
78 # my @pkey = map { $_->{'Column_name'} } grep {
79 # $_->{'Key_name'} eq "PRIMARY"
80 # } @{ $sth->fetchall_arrayref( {} ) };
81 # scalar(@pkey) ? $pkey[0] : '';
82 #}
83  
84 sub primary_key {
85 0 ▶ my($proto, $dbh, $table) = @_;
Subroutine primary_key: not called CC=1 SCAR=6.9
Pod documented
86 0 my($pkey, $unique_href, $index_href) = $proto->_show_index($dbh, $table);
87 0 $pkey;
88 }
89  
90 sub unique {
91 0 ▶ my($proto, $dbh, $table) = @_;
Subroutine unique: not called CC=1 SCAR=6.9
Pod documented
92 0 my($pkey, $unique_href, $index_href) = $proto->_show_index($dbh, $table);
93 0 $unique_href;
94 }
95  
96 sub index {
97 0 ▶ my($proto, $dbh, $table) = @_;
Subroutine index: not called CC=1 SCAR=6.9
Pod documented
98 0 my($pkey, $unique_href, $index_href) = $proto->_show_index($dbh, $table);
99 0 $index_href;
100 }
101  
102 sub _show_index {
103 0 ▶ my($proto, $dbh, $table ) = @_;
Subroutine _show_index: not called CC=6 SCAR=37.4
104 0 my $oldkhv=$dbh->{FetchHashKeyName};
105 0 $dbh->{FetchHashKeyName}="NAME";
106 0 ▶ my $sth = $dbh->prepare("SHOW INDEX FROM $table")
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("SHOW INDEX FROM $table") or die $dbh->errstr 0 0 0
107 or die $dbh->errstr;
108 0 ▶ $sth->execute or die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 0 0
109  
110 0 my $pkey = '';
111 0 my(%index, %unique);
112 0 foreach my $row ( @{ $sth->fetchall_arrayref({}) } ) {
113 0 ▶ if ( $row->{'Key_name'} eq 'PRIMARY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($row->{'Key_name'} eq 'PRIMARY') { } 0 0 0
elsif ($row->{'Non_unique'}) { } 0 0 0
114 0 $pkey = $row->{'Column_name'};
115 } elsif ( $row->{'Non_unique'} ) { #index
116 0 push @{ $index{ $row->{'Key_name'} } }, $row->{'Column_name'};
117 } else { #unique
118 0 push @{ $unique{ $row->{'Key_name'} } }, $row->{'Column_name'};
119 }
120 }
121 0 $dbh->{FetchHashKeyName}=$oldkhv;
122  
123 0 ( $pkey, \%unique, \%index );
124 }
125  
126 sub column_callback {
127 1 ▶ my( $proto, $dbh, $table, $column_obj ) = @_;
Subroutine column_callback: called CC=6 SCAR=21.8
Pod documented
128  
129 1 my $hashref = { 'explicit_null' => 1, };
130  
131 1 ▶ $hashref->{'effective_local'} = 'AUTO_INCREMENT'
Branch: 1 branch
BranchTrueFalseTotal
if $column_obj->type =~ /^(\w*)SERIAL$/i 0 1 1
132 if $column_obj->type =~ /^(\w*)SERIAL$/i;
133  
134 1 ▶ if ( $column_obj->quoted_default =~ /^(NOW)\(\)$/i
Branch: 1 branch
BranchTrueFalseTotal
if ($column_obj->quoted_default =~ /^(NOW)\(\)$/i and $column_obj->type =~ /^(TIMESTAMP|DATETIME)$/i) 0 1 1
Condition33%1/3
$column_obj->quoted_default =~ /^(NOW)\(\)$/i and $column_obj->type =~ /^(TIMESTAMP|DATETIME)$/i
!ll&&!rl&&r
100
MC/DC0%0/2
$column_obj->quoted_default =~ /^(NOW)\(\)$/i and $column_obj->type =~ /^(TIMESTAMP|DATETIME)$/i
$column_obj->quoted_default =~ /^(NOW)\(\)$/i $column_obj->type =~ /^(TIMESTAMP|DATETIME)$/i
Truth table33%1/3
A and B
A= $column_obj->quoted_default =~ /^(NOW)\(\)$/iB= $column_obj->type =~ /^(TIMESTAMP|DATETIME)$/i
ABresult
0X0
100
111
135 && $column_obj->type =~ /^(TIMESTAMP|DATETIME)$/i ) {
136  
137 0 $hashref->{'effective_default'} = 'CURRENT_TIMESTAMP';
138 0 $hashref->{'effective_type'} = 'TIMESTAMP';
139  
140 }
141  
142 # MySQL no longer supports defaults for text/blob columns
143 1 ▶ if ( $column_obj->type =~ /(TEXT|BLOB)/i
Branch: 1 branch
BranchTrueFalseTotal
if ($column_obj->type =~ /(TEXT|BLOB)/i and defined $column_obj->default) 1 0 1
Condition33%1/3
$column_obj->type =~ /(TEXT|BLOB)/i and defined $column_obj->default
!ll&&!rl&&r
001
MC/DC0%0/2
$column_obj->type =~ /(TEXT|BLOB)/i and defined $column_obj->default
$column_obj->type =~ /(TEXT|BLOB)/i defined $column_obj->default
Truth table33%1/3
A and B
A= $column_obj->type =~ /(TEXT|BLOB)/iB= defined $column_obj->default
ABresult
0X0
100
111
144 and defined $column_obj->default ) {
145  
146 # There's no way to unset the default cleanly.
147 # An empty string isn't quite right.
148 1 $column_obj->{'default'} = undef;
149 }
150  
151 1 $hashref;
152  
153 }
154  
155 sub alter_column_callback {
156 0 ▶ my( $proto, $dbh, $table, $old_column, $new_column ) = @_;
Subroutine alter_column_callback: not called CC=11 SCAR=48.8
Pod documented
157 0 my $old_name = $old_column->name;
158 0 my $new_def = $new_column->line($dbh);
159  
160 0 my $hashref = {};
161  
162 0 my %canonical = (
163 'INTEGER' => 'INT',
164 'SERIAL' => 'INT',
165 'BIGSERIAL' => 'BIGINT',
166 'REAL' => 'DOUBLE', #'FLOAT',
167 'DOUBLE PRECISION' => 'DOUBLE',
168 );
169 0 foreach ($old_column, $new_column) {
170 0 ▶ $_->type($canonical{uc($_->type)}) if $canonical{uc($_->type)};
Branch: 1 branch
BranchTrueFalseTotal
if $canonical{uc $_->type} 0 0 0
171 }
172  
173 0 my %canonical_length = (
174 'INT' => 11,
175 'BIGINT' => 20,
176 'DECIMAL' => '10,0',
177 );
178 $new_column->length( $canonical_length{uc($new_column->type)} )
179 0 ▶ if $canonical_length{uc($new_column->type)}
Branch: 1 branch
BranchTrueFalseTotal
if $canonical_length{uc $new_column->type} and ($new_column->length || "") eq "" 0 0 0
Condition0%0/5
$canonical_length{uc $new_column->type} and ($new_column->length || "") eq ""
!ll&&!rl&&r
000
$new_column->length || ''
l!l
00
MC/DC0%0/2
$canonical_length{uc $new_column->type} and ($new_column->length || "") eq ""
$canonical_length{uc $new_column->type} $new_column->length
Truth table0%0/3
A and B
A= $canonical_length{uc $new_column->type}B= $new_column->length
ABresult
0X0
100
111
180 && ($new_column->length||'') eq '';
181  
182 #change type/length
183 0 ▶ if ( uc($old_column->type) ne uc($new_column->type)
Branch: 1 branch
BranchTrueFalseTotal
if (uc $old_column->type ne uc $new_column->type or ($old_column->length || "") ne ($new_column->length || "")) 0 0 0
Condition0%0/7
uc $old_column->type ne uc $new_column->type or ($old_column->length || "") ne ($new_column->length || "")
l!l&&r!l&&!r
000
$old_column->length || ''
l!l
00
$new_column->length || ''
l!l
00
MC/DC0%0/2
uc $old_column->type ne uc $new_column->type or ($old_column->length || "") ne ($new_column->length || "")
uc $old_column->type ne uc $new_column->type $old_column->length
MC/DC0%0/1
$new_column->length || ''
$new_column->length
Truth table0%0/3
A or B
A= uc $old_column->type ne uc $new_column->typeB= $old_column->length
ABresult
1X1
011
000
Truth table0%0/2
A
A= $new_column->length
Aresult
11
00
184 || ($old_column->length||'') ne ($new_column->length||'')
185 )
186 {
187 0 my $old_def = $old_column->line($dbh);
188 0 $hashref->{'sql_alter_type'} =
189 "CHANGE $old_name $new_def";
190 }
191  
192 #change nullability
193 0 ▶ if ( $old_column->null ne $new_column->null ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_column->null ne $new_column->null) 0 0 0
194 0 $hashref->{'sql_alter_null'} =
195 "ALTER TABLE $table MODIFY $new_def";
196 }
197  
198 0 $hashref;
199 }
200  
201 =head1 AUTHOR
202  
203 Ivan Kohler <ivan-dbix-dbschema@420.am>
204  
205 =head1 COPYRIGHT
206  
207 Copyright (c) 2000 Ivan Kohler
208 Copyright (c) 2000 Mail Abuse Prevention System LLC
209 Copyright (c) 2007-2013 Freeside Internet Services, Inc.
210 All rights reserved.
211 This program is free software; you can redistribute it and/or modify it under
212 the same terms as Perl itself.
213  
214 =head1 BUGS
215  
216 =head1 SEE ALSO
217  
218 L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>, L<DBI::DBD>
219  
220 =cut
221  
222 2 1;
223  
« DBSchema/Column.pm ↑ Summary DBSchema.pm »