blib/lib/DBIO/MySQL/Introspect/Columns.pm

filter: Statementstmt 100% 41 / 41 Branchbran 85.0% 17 / 20 Conditioncond 77.7% 14 / 18 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 88.4% 84 / 95 CC 18 SCAR 21.6
CC
18
Cov
91%
fetch
12.1
_default_is_expression
7.2
BEGIN
1.0
CRAP 8.7
« blib/lib/DBIO/MySQL/NotifyTable.pm ↑ Summary blib/lib/DBIO/MySQL/SQLMaker.pm »
1 package DBIO::MySQL::Introspect::Columns;
2 # ABSTRACT: Introspect MySQL/MariaDB columns
3  
4 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 45 our $VERSION = '0.900007';
8  
9 45 ▶use DBIO::MySQL::Introspect::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 45 ▶use DBIO::MySQL::DefaultValue ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13  
14 10 ▶sub fetch {
Subroutine fetch: called CC=12 SCAR=24.9
Pod documented
15 10 my ($class, $dbh, $tables) = @_;
16 10 my %columns;
17  
18 # On MySQL 8 the information_schema result-column labels come back UPPERCASE
19 # regardless of the case written in the SELECT, while MariaDB returns them
20 # lowercase. Force lowercase hash keys for this fetch so the $row->{lc} reads
21 # below resolve on both engines. The sth captures FetchHashKeyName from the
22 # dbh at prepare time, so it must be set before prepare (Tables.pm sidesteps
23 # this by aliasing every column, which echoes the alias case verbatim).
24 10 local $dbh->{FetchHashKeyName} = 'NAME_lc';
25  
26 # Only a MariaDB that reports COLUMN_DEFAULT as an expression says "no
27 # default" with the text NULL; from a MySQL that text is a default (see
28 # default_value in the POD above).
29 10 my $null_is_keyword = _default_is_expression(scalar $dbh->selectrow_array('SELECT VERSION()'));
30  
31 # is_pk comes from the index PRIMARY, not from COLUMN_KEY: both servers
32 # report COLUMN_KEY = 'PRI' for the first column of a UNIQUE key over NOT
33 # NULL columns when the table has no primary key at all. The index name is
34 # what Introspect::Indexes reads as origin 'pk' as well.
35 10 my %in_pk;
36 10 my $pk_sth = $dbh->prepare(q{
37 SELECT table_name, column_name, seq_in_index
38 FROM information_schema.statistics
39 WHERE table_schema = DATABASE() AND index_name = 'PRIMARY'
40 });
41 10 $pk_sth->execute;
42 10 ▶ while (my $row = $pk_sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $pk_sth->fetchrow_hashref) 3 10 13
43 3 $in_pk{ $row->{table_name} }{ $row->{column_name} } = $row->{seq_in_index};
44 }
45  
46 10 my $sth = $dbh->prepare(q{
47 SELECT
48 table_name,
49 column_name,
50 ordinal_position,
51 data_type,
52 column_type,
53 is_nullable,
54 column_default,
55 extra,
56 character_set_name,
57 collation_name,
58 column_comment
59 FROM information_schema.columns
60 WHERE table_schema = DATABASE()
61 ORDER BY table_name, ordinal_position
62 });
63 10 $sth->execute;
64  
65 10 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 78 10 88
66 78 ▶ next unless DBIO::MySQL::Introspect::Util->keep_table($tables, $row->{table_name});
Branch: 1 branch
BranchTrueFalseTotal
unless "DBIO::MySQL::Introspect::Util"->keep_table($tables, $row->{"table_name"}) 0 78 78
67  
68 78 ▶ my $extra = $row->{extra} // '';
Condition50%1/2
$row->{"extra"} // ""
l!l
780
MC/DC0%0/1
$row->{"extra"} // ""
$row->{"extra"}
Truth table50%1/2
A
A= $row->{"extra"}
Aresult
11
00
69 78 ▶ my $auto = ($extra =~ /\bauto_increment\b/i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$extra =~ /\bauto_increment\b/i ? : 0 78 78
70 78 my $pk_pos = $in_pk{ $row->{table_name} }{ $row->{column_name} };
71 78 ▶ my $is_pk = defined $pk_pos ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $pk_pos ? : 3 75 78
72  
73 78 my $default = $row->{column_default};
74 78 ▶ undef $default if $null_is_keyword && defined $default && $default eq 'NULL';
Branch: 1 branch
BranchTrueFalseTotal
if $null_is_keyword and defined $default and $default eq "NULL" 10 68 78
Condition100%6/6
$null_is_keyword and defined $default and $default eq "NULL"
!ll&&!rl&&r
452310
$null_is_keyword and defined $default
!ll&&!rl&&r
43233
MC/DC100%3/3
$null_is_keyword and defined $default and $default eq "NULL"
$null_is_keyword defined $default $default eq "NULL"
Truth table100%4/4
A and B and C
A= $null_is_keywordB= defined $defaultC= $default eq "NULL"
ABCresult
0XX0
10X0
1100
1111
75 ($default, my $is_expression) = DBIO::MySQL::DefaultValue::catalog_default(
76 raw => $default,
77 extra => $extra,
78 data_type => $row->{data_type},
79 78 mariadb_expression => $null_is_keyword,
80 );
81  
82 78 ▶ push @{ $columns{ $row->{table_name} } }, {
Branch: 1 branch
BranchTrueFalseTotal
uc($row->{'is_nullable'} // '') eq 'NO' ? : 25 53 78
Condition50%1/2
$row->{"is_nullable"} // ""
l!l
780
MC/DC0%0/1
$row->{"is_nullable"} // ""
$row->{"is_nullable"}
Truth table50%1/2
A
A= $row->{"is_nullable"}
Aresult
11
00
83 column_name => $row->{column_name},
84 data_type => $row->{data_type},
85 column_type => $row->{column_type},
86 # `size` is the canonical companion to `data_type`: extracted from
87 # `column_type`'s parenthesised number (varchar(100) -> 100,
88 # int(11) -> 11, decimal(10,2) -> 10). DBIO::Introspect::Base
89 # relies on this to size columns it builds.
90 size => DBIO::MySQL::Introspect::Util->column_size($row->{column_type}),
91 not_null => (uc($row->{is_nullable} // '') eq 'NO') ? 1 : 0,
92 default_value => $default,
93 default_is_expression => $is_expression,
94 on_update => DBIO::MySQL::DefaultValue::catalog_on_update($extra),
95 is_auto_increment => $auto,
96 is_pk => $is_pk,
97 # `pk_position` lets DBIO::Introspect::Base::table_pk_info sort
98 # composite primary keys in the right order: the position in the key
99 # (seq_in_index), not the position of the column in the table
100 # (karr k107). Single-column PKs always get position 1.
101 pk_position => $pk_pos,
102 character_set => $row->{character_set_name},
103 collation => $row->{collation_name},
104 comment => $row->{column_comment},
105 78 extra => $extra,
106 };
107 }
108  
109 10 return \%columns;
110 }
111  
112 # True for a server that reports COLUMN_DEFAULT as an expression: a MariaDB
113 # from 10.2.7 on (MDEV-13132), going by the version string it answers.
114 10 ▶sub _default_is_expression {
Subroutine _default_is_expression: called CC=7 SCAR=19.8
115 10 my ($version) = @_;
116 10 ▶ return 0 unless ($version // '') =~ /mariadb/i;
Branch: 1 branch
BranchTrueFalseTotal
unless ($version // "") =~ /mariadb/i 5 5 10
Condition50%1/2
$version // ""
l!l
100
MC/DC0%0/1
$version // ""
$version
Truth table50%1/2
A
A= $version
Aresult
11
00
117 5 ▶ my ($major, $minor, $patch) = $version =~ /(\d+)\.(\d+)\.(\d+)/ or return 0;
Branch: 1 branch
BranchTrueFalseTotal
() = $version =~ /(\d+)\.(\d+)\.(\d+)/ or return 0 5 0 5
118 5 ▶ return ($major <=> 10 || $minor <=> 2 || $patch <=> 7) >= 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($major <=> 10 || $minor <=> 2 || $patch <=> 7) >= 0 ? : 4 1 5
Condition83%5/6
$major <=> 10 || $minor <=> 2 || $patch <=> 7
l!l&&r!l&&!r
401
$major <=> 10 or $minor <=> 2
l!l&&r!l&&!r
311
MC/DC66%2/3
$major <=> 10 || $minor <=> 2 || $patch <=> 7
$major <=> 10 $minor <=> 2 $patch <=> 7
Truth table75%3/4
A or B || C
A= $major <=> 10B= $minor <=> 2C= $patch <=> 7
ABCresult
1XX1
01X1
0011
0000
119 }
120  
121 45 1;
122  
123 __END__
« blib/lib/DBIO/MySQL/NotifyTable.pm ↑ Summary blib/lib/DBIO/MySQL/SQLMaker.pm »