DB2/Introspect/Columns.pm

filter: Statementstmt 100% 63 / 63 Branchbran 88.4% 46 / 52 Conditioncond 65.2% 15 / 23 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 86.1% 137 / 159 CC 38 SCAR 25.8
CC
38
Cov
90%
fetch
21.0
_normalized_size
10.4
_normalized_default
6.0
CRAP 13.2
« DB2/Storage/Cursor.pm ↑ Summary DB2/Diff/Column.pm »
1 package DBIO::DB2::Introspect::Columns;
2 # ABSTRACT: Introspect DB2 columns
3  
4 38 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 38 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 38 our $VERSION = '0.900007';
8  
9  
10  
11 # Types Db2 declares without a length: the catalog's LENGTH is a storage length
12 # (SMALLINT 2, INTEGER 4, DATE 4, ...) and no size is normalized from it.
13 38 my %DECLARED_WITHOUT_LENGTH = map { $_ => 1 } (
14 'SMALLINT', 'INTEGER', 'BIGINT', 'REAL', 'DOUBLE', 'DATE', 'TIME', 'XML',
15 'BOOLEAN', 'LONG VARCHAR', 'LONG VARGRAPHIC',
16 );
17  
18 38 my %DATA_TYPE = (
19 'CHARACTER' => 'char',
20 'DOUBLE' => 'double precision',
21 );
22  
23 37 ▶sub _normalized_type {
Subroutine _normalized_type: called CC=2 SCAR=6.9
24 37 my ($typename) = @_;
25 37 ▶ return $DATA_TYPE{$typename} // lc $typename;
Condition100%2/2
$DATA_TYPE{$typename} // lc $typename
l!l
334
MC/DC100%1/1
$DATA_TYPE{$typename} // lc $typename
$DATA_TYPE{$typename}
Truth table100%2/2
A
A= $DATA_TYPE{$typename}
Aresult
11
00
26 }
27  
28 37 ▶sub _normalized_size {
Subroutine _normalized_size: called CC=10 SCAR=23.4
29 37 my ($typename, $length, $scale) = @_;
30 37 ▶ return undef if $DECLARED_WITHOUT_LENGTH{$typename};
Branch: 1 branch
BranchTrueFalseTotal
if $DECLARED_WITHOUT_LENGTH{$typename} 18 19 37
31 19 ▶ return undef unless defined $length;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length 0 19 19
32 19 ▶ return $scale // 0 if $typename eq 'TIMESTAMP';
Branch: 1 branch
BranchTrueFalseTotal
if $typename eq "TIMESTAMP" 4 15 19
Condition50%1/2
$scale // 0
l!l
40
MC/DC0%0/1
$scale // 0
$scale
Truth table50%1/2
A
A= $scale
Aresult
11
00
33 15 ▶ return ($length == 8 ? 16 : $length == 16 ? 34 : $length) if $typename eq 'DECFLOAT';
Branch: 3 branches
BranchTrueFalseTotal
if $typename eq "DECFLOAT" 2 13 15
$length == 8 ? : 1 1 2
$length == 16 ? : 1 0 1
34 13 ▶ return [ $length + 0, ($scale // 0) + 0 ] if $typename eq 'DECIMAL';
Branch: 1 branch
BranchTrueFalseTotal
if $typename eq "DECIMAL" 2 11 13
Condition50%1/2
$scale // 0
l!l
20
MC/DC0%0/1
$scale // 0
$scale
Truth table50%1/2
A
A= $scale
Aresult
11
00
35 11 return $length + 0;
36 }
37  
38 # The catalog's DEFAULT is SQL text: a string literal becomes the string, a
39 # number stays its text, anything else is an expression and a SCALAR reference.
40 37 ▶sub _normalized_default {
Subroutine _normalized_default: called CC=6 SCAR=17.9
41 37 my ($text) = @_;
42 37 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 20 17 37
43 17 (my $t = $text) =~ s/\A\s+|\s+\z//g;
44 17 ▶ return undef if $t eq '' || uc($t) eq 'NULL';
Branch: 1 branch
BranchTrueFalseTotal
if $t eq "" or uc $t eq "NULL" 1 16 17
Condition66%2/3
$t eq "" or uc $t eq "NULL"
l!l&&r!l&&!r
0116
MC/DC50%1/2
$t eq "" or uc $t eq "NULL"
$t eq "" uc $t eq "NULL"
Truth table66%2/3
A or B
A= $t eq ""B= uc $t eq "NULL"
ABresult
1X1
011
000
45 16 ▶ if ($t =~ /\A'((?:[^']|'')*)'\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t =~ /\A'((?:[^']|'')*)'\z/s) 8 8 16
46 8 (my $v = $1) =~ s/''/'/g;
47 8 return $v;
48 }
49 8 ▶ return $t if $t =~ /\A[+-]?\d+(?:\.\d+)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /\A[+-]?\d+(?:\.\d+)?\z/ 3 5 8
50 5 return \$t;
51 }
52  
53 # The catalog keeps the text of a DEFAULT as it was written. For a BOOLEAN a quoted
54 # number ('1') and the bare number (1) are one default, and install_ddl writes a
55 # class value of digits bare, so the catalog form is read as the number: the
56 # column made by hand with DEFAULT '1' and the one made from its class compare
57 # equal (karr k55). Other defaults, and other types, stay as the catalog has them.
58 37 ▶sub _catalog_default {
Subroutine _catalog_default: called CC=4 SCAR=13.9
59 37 my ($typename, $text) = @_;
60 37 ▶ return $text unless defined $text && $typename eq 'BOOLEAN';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and $typename eq "BOOLEAN" 29 8 37
Condition100%3/3
defined $text and $typename eq "BOOLEAN"
!ll&&!rl&&r
2098
MC/DC100%2/2
defined $text and $typename eq "BOOLEAN"
defined $text $typename eq "BOOLEAN"
Truth table100%3/3
A and B
A= defined $textB= $typename eq "BOOLEAN"
ABresult
0X0
100
111
61 8 (my $t = $text) =~ s/\A\s+|\s+\z//g;
62 8 ▶ return $1 if $t =~ /\A'([+-]?\d+(?:\.\d+)?)'\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /\A'([+-]?\d+(?:\.\d+)?)'\z/ 2 6 8
63 6 return $text;
64 }
65  
66 2 ▶sub fetch {
Subroutine fetch: called CC=20 SCAR=30.5
Pod documented
67 2 my ($class, $dbh, $schema, $tables) = @_;
68 2 local $dbh->{FetchHashKeyName} = 'NAME_lc'; # DBD::DB2 upper-cases hashref keys
69 2 my %columns;
70  
71 # Pull all columns for the schema in one go.
72 2 my $col_sth = $dbh->prepare(q{
73 SELECT colname, tabname, colno, typename, nulls, default,
74 length, scale, identity
75 FROM syscat.columns
76 WHERE tabschema = ?
77 ORDER BY tabname, colno
78 });
79 2 $col_sth->execute($schema);
80  
81 2 ▶ while (my $row = $col_sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $col_sth->fetchrow_hashref) 37 2 39
82 37 ▶ next unless exists $tables->{ $row->{tabname} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$tables{$row->{"tabname"}} 0 37 37
83 37 my $type = $row->{typename};
84 37 my $size = $row->{length};
85 37 ▶ if ($type eq 'TIMESTAMP') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'TIMESTAMP') { } 4 33 37
elsif ($type eq 'DECFLOAT') { } 2 31 33
elsif (defined $row->{'scale'} and $row->{'scale'} > 0) { } 2 29 31
86 # The one type whose LENGTH is not what its declaration takes: LENGTH is
87 # the storage length (7 to 13) and SCALE the fractional-seconds precision
88 # (0 to 12), and TIMESTAMP(p) takes the precision alone. "10,6" made the
89 # diff write TIMESTAMP(10,6) (SQL0104N); the bare length 7 of a
90 # TIMESTAMP(0) was accepted and gave a TIMESTAMP(7) (karr k41).
91 4 $size = $row->{scale};
92 }
93 elsif ($type eq 'DECFLOAT') {
94 # LENGTH is the storage length, 8 or 16 bytes, of DECFLOAT(16) and
95 # DECFLOAT(34); the declaration takes the digits. Carried as the digits,
96 # size is what a result class writes and what the diff renders back
97 # (karr k45).
98 2 ▶ $size = $size == 8 ? 16 : $size == 16 ? 34 : $size;
Branch: 2 branches
BranchTrueFalseTotal
$size == 8 ? : 1 1 2
$size == 16 ? : 1 0 1
99 }
100 ▶ elsif (defined $row->{scale} && $row->{scale} > 0) {
Condition66%2/3
defined $row->{"scale"} and $row->{"scale"} > 0
!ll&&!rl&&r
0292
MC/DC50%1/2
defined $row->{"scale"} and $row->{"scale"} > 0
defined $row->{"scale"} $row->{"scale"} > 0
Truth table66%2/3
A and B
A= defined $row->{"scale"}B= $row->{"scale"} > 0
ABresult
0X0
100
111
101 2 $size .= ",$row->{scale}";
102 }
103 my %col = (
104 column_name => $row->{colname},
105 data_type => _normalized_type($type),
106 not_null => (uc($row->{nulls} // 'Y') eq 'N') ? 1 : 0,
107 default_value => _normalized_default($row->{default}),
108 is_pk => 0,
109 pk_position => 0,
110 # What the catalog holds, for the diff (ADR 0013).
111 catalog_type => $type,
112 catalog_size => $size // undef,
113 37 ▶ catalog_default => _catalog_default($type, $row->{default}),
Branch: 1 branch
BranchTrueFalseTotal
uc($row->{'nulls'} // 'Y') eq 'N' ? : 11 26 37
Condition50%2/4
$row->{"nulls"} // "Y"
l!l
370
$size // undef
l!l
370
MC/DC0%0/1
$row->{"nulls"} // "Y"
$row->{"nulls"}
MC/DC0%0/1
$size // undef
$size
Truth table50%1/2
A
A= $row->{"nulls"}
Aresult
11
00
Truth table50%1/2
A
A= $size
Aresult
11
00
114 );
115 37 my $norm_size = _normalized_size($type, $row->{length}, $row->{scale});
116 37 ▶ $col{size} = $norm_size if defined $norm_size;
Branch: 1 branch
BranchTrueFalseTotal
if defined $norm_size 19 18 37
117 # GENERATED ... AS IDENTITY -> syscat.columns.identity = 'Y'
118 37 ▶ $col{is_auto_increment} = 1 if lc($row->{identity} // 'N') eq 'y';
Branch: 1 branch
BranchTrueFalseTotal
if lc($row->{"identity"} // "N") eq "y" 1 36 37
Condition50%1/2
$row->{"identity"} // "N"
l!l
370
MC/DC0%0/1
$row->{"identity"} // "N"
$row->{"identity"}
Truth table50%1/2
A
A= $row->{"identity"}
Aresult
11
00
119 37 push @{ $columns{ $row->{tabname} } }, \%col;
120 }
121  
122 # Primary-key membership.
123 2 my $pk_sth = $dbh->prepare(q{
124 SELECT kcu.tabname, kcu.colname, kcu.colseq
125 FROM syscat.keycoluse kcu
126 JOIN syscat.tabconst tc
127 ON kcu.constname = tc.constname
128 AND kcu.tabschema = tc.tabschema
129 AND kcu.tabname = tc.tabname
130 WHERE tc.tabschema = ?
131 AND tc.type = 'P'
132 ORDER BY kcu.tabname, kcu.colseq
133 });
134 2 $pk_sth->execute($schema);
135  
136 2 ▶ while (my $row = $pk_sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $pk_sth->fetchrow_hashref) 1 2 3
137 1 ▶ my $list = $columns{ $row->{tabname} } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $list = $columns{$row->{"tabname"}} or next 1 0 1
138 1 for my $col (@$list) {
139 1 ▶ if ($col->{column_name} eq $row->{colname}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col->{"column_name"} eq $row->{"colname"}) 1 0 1
140 1 $col->{is_pk} = 1;
141 1 ▶ $col->{pk_position} = $row->{colseq} || 1;
Condition50%1/2
$row->{'colseq'} || 1
l!l
10
MC/DC0%0/1
$row->{'colseq'} || 1
$row->{'colseq'}
Truth table50%1/2
A
A= $row->{'colseq'}
Aresult
11
00
142 1 last;
143 }
144 }
145 }
146  
147 2 return \%columns;
148 }
149  
150 38 1;
151  
152 __END__
« DB2/Storage/Cursor.pm ↑ Summary DB2/Diff/Column.pm »