| 1 | package DBIO::DuckDB::DDL::Emit; | ||||||||||||||||||||||||
| 2 | # ABSTRACT: Single source of DuckDB DDL statement shape | ||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||
| 4 | 13 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 5 | 13 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||
| 7 | 13 | our $VERSION = '0.900007'; | |||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||
| 9 | 13 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||
| 11 | 13 | our @EXPORT_OK = qw( | |||||||||||||||||||||||
| 12 | quote_ident quote_table split_table column_def pk_clause unique_clause fk_clause | ||||||||||||||||||||||||
| 13 | create_table create_index create_sequence | ||||||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||
| 18 | 61 | ▶ | sub quote_ident { | ||||||||||||||||||||||
|
Subroutine
quote_ident: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||
| 19 | 61 | my ($name) = @_; | |||||||||||||||||||||||
| 20 | 61 | $name =~ s/"/""/g; | |||||||||||||||||||||||
| 21 | 61 | return qq{"$name"}; | |||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||
| 25 | 19 | ▶ | sub split_table { | ||||||||||||||||||||||
|
Subroutine
split_table: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||
| 26 | 19 | my ($name) = @_; | |||||||||||||||||||||||
| 27 | 19 | ▶ | return ($1, $2) if $name =~ /\A([^.]+)\.([^.]+)\z/; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 28 | 19 | return (undef, $name); | |||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||
| 32 | 16 | ▶ | sub quote_table { | ||||||||||||||||||||||
|
Subroutine
quote_table: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||
| 33 | 16 | my ($name) = @_; | |||||||||||||||||||||||
| 34 | 16 | my ($schema, $table) = split_table($name); | |||||||||||||||||||||||
| 35 | 16 | ▶ | return defined $schema | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 36 | ? quote_ident($schema) . '.' . quote_ident($table) | ||||||||||||||||||||||||
| 37 | : quote_ident($table); | ||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||
| 41 | 17 | ▶ | sub column_def { | ||||||||||||||||||||||
|
Subroutine
column_def: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||
| 42 | 17 | my (%a) = @_; | |||||||||||||||||||||||
| 43 | 17 | ▶ | my $def = sprintf '%s %s', quote_ident($a{name}), ($a{type} // 'VARCHAR'); | ||||||||||||||||||||||
|
Condition100%2/2
$a{"type"} // "VARCHAR"
MC/DC100%1/1
$a{"type"} // "VARCHAR"
$a{"type"}
Truth table100%2/2
A
A= $a{"type"}
| |||||||||||||||||||||||||
| 44 | 17 | ▶ | $def .= ' NOT NULL' if $a{not_null}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 45 | 17 | ▶ | $def .= " DEFAULT $a{default}" if defined $a{default}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 46 | 17 | return $def; | |||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||
| 50 | 4 | ▶ | sub pk_clause { | ||||||||||||||||||||||
|
Subroutine
pk_clause: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||
| 51 | 4 | my (@cols) = @_; | |||||||||||||||||||||||
| 52 | 4 | return sprintf 'PRIMARY KEY (%s)', join ', ', map { quote_ident($_) } @cols; | |||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||
| 56 | 1 | ▶ | sub unique_clause { | ||||||||||||||||||||||
|
Subroutine
unique_clause: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||
| 57 | 1 | my (@cols) = @_; | |||||||||||||||||||||||
| 58 | 1 | return sprintf 'UNIQUE (%s)', join ', ', map { quote_ident($_) } @cols; | |||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||
| 62 | 1 | ▶ | sub fk_clause { | ||||||||||||||||||||||
|
Subroutine
fk_clause: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||
| 63 | 1 | my (%a) = @_; | |||||||||||||||||||||||
| 64 | my $sql = sprintf 'FOREIGN KEY (%s) REFERENCES %s(%s)', | ||||||||||||||||||||||||
| 65 | 1 | join(', ', map { quote_ident($_) } @{ $a{from} }), | |||||||||||||||||||||||
| 66 | quote_table($a{to_table}), | ||||||||||||||||||||||||
| 67 | 1 | join(', ', map { quote_ident($_) } @{ $a{to} }); | |||||||||||||||||||||||
| 68 | 1 | ▶ | $sql .= " ON DELETE $a{on_delete}" if defined $a{on_delete}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 69 | 1 | ▶ | $sql .= " ON UPDATE $a{on_update}" if defined $a{on_update}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 70 | 1 | return $sql; | |||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||
| 74 | 4 | ▶ | sub create_table { | ||||||||||||||||||||||
|
Subroutine
create_table: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||
| 75 | 4 | my ($name, @defs) = @_; | |||||||||||||||||||||||
| 76 | return sprintf "CREATE TABLE %s (\n%s\n);", | ||||||||||||||||||||||||
| 77 | 4 | quote_table($name), join ",\n", map { " $_" } @defs; | |||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||
| 81 | 8 | ▶ | sub create_index { | ||||||||||||||||||||||
|
Subroutine
create_index: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||
| 82 | 8 | my (%a) = @_; | |||||||||||||||||||||||
| 83 | # An expression is the whole list, as written; no column is quoted then. | ||||||||||||||||||||||||
| 84 | # An element of columns that is wrapped in parentheses is the expression | ||||||||||||||||||||||||
| 85 | # of an introspected index and goes in as it is. | ||||||||||||||||||||||||
| 86 | my $list = defined $a{expression} | ||||||||||||||||||||||||
| 87 | ? $a{expression} | ||||||||||||||||||||||||
| 88 | 8 | ▶ | : join(', ', map { /^\(/ ? $_ : quote_ident($_) } @{ $a{columns} // [] }); | ||||||||||||||||||||||
|
Branch: 2 branches
Condition50%1/2
$a{"columns"} // []
MC/DC0%0/1
$a{"columns"} // []
$a{"columns"}
Truth table50%1/2
A
A= $a{"columns"}
| |||||||||||||||||||||||||
| 89 | return sprintf 'CREATE %sINDEX %s ON %s (%s);', | ||||||||||||||||||||||||
| 90 | ($a{unique} ? 'UNIQUE ' : ''), | ||||||||||||||||||||||||
| 91 | 8 | ▶ | quote_ident($a{name}), quote_table($a{table}), $list; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||
| 95 | 2 | ▶ | sub create_sequence { | ||||||||||||||||||||||
|
Subroutine
create_sequence: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 96 | 2 | my (%a) = @_; | |||||||||||||||||||||||
| 97 | 2 | my $sql = sprintf 'CREATE SEQUENCE IF NOT EXISTS %s', quote_table($a{name}); | |||||||||||||||||||||||
| 98 | 2 | ▶ | $sql .= " START $a{start}" if defined $a{start}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 99 | 2 | return "$sql;"; | |||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||
| 102 | 13 | 1; | |||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||
| 104 | __END__ | ||||||||||||||||||||||||