blib/lib/DBIO/Introspect/DBI.pm

filter: Statementstmt 84.4% 147 / 174 Branchbran 74.3% 55 / 74 Conditioncond 66.6% 48 / 72 MC/DCmcdc 34.2% 13 / 38 Subroutinesub 89.4% 17 / 19 Podpod 100% 10 / 10 totaltotal 74.9% 290 / 387 CC 69 SCAR 25.8
CC
69
Cov
78%
table_columns
56.0
table_fk_info
16.6
table_columns_info
14.0
CRAP 13.3
« blib/lib/DBIO/Row.pm ↑ Summary blib/lib/DBIO/SQLMaker.pm »
1 package DBIO::Introspect::DBI;
2 # ABSTRACT: DBI-based introspection via standard metadata APIs
3  
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 our $VERSION = '0.900007';
8  
9 10 ▶use base qw/DBIO::Introspect::Base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use DBI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use DBIO::Componentised ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16  
17 5 ▶sub dbms_name { $_[0]->{dbms_name} //= $_[0]->dbh->{Driver}->{Name} }
Subroutine dbms_name: called CC=2 SCAR=7.2
Pod documented
Condition66%2/3
$_[0]{"dbms_name"} //= $_[0]->dbh->{"Driver"}{"Name"}
l!l&&r!l&&!r
230
MC/DC0%0/2
$_[0]{"dbms_name"} //= $_[0]->dbh->{"Driver"}{"Name"}
$_[0]{"dbms_name"} $_[0]->dbh->{"Driver"}{"Name"}
Truth table66%2/3
A //= B
A= $_[0]{"dbms_name"}B= $_[0]->dbh->{"Driver"}{"Name"}
ABresult
1X1
011
000
18  
19  
20 11 ▶sub class_for_dbh {
Subroutine class_for_dbh: called CC=8 SCAR=20.8
Pod documented
21 11 my ($class, $dbh) = @_;
22  
23 11 ▶ my $driver_name = eval { $dbh->{Driver}{Name} } or return $class;
Branch: 1 branch
BranchTrueFalseTotal
my $driver_name = eval { do { $dbh->{"Driver"}{"Name"} } } or return $class 10 1 11
24  
25 10 require DBIO::Storage::DBI;
26 10 ▶ my $storage_class = DBIO::Storage::DBI->driver_storage_class($driver_name)
Branch: 1 branch
BranchTrueFalseTotal
my $storage_class = "DBIO::Storage::DBI"->driver_storage_class($driver_name) or return $class 8 2 10
27 or return $class;
28  
29 # DBIO::MySQL::Storage and DBIO::MySQL::Storage::MariaDB both live under
30 # DBIO::MySQL. DBIO::Storage::DBI::ODBC would reduce to bare DBIO, which
31 # owns no driver introspector -- that is the generic path by definition.
32 8 (my $namespace = $storage_class) =~ s/::Storage(?:::.*)?\z//;
33 8 ▶ return $class if $namespace eq 'DBIO' || $namespace eq $storage_class;
Branch: 1 branch
BranchTrueFalseTotal
if $namespace eq "DBIO" or $namespace eq $storage_class 2 6 8
Condition66%2/3
$namespace eq "DBIO" or $namespace eq $storage_class
l!l&&r!l&&!r
206
MC/DC50%1/2
$namespace eq "DBIO" or $namespace eq $storage_class
$namespace eq "DBIO" $namespace eq $storage_class
Truth table66%2/3
A or B
A= $namespace eq "DBIO"B= $namespace eq $storage_class
ABresult
1X1
011
000
34  
35 6 my $candidate = "${namespace}::Introspect";
36  
37 # returns 0 when the .pm is simply not there, throws when it is there and
38 # fails to compile -- the two cases deserve different treatment
39 6 my $loaded = eval { DBIO::Componentised->load_optional_class($candidate) };
40 6 ▶ unless (defined $loaded) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $loaded) 1 5 6
41 1 warn "$candidate is installed but failed to load: $@"
42 . "Falling back to $class -- the generated classes will be less accurate.\n";
43 1 return $class;
44 }
45 5 ▶ return $class unless $loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded 1 4 5
46 4 ▶ return $class unless $candidate->isa('DBIO::Introspect::Base');
Branch: 1 branch
BranchTrueFalseTotal
unless $candidate->isa("DBIO::Introspect::Base") 1 3 4
47  
48 3 return $candidate;
49 }
50  
51  
52 2 ▶sub table_keys {
Subroutine table_keys: called CC=7 SCAR=20.0
Pod documented
53 2 my ($self) = @_;
54 2 my @tables;
55  
56 # DBI spec: $type is a comma-separated STRING, never an arrayref -- an
57 # arrayref stringifies to "ARRAY(0x...)" and silently matches nothing.
58 # Unquoted and whitespace-free is the form every driver reads correctly:
59 # DBD::SQLite/Pg/Oracle split on ',' and add the quoting themselves (Pg does
60 # not trim, so "TABLE, VIEW" would become ' VIEW' and match no row),
61 # DBD::MariaDB/mysql only tests m/table/i and m/view/i, DBD::Firebird strips
62 # quotes back off. The quoted form the spec also allows would break any
63 # driver that compares the split values literally.
64 2 my $sth = $self->dbh->table_info(undef, undef, '%', 'TABLE,VIEW');
65 2 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 4 2 6
66 4 ▶ my $schema = $row->{TABLE_SCHEM} // '';
Condition100%2/2
$row->{"TABLE_SCHEM"} // ""
l!l
31
MC/DC100%1/1
$row->{"TABLE_SCHEM"} // ""
$row->{"TABLE_SCHEM"}
Truth table100%2/2
A
A= $row->{"TABLE_SCHEM"}
Aresult
11
00
67 4 ▶ my $name = $row->{TABLE_NAME} // next;
Condition50%1/2
$row->{"TABLE_NAME"} // (next)
l!l
40
MC/DC0%0/1
$row->{"TABLE_NAME"} // (next)
$row->{"TABLE_NAME"}
Truth table50%1/2
A
A= $row->{"TABLE_NAME"}
Aresult
11
00
68  
69 # SQLite: only tables in main TEMP etc
70 # Oracle: filter out system schemas
71 4 ▶ if ($self->dbms_name eq 'Oracle' && $schema =~ /^(SYS|SYSTEM)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dbms_name eq "Oracle" and $schema =~ /^(SYS|SYSTEM)/) 0 4 4
Condition33%1/3
$self->dbms_name eq "Oracle" and $schema =~ /^(SYS|SYSTEM)/
!ll&&!rl&&r
400
MC/DC0%0/2
$self->dbms_name eq "Oracle" and $schema =~ /^(SYS|SYSTEM)/
$self->dbms_name eq "Oracle" $schema =~ /^(SYS|SYSTEM)/
Truth table33%1/3
A and B
A= $self->dbms_name eq "Oracle"B= $schema =~ /^(SYS|SYSTEM)/
ABresult
0X0
100
111
72 0 next;
73 }
74  
75 4 ▶ my $key = $schema ? "${schema}.${name}" : $name;
Branch: 1 branch
BranchTrueFalseTotal
$schema ? : 2 2 4
76 4 push @tables, $key;
77 }
78 2 $sth->finish;
79  
80 2 return \@tables;
81 }
82  
83  
84 0 ▶sub table_columns {
Subroutine table_columns: not called CC=7 SCAR=40.3
Pod documented
85 0 my ($self, $key) = @_;
86 0 my ($schema, $table) = $self->_split_key($key);
87  
88 0 my $sth = $self->dbh->column_info($schema, undef, $table, '%');
89 0 my @cols;
90 my %seen;
91 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
92 0 ▶ my $col = $row->{COLUMN_NAME} // next;
Condition0%0/2
$row->{"COLUMN_NAME"} // (next)
l!l
00
MC/DC0%0/1
$row->{"COLUMN_NAME"} // (next)
$row->{"COLUMN_NAME"}
Truth table0%0/2
A
A= $row->{"COLUMN_NAME"}
Aresult
11
00
93 0 ▶ push @cols, $col unless $seen{$col}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$col}++ 0 0 0
94 }
95 0 $sth->finish;
96  
97 # DBI doesn't guarantee order, so query directly for known drivers
98 0 ▶ if ($self->dbms_name eq 'SQLite') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dbms_name eq "SQLite") 0 0 0
99 0 (my $quoted = $table) =~ s/'/''/g;
100 0 my $info = $self->dbh->prepare("PRAGMA table_info('$quoted')");
101 0 $info->execute;
102 0 @cols = ();
103 0 %seen = ();
104 0 ▶ while (my $row = $info->fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $info->fetch) 0 0 0
105 0 ▶ push @cols, $row->[1] unless $seen{$row->[1]}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$row->[1]}++ 0 0 0
106 }
107 }
108  
109 0 return \@cols;
110 }
111  
112  
113 9 ▶sub table_columns_info {
Subroutine table_columns_info: called CC=14 SCAR=26.4
Pod documented
114 9 my ($self, $key) = @_;
115 9 my ($schema, $table) = $self->_split_key($key);
116  
117 # A primary-key column cannot hold NULL whatever the DBD says
118 # about its NOT NULL attribute. The driver introspectors already fold this
119 # in (DBIO::SQLite::Introspect::table_columns_info); the generic path is
120 # what a user without the driver distribution gets, so it folds it in too
121 # and both answer the same thing for the same database. ADR 0038.
122 9 my %is_pk = map { $_ => 1 } @{ $self->table_pk_info($key) };
123  
124 9 my %cols;
125 9 my $sth = $self->dbh->column_info($schema, undef, $table, '%');
126 9 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 17 9 26
127 17 ▶ my $col = $row->{COLUMN_NAME} // next;
Condition50%1/2
$row->{"COLUMN_NAME"} // (next)
l!l
170
MC/DC0%0/1
$row->{"COLUMN_NAME"} // (next)
$row->{"COLUMN_NAME"}
Truth table50%1/2
A
A= $row->{"COLUMN_NAME"}
Aresult
11
00
128  
129 # The column's data_type is TYPE_NAME, the readable type name.
130 # DATA_TYPE is the numeric SQL type code (SQL_INTEGER == 4) and used to be
131 # what landed in the generated result class. TYPE_NAME is also what the
132 # driver introspectors report, so the fallback path and the driver path now
133 # answer the same thing; a private code->name table here would be a second
134 # truth next to them and would drift.
135 17 ▶ my $type_str = $row->{TYPE_NAME} // '';
Condition100%2/2
$row->{"TYPE_NAME"} // ""
l!l
161
MC/DC100%1/1
$row->{"TYPE_NAME"} // ""
$row->{"TYPE_NAME"}
Truth table100%2/2
A
A= $row->{"TYPE_NAME"}
Aresult
11
00
136 17 my $size = $row->{COLUMN_SIZE};
137  
138 # MySQL reports TEXT as varchar(65535)
139 17 ▶ if ($type_str =~ /varchar/i && defined $size && $size == 65535) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_str =~ /varchar/i and defined $size and $size == 65535) 1 16 17
Condition100%6/6
$type_str =~ /varchar/i and defined $size and $size == 65535
!ll&&!rl&&r
1511
$type_str =~ /varchar/i and defined $size
!ll&&!rl&&r
1322
MC/DC100%3/3
$type_str =~ /varchar/i and defined $size and $size == 65535
$type_str =~ /varchar/i defined $size $size == 65535
Truth table100%4/4
A and B and C
A= $type_str =~ /varchar/iB= defined $sizeC= $size == 65535
ABCresult
0XX0
10X0
1100
1111
140 1 $type_str = 'text';
141 }
142  
143 # No type information at all. Keep generating -- a class with one guessed
144 # column is more use than an aborted run -- but say so per column, because
145 # the guess is indistinguishable from a real varchar once it is in the
146 # emitted code.
147 17 ▶ unless (length $type_str) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $type_str) 1 16 17
148 1 warn "$key.$col: this DBD reports no TYPE_NAME for the column, guessing"
149 . " data_type => 'varchar'. Correct it in the generated class, or"
150 . " install the DBIO driver distribution for this database -- its"
151 . " introspector reads the native catalog instead of the generic DBI"
152 . " metadata.\n";
153 1 $type_str = 'varchar';
154 }
155  
156 17 my $default = $row->{COLUMN_DEF};
157 # Strip quotes from default values
158 17 ▶ if (defined $default && $default =~ /^'(.*)'$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default and $default =~ /^'(.*)'$/) 1 16 17
Condition100%3/3
defined $default and $default =~ /^'(.*)'$/
!ll&&!rl&&r
1511
MC/DC100%2/2
defined $default and $default =~ /^'(.*)'$/
defined $default $default =~ /^'(.*)'$/
Truth table100%3/3
A and B
A= defined $defaultB= $default =~ /^'(.*)'$/
ABresult
0X0
100
111
159 1 $default = $1;
160 }
161  
162 my $auto_increment =
163 ($row->{COLUMN_DEF} // '') =~ /nextval/i # PostgreSQL serial
164 || $row->{mysql_is_auto_increment} # DBD::mysql / DBD::MariaDB
165 17 ▶ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($row->{'COLUMN_DEF'} // '') =~ /nextval/i || $row->{'mysql_is_auto_increment'} ? : 1 16 17
Condition80%4/5
($row->{"COLUMN_DEF"} // "") =~ /nextval/i or $row->{"mysql_is_auto_increment"}
l!l&&r!l&&!r
1016
$row->{"COLUMN_DEF"} // ""
l!l
215
MC/DC50%1/2
($row->{"COLUMN_DEF"} // "") =~ /nextval/i or $row->{"mysql_is_auto_increment"}
$row->{"COLUMN_DEF"} $row->{"mysql_is_auto_increment"}
Truth table66%2/3
A or B
A= $row->{"COLUMN_DEF"}B= $row->{"mysql_is_auto_increment"}
ABresult
1X1
011
000
166  
167 $cols{$col} = {
168 data_type => $type_str,
169 type => $type_str,
170 size => looks_like_number($size) ? $size : undef,
171 is_nullable => $self->_is_nullable($key, $col, $row->{NULLABLE},
172 17 ▶ $is_pk{$col}),
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($size) ? : 3 14 17
173 default_value => $default,
174 is_auto_increment => $auto_increment,
175 };
176 }
177 9 $sth->finish;
178  
179 9 return \%cols;
180 }
181  
182 # DBI NULLABLE: 0 = no, 1 = yes, 2 = unknown; a DBD may also leave
183 # the field out entirely, which says the same thing as 2.
184 #
185 # The primary key is asked first because it is knowledge, not a reading of the
186 # attribute: no engine lets a key column hold NULL, so there is nothing left to
187 # be unsure about and nothing to announce.
188 #
189 # "Unknown" then resolves to nullable. The two guesses are not symmetrical:
190 # is_nullable => 0 is a claim the generated class enforces before the statement
191 # is ever sent, so a wrong 0 rejects INSERTs the database would have taken and
192 # the error blames DBIO. A wrong 1 only declines to duplicate a constraint the
193 # engine already has -- the INSERT goes out and the database says no itself,
194 # accurately. Guessing toward the side the database can still correct is the
195 # same call made for the other unknowns on this path, and it
196 # warns for the same reason: an assumption baked into emitted code is invisible
197 # once the file is written.
198 17 ▶sub _is_nullable {
Subroutine _is_nullable: called CC=5 SCAR=16.1
199 17 my ($self, $key, $col, $nullable, $is_pk) = @_;
200  
201 17 ▶ return 0 if $is_pk;
Branch: 1 branch
BranchTrueFalseTotal
if $is_pk 4 13 17
202  
203 13 ▶ if (!defined $nullable || $nullable == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $nullable or $nullable == 2) 2 11 13
Condition100%3/3
not defined $nullable or $nullable == 2
l!l&&r!l&&!r
1111
MC/DC100%2/2
not defined $nullable or $nullable == 2
not defined $nullable $nullable == 2
Truth table100%3/3
not(A) or B
A= not defined $nullableB= $nullable == 2
ABresult
1X1
011
000
204 2 warn "$key.$col: this DBD reports no usable NULLABLE for the column,"
205 . " assuming is_nullable => 1. Correct it in the generated class, or"
206 . " install the DBIO driver distribution for this database -- its"
207 . " introspector reads the native catalog instead of the generic DBI"
208 . " metadata.\n";
209 2 return 1;
210 }
211  
212 11 ▶ return $nullable == 1 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$nullable == 1 ? : 7 4 11
213 }
214  
215  
216 10 ▶sub table_pk_info {
Subroutine table_pk_info: called CC=6 SCAR=18.3
Pod documented
217 10 my ($self, $key) = @_;
218 10 my ($schema, $table) = $self->_split_key($key);
219  
220 10 ▶ my $sth = $self->dbh->primary_key_info($schema, undef, $table)
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $self->dbh->primary_key_info($schema, undef, $table) or return [] 10 0 10
221 or return [];
222 10 my @rows;
223 10 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 6 10 16
224 6 ▶ push @rows, $row if defined $row->{COLUMN_NAME};
Branch: 1 branch
BranchTrueFalseTotal
if defined $row->{"COLUMN_NAME"} 6 0 6
225 }
226 10 $sth->finish;
227  
228 # Composite keys: order by KEY_SEQ
229 6 my @pk = map { $_->{COLUMN_NAME} }
230 10 ▶ sort { ($a->{KEY_SEQ} // 0) <=> ($b->{KEY_SEQ} // 0) } @rows;
Condition50%2/4
$DBIO::Introspect::DBI::a->{"KEY_SEQ"} // 0
l!l
20
$DBIO::Introspect::DBI::b->{"KEY_SEQ"} // 0
l!l
20
MC/DC0%0/1
$DBIO::Introspect::DBI::a->{"KEY_SEQ"} // 0
$DBIO::Introspect::DBI::a->{"KEY_SEQ"}
MC/DC0%0/1
$DBIO::Introspect::DBI::b->{"KEY_SEQ"} // 0
$DBIO::Introspect::DBI::b->{"KEY_SEQ"}
Truth table50%1/2
A
A= $DBIO::Introspect::DBI::a->{"KEY_SEQ"}
Aresult
11
00
Truth table50%1/2
A
A= $DBIO::Introspect::DBI::b->{"KEY_SEQ"}
Aresult
11
00
231  
232 10 return \@pk;
233 }
234  
235  
236 1 ▶sub table_uniq_info {
Subroutine table_uniq_info: called CC=9 SCAR=22.1
Pod documented
237 1 my ($self, $key) = @_;
238 1 my ($schema, $table) = $self->_split_key($key);
239  
240 # Not all DBD drivers implement statistics_info
241 1 ▶ my $sth = eval { $self->dbh->statistics_info(undef, $schema, $table, 1, 1) }
Branch: 1 branch
BranchTrueFalseTotal
my $sth = eval { do { $self->dbh->statistics_info(undef, $schema, $table, 1, 1) } } or return [] 1 0 1
242 or return [];
243  
244 # Group index rows: each row is one column of one index
245 1 my (%idx_cols, @idx_order);
246 1 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 4 1 5
247 4 ▶ my $idx_name = $row->{INDEX_NAME} // next;
Condition50%1/2
$row->{"INDEX_NAME"} // (next)
l!l
40
MC/DC0%0/1
$row->{"INDEX_NAME"} // (next)
$row->{"INDEX_NAME"}
Truth table50%1/2
A
A= $row->{"INDEX_NAME"}
Aresult
11
00
248 4 ▶ my $col = $row->{COLUMN_NAME} // next;
Condition50%1/2
$row->{"COLUMN_NAME"} // (next)
l!l
40
MC/DC0%0/1
$row->{"COLUMN_NAME"} // (next)
$row->{"COLUMN_NAME"}
Truth table50%1/2
A
A= $row->{"COLUMN_NAME"}
Aresult
11
00
249 4 ▶ next if $row->{NON_UNIQUE};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"NON_UNIQUE"} 1 3 4
250  
251 3 ▶ push @idx_order, $idx_name unless $idx_cols{$idx_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $idx_cols{$idx_name} 2 1 3
252 3 ▶ push @{ $idx_cols{$idx_name} }, [ $row->{ORDINAL_POSITION} // 0, $col ];
Condition50%1/2
$row->{"ORDINAL_POSITION"} // 0
l!l
30
MC/DC0%0/1
$row->{"ORDINAL_POSITION"} // 0
$row->{"ORDINAL_POSITION"}
Truth table50%1/2
A
A= $row->{"ORDINAL_POSITION"}
Aresult
11
00
253 }
254 1 $sth->finish;
255  
256 1 my @uniq;
257 1 for my $idx_name (@idx_order) {
258 2 my @cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @{ $idx_cols{$idx_name} };
259 2 push @uniq, [ $idx_name, \@cols ];
260 }
261  
262 1 return \@uniq;
263 }
264  
265  
266 2 ▶sub table_fk_info {
Subroutine table_fk_info: called CC=15 SCAR=28.1
Pod documented
267 2 my ($self, $key) = @_;
268 2 my ($schema, $table) = $self->_split_key($key);
269  
270 # Drivers without FK support return undef
271 2 ▶ my $sth = $self->dbh->foreign_key_info(undef, undef, undef, undef, $schema, $table)
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $self->dbh->foreign_key_info(undef, undef, undef, undef, $schema, $table) or return [] 2 0 2
272 or return [];
273  
274 # One row per FK column; group composite FKs by constraint name.
275 # DBI allows two naming styles: SQL/CLI (UK_/FK_) and ODBC (PK/FK).
276 2 my (%fk, @fk_order);
277 2 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 4 2 6
278 4 ▶ my $local_col = $row->{FK_COLUMN_NAME} // $row->{FKCOLUMN_NAME} // next;
Condition60%3/5
($row->{"FK_COLUMN_NAME"} // $row->{"FKCOLUMN_NAME"}) // (next)
l!l
40
$row->{"FK_COLUMN_NAME"} // $row->{"FKCOLUMN_NAME"}
l!l&&r!l&&!r
310
MC/DC0%0/2
($row->{"FK_COLUMN_NAME"} // $row->{"FKCOLUMN_NAME"}) // (next)
$row->{"FK_COLUMN_NAME"} $row->{"FKCOLUMN_NAME"}
Truth table66%2/3
A // B
A= $row->{"FK_COLUMN_NAME"}B= $row->{"FKCOLUMN_NAME"}
ABresult
1X1
011
000
279 4 ▶ my $remote_tab = $row->{UK_TABLE_NAME} // $row->{PKTABLE_NAME} // next;
Condition60%3/5
($row->{"UK_TABLE_NAME"} // $row->{"PKTABLE_NAME"}) // (next)
l!l
40
$row->{"UK_TABLE_NAME"} // $row->{"PKTABLE_NAME"}
l!l&&r!l&&!r
310
MC/DC0%0/2
($row->{"UK_TABLE_NAME"} // $row->{"PKTABLE_NAME"}) // (next)
$row->{"UK_TABLE_NAME"} $row->{"PKTABLE_NAME"}
Truth table66%2/3
A // B
A= $row->{"UK_TABLE_NAME"}B= $row->{"PKTABLE_NAME"}
ABresult
1X1
011
000
280 4 ▶ my $fk_name = $row->{FK_NAME} // $remote_tab;
Condition33%1/3
$row->{"FK_NAME"} // $remote_tab
l!l&&r!l&&!r
400
MC/DC0%0/2
$row->{"FK_NAME"} // $remote_tab
$row->{"FK_NAME"} $remote_tab
Truth table33%1/3
A // B
A= $row->{"FK_NAME"}B= $remote_tab
ABresult
1X1
011
000
281 4 ▶ my $seq = $row->{ORDINAL_POSITION} // $row->{KEY_SEQ} // 0;
Condition60%3/5
($row->{"ORDINAL_POSITION"} // $row->{"KEY_SEQ"}) // 0
l!l
40
$row->{"ORDINAL_POSITION"} // $row->{"KEY_SEQ"}
l!l&&r!l&&!r
310
MC/DC0%0/2
($row->{"ORDINAL_POSITION"} // $row->{"KEY_SEQ"}) // 0
$row->{"ORDINAL_POSITION"} $row->{"KEY_SEQ"}
Truth table66%2/3
A // B
A= $row->{"ORDINAL_POSITION"}B= $row->{"KEY_SEQ"}
ABresult
1X1
011
000
282  
283 4 ▶ push @fk_order, $fk_name unless $fk{$fk_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $fk{$fk_name} 3 1 4
284 my $fk = $fk{$fk_name} //= {
285 remote_table => $remote_tab,
286 remote_schema => $row->{UK_TABLE_SCHEM} // $row->{PKTABLE_SCHEM},
287 4 ▶ cols => [],
Condition80%4/5
$fk{$fk_name} //= {"remote_table", $remote_tab, "remote_schema", $row->{"UK_TABLE_SCHEM"} // $row->{"PKTABLE_SCHEM"}, "cols", []}
l!l
13
$row->{"UK_TABLE_SCHEM"} // $row->{"PKTABLE_SCHEM"}
l!l&&r!l&&!r
201
MC/DC100%1/1
$fk{$fk_name} //= {"remote_table", $remote_tab, "remote_schema", $row->{"UK_TABLE_SCHEM"} // $row->{"PKTABLE_SCHEM"}, "cols", []}
$fk{$fk_name}
MC/DC50%1/2
$row->{"UK_TABLE_SCHEM"} // $row->{"PKTABLE_SCHEM"}
$row->{"UK_TABLE_SCHEM"} $row->{"PKTABLE_SCHEM"}
Truth table100%2/2
A
A= $fk{$fk_name}
Aresult
11
00
Truth table66%2/3
A // B
A= $row->{"UK_TABLE_SCHEM"}B= $row->{"PKTABLE_SCHEM"}
ABresult
1X1
011
000
288 };
289 4 ▶ push @{ $fk->{cols} },
Condition66%2/3
$row->{"UK_COLUMN_NAME"} // $row->{"PKCOLUMN_NAME"}
l!l&&r!l&&!r
310
MC/DC0%0/2
$row->{"UK_COLUMN_NAME"} // $row->{"PKCOLUMN_NAME"}
$row->{"UK_COLUMN_NAME"} $row->{"PKCOLUMN_NAME"}
Truth table66%2/3
A // B
A= $row->{"UK_COLUMN_NAME"}B= $row->{"PKCOLUMN_NAME"}
ABresult
1X1
011
000
290 4 [ $seq, $local_col, $row->{UK_COLUMN_NAME} // $row->{PKCOLUMN_NAME} ];
291 }
292 2 $sth->finish;
293  
294 2 my @fks;
295 2 for my $fk_name (@fk_order) {
296 3 my $fk = $fk{$fk_name};
297 3 my @cols = sort { $a->[0] <=> $b->[0] } @{ $fk->{cols} };
298 push @fks, {
299 4 local_columns => [ map { $_->[1] } @cols ],
300 remote_table => $fk->{remote_table},
301 remote_schema => $fk->{remote_schema},
302 3 remote_columns => [ grep { defined } map { $_->[2] } @cols ],
303 attrs => {},
304 };
305 }
306  
307 2 return \@fks;
308 }
309  
310  
311 3 ▶sub table_is_view {
Subroutine table_is_view: called CC=2 SCAR=6.9
Pod documented
312 3 my ($self, $key) = @_;
313 3 my ($schema, $table) = $self->_split_key($key);
314  
315 # comma-separated string, not an arrayref -- see table_keys
316 3 my $sth = $self->dbh->table_info($schema, undef, $table, 'VIEW');
317 3 my $row = $sth->fetchrow_hashref;
318 3 $sth->finish;
319  
320 3 ▶ return $row ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$row ? : 2 1 3
321 }
322  
323  
324 0 ▶sub view_definition {
Subroutine view_definition: not called CC=3 SCAR=24.8
Pod documented
325 0 my ($self, $key) = @_;
326 0 my ($schema, $table) = $self->_split_key($key);
327  
328 # Most DBI drivers don't support this, but SQLite does
329 0 ▶ if ($self->dbms_name eq 'SQLite') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dbms_name eq "SQLite") 0 0 0
330 0 my $sth = $self->dbh->prepare("SELECT sql FROM sqlite_master WHERE type='view' AND name=?");
331 0 $sth->execute($table);
332 0 my $row = $sth->fetch;
333 0 $sth->finish;
334 0 ▶ return $row ? $row->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$row ? : 0 0 0
335 }
336  
337 0 return undef;
338 }
339  
340 25 ▶sub _split_key {
Subroutine _split_key: called CC=2 SCAR=6.9
341 25 my ($self, $key) = @_;
342 25 ▶ if ($key =~ /^(.+)\.(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(.+)\.(.+)$/) 7 18 25
343 7 return ($1, $2);
344 }
345 18 return (undef, $key);
346 }
347  
348 10 1;
349  
350 __END__
« blib/lib/DBIO/Row.pm ↑ Summary blib/lib/DBIO/SQLMaker.pm »