| 1 | package DBIO::DuckDB::Introspect::Columns; | ||||||||||||||||||||
| 2 | # ABSTRACT: Introspect DuckDB columns | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 0 | ▶ | sub fetch { | ||||||||||||||||||
|
Subroutine
fetch: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||
| 10 | 0 | my ($class, $dbh, $schema, $tables, $catalog) = @_; | |||||||||||||||||||
| 11 | 0 | ▶ | $schema //= 'main'; | ||||||||||||||||||
|
Condition0%0/2
$schema //= "main"
MC/DC0%0/1
$schema //= "main"
$schema
Truth table0%0/2
A
A= $schema
| |||||||||||||||||||||
| 12 | 0 | my %columns; | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | # Pull all columns for the schema in one go. | ||||||||||||||||||||
| 15 | 0 | my $col_sql = q{ | |||||||||||||||||||
| 16 | SELECT table_name, column_name, data_type, | ||||||||||||||||||||
| 17 | is_nullable, column_default, ordinal_position | ||||||||||||||||||||
| 18 | FROM information_schema.columns | ||||||||||||||||||||
| 19 | WHERE table_schema = ? | ||||||||||||||||||||
| 20 | }; | ||||||||||||||||||||
| 21 | 0 | my @col_bind = ($schema); | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 0 | ▶ | if (defined $catalog) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 24 | 0 | $col_sql .= q{ AND table_catalog = ?}; | |||||||||||||||||||
| 25 | 0 | push @col_bind, $catalog; | |||||||||||||||||||
| 26 | } | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 0 | $col_sql .= q{ ORDER BY table_name, ordinal_position}; | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 0 | my $col_sth = $dbh->prepare($col_sql); | |||||||||||||||||||
| 31 | 0 | $col_sth->execute(@col_bind); | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 0 | ▶ | while (my $row = $col_sth->fetchrow_hashref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | 0 | ▶ | next unless exists $tables->{ $row->{table_name} }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | 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"}
| |||||||||||||||||||||
| 36 | column_name => $row->{column_name}, | ||||||||||||||||||||
| 37 | data_type => $row->{data_type}, | ||||||||||||||||||||
| 38 | not_null => (lc($row->{is_nullable} // 'YES') eq 'no') ? 1 : 0, | ||||||||||||||||||||
| 39 | default_value => $row->{column_default}, | ||||||||||||||||||||
| 40 | 0 | is_pk => 0, | |||||||||||||||||||
| 41 | pk_position => 0, | ||||||||||||||||||||
| 42 | }; | ||||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | # Primary-key membership. | ||||||||||||||||||||
| 46 | 0 | my $pk_sql = q{ | |||||||||||||||||||
| 47 | SELECT kcu.table_name, kcu.column_name, kcu.ordinal_position | ||||||||||||||||||||
| 48 | FROM information_schema.table_constraints tc | ||||||||||||||||||||
| 49 | JOIN information_schema.key_column_usage kcu | ||||||||||||||||||||
| 50 | ON tc.constraint_name = kcu.constraint_name | ||||||||||||||||||||
| 51 | AND tc.table_schema = kcu.table_schema | ||||||||||||||||||||
| 52 | AND tc.table_name = kcu.table_name | ||||||||||||||||||||
| 53 | WHERE tc.constraint_type = 'PRIMARY KEY' | ||||||||||||||||||||
| 54 | AND tc.table_schema = ? | ||||||||||||||||||||
| 55 | }; | ||||||||||||||||||||
| 56 | 0 | my @pk_bind = ($schema); | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 0 | ▶ | if (defined $catalog) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 0 | $pk_sql .= q{ AND kcu.table_catalog = ?}; | |||||||||||||||||||
| 60 | 0 | push @pk_bind, $catalog; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | my $pk_sth = $dbh->prepare($pk_sql); | |||||||||||||||||||
| 64 | 0 | $pk_sth->execute(@pk_bind); | |||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 0 | ▶ | while (my $row = $pk_sth->fetchrow_hashref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 0 | ▶ | my $list = $columns{ $row->{table_name} } or next; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | 0 | for my $col (@$list) { | |||||||||||||||||||
| 69 | 0 | ▶ | if ($col->{column_name} eq $row->{column_name}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 0 | $col->{is_pk} = 1; | |||||||||||||||||||
| 71 | 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'}
| |||||||||||||||||||||
| 72 | 0 | last; | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 0 | return \%columns; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 5 | 1; | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | __END__ | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =pod | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =encoding UTF-8 | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =head1 NAME | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | DBIO::DuckDB::Introspect::Columns - Introspect DuckDB columns | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | =head1 VERSION | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | version 0.900001 | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =head1 DESCRIPTION | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | Fetches column metadata via C<information_schema.columns>. Primary-key | ||||||||||||||||||||
| 99 | information is joined in via C<information_schema.key_column_usage> + | ||||||||||||||||||||
| 100 | C<table_constraints>. | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | =head1 METHODS | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | =head2 fetch | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | my $columns = DBIO::DuckDB::Introspect::Columns->fetch($dbh, $schema, $tables); | ||||||||||||||||||||
| 107 | my $columns = DBIO::DuckDB::Introspect::Columns->fetch($dbh, $schema, $tables, $catalog); | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | Given the tables hashref from L<DBIO::DuckDB::Introspect::Tables>, | ||||||||||||||||||||
| 110 | returns a hashref keyed by table name. Each value is an arrayref of | ||||||||||||||||||||
| 111 | column hashrefs in C<ordinal_position> order with keys: | ||||||||||||||||||||
| 112 | C<column_name>, C<data_type>, C<not_null>, C<default_value>, | ||||||||||||||||||||
| 113 | C<is_pk>, C<pk_position>. | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | When C<$catalog> is defined, C<AND table_catalog = ?> clauses are added | ||||||||||||||||||||
| 116 | to both the columns and primary-key queries. | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =head1 AUTHOR | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | DBIO Authors | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 127 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | =cut | ||||||||||||||||||||