| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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"} // ""
MC/DC0%0/1
$row->{"type"} // ""
$row->{"type"}
Truth table50%1/2
A
A= $row->{"type"}
| |||||||||||||
| 53 | 6 | ▶ | my $kind = $type eq 'V' ? 'view' : 'table'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 59 | }; | ||||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | 1 | return \%tables; | |||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | 38 | 1; | |||||||||||
| 66 | |||||||||||||
| 67 | __END__ | ||||||||||||