DB2/Introspect/Tables.pm

filter: Statementstmt 100% 24 / 24 Branchbran 90.0% 9 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 92.8% 39 / 42 CC 7 SCAR 12.8
CC
7
Cov
94%
fetch
5.0
_view_definition
3.0
BEGIN
1.0
CRAP 3.6
« DB2/Diff/UniqueConstraint.pm ↑ Summary DB2/Introspect.pm »
1 package DBIO::DB2::Introspect::Tables;
2 # ABSTRACT: Introspect DB2 tables and views
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 # SYSCAT.VIEWS.TEXT is the whole CREATE VIEW statement as it was written:
11 # CREATE VIEW <name> [(columns)] AS <select>. The view's name, with the schema
12 # in front when the statement had one, is cut off, so the view of the live
13 # schema and that of the compare schema (ADR 0006) compare by what they read.
14 38 my $IDENT = qr/"(?:[^"]|"")*"|[A-Za-z_][\w\$#@]*/;
15  
16 3 ▶sub _view_definition {
Subroutine _view_definition: called CC=3 SCAR=11.0
17 3 my ($text) = @_;
18 3 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 3 3
19 3 $text =~ s/\A\s+//;
20 3 $text =~ s/[\s;]+\z//;
21 3 ▶ return $text =~ /\ACREATE\s+(?:OR\s+REPLACE\s+)?VIEW\s+(?:$IDENT\s*\.\s*)?$IDENT\s*(.*)\z/is
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /\ACREATE\s+(?:OR\s+REPLACE\s+)?VIEW\s+(?:$IDENT\s*\.\s*)?$IDENT\s*(.*)\z/is ? : 2 1 3
22 ? $1 : undef;
23 }
24  
25  
26 1 ▶sub fetch {
Subroutine fetch: called CC=5 SCAR=16.1
Pod documented
27 1 my ($class, $dbh, $schema) = @_;
28 1 local $dbh->{FetchHashKeyName} = 'NAME_lc'; # DBD::DB2 upper-cases hashref keys
29  
30 1 my $sth = $dbh->prepare(q{
31 SELECT t.tabname, t.type, t.tabschema,
32 CASE WHEN t.type = 'V'
33 THEN (SELECT CAST(v.text AS VARCHAR(32000))
34 FROM syscat.views v
35 WHERE v.viewschema = t.tabschema AND v.viewname = t.tabname
36 FETCH FIRST 1 ROW ONLY)
37 END AS view_text
38 FROM syscat.tables t
39 WHERE t.tabschema = ?
40 ORDER BY t.tabname
41 });
42 # No tabname filter, deliberately: a `NOT LIKE 'EX%'` here once hid real
43 # user tables (EXPENSES, EXPORT, ...) and ghosted them into every diff as
44 # phantom CREATE TABLE (SQL0601N). The EXPLAIN_* tables that filter was
45 # aimed at live in SYSTOOLS (SYSINSTALLOBJECTS, karr k27), a schema this
46 # query never reads -- tabschema is the only scope.
47 1 $sth->execute($schema);
48  
49 1 my %tables;
50 1 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 6 1 7
51 # syscat.tables.type is a single char: 'V' = view, 'T' = table, etc.
52 6 ▶ my $type = uc($row->{type} // '');
Condition50%1/2
$row->{"type"} // ""
l!l
60
MC/DC0%0/1
$row->{"type"} // ""
$row->{"type"}
Truth table50%1/2
A
A= $row->{"type"}
Aresult
11
00
53 6 ▶ my $kind = $type eq 'V' ? 'view' : 'table';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'V' ? : 3 3 6
54 $tables{ $row->{tabname} } = {
55 table_name => $row->{tabname},
56 kind => $kind,
57 schema => $row->{tabschema},
58 6 ▶ ($kind eq 'view' ? (view_definition => _view_definition($row->{view_text})) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'view' ? : 3 3 6
59 };
60 }
61  
62 1 return \%tables;
63 }
64  
65 38 1;
66  
67 __END__
« DB2/Diff/UniqueConstraint.pm ↑ Summary DB2/Introspect.pm »