DuckDB/Introspect.pm

filter: Statementstmt 74.2% 75 / 101 Branchbran 66.6% 24 / 36 Conditioncond 42.8% 6 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 73.3% 11 / 15 Podpod 100% 5 / 5 totaltotal 68.1% 122 / 179 CC 27 SCAR 20.6
CC
27
Cov
70%
view_definition
20.0
_normalize_default_value
12.4
table_uniq_info
7.1
CRAP 7.8
« DuckDB/Test.pm ↑ Summary DuckDB/Diff/Index.pm »
1 package DBIO::DuckDB::Introspect;
2 # ABSTRACT: Introspect a DuckDB database via information_schema + duckdb_* views
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 5 ▶use base 'DBIO::Introspect::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶use DBIO::DuckDB::Introspect::Tables;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use DBIO::DuckDB::Introspect::Columns;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use DBIO::DuckDB::Introspect::Indexes;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use DBIO::DuckDB::Introspect::ForeignKeys;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 0 ▶sub schema { $_[0]->{schema} // 'main' }
Subroutine schema: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[0]{"schema"} // "main"
l!l
00
MC/DC0%0/1
$_[0]{"schema"} // "main"
$_[0]{"schema"}
Truth table0%0/2
A
A= $_[0]{"schema"}
Aresult
11
00
16 0 ▶sub catalog { $_[0]->{catalog} }
Subroutine catalog: not called CC=1 SCAR=6.9
Pod documented
17  
18  
19  
20 0 ▶sub _build_model {
Subroutine _build_model: not called CC=1 SCAR=6.9
21 0 my ($self) = @_;
22 0 my $dbh = $self->dbh;
23 0 my $schema = $self->schema;
24 0 my $catalog = $self->catalog;
25  
26 0 my $tables = DBIO::DuckDB::Introspect::Tables->fetch($dbh, $schema, $catalog);
27 0 my $columns = DBIO::DuckDB::Introspect::Columns->fetch($dbh, $schema, $tables, $catalog);
28 0 my $indexes = DBIO::DuckDB::Introspect::Indexes->fetch($dbh, $schema, $tables, $catalog);
29 0 my $fks = DBIO::DuckDB::Introspect::ForeignKeys->fetch($dbh, $schema, $tables, $catalog);
30  
31 return {
32 0 tables => $tables,
33 columns => $columns,
34 indexes => $indexes,
35 foreign_keys => $fks,
36 };
37 }
38  
39  
40 2 ▶sub table_columns_info {
Subroutine table_columns_info: called CC=4 SCAR=13.9
Pod documented
41 2 my ($self, $key) = @_;
42  
43 2 my %pk = map { $_ => 1 } @{ $self->table_pk_info($key) };
44 2 my %info;
45  
46 2 ▶ for my $col (@{ $self->model->{columns}{$key} || [] }) {
Condition50%1/2
$self->model->{'columns'}{$key} || []
l!l
20
MC/DC0%0/1
$self->model->{'columns'}{$key} || []
$self->model->{'columns'}{$key}
Truth table50%1/2
A
A= $self->model->{'columns'}{$key}
Aresult
11
00
47 8 my $name = $col->{column_name};
48 8 ▶ my $i = { is_nullable => $col->{not_null} ? 0 : 1 };
Branch: 1 branch
BranchTrueFalseTotal
$col->{'not_null'} ? : 5 3 8
49 8 $self->_normalize_data_type($i, $col->{data_type});
50 8 $self->_normalize_default_value($i, $col->{default_value}, $pk{$name});
51 8 $info{$name} = $i;
52 }
53  
54 2 return \%info;
55 }
56  
57 8 ▶sub _normalize_data_type {
Subroutine _normalize_data_type: called CC=4 SCAR=13.9
58 8 my ($self, $info, $raw) = @_;
59 8 ▶ my $type = defined $raw ? $raw : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $raw ? : 8 0 8
60 8 $type =~ s/^\s+//;
61 8 $type =~ s/\s+\z//;
62  
63 # DECIMAL(p,s) / NUMERIC(p,s)
64 8 ▶ if ($type =~ /^(decimal|numeric)\s*\(\s*(\d+)\s*,\s*(\d+)\s*\)\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^(decimal|numeric)\s*\(\s*(\d+)\s*,\s*(\d+)\s*\)\z/i) 1 7 8
65 1 $info->{data_type} = lc $1;
66 1 $info->{size} = [ 0 + $2, 0 + $3 ];
67 1 return;
68 }
69  
70 # VARCHAR(n) / CHAR(n) / DECIMAL(p) etc.
71 7 ▶ if ($type =~ /^([A-Za-z][\w ]*?)\s*\(\s*(\d+)\s*\)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^([A-Za-z][\w ]*?)\s*\(\s*(\d+)\s*\)\z/) 1 6 7
72 1 $info->{data_type} = lc $1;
73 1 $info->{size} = 0 + $2;
74 1 return;
75 }
76  
77 6 $info->{data_type} = lc $type;
78 }
79  
80 8 ▶sub _normalize_default_value {
Subroutine _normalize_default_value: called CC=11 SCAR=25.2
81 8 my ($self, $info, $default, $is_pk) = @_;
82 8 ▶ return unless defined $default;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default 4 4 8
83  
84 4 my $value = $default;
85 4 $value =~ s/^\s+//;
86 4 $value =~ s/\s+\z//;
87  
88 # Auto-increment: DEFAULT nextval('seq')
89 4 ▶ if ($value =~ /\bnextval\(\s*'?"?([^'")]+)"?'?\s*\)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\bnextval\(\s*'?"?([^'")]+)"?'?\s*\)/i) 1 3 4
90 1 $info->{is_auto_increment} = 1;
91 1 $info->{sequence} = $1;
92 1 ▶ $info->{retrieve_on_insert} = 1 if $is_pk;
Branch: 1 branch
BranchTrueFalseTotal
if $is_pk 1 0 1
93 1 return;
94 }
95  
96 # String literal, optionally CAST(...)
97 3 ▶ if ($value =~ /^CAST\(\s*'(.*?)'\s+AS\s+[\w ]+\)\z/is
Branch: 3 branches
BranchTrueFalseTotal
if ($value =~ /^CAST\(\s*'(.*?)'\s+AS\s+[\w ]+\)\z/is or $value =~ /^'(.*?)'(?:::[\w ]+)?\z/s) { } 1 2 3
elsif ($value =~ /^-?\d+(?:\.\d+)?\z/) { } 1 1 2
elsif ($value =~ /^NULL\z/i) { } 0 1 1
Condition66%2/3
$value =~ /^CAST\(\s*'(.*?)'\s+AS\s+[\w ]+\)\z/is or $value =~ /^'(.*?)'(?:::[\w ]+)?\z/s
l!l&&r!l&&!r
012
MC/DC50%1/2
$value =~ /^CAST\(\s*'(.*?)'\s+AS\s+[\w ]+\)\z/is or $value =~ /^'(.*?)'(?:::[\w ]+)?\z/s
$value =~ /^CAST\(\s*'(.*?)'\s+AS\s+[\w ]+\)\z/is $value =~ /^'(.*?)'(?:::[\w ]+)?\z/s
Truth table66%2/3
A or B
A= $value =~ /^CAST\(\s*'(.*?)'\s+AS\s+[\w ]+\)\z/isB= $value =~ /^'(.*?)'(?:::[\w ]+)?\z/s
ABresult
1X1
011
000
98 || $value =~ /^'(.*?)'(?:::[\w ]+)?\z/s) {
99 1 $info->{default_value} = $1;
100 }
101 # Bare number
102 elsif ($value =~ /^-?\d+(?:\.\d+)?\z/) {
103 1 $info->{default_value} = $value;
104 }
105 # NULL
106 elsif ($value =~ /^NULL\z/i) {
107 0 my $null = 'null';
108 0 $info->{default_value} = \$null;
109 }
110 # Function / keyword literal (CURRENT_TIMESTAMP, now(), ...)
111 else {
112 1 ▶ my $literal = lc($value) eq 'now()' ? 'current_timestamp' : $value;
Branch: 1 branch
BranchTrueFalseTotal
lc $value eq 'now()' ? : 0 1 1
113 1 $info->{default_value} = \$literal;
114 }
115  
116 3 ▶ $info->{retrieve_on_insert} = 1 if $is_pk && !$info->{is_auto_increment};
Branch: 1 branch
BranchTrueFalseTotal
if $is_pk and not $info->{"is_auto_increment"} 0 3 3
Condition33%1/3
$is_pk and not $info->{"is_auto_increment"}
!ll&&!rl&&r
300
MC/DC0%0/2
$is_pk and not $info->{"is_auto_increment"}
$is_pk not $info->{"is_auto_increment"}
Truth table33%1/3
A and not(B)
A= $is_pkB= not $info->{"is_auto_increment"}
ABresult
0X0
100
111
117 }
118  
119  
120 1 ▶sub table_uniq_info {
Subroutine table_uniq_info: called CC=7 SCAR=19.7
Pod documented
121 1 my ($self, $key) = @_;
122 1 ▶ my $indexes = $self->model->{indexes}{$key} || {};
Condition50%1/2
$self->model->{'indexes'}{$key} || {}
l!l
10
MC/DC0%0/1
$self->model->{'indexes'}{$key} || {}
$self->model->{'indexes'}{$key}
Truth table50%1/2
A
A= $self->model->{'indexes'}{$key}
Aresult
11
00
123 1 my @uniq;
124 1 for my $name (sort keys %$indexes) {
125 3 my $idx = $indexes->{$name};
126 3 ▶ next unless $idx->{is_unique};
Branch: 1 branch
BranchTrueFalseTotal
unless $idx->{"is_unique"} 1 2 3
127 2 ▶ next if $idx->{partial};
Branch: 1 branch
BranchTrueFalseTotal
if $idx->{"partial"} 1 1 2
128 1 ▶ next unless @{ $idx->{columns} || [] };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$idx->{"columns"} or [];} 0 1 1
Condition50%1/2
$idx->{'columns'} || []
l!l
10
MC/DC0%0/1
$idx->{'columns'} || []
$idx->{'columns'}
Truth table50%1/2
A
A= $idx->{'columns'}
Aresult
11
00
129 1 push @uniq, [ $name => $idx->{columns} ];
130 }
131 1 return \@uniq;
132 }
133  
134  
135 0 ▶sub view_definition {
Subroutine view_definition: not called CC=4 SCAR=30.0
Pod documented
136 0 my ($self, $key) = @_;
137 0 ▶ return undef unless $self->table_is_view($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->table_is_view($key) 0 0 0
138  
139 0 my $sql = q{
140 SELECT view_definition
141 FROM information_schema.views
142 WHERE table_schema = ? AND table_name = ?
143 };
144 0 my @bind = ($self->schema, $key);
145 0 ▶ if (defined $self->catalog) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->catalog) 0 0 0
146 0 $sql .= q{ AND table_catalog = ?};
147 0 push @bind, $self->catalog;
148 }
149  
150 0 my ($def) = $self->dbh->selectrow_array($sql, undef, @bind);
151 0 ▶ return undef unless defined $def;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def 0 0 0
152 0 $def =~ s/^\s+//;
153 0 $def =~ s/\s+\z//;
154 0 $def =~ s/\s*;\s*\z//;
155 0 return $def;
156 }
157  
158 5 1;
159  
160 __END__
161  
162 =pod
163  
164 =encoding UTF-8
165  
166 =head1 NAME
167  
168 DBIO::DuckDB::Introspect - Introspect a DuckDB database via information_schema + duckdb_* views
169  
170 =head1 VERSION
171  
172 version 0.900001
173  
174 =head1 DESCRIPTION
175  
176 C<DBIO::DuckDB::Introspect> reads the live state of a DuckDB database
177 via the standard SQL C<information_schema> views and DuckDB's system
178 views (C<duckdb_tables()>, C<duckdb_columns()>, C<duckdb_indexes()>,
179 C<duckdb_constraints()>). It is the source side of the
180 test-deploy-and-compare strategy used by L<DBIO::DuckDB::Deploy>.
181  
182 my $intro = DBIO::DuckDB::Introspect->new(dbh => $dbh);
183 my $model = $intro->model;
184  
185 With a locally-attached file or DuckLake catalog pass C<catalog>:
186  
187 my $intro = DBIO::DuckDB::Introspect->new(dbh => $dbh, catalog => 'mycat');
188 my $model = $intro->model;
189  
190 Model shape mirrors L<DBIO::SQLite::Introspect>:
191  
192 {
193 tables => { $name => { ... } },
194 columns => { $table => [ { ... }, ... ] },
195 indexes => { $table => { $name => { ... } } },
196 foreign_keys => { $table => [ { ... }, ... ] },
197 }
198  
199 B<Quack RPC catalogs are opaque:> C<information_schema> and
200 C<duckdb_indexes()> return no rows for tables in a remote Quack-attached
201 catalog. Do not pass C<catalog> for a Quack remote. To inspect a remote
202 table's columns from the client side, use C<PRAGMA table_info('remote.tablename')>
203 directly on a known table.
204  
205 Only the default C<main> schema is introspected unless C<schema> is overridden.
206  
207 =head1 ATTRIBUTES
208  
209 =head2 schema
210  
211 Schema name to introspect. Defaults to C<main>.
212  
213 =head2 catalog
214  
215 Optional catalog name. When set, restricts introspection to the named
216 catalog (e.g. a locally-attached file DB or DuckLake volume). Adds
217 C<AND table_catalog = ?> / C<AND database_name = ?> filters to all
218 queries. When undef (the default) the existing schema-only queries run
219 unchanged.
220  
221 B<Do not use with Quack RPC catalogs> -- remote quack catalogs are
222 opaque to C<information_schema> and DuckDB system views.
223  
224 =head1 METHODS
225  
226 =head2 table_columns_info
227  
228 Hashref C<{ col_name => { data_type, size, is_nullable, default_value,
229 is_auto_increment, sequence, ... } }>.
230  
231 =head2 table_uniq_info
232  
233 List of C<[ $index_name, \@col_names ]> pairs for non-partial unique
234 indexes. PK-backed indexes are already excluded from the model.
235  
236 =head2 view_definition
237  
238 SQL text of the view definition, or C<undef>. Fetched lazily from
239 C<information_schema.views> on demand.
240  
241 =head1 NORMALIZED CONTRACT
242  
243 These methods implement the generation contract defined in
244 L<DBIO::Introspect::Base>. The canonical-model defaults in
245 L<DBIO::Introspect::Base> (C<table_keys>, C<table_columns>,
246 C<table_pk_info>, C<table_fk_info>, C<table_is_view>) are inherited
247 unchanged -- the DuckDB native model follows the canonical shape. Only
248 C<table_columns_info> (DuckDB-specific C<nextval()> auto-increment and
249 type-size normalization), C<table_uniq_info> (partial-unique exclusion),
250 and C<view_definition> (lazy C<information_schema.views> lookup) are
251 overridden below. Table keys are plain table names (only the C<main>
252 schema is introspected).
253  
254 =head1 AUTHOR
255  
256 DBIO Authors
257  
258 =head1 COPYRIGHT AND LICENSE
259  
260 Copyright (C) 2026 DBIO Authors
261  
262 This is free software; you can redistribute it and/or modify it under
263 the same terms as the Perl 5 programming language system itself.
264  
265 =cut
« DuckDB/Test.pm ↑ Summary DuckDB/Diff/Index.pm »