« blib/lib/DBIO/DuckDB/Test/Conformance.pm
↑ Summary
blib/lib/DBIO/DuckDB/Introspect/ForeignKeys.pm »
| 1 | package DBIO::DuckDB::Introspect::Columns; | ||||||||||||||||||||
| 2 | # ABSTRACT: Introspect DuckDB columns | ||||||||||||||||||||
| 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 | |||||||||||||||||||||
| 11 | 0 | ▶ | sub fetch { | ||||||||||||||||||
|
Subroutine
fetch: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||
| 12 | 0 | my ($class, $dbh, $schema, $tables, $catalog) = @_; | |||||||||||||||||||
| 13 | 0 | ▶ | $schema //= 'main'; | ||||||||||||||||||
|
Condition0%0/2
$schema //= "main"
MC/DC0%0/1
$schema //= "main"
$schema
Truth table0%0/2
A
A= $schema
| |||||||||||||||||||||
| 14 | 0 | my %columns; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | # Pull all columns for the schema in one go. | ||||||||||||||||||||
| 17 | 0 | my $col_sql = q{ | |||||||||||||||||||
| 18 | SELECT table_name, column_name, data_type, | ||||||||||||||||||||
| 19 | is_nullable, column_default, ordinal_position | ||||||||||||||||||||
| 20 | FROM information_schema.columns | ||||||||||||||||||||
| 21 | WHERE table_schema = ? | ||||||||||||||||||||
| 22 | }; | ||||||||||||||||||||
| 23 | 0 | my @col_bind = ($schema); | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 0 | ▶ | if (defined $catalog) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | 0 | $col_sql .= q{ AND table_catalog = ?}; | |||||||||||||||||||
| 27 | 0 | push @col_bind, $catalog; | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 0 | $col_sql .= q{ ORDER BY table_name, ordinal_position}; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 0 | my $col_sth = $dbh->prepare($col_sql); | |||||||||||||||||||
| 33 | 0 | $col_sth->execute(@col_bind); | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 0 | ▶ | while (my $row = $col_sth->fetchrow_hashref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 0 | ▶ | next unless exists $tables->{ $row->{table_name} }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 0 | ▶ | push @{ $columns{ $row->{table_name} } }, { | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$row->{"is_nullable"} // "YES"
MC/DC0%0/1
$row->{"is_nullable"} // "YES"
$row->{"is_nullable"}
Truth table0%0/2
A
A= $row->{"is_nullable"}
| |||||||||||||||||||||
| 38 | column_name => $row->{column_name}, | ||||||||||||||||||||
| 39 | data_type => $row->{data_type}, | ||||||||||||||||||||
| 40 | not_null => (lc($row->{is_nullable} // 'YES') eq 'no') ? 1 : 0, | ||||||||||||||||||||
| 41 | default_value => $row->{column_default}, | ||||||||||||||||||||
| 42 | 0 | is_pk => 0, | |||||||||||||||||||
| 43 | pk_position => 0, | ||||||||||||||||||||
| 44 | }; | ||||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | # Primary-key membership. | ||||||||||||||||||||
| 48 | 0 | my $pk_sql = q{ | |||||||||||||||||||
| 49 | SELECT kcu.table_name, kcu.column_name, kcu.ordinal_position | ||||||||||||||||||||
| 50 | FROM information_schema.table_constraints tc | ||||||||||||||||||||
| 51 | JOIN information_schema.key_column_usage kcu | ||||||||||||||||||||
| 52 | ON tc.constraint_name = kcu.constraint_name | ||||||||||||||||||||
| 53 | AND tc.table_schema = kcu.table_schema | ||||||||||||||||||||
| 54 | AND tc.table_name = kcu.table_name | ||||||||||||||||||||
| 55 | WHERE tc.constraint_type = 'PRIMARY KEY' | ||||||||||||||||||||
| 56 | AND tc.table_schema = ? | ||||||||||||||||||||
| 57 | }; | ||||||||||||||||||||
| 58 | 0 | my @pk_bind = ($schema); | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 0 | ▶ | if (defined $catalog) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | 0 | $pk_sql .= q{ AND kcu.table_catalog = ?}; | |||||||||||||||||||
| 62 | 0 | push @pk_bind, $catalog; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | my $pk_sth = $dbh->prepare($pk_sql); | |||||||||||||||||||
| 66 | 0 | $pk_sth->execute(@pk_bind); | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 0 | ▶ | while (my $row = $pk_sth->fetchrow_hashref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 0 | ▶ | my $list = $columns{ $row->{table_name} } or next; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 0 | for my $col (@$list) { | |||||||||||||||||||
| 71 | 0 | ▶ | if ($col->{column_name} eq $row->{column_name}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 0 | $col->{is_pk} = 1; | |||||||||||||||||||
| 73 | 0 | ▶ | $col->{pk_position} = $row->{ordinal_position} || 1; | ||||||||||||||||||
|
Condition0%0/2
$row->{'ordinal_position'} || 1
MC/DC0%0/1
$row->{'ordinal_position'} || 1
$row->{'ordinal_position'}
Truth table0%0/2
A
A= $row->{'ordinal_position'}
| |||||||||||||||||||||
| 74 | 0 | last; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 0 | return \%columns; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 10 | 1; | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | __END__ | ||||||||||||||||||||