Firebird/Introspect/Columns.pm

filter: Statementstmt 21.7% 10 / 46 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 75.0% 3 / 4 Podpod 100% 1 / 1 totaltotal 17.7% 14 / 79 CC 14 SCAR 51.5
CC
14
Cov
14%
fetch
210.0
BEGIN
1.0
BEGIN
1.0
CRAP 173.1
« Firebird/DDL.pm ↑ Summary Firebird/Introspect/Indexes.pm »
1 package DBIO::Firebird::Introspect::Columns;
2 # ABSTRACT: Introspect Firebird columns via rdb$fields / rdb$relation_fields
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 3 ▶use DBIO::Firebird::Type qw(sql_type_from_rdb);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10  
11 0 ▶sub fetch {
Subroutine fetch: not called CC=14 SCAR=53.5
Pod documented
12 0 my ($class, $dbh, $tables) = @_;
13 0 my %columns;
14  
15 0 my $sth = $dbh->prepare(q{
16 SELECT rf.rdb$relation_name, rf.rdb$field_name, rf.rdb$field_position,
17 rf.rdb$null_flag,
18 f.rdb$field_type, f.rdb$field_sub_type, f.rdb$field_scale,
19 f.rdb$field_precision, f.rdb$character_set_id, f.rdb$character_length,
20 rf.rdb$default_source
21 FROM rdb$relation_fields rf
22 JOIN rdb$fields f ON rf.rdb$field_source = f.rdb$field_name
23 WHERE rf.rdb$system_flag = 0
24 ORDER BY rf.rdb$relation_name, rf.rdb$field_position
25 });
26 0 $sth->execute;
27  
28 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
29 0 my $table = $row->{'rdb$relation_name'};
30 0 $table =~ s/\s+$//;
31 0 ▶ next unless exists $tables->{$table};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tables->{$table} 0 0 0
32  
33 0 my $name = $row->{'rdb$field_name'};
34 0 $name =~ s/\s+$//;
35  
36 0 my $type = sql_type_from_rdb($row->{'rdb$field_type'}, $row->{'rdb$field_sub_type'});
37  
38 0 my $size;
39 0 ▶ if ($type =~ /^(?:char|varchar)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /^(?:char|varchar)/) { } 0 0 0
elsif ($type =~ /^(?:numeric|decimal)/ and defined $row->{'rdb$field_precision'}) { } 0 0 0
40 0 $size = $row->{'rdb$character_length'};
41 ▶ } elsif ($type =~ /^(?:numeric|decimal)/ && defined $row->{'rdb$field_precision'}) {
Condition0%0/3
$type =~ /^(?:numeric|decimal)/ and defined $row->{"rdb\$field_precision"}
!ll&&!rl&&r
000
MC/DC0%0/2
$type =~ /^(?:numeric|decimal)/ and defined $row->{"rdb\$field_precision"}
$type =~ /^(?:numeric|decimal)/ defined $row->{"rdb\$field_precision"}
Truth table0%0/3
A and B
A= $type =~ /^(?:numeric|decimal)/B= defined $row->{"rdb\$field_precision"}
ABresult
0X0
100
111
42 # rdb$field_scale is stored as a negative number of fractional digits.
43 0 ▶ $size = [$row->{'rdb$field_precision'}, abs($row->{'rdb$field_scale'} // 0)];
Condition0%0/2
$row->{"rdb\$field_scale"} // 0
l!l
00
MC/DC0%0/1
$row->{"rdb\$field_scale"} // 0
$row->{"rdb\$field_scale"}
Truth table0%0/2
A
A= $row->{"rdb\$field_scale"}
Aresult
11
00
44 }
45  
46 0 my $default;
47 0 ▶ if (my $src = $row->{'rdb$default_source'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $src = $row->{"rdb\$default_source"}) 0 0 0
48 0 $src =~ s/^\s+|\s+$//g;
49 0 ▶ if (my ($def) = $src =~ /^DEFAULT \s+(\S+)/ix) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($def) = $src =~ /^DEFAULT \s+(\S+)/xi) 0 0 0
50 0 $default = $def;
51 }
52 }
53  
54 0 ▶ push @{ $columns{$table} }, {
Branch: 1 branch
BranchTrueFalseTotal
$row->{'rdb$null_flag'} ? : 0 0 0
55 column_name => $name,
56 data_type => $type,
57 0 not_null => $row->{'rdb$null_flag'} ? 1 : 0,
58 default_value => $default,
59 is_pk => 0,
60 pk_position => 0,
61 size => $size,
62 };
63 }
64  
65 # Primary-key membership
66 0 my $pk_sth = $dbh->prepare(q{
67 SELECT rc.rdb$relation_name, iseg.rdb$field_name, iseg.rdb$field_position
68 FROM rdb$relation_constraints rc
69 JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name
70 WHERE rc.rdb$constraint_type = 'PRIMARY KEY'
71 ORDER BY rc.rdb$relation_name, iseg.rdb$field_position
72 });
73 0 $pk_sth->execute;
74  
75 0 ▶ while (my $row = $pk_sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $pk_sth->fetchrow_hashref) 0 0 0
76 0 my $table = $row->{'rdb$relation_name'};
77 0 $table =~ s/\s+$//;
78 0 my $col = $row->{'rdb$field_name'};
79 0 $col =~ s/\s+$//;
80 0 ▶ my $list = $columns{$table} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $list = $columns{$table} or next 0 0 0
81 0 for my $c (@$list) {
82 0 ▶ if ($c->{column_name} eq $col) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"column_name"} eq $col) 0 0 0
83 0 $c->{is_pk} = 1;
84 # rdb$field_position is 0-based; keep it raw so composite-PK ordering
85 # (table_pk_info sorts on it) stays correct. (`|| 1` collapsed pos 0
86 # and 1 to the same value.)
87 0 $c->{pk_position} = $row->{'rdb$field_position'};
88 0 last;
89 }
90 }
91 }
92  
93 0 return \%columns;
94 }
95  
96 3 1;
97  
98 __END__
99  
100 =pod
101  
102 =encoding UTF-8
103  
104 =head1 NAME
105  
106 DBIO::Firebird::Introspect::Columns - Introspect Firebird columns via rdb$fields / rdb$relation_fields
107  
108 =head1 VERSION
109  
110 version 0.900001
111  
112 =head1 DESCRIPTION
113  
114 Fetches column metadata via C<rdb$relation_fields> joined against
115 C<rdb$fields>. Primary-key information is derived from
116 C<rdb$index_segments> joined against C<rdb$relation_constraints>.
117  
118 =head1 METHODS
119  
120 =head2 fetch
121  
122 my $columns = DBIO::Firebird::Introspect::Columns->fetch($dbh, $tables);
123  
124 Given the tables hashref from L<DBIO::Firebird::Introspect::Tables>,
125 returns a hashref keyed by table name. Each value is an arrayref of
126 column hashrefs in ordinal position order with keys:
127 C<column_name>, C<data_type>, C<not_null>, C<default_value>,
128 C<is_pk>, C<pk_position>, C<size>.
129  
130 =head1 AUTHOR
131  
132 DBIO & DBIx::Class Authors
133  
134 =head1 COPYRIGHT AND LICENSE
135  
136 Copyright (C) 2026 DBIO Authors
137 Portions Copyright (C) 2005-2025 DBIx::Class Authors
138 Based on DBIx::Class, heavily modified.
139  
140 This is free software; you can redistribute it and/or modify it under
141 the same terms as the Perl 5 programming language system itself.
142  
143 =cut
« Firebird/DDL.pm ↑ Summary Firebird/Introspect/Indexes.pm »