| 1 | package DBIO::DuckDB::DDL; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Generate DuckDB DDL from DBIO Result classes | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 11 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use DBIO::DuckDB::DDL::Emit qw( | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | quote_ident quote_table split_table column_def pk_clause unique_clause create_table create_index create_sequence fk_clause | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 11 | ); | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | sub install_ddl { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
install_ddl: called CC=41 SCAR=54.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | my ($class, $schema) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | my @stmts; | |||||||||||||||||||||||||||||||||||||||||
| 19 | my %seen_table; | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | my %seen_schema; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | my @view_stmts; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | my %seen_view; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 3 | for my $source_name (_topo_sort_sources($schema)) { | |||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | my $source = $schema->source($source_name); | |||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | my $result_class = $source->result_class; | |||||||||||||||||||||||||||||||||||||||||
| 27 | # Views: emit CREATE VIEW after all tables; skip virtual views. | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 3 | ▶ | if ($source->isa('DBIO::ResultSource::View')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | next if $source->can('is_virtual') && $source->is_virtual; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$source->can("is_virtual") and $source->is_virtual
MC/DC0%0/2
$source->can("is_virtual") and $source->is_virtual
$source->can("is_virtual")
$source->is_virtual
Truth table0%0/3
A and B
A= $source->can("is_virtual")B= $source->is_virtual
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | my $vname = _resolve_table_name($source->name); | |||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | next if !defined $vname || $seen_view{$vname}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $vname or $seen_view{$vname}++
MC/DC0%0/2
not defined $vname or $seen_view{$vname}++
not defined $vname
$seen_view{$vname}++
Truth table0%0/3
not(A) or B
A= not defined $vnameB= $seen_view{$vname}++
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | my $def = $source->can('view_definition') ? $source->view_definition : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | next unless defined $def && length $def; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $def and length $def
MC/DC0%0/2
defined $def and length $def
defined $def
length $def
Truth table0%0/3
A and B
A= defined $defB= length $def
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | push @view_stmts, sprintf 'CREATE VIEW %s AS %s;', quote_ident($vname), $def; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | my $table_name = _resolve_table_name($source->name); | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | # Virtual / view source whose name is inline SQL: skip here. A | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # CREATE VIEW path can come later. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | next unless defined $table_name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | # Multiple Result classes may share one physical table (e.g. Artist / | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # ArtistSubclass / SourceNameArtists all map to the "artist" table). | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # Emit CREATE TABLE only once per distinct table name. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | ▶ | next if $seen_table{$table_name}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 3 | my @col_defs; | |||||||||||||||||||||||||||||||||||||||||
| 50 | my %is_pk; | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | my @pk_cols = $source->primary_columns; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 3 | @is_pk{@pk_cols} = (1) x @pk_cols; | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | for my $col_name ($source->columns) { | |||||||||||||||||||||||||||||||||||||||||
| 55 | 10 | my $info = $source->column_info($col_name); | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # Resolve the DEFAULT fragment. DuckDB prefers sequences, so a | ||||||||||||||||||||||||||||||||||||||||||
| 58 | # simple auto-increment becomes a generated-by-sequence default; | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # a dedicated sequence per table keeps round-trip predictable. | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 10 | my $default; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 10 | ▶ | if ($info->{is_auto_increment}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if (_is_guid_type($info)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | # A UUID column cannot default from an integer sequence; DuckDB | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # generates GUIDs with uuid(). | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $default = 'uuid()'; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | my $seq = "${table_name}_${col_name}_seq"; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $default = sprintf q{nextval('%s')}, $seq; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | elsif (defined $info->{default_value}) { | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my $dv = $info->{default_value}; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if (ref $dv eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $default = $$dv; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # A string literal: a quote inside it is doubled. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | (my $lit = $dv) =~ s/'/''/g; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | $default = "'$lit'"; | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | push @col_defs, column_def( | ||||||||||||||||||||||||||||||||||||||||||
| 85 | name => $col_name, | ||||||||||||||||||||||||||||||||||||||||||
| 86 | type => _duckdb_column_type($info), | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # ADR 0043 (core): canonical_column turns a MISSING is_nullable | ||||||||||||||||||||||||||||||||||||||||||
| 88 | # into not_null=1, so a column without the key must deploy as | ||||||||||||||||||||||||||||||||||||||||||
| 89 | # NOT NULL too -- not just one that says is_nullable => 0. | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 10 | not_null => (!$info->{is_nullable}), | |||||||||||||||||||||||||||||||||||||||||
| 91 | default => $default, | ||||||||||||||||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 3 | ▶ | push @col_defs, pk_clause(@pk_cols) if @pk_cols; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | ▶ | if ($source->can('unique_constraints')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | my %uniques = $source->unique_constraints; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | for my $uname (sort keys %uniques) { | |||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | ▶ | next if $uname eq 'primary'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | push @col_defs, unique_clause(@{ $uniques{$uname} }); | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | # Foreign keys: DuckDB enforces them (docs/adr/0001, addendum 2026-10-25). | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 3 | push @col_defs, _foreign_key_clauses($schema, $source, $table_name, \%seen_table); | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | # table('other.t') is table t of schema other: the schema first, once | ||||||||||||||||||||||||||||||||||||||||||
| 109 | # (karr k49, maintainer decision 2026-10-25). | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 3 | my ($table_schema) = split_table($table_name); | |||||||||||||||||||||||||||||||||||||||||
| 111 | push @stmts, 'CREATE SCHEMA IF NOT EXISTS ' . quote_ident($table_schema) . ';' | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | ▶ | if defined $table_schema && !$seen_schema{$table_schema}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $table_schema and not $seen_schema{$table_schema}++
MC/DC0%0/2
defined $table_schema and not $seen_schema{$table_schema}++
defined $table_schema
not $seen_schema{$table_schema}++
Truth table33%1/3
A and not(B)
A= defined $table_schemaB= not $seen_schema{$table_schema}++
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | # Emit any required sequences BEFORE the table. | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 3 | for my $col_name ($source->columns) { | |||||||||||||||||||||||||||||||||||||||||
| 116 | 10 | my $info = $source->column_info($col_name); | |||||||||||||||||||||||||||||||||||||||||
| 117 | 10 | ▶ | next unless $info->{is_auto_increment}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | next if _is_guid_type($info); # GUID auto columns default via uuid(), no sequence | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my $seq = "${table_name}_${col_name}_seq"; | |||||||||||||||||||||||||||||||||||||||||
| 120 | # Start high enough that test fixtures / manual inserts with small | ||||||||||||||||||||||||||||||||||||||||||
| 121 | # explicit IDs don't collide with nextval(). DuckDB sequences do | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # not auto-advance on manual inserts the way PG IDENTITY columns | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # do, so we bias the range instead. | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | push @stmts, create_sequence(name => $seq, start => 1000000); | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 3 | push @stmts, create_table($table_name, @col_defs); | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | # Standalone indexes: the ones written by hand with duckdb_indexes, and | ||||||||||||||||||||||||||||||||||||||||||
| 130 | # the canonical declarations of the result source (Cake idx, indices; | ||||||||||||||||||||||||||||||||||||||||||
| 131 | # ADR 0061 in core, ADR 0008 here, karr k41). A canonical declaration of | ||||||||||||||||||||||||||||||||||||||||||
| 132 | # the same name wins over the hand-written one. | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 3 | my %indexes; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 3 | ▶ | if ($result_class->can('duckdb_indexes')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | my $hand = $result_class->duckdb_indexes; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | $indexes{$_} = $hand->{$_} for keys %$hand; | |||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # The own block takes `expression` only: DuckDB refuses a partial index | ||||||||||||||||||||||||||||||||||||||||||
| 139 | # (WHERE) and has no USING clause. Any other key throws; so does a type | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # other than unique and any options -- never a plainer index than the | ||||||||||||||||||||||||||||||||||||||||||
| 141 | # one declared. `expression` is the whole list between the parentheses | ||||||||||||||||||||||||||||||||||||||||||
| 142 | # as written and stands in for `columns` (ADR 0008 addendum, karr k44). | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 3 | for my $decl ($source->index_declarations_for('duckdb', [qw(expression)])) { | |||||||||||||||||||||||||||||||||||||||||
| 144 | 5 | my $label = "index '$decl->{name}' of table '$table_name'"; | |||||||||||||||||||||||||||||||||||||||||
| 145 | $source->throw_exception( | ||||||||||||||||||||||||||||||||||||||||||
| 146 | "DuckDB cannot create $label with type '$decl->{type}': only a " | ||||||||||||||||||||||||||||||||||||||||||
| 147 | . "plain or unique index exists in DuckDB" | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 5 | ▶ | ) if defined $decl->{type} && lc $decl->{type} ne 'unique'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $decl->{"type"} and lc $decl->{"type"} ne "unique"
MC/DC0%0/2
defined $decl->{"type"} and lc $decl->{"type"} ne "unique"
defined $decl->{"type"}
lc $decl->{"type"} ne "unique"
Truth table66%2/3
A and B
A= defined $decl->{"type"}B= lc $decl->{"type"} ne "unique"
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 5 | my $opts = $decl->{options}; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 5 | ▶ | $source->throw_exception( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%1/3
defined $opts and length(ref $opts eq "HASH" ? join("", %$opts) : (ref $opts eq "ARRAY" ? join("", @$opts) : $opts))
MC/DC0%0/2
defined $opts and length(ref $opts eq "HASH" ? join("", %$opts) : (ref $opts eq "ARRAY" ? join("", @$opts) : $opts))
defined $opts
length(ref $opts eq "HASH" ? join("", %$opts) : (ref $opts eq "ARRAY" ? join("", @$opts) : $opts))
Truth table33%1/3
A and B
A= defined $optsB= length(ref $opts eq "HASH" ? join("", %$opts) : (ref $opts eq "ARRAY" ? join("", @$opts) : $opts))
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | "DuckDB cannot create $label with options: it takes none" | ||||||||||||||||||||||||||||||||||||||||||
| 152 | ) if defined $opts && length( | ||||||||||||||||||||||||||||||||||||||||||
| 153 | ref $opts eq 'HASH' ? join('', %$opts) | ||||||||||||||||||||||||||||||||||||||||||
| 154 | : ref $opts eq 'ARRAY' ? join('', @$opts) : $opts | ||||||||||||||||||||||||||||||||||||||||||
| 155 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 5 | ▶ | my $own = ($decl->{driver} || {})->{duckdb} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
+($decl->{'driver'} || {})->{'duckdb'} || {}
$decl->{'driver'} || {}
MC/DC0%0/1
+($decl->{'driver'} || {})->{'duckdb'} || {}
$decl->{'driver'}
Truth table50%1/2
A
A= $decl->{'driver'}
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | $source->throw_exception( | ||||||||||||||||||||||||||||||||||||||||||
| 158 | "DuckDB cannot create $label with an empty expression" | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 5 | ▶ | ) if defined $own->{expression} && $own->{expression} !~ /\S/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $own->{"expression"} and not $own->{"expression"} =~ /\S/
MC/DC0%0/2
defined $own->{"expression"} and not $own->{"expression"} =~ /\S/
defined $own->{"expression"}
not $own->{"expression"} =~ /\S/
Truth table33%1/3
A and not(B)
A= defined $own->{"expression"}B= not $own->{"expression"} =~ /\S/
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | $indexes{ $decl->{name} } = { | ||||||||||||||||||||||||||||||||||||||||||
| 161 | columns => $decl->{columns}, | ||||||||||||||||||||||||||||||||||||||||||
| 162 | unique => $decl->{unique}, | ||||||||||||||||||||||||||||||||||||||||||
| 163 | expression => $own->{expression}, | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 3 | for my $idx_name (sort keys %indexes) { | |||||||||||||||||||||||||||||||||||||||||
| 167 | 6 | my $idx = $indexes{$idx_name}; | |||||||||||||||||||||||||||||||||||||||||
| 168 | push @stmts, create_index( | ||||||||||||||||||||||||||||||||||||||||||
| 169 | name => $idx_name, | ||||||||||||||||||||||||||||||||||||||||||
| 170 | table => $table_name, | ||||||||||||||||||||||||||||||||||||||||||
| 171 | columns => $idx->{columns} // [], | ||||||||||||||||||||||||||||||||||||||||||
| 172 | unique => $idx->{unique}, | ||||||||||||||||||||||||||||||||||||||||||
| 173 | expression => $idx->{expression}, | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 6 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$idx->{"columns"} // []
MC/DC0%0/1
$idx->{"columns"} // []
$idx->{"columns"}
Truth table50%1/2
A
A= $idx->{"columns"}
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 3 | return join " | |||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | ", @stmts, @view_stmts; | ||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | sub source_schemas { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
source_schemas: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | my ($class, $schema) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | my %seen; | |||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | for my $source_name ($schema->sources) { | |||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | my $source = $schema->source($source_name); | |||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | next if $source->isa('DBIO::ResultSource::View'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | my $name = _resolve_table_name($source->name); | |||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | ▶ | next unless defined $name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | my ($ns) = split_table($name); | |||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | $seen{$ns} = 1 if defined $ns; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | return sort keys %seen; | |||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | # The FOREIGN KEY clauses of a table, one per belongs_to relationship that is | ||||||||||||||||||||||||||||||||||||||||||
| 199 | # a foreign key constraint, in the order of the relationship names, the same | ||||||||||||||||||||||||||||||||||||||||||
| 200 | # key once however many relationships declare it. DuckDB takes a key only to | ||||||||||||||||||||||||||||||||||||||||||
| 201 | # the columns of a PRIMARY KEY or UNIQUE constraint of the parent, in that | ||||||||||||||||||||||||||||||||||||||||||
| 202 | # constraint's own order (measured, libduckdb 1.5.3: "does not have a primary | ||||||||||||||||||||||||||||||||||||||||||
| 203 | # key or unique constraint on the columns b,a" for a key written (b,a) to a | ||||||||||||||||||||||||||||||||||||||||||
| 204 | # key (a,b)), so the column pairs are laid out in the order of the parent's | ||||||||||||||||||||||||||||||||||||||||||
| 205 | # key and not in the alphabetical one of the condition. CASCADE, SET NULL and | ||||||||||||||||||||||||||||||||||||||||||
| 206 | # SET DEFAULT are refused by DuckDB's parser; they are refused here first, | ||||||||||||||||||||||||||||||||||||||||||
| 207 | # before any statement, naming table and column. | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 11 | my %FK_ACTION_OK = ('RESTRICT' => 1, 'NO ACTION' => 1); | |||||||||||||||||||||||||||||||||||||||||
| 209 | 11 | my $FK_DOC = 'https://duckdb.org/docs/stable/sql/constraints#foreign-keys'; | |||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | 3 | ▶ | sub _foreign_key_clauses { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_foreign_key_clauses: called CC=23 SCAR=61.9
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 3 | my ($schema, $source, $table_name, $created) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 213 | 3 | my (@clauses, %seen); | |||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | 3 | for my $rel (sort $source->relationships) { | |||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | my $info = $source->relationship_info($rel); | |||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | next unless $info && $info->{attrs} && $info->{attrs}{is_foreign_key_constraint}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
$info and $info->{"attrs"}
MC/DC0%0/3
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
$info
$info->{"attrs"}
$info->{"attrs"}{"is_foreign_key_constraint"}
Truth table0%0/4
A and B and C
A= $infoB= $info->{"attrs"}C= $info->{"attrs"}{"is_foreign_key_constraint"}
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | my $foreign = $info->{class}; | |||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | my $fs = eval { $schema->source($foreign) } | |||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | // eval { $schema->source($foreign =~ s/.*:://r) }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
eval {
do {
$schema->source($foreign)
}
} // eval {
do {
$schema->source($foreign =~ s/.*:://r)
}
}
MC/DC0%0/2
eval {
do {
$schema->source($foreign)
}
} // eval {
do {
$schema->source($foreign =~ s/.*:://r)
}
}
eval {
do {
$schema->source($foreign)
}
}
eval {
do {
$schema->source($foreign =~ s/.*:://r)
}
}
Truth table0%0/3
A // B
A= eval {
do {
$schema->source($foreign)
}
}B= eval {
do {
$schema->source($foreign =~ s/.*:://r)
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | next unless $fs; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | next if $fs->isa('DBIO::ResultSource::View'); # no key to a view | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | my $foreign_table = _resolve_table_name($fs->name); | |||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | next unless defined $foreign_table; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | my $cond = $info->{cond}; | |||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | ▶ | next unless ref $cond eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | # A table is created whole and DuckDB cannot add a foreign key to one that | ||||||||||||||||||||||||||||||||||||||||||
| 231 | # exists, so a key to a table that does not exist yet -- the two point at | ||||||||||||||||||||||||||||||||||||||||||
| 232 | # each other, directly or through others -- cannot be written. | ||||||||||||||||||||||||||||||||||||||||||
| 233 | $source->throw_exception(sprintf | ||||||||||||||||||||||||||||||||||||||||||
| 234 | "DuckDB cannot create the foreign key of relationship '%s' of table '%s': " | ||||||||||||||||||||||||||||||||||||||||||
| 235 | . "it points to table '%s', which points back to it (directly or " | ||||||||||||||||||||||||||||||||||||||||||
| 236 | . "through other tables) -- both are created whole and DuckDB cannot add " | ||||||||||||||||||||||||||||||||||||||||||
| 237 | . "a foreign key to a table that exists (%s). Declare one of the two " | ||||||||||||||||||||||||||||||||||||||||||
| 238 | . "relationships with is_foreign_key_constraint => 0", | ||||||||||||||||||||||||||||||||||||||||||
| 239 | $rel, $table_name, $foreign_table, | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 'https://duckdb.org/docs/stable/sql/statements/alter_table') | ||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | if $foreign_table ne $table_name && !$created->{$foreign_table}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$foreign_table ne $table_name and not $created->{$foreign_table}
MC/DC0%0/2
$foreign_table ne $table_name and not $created->{$foreign_table}
$foreign_table ne $table_name
not $created->{$foreign_table}
Truth table0%0/3
A and not(B)
A= $foreign_table ne $table_nameB= not $created->{$foreign_table}
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | my %to_from; | |||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | for my $fcol (keys %$cond) { | |||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | (my $to = $fcol) =~ s/^foreign\.//; | |||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | (my $from = $cond->{$fcol}) =~ s/^self\.//; | |||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | $to_from{$to} = $from; | |||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | my @to_sorted = sort keys %to_from; | |||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | # the key of the parent that has exactly these columns | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | my %keys; | |||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | $keys{primary} = [ $fs->primary_columns ]; | |||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | ▶ | if ($fs->can('unique_constraints')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | my %u = $fs->unique_constraints; | |||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | ▶ | $keys{$_} //= $u{$_} for sort keys %u; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$keys{$_} //= $u{$_}
MC/DC0%0/2
$keys{$_} //= $u{$_}
$keys{$_}
$u{$_}
Truth table0%0/3
A //= B
A= $keys{$_}B= $u{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | my ($key) = grep { | ||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | my $cols = $keys{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | ▶ | @$cols == @to_sorted && join("\0", sort @$cols) eq join("\0", @to_sorted) | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
@$cols == @to_sorted && join("\000", sort(@$cols)) eq join("\000", @to_sorted)
MC/DC0%0/2
@$cols == @to_sorted && join("\000", sort(@$cols)) eq join("\000", @to_sorted)
@$cols == @to_sorted
join("\000", sort(@$cols)) eq join("\000", @to_sorted)
Truth table0%0/3
A && B
A= @$cols == @to_sortedB= join("\000", sort(@$cols)) eq join("\000", @to_sorted)
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | } 'primary', sort grep { $_ ne 'primary' } keys %keys; | |||||||||||||||||||||||||||||||||||||||||
| 262 | $source->throw_exception(sprintf | ||||||||||||||||||||||||||||||||||||||||||
| 263 | "DuckDB cannot create the foreign key of relationship '%s' of table '%s': " | ||||||||||||||||||||||||||||||||||||||||||
| 264 | . "the columns %s of table '%s' are neither its PRIMARY KEY or UNIQUE " | ||||||||||||||||||||||||||||||||||||||||||
| 265 | . "constraint, and DuckDB takes a foreign key only to one of those (%s)", | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | $rel, $table_name, join(', ', map { "'$_'" } @to_sorted), $foreign_table, $FK_DOC) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | unless defined $key; | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | my @to = @{ $keys{$key} }; | |||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | my @from = map { $to_from{$_} } @to; | |||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | my %action; | |||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | for my $event (qw(delete update)) { | |||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | my $value = $info->{attrs}{"on_$event"}; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | ▶ | next unless defined $value && length $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $value and length $value
MC/DC0%0/2
defined $value and length $value
defined $value
length $value
Truth table0%0/3
A and B
A= defined $valueB= length $value
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | (my $word = uc $value) =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | $word =~ s/^ | $//g; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | ▶ | unless ($FK_ACTION_OK{$word}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | $source->throw_exception(sprintf | ||||||||||||||||||||||||||||||||||||||||||
| 279 | "DuckDB cannot create the foreign key of relationship '%s' (%s): " | ||||||||||||||||||||||||||||||||||||||||||
| 280 | . "ON %s %s is not available, DuckDB takes RESTRICT and NO ACTION only " | ||||||||||||||||||||||||||||||||||||||||||
| 281 | . "and refuses CASCADE, SET NULL and SET DEFAULT (%s)", | ||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | $rel, join(', ', map { "$table_name.$_" } @from), uc $event, $word, $FK_DOC); | |||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | $action{$event} = $word; | |||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | my $clause = fk_clause( | ||||||||||||||||||||||||||||||||||||||||||
| 288 | from => \@from, to_table => $foreign_table, to => \@to, | ||||||||||||||||||||||||||||||||||||||||||
| 289 | on_delete => $action{delete}, on_update => $action{update}, | ||||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | ▶ | next if $seen{ lc $clause }++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | push @clauses, $clause; | |||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | 3 | return @clauses; | |||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | 9 | ▶ | sub _resolve_table_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_resolve_table_name: called CC=5 SCAR=28.8
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | 9 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 300 | # Plain string: real table name. | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 9 | ▶ | return $name unless ref $name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | ▶ | return undef unless ref $name eq 'SCALAR'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | # SCALAR ref: either a pointer to a plain identifier (DBIx::Class test | ||||||||||||||||||||||||||||||||||||||||||
| 304 | # idiom for "this is a real table whose name happens to come from a | ||||||||||||||||||||||||||||||||||||||||||
| 305 | # ref") or inline SQL for a virtual view. Treat word-only content as | ||||||||||||||||||||||||||||||||||||||||||
| 306 | # a real identifier; anything else (whitespace, SELECT, parens) is a | ||||||||||||||||||||||||||||||||||||||||||
| 307 | # virtual view and is skipped. | ||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | my $v = $$name; | |||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | ▶ | return undef unless defined $v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | return $v if $v =~ /\A\w+\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | 3 | ▶ | sub _topo_sort_sources { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_topo_sort_sources: called CC=17 SCAR=42.9
| |||||||||||||||||||||||||||||||||||||||||||
| 315 | 3 | my ($schema) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | 3 | my %deps; # source_name => { dep_source_name => 1 } | |||||||||||||||||||||||||||||||||||||||||
| 318 | my %by_table; # table_name => first source_name seen | ||||||||||||||||||||||||||||||||||||||||||
| 319 | 3 | my @sources = sort $schema->sources; | |||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | 3 | for my $name (@sources) { | |||||||||||||||||||||||||||||||||||||||||
| 322 | 3 | my $s = $schema->source($name); | |||||||||||||||||||||||||||||||||||||||||
| 323 | 3 | my $t = _resolve_table_name($s->name); | |||||||||||||||||||||||||||||||||||||||||
| 324 | 3 | ▶ | next unless defined $t; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 3 | ▶ | $by_table{$t} //= $name; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$by_table{$t} //= $name
MC/DC0%0/2
$by_table{$t} //= $name
$by_table{$t}
$name
Truth table33%1/3
A //= B
A= $by_table{$t}B= $name
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | 3 | for my $name (@sources) { | |||||||||||||||||||||||||||||||||||||||||
| 329 | 3 | my $s = $schema->source($name); | |||||||||||||||||||||||||||||||||||||||||
| 330 | 3 | ▶ | next unless defined _resolve_table_name($s->name); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | 3 | ▶ | $deps{$name} ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$deps{$name} ||= {}
MC/DC0%0/1
$deps{$name} ||= {}
$deps{$name}
Truth table50%1/2
A
A= $deps{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 3 | for my $rel ($s->relationships) { | |||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | my $info = $s->relationship_info($rel); | |||||||||||||||||||||||||||||||||||||||||
| 334 | next unless $info && $info->{attrs} | ||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | ▶ | && $info->{attrs}{is_foreign_key_constraint}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
$info and $info->{"attrs"}
MC/DC0%0/3
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
$info
$info->{"attrs"}
$info->{"attrs"}{"is_foreign_key_constraint"}
Truth table0%0/4
A and B and C
A= $infoB= $info->{"attrs"}C= $info->{"attrs"}{"is_foreign_key_constraint"}
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | my $foreign = $info->{class}; | |||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | my $fs = eval { $schema->source($foreign) } | |||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | // eval { $schema->source($foreign =~ s/.*:://r) }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
eval {
do {
$schema->source($foreign)
}
} // eval {
do {
$schema->source($foreign =~ s/.*:://r)
}
}
MC/DC0%0/2
eval {
do {
$schema->source($foreign)
}
} // eval {
do {
$schema->source($foreign =~ s/.*:://r)
}
}
eval {
do {
$schema->source($foreign)
}
}
eval {
do {
$schema->source($foreign =~ s/.*:://r)
}
}
Truth table0%0/3
A // B
A= eval {
do {
$schema->source($foreign)
}
}B= eval {
do {
$schema->source($foreign =~ s/.*:://r)
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | ▶ | next unless $fs; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | my $ft = _resolve_table_name($fs->name); | |||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | ▶ | next unless defined $ft; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | # Depend on whichever source "owns" the foreign table. | ||||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | my $owner = $by_table{$ft}; | |||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | ▶ | next unless $owner; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | ▶ | next if $owner eq $name; # self-reference | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | $deps{$name}{$owner} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | # Kahn's algorithm with stable alphabetical tiebreak. | ||||||||||||||||||||||||||||||||||||||||||
| 351 | 3 | my @out; | |||||||||||||||||||||||||||||||||||||||||
| 352 | my %visited; | ||||||||||||||||||||||||||||||||||||||||||
| 353 | 3 | my $visit; | |||||||||||||||||||||||||||||||||||||||||
| 354 | 3 | ▶ | $visit = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.9
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | 3 | my ($n) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 356 | 3 | ▶ | return if $visited{$n}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 357 | 3 | ▶ | for my $d (sort keys %{ $deps{$n} || {} }) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$deps{$n} || {}
MC/DC0%0/1
$deps{$n} || {}
$deps{$n}
Truth table50%1/2
A
A= $deps{$n}
| |||||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | $visit->($d); | |||||||||||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||||||||||||
| 360 | 3 | push @out, $n; | |||||||||||||||||||||||||||||||||||||||||
| 361 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 362 | 3 | $visit->($_) for @sources; | |||||||||||||||||||||||||||||||||||||||||
| 363 | 3 | return @out; | |||||||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | 22 | ▶ | sub _duckdb_column_type { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_duckdb_column_type: called CC=13 SCAR=31.2
| |||||||||||||||||||||||||||||||||||||||||||
| 367 | 22 | my ($info) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 368 | 22 | ▶ | my $type = $info->{data_type} // 'VARCHAR'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"data_type"} // "VARCHAR"
MC/DC0%0/1
$info->{"data_type"} // "VARCHAR"
$info->{"data_type"}
Truth table50%1/2
A
A= $info->{"data_type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | 22 | ▶ | return $type if $type =~ /\(.+\)$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | # ENUM: DuckDB takes the values in the type, ENUM('a', 'b'); a quote in a | ||||||||||||||||||||||||||||||||||||||||||
| 373 | # value is doubled. | ||||||||||||||||||||||||||||||||||||||||||
| 374 | 21 | ▶ | if (lc $type eq 'enum') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | ▶ | my $list = ($info->{extra} || {})->{list}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$info->{'extra'} || {}
MC/DC0%0/1
$info->{'extra'} || {}
$info->{'extra'}
Truth table0%0/2
A
A= $info->{'extra'}
| |||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | ▶ | return $type unless ref $list eq 'ARRAY' && @$list; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $list eq "ARRAY" and @$list
MC/DC0%0/2
ref $list eq "ARRAY" and @$list
ref $list eq "ARRAY"
@$list
Truth table0%0/3
A and B
A= ref $list eq "ARRAY"B= @$list
| |||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | return 'ENUM(' . join(', ', map { (my $v = $_) =~ s/'/''/g; "'$v'" } @$list) . ')'; | |||||||||||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | 21 | my %type_map = ( | |||||||||||||||||||||||||||||||||||||||||
| 381 | # integers | ||||||||||||||||||||||||||||||||||||||||||
| 382 | tinyint => 'TINYINT', | ||||||||||||||||||||||||||||||||||||||||||
| 383 | smallint => 'SMALLINT', | ||||||||||||||||||||||||||||||||||||||||||
| 384 | int => 'INTEGER', | ||||||||||||||||||||||||||||||||||||||||||
| 385 | integer => 'INTEGER', | ||||||||||||||||||||||||||||||||||||||||||
| 386 | bigint => 'BIGINT', | ||||||||||||||||||||||||||||||||||||||||||
| 387 | hugeint => 'HUGEINT', | ||||||||||||||||||||||||||||||||||||||||||
| 388 | serial => 'INTEGER', | ||||||||||||||||||||||||||||||||||||||||||
| 389 | bigserial => 'BIGINT', | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | # floats / decimals | ||||||||||||||||||||||||||||||||||||||||||
| 392 | real => 'REAL', | ||||||||||||||||||||||||||||||||||||||||||
| 393 | float => 'FLOAT', | ||||||||||||||||||||||||||||||||||||||||||
| 394 | double => 'DOUBLE', | ||||||||||||||||||||||||||||||||||||||||||
| 395 | 'double precision'=> 'DOUBLE', | ||||||||||||||||||||||||||||||||||||||||||
| 396 | money => 'DECIMAL(19,4)', # DuckDB has no MONEY type | ||||||||||||||||||||||||||||||||||||||||||
| 397 | numeric => 'DECIMAL', | ||||||||||||||||||||||||||||||||||||||||||
| 398 | decimal => 'DECIMAL', | ||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | # strings | ||||||||||||||||||||||||||||||||||||||||||
| 401 | text => 'VARCHAR', | ||||||||||||||||||||||||||||||||||||||||||
| 402 | string => 'VARCHAR', | ||||||||||||||||||||||||||||||||||||||||||
| 403 | varchar => 'VARCHAR', | ||||||||||||||||||||||||||||||||||||||||||
| 404 | char => 'VARCHAR', | ||||||||||||||||||||||||||||||||||||||||||
| 405 | character => 'VARCHAR', | ||||||||||||||||||||||||||||||||||||||||||
| 406 | clob => 'VARCHAR', | ||||||||||||||||||||||||||||||||||||||||||
| 407 | tinytext => 'VARCHAR', | ||||||||||||||||||||||||||||||||||||||||||
| 408 | mediumtext => 'VARCHAR', | ||||||||||||||||||||||||||||||||||||||||||
| 409 | longtext => 'VARCHAR', | ||||||||||||||||||||||||||||||||||||||||||
| 410 | ntext => 'VARCHAR', | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | # booleans | ||||||||||||||||||||||||||||||||||||||||||
| 413 | boolean => 'BOOLEAN', | ||||||||||||||||||||||||||||||||||||||||||
| 414 | bool => 'BOOLEAN', | ||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||
| 416 | # blobs | ||||||||||||||||||||||||||||||||||||||||||
| 417 | blob => 'BLOB', | ||||||||||||||||||||||||||||||||||||||||||
| 418 | bytea => 'BLOB', | ||||||||||||||||||||||||||||||||||||||||||
| 419 | tinyblob => 'BLOB', | ||||||||||||||||||||||||||||||||||||||||||
| 420 | mediumblob => 'BLOB', | ||||||||||||||||||||||||||||||||||||||||||
| 421 | longblob => 'BLOB', | ||||||||||||||||||||||||||||||||||||||||||
| 422 | binary => 'BLOB', | ||||||||||||||||||||||||||||||||||||||||||
| 423 | varbinary => 'BLOB', | ||||||||||||||||||||||||||||||||||||||||||
| 424 | 'bit varying' => 'BLOB', | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | # temporal | ||||||||||||||||||||||||||||||||||||||||||
| 427 | date => 'DATE', | ||||||||||||||||||||||||||||||||||||||||||
| 428 | time => 'TIME', | ||||||||||||||||||||||||||||||||||||||||||
| 429 | datetime => 'TIMESTAMP', | ||||||||||||||||||||||||||||||||||||||||||
| 430 | timestamp => 'TIMESTAMP', | ||||||||||||||||||||||||||||||||||||||||||
| 431 | timestamptz => 'TIMESTAMPTZ', | ||||||||||||||||||||||||||||||||||||||||||
| 432 | 'timestamp with time zone' => 'TIMESTAMPTZ', | ||||||||||||||||||||||||||||||||||||||||||
| 433 | interval => 'INTERVAL', | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | # misc | ||||||||||||||||||||||||||||||||||||||||||
| 436 | uuid => 'UUID', | ||||||||||||||||||||||||||||||||||||||||||
| 437 | guid => 'UUID', | ||||||||||||||||||||||||||||||||||||||||||
| 438 | uniqueidentifier => 'UUID', # MSSQL/Sybase GUID type -> DuckDB native UUID | ||||||||||||||||||||||||||||||||||||||||||
| 439 | json => 'JSON', | ||||||||||||||||||||||||||||||||||||||||||
| 440 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | 21 | ▶ | my $mapped = $type_map{ lc $type } // uc $type; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$type_map{lc $type} // uc $type
MC/DC0%0/1
$type_map{lc $type} // uc $type
$type_map{lc $type}
Truth table50%1/2
A
A= $type_map{lc $type}
| |||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | # %type_map above collapses every varchar/char/text-family alias to a | ||||||||||||||||||||||||||||||||||||||||||
| 445 | # bare VARCHAR and numeric/decimal to a bare DECIMAL, dropping any | ||||||||||||||||||||||||||||||||||||||||||
| 446 | # declared $info->{size}. Re-attach it now that the DuckDB-native name | ||||||||||||||||||||||||||||||||||||||||||
| 447 | # is known. (money's DECIMAL(19,4) already has parens and is excluded | ||||||||||||||||||||||||||||||||||||||||||
| 448 | # since it isn't string-equal to bare 'DECIMAL'.) | ||||||||||||||||||||||||||||||||||||||||||
| 449 | 21 | ▶ | if (defined $info->{size}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | 12 | ▶ | if ($mapped eq 'VARCHAR' && !ref $info->{size}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$mapped eq "VARCHAR" and not ref $info->{"size"}
MC/DC50%1/2
$mapped eq "VARCHAR" and not ref $info->{"size"}
$mapped eq "VARCHAR"
not ref $info->{"size"}
Truth table66%2/3
A and not(B)
A= $mapped eq "VARCHAR"B= not ref $info->{"size"}
| |||||||||||||||||||||||||||||||||||||||||||
| 451 | 9 | $mapped .= "($info->{size})"; | |||||||||||||||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||||||||||||||||
| 453 | ▶ | elsif ($mapped eq 'DECIMAL' && ref $info->{size} eq 'ARRAY') { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$mapped eq "DECIMAL" and ref $info->{"size"} eq "ARRAY"
MC/DC50%1/2
$mapped eq "DECIMAL" and ref $info->{"size"} eq "ARRAY"
$mapped eq "DECIMAL"
ref $info->{"size"} eq "ARRAY"
Truth table66%2/3
A and B
A= $mapped eq "DECIMAL"B= ref $info->{"size"} eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 454 | 2 | $mapped .= '(' . join(',', @{ $info->{size} }) . ')'; | |||||||||||||||||||||||||||||||||||||||||
| 455 | } | ||||||||||||||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | 21 | return $mapped; | |||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | # A GUID-family column maps to DuckDB's native UUID type, which (unlike an | ||||||||||||||||||||||||||||||||||||||||||
| 462 | # integer surrogate key) cannot take a nextval() sequence default — its | ||||||||||||||||||||||||||||||||||||||||||
| 463 | # auto-generated value comes from uuid() instead. | ||||||||||||||||||||||||||||||||||||||||||
| 464 | 0 | ▶ | sub _is_guid_type { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_guid_type: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 465 | 0 | my $info = shift; | |||||||||||||||||||||||||||||||||||||||||
| 466 | 0 | ▶ | return ( ($info->{data_type} // '') =~ /\A(?:uniqueidentifier|guid|uuid)\z/i ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$info->{"data_type"} // ""
MC/DC0%0/1
$info->{"data_type"} // ""
$info->{"data_type"}
Truth table0%0/2
A
A= $info->{"data_type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | 11 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | __END__ | ||||||||||||||||||||||||||||||||||||||||||