| 1 | package DBIO::DB2::Introspect::Columns; | ||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect DB2 columns | ||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||||||||||||||||||
| 10 | 0 | my ($class, $dbh, $schema, $tables) = @_; | |||||||||||||||||||||||||||||
| 11 | 0 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; # DBD::DB2 upper-cases hashref keys | |||||||||||||||||||||||||||||
| 12 | 0 | my %columns; | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | # Pull all columns for the schema in one go. | ||||||||||||||||||||||||||||||
| 15 | 0 | my $col_sth = $dbh->prepare(q{ | |||||||||||||||||||||||||||||
| 16 | SELECT colname, tabname, colno, typename, nulls, default, | ||||||||||||||||||||||||||||||
| 17 | length, scale, identity | ||||||||||||||||||||||||||||||
| 18 | FROM syscat.columns | ||||||||||||||||||||||||||||||
| 19 | WHERE tabschema = ? | ||||||||||||||||||||||||||||||
| 20 | ORDER BY tabname, colno | ||||||||||||||||||||||||||||||
| 21 | }); | ||||||||||||||||||||||||||||||
| 22 | 0 | $col_sth->execute($schema); | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | while (my $row = $col_sth->fetchrow_hashref) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | next unless exists $tables->{ $row->{tabname} }; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 26 | 0 | my $type = $row->{typename}; | |||||||||||||||||||||||||||||
| 27 | 0 | my $size = $row->{length}; | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | $size .= ",$row->{scale}" if defined $row->{scale} && $row->{scale} > 0; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $row->{"scale"} and $row->{"scale"} > 0
MC/DC0%0/2
defined $row->{"scale"} and $row->{"scale"} > 0
defined $row->{"scale"}
$row->{"scale"} > 0
Truth table0%0/3
A and B
A= defined $row->{"scale"}B= $row->{"scale"} > 0
| |||||||||||||||||||||||||||||||
| 29 | my %col = ( | ||||||||||||||||||||||||||||||
| 30 | column_name => $row->{colname}, | ||||||||||||||||||||||||||||||
| 31 | data_type => $type, | ||||||||||||||||||||||||||||||
| 32 | not_null => (uc($row->{nulls} // 'Y') eq 'N') ? 1 : 0, | ||||||||||||||||||||||||||||||
| 33 | default_value => $row->{default}, | ||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | is_pk => 0, | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/4
$row->{"nulls"} // "Y"
$size // undef
MC/DC0%0/1
$row->{"nulls"} // "Y"
$row->{"nulls"}
MC/DC0%0/1
$size // undef
$size
Truth table0%0/2
A
A= $row->{"nulls"}
Truth table0%0/2
A
A= $size
| |||||||||||||||||||||||||||||||
| 35 | pk_position => 0, | ||||||||||||||||||||||||||||||
| 36 | size => $size // undef, | ||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||
| 38 | # GENERATED ... AS IDENTITY -> syscat.columns.identity = 'Y' | ||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | $col{is_auto_increment} = 1 if lc($row->{identity} // 'N') eq 'y'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$row->{"identity"} // "N"
MC/DC0%0/1
$row->{"identity"} // "N"
$row->{"identity"}
Truth table0%0/2
A
A= $row->{"identity"}
| |||||||||||||||||||||||||||||||
| 40 | 0 | push @{ $columns{ $row->{tabname} } }, \%col; | |||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||
| 43 | # Primary-key membership. | ||||||||||||||||||||||||||||||
| 44 | 0 | my $pk_sth = $dbh->prepare(q{ | |||||||||||||||||||||||||||||
| 45 | SELECT kcu.tabname, kcu.colname, kcu.colseq | ||||||||||||||||||||||||||||||
| 46 | FROM syscat.keycoluse kcu | ||||||||||||||||||||||||||||||
| 47 | JOIN syscat.tabconst tc | ||||||||||||||||||||||||||||||
| 48 | ON kcu.constname = tc.constname | ||||||||||||||||||||||||||||||
| 49 | AND kcu.tabschema = tc.tabschema | ||||||||||||||||||||||||||||||
| 50 | AND kcu.tabname = tc.tabname | ||||||||||||||||||||||||||||||
| 51 | WHERE tc.tabschema = ? | ||||||||||||||||||||||||||||||
| 52 | AND tc.type = 'P' | ||||||||||||||||||||||||||||||
| 53 | ORDER BY kcu.tabname, kcu.colseq | ||||||||||||||||||||||||||||||
| 54 | }); | ||||||||||||||||||||||||||||||
| 55 | 0 | $pk_sth->execute($schema); | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | while (my $row = $pk_sth->fetchrow_hashref) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $list = $columns{ $row->{tabname} } or next; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 59 | 0 | for my $col (@$list) { | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ($col->{column_name} eq $row->{colname}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 61 | 0 | $col->{is_pk} = 1; | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | $col->{pk_position} = $row->{colseq} || 1; | ||||||||||||||||||||||||||||
|
Condition0%0/2
$row->{'colseq'} || 1
MC/DC0%0/1
$row->{'colseq'} || 1
$row->{'colseq'}
Truth table0%0/2
A
A= $row->{'colseq'}
| |||||||||||||||||||||||||||||||
| 63 | 0 | last; | |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||
| 68 | 0 | return \%columns; | |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||
| 71 | 3 | 1; | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||
| 73 | __END__ | ||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||
| 75 | =pod | ||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||
| 77 | =encoding UTF-8 | ||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||
| 79 | =head1 NAME | ||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | DBIO::DB2::Introspect::Columns - Introspect DB2 columns | ||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||
| 83 | =head1 VERSION | ||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||
| 85 | version 0.900001 | ||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||
| 87 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||
| 89 | Fetches column metadata via C<SYSCAT.COLUMNS>. Primary-key membership | ||||||||||||||||||||||||||||||
| 90 | is determined via C<SYSCAT.KEYCOLUSE> joined with C<SYSCAT.TABCONST>. | ||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||
| 92 | =head1 METHODS | ||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||
| 94 | =head2 fetch | ||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||
| 96 | my $columns = DBIO::DB2::Introspect::Columns->fetch($dbh, $schema, $tables); | ||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||
| 98 | Given the tables hashref from L<DBIO::DB2::Introspect::Tables>, | ||||||||||||||||||||||||||||||
| 99 | returns a hashref keyed by table name. Each value is an arrayref of | ||||||||||||||||||||||||||||||
| 100 | column hashrefs in C<COLNO> order with keys: | ||||||||||||||||||||||||||||||
| 101 | C<column_name>, C<data_type>, C<not_null>, C<default_value>, | ||||||||||||||||||||||||||||||
| 102 | C<is_pk>, C<pk_position>, C<size>. C<is_auto_increment> is set (to 1) only | ||||||||||||||||||||||||||||||
| 103 | for C<GENERATED ... AS IDENTITY> columns, matching the canonical model shape | ||||||||||||||||||||||||||||||
| 104 | consumed by L<DBIO::Introspect::Base/table_columns_info>. | ||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||
| 106 | =head1 AUTHOR | ||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||
| 108 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||
| 112 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||||
| 113 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||||
| 114 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||
| 116 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||
| 117 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||||||||||||