| 1 | package DBIO::Firebird::Introspect::Columns; | ||||||||||||||||||||
| 2 | # ABSTRACT: Introspect Firebird columns via rdb$fields / rdb$relation_fields | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 7 | our $VERSION = '0.900007'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 7 | ▶ | use DBIO::Firebird::Type qw(sql_type_from_rdb); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | ▶ | sub fetch { | ||||||||||||||||||
|
Subroutine
fetch: called CC=16 SCAR=38.1
Pod
documented
| |||||||||||||||||||||
| 14 | 1 | my ($class, $dbh, $tables) = @_; | |||||||||||||||||||
| 15 | 1 | my %columns; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 1 | my $sth = $dbh->prepare(q{ | |||||||||||||||||||
| 18 | SELECT rf.rdb$relation_name, rf.rdb$field_name, rf.rdb$field_position, | ||||||||||||||||||||
| 19 | rf.rdb$null_flag, | ||||||||||||||||||||
| 20 | f.rdb$field_type, f.rdb$field_sub_type, f.rdb$field_scale, | ||||||||||||||||||||
| 21 | f.rdb$field_precision, f.rdb$character_set_id, f.rdb$character_length, | ||||||||||||||||||||
| 22 | rf.rdb$default_source, rf.rdb$identity_type | ||||||||||||||||||||
| 23 | FROM rdb$relation_fields rf | ||||||||||||||||||||
| 24 | JOIN rdb$fields f ON rf.rdb$field_source = f.rdb$field_name | ||||||||||||||||||||
| 25 | WHERE rf.rdb$system_flag = 0 | ||||||||||||||||||||
| 26 | ORDER BY rf.rdb$relation_name, rf.rdb$field_position | ||||||||||||||||||||
| 27 | }); | ||||||||||||||||||||
| 28 | 1 | $sth->execute; | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 1 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | 3 | my $table = $row->{'rdb$relation_name'}; | |||||||||||||||||||
| 32 | 3 | $table =~ s/\s+$//; | |||||||||||||||||||
| 33 | 3 | ▶ | next unless exists $tables->{$table}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 3 | my $name = $row->{'rdb$field_name'}; | |||||||||||||||||||
| 36 | 3 | $name =~ s/\s+$//; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | my $type = sql_type_from_rdb( | ||||||||||||||||||||
| 39 | 3 | $row->{'rdb$field_type'}, $row->{'rdb$field_sub_type'}, $row->{'rdb$character_set_id'}, | |||||||||||||||||||
| 40 | ); | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 3 | my $size; | |||||||||||||||||||
| 43 | 3 | ▶ | if ($type =~ /^(?:char|varchar|binary|varbinary)$/) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 44 | 0 | $size = $row->{'rdb$character_length'}; | |||||||||||||||||||
| 45 | } elsif ($type eq 'decfloat') { | ||||||||||||||||||||
| 46 | 0 | $size = $row->{'rdb$field_precision'}; | |||||||||||||||||||
| 47 | ▶ | } elsif ($type =~ /^(?:numeric|decimal)$/ && defined $row->{'rdb$field_precision'}) { | |||||||||||||||||||
|
Condition33%1/3
$type =~ /^(?:numeric|decimal)$/ and defined $row->{"rdb\$field_precision"}
MC/DC0%0/2
$type =~ /^(?:numeric|decimal)$/ and defined $row->{"rdb\$field_precision"}
$type =~ /^(?:numeric|decimal)$/
defined $row->{"rdb\$field_precision"}
Truth table33%1/3
A and B
A= $type =~ /^(?:numeric|decimal)$/B= defined $row->{"rdb\$field_precision"}
| |||||||||||||||||||||
| 48 | # rdb$field_scale is stored as a negative number of fractional digits. | ||||||||||||||||||||
| 49 | 0 | ▶ | $size = [$row->{'rdb$field_precision'}, abs($row->{'rdb$field_scale'} // 0)]; | ||||||||||||||||||
|
Condition0%0/2
$row->{"rdb\$field_scale"} // 0
MC/DC0%0/1
$row->{"rdb\$field_scale"} // 0
$row->{"rdb\$field_scale"}
Truth table0%0/2
A
A= $row->{"rdb\$field_scale"}
| |||||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 3 | my $default; | |||||||||||||||||||
| 53 | 3 | ▶ | if (my $src = $row->{'rdb$default_source'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 0 | $src =~ s/^\s+|\s+$//g; | |||||||||||||||||||
| 55 | # the whole expression: a quoted literal may hold spaces | ||||||||||||||||||||
| 56 | 0 | ▶ | if (my ($def) = $src =~ /^DEFAULT \s+(.+)$/isx) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 0 | $default = $def; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 3 | ▶ | push @{ $columns{$table} }, { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 62 | column_name => $name, | ||||||||||||||||||||
| 63 | data_type => $type, | ||||||||||||||||||||
| 64 | not_null => $row->{'rdb$null_flag'} ? 1 : 0, | ||||||||||||||||||||
| 65 | default_value => $default, | ||||||||||||||||||||
| 66 | # rdb$identity_type: 0 ALWAYS, 1 BY DEFAULT, NULL not an identity column | ||||||||||||||||||||
| 67 | 3 | is_auto_increment => defined $row->{'rdb$identity_type'} ? 1 : 0, | |||||||||||||||||||
| 68 | is_pk => 0, | ||||||||||||||||||||
| 69 | pk_position => 0, | ||||||||||||||||||||
| 70 | size => $size, | ||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | # Primary-key membership | ||||||||||||||||||||
| 75 | 1 | my $pk_sth = $dbh->prepare(q{ | |||||||||||||||||||
| 76 | SELECT rc.rdb$relation_name, iseg.rdb$field_name, iseg.rdb$field_position | ||||||||||||||||||||
| 77 | FROM rdb$relation_constraints rc | ||||||||||||||||||||
| 78 | JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name | ||||||||||||||||||||
| 79 | WHERE rc.rdb$constraint_type = 'PRIMARY KEY' | ||||||||||||||||||||
| 80 | ORDER BY rc.rdb$relation_name, iseg.rdb$field_position | ||||||||||||||||||||
| 81 | }); | ||||||||||||||||||||
| 82 | 1 | $pk_sth->execute; | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 1 | ▶ | while (my $row = $pk_sth->fetchrow_hashref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 0 | my $table = $row->{'rdb$relation_name'}; | |||||||||||||||||||
| 86 | 0 | $table =~ s/\s+$//; | |||||||||||||||||||
| 87 | 0 | my $col = $row->{'rdb$field_name'}; | |||||||||||||||||||
| 88 | 0 | $col =~ s/\s+$//; | |||||||||||||||||||
| 89 | 0 | ▶ | my $list = $columns{$table} or next; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 0 | for my $c (@$list) { | |||||||||||||||||||
| 91 | 0 | ▶ | if ($c->{column_name} eq $col) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 0 | $c->{is_pk} = 1; | |||||||||||||||||||
| 93 | # rdb$field_position is 0-based; keep it raw so composite-PK ordering | ||||||||||||||||||||
| 94 | # (table_pk_info sorts on it) stays correct. (`|| 1` collapsed pos 0 | ||||||||||||||||||||
| 95 | # and 1 to the same value.) | ||||||||||||||||||||
| 96 | 0 | $c->{pk_position} = $row->{'rdb$field_position'}; | |||||||||||||||||||
| 97 | 0 | last; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 1 | return \%columns; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 7 | 1; | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||