| 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
MC/DC100%1/1
$DATA_TYPE{$typename} // lc $typename
$DATA_TYPE{$typename}
Truth table100%2/2
A
A= $DATA_TYPE{$typename}
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 31 | 19 | ▶ | return undef unless defined $length; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 32 | 19 | ▶ | return $scale // 0 if $typename eq 'TIMESTAMP'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$scale // 0
MC/DC0%0/1
$scale // 0
$scale
Truth table50%1/2
A
A= $scale
| |||||||||||||||||||||||||||||||
| 33 | 15 | ▶ | return ($length == 8 ? 16 : $length == 16 ? 34 : $length) if $typename eq 'DECFLOAT'; | ||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||
| 34 | 13 | ▶ | return [ $length + 0, ($scale // 0) + 0 ] if $typename eq 'DECIMAL'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$scale // 0
MC/DC0%0/1
$scale // 0
$scale
Truth table50%1/2
A
A= $scale
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$t eq "" or uc $t eq "NULL"
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"
| |||||||||||||||||||||||||||||||
| 45 | 16 | ▶ | if ($t =~ /\A'((?:[^']|'')*)'\z/s) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $text and $typename eq "BOOLEAN"
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"
| |||||||||||||||||||||||||||||||
| 61 | 8 | (my $t = $text) =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||||
| 62 | 8 | ▶ | return $1 if $t =~ /\A'([+-]?\d+(?:\.\d+)?)'\z/; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 82 | 37 | ▶ | next unless exists $tables->{ $row->{tabname} }; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 83 | 37 | my $type = $row->{typename}; | |||||||||||||||||||||||||||||
| 84 | 37 | my $size = $row->{length}; | |||||||||||||||||||||||||||||
| 85 | 37 | ▶ | if ($type eq 'TIMESTAMP') { | ||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||
| 100 | ▶ | elsif (defined $row->{scale} && $row->{scale} > 0) { | |||||||||||||||||||||||||||||
|
Condition66%2/3
defined $row->{"scale"} and $row->{"scale"} > 0
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
| |||||||||||||||||||||||||||||||
| 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
Condition50%2/4
$row->{"nulls"} // "Y"
$size // undef
MC/DC0%0/1
$row->{"nulls"} // "Y"
$row->{"nulls"}
MC/DC0%0/1
$size // undef
$size
Truth table50%1/2
A
A= $row->{"nulls"}
Truth table50%1/2
A
A= $size
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$row->{"identity"} // "N"
MC/DC0%0/1
$row->{"identity"} // "N"
$row->{"identity"}
Truth table50%1/2
A
A= $row->{"identity"}
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 137 | 1 | ▶ | my $list = $columns{ $row->{tabname} } or next; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 138 | 1 | for my $col (@$list) { | |||||||||||||||||||||||||||||
| 139 | 1 | ▶ | if ($col->{column_name} eq $row->{colname}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 140 | 1 | $col->{is_pk} = 1; | |||||||||||||||||||||||||||||
| 141 | 1 | ▶ | $col->{pk_position} = $row->{colseq} || 1; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$row->{'colseq'} || 1
MC/DC0%0/1
$row->{'colseq'} || 1
$row->{'colseq'}
Truth table50%1/2
A
A= $row->{'colseq'}
| |||||||||||||||||||||||||||||||
| 142 | 1 | last; | |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||
| 147 | 2 | return \%columns; | |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||
| 150 | 38 | 1; | |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||||||||||||