blib/lib/DBIO/DuckDB/DDL/Emit.pm

filter: Statementstmt 100% 53 / 53 Branchbran 70.0% 14 / 20 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 10 / 10 totaltotal 92.1% 94 / 102 CC 13 SCAR 10.4
CC
13
Cov
91%
create_index
5.3
column_def
4.0
fk_clause
3.0
CRAP 2.8
« blib/lib/DBIO/DuckDB/Extension.pm ↑ Summary blib/lib/DBIO/DuckDB/Arrow.pm »
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
BranchTrueFalseTotal
if $name =~ /\A([^.]+)\.([^.]+)\z/ 0 19 19
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
BranchTrueFalseTotal
defined $schema ? : 0 16 16
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"
l!l
161
MC/DC100%1/1
$a{"type"} // "VARCHAR"
$a{"type"}
Truth table100%2/2
A
A= $a{"type"}
Aresult
11
00
44 17 ▶ $def .= ' NOT NULL' if $a{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"not_null"} 11 6 17
45 17 ▶ $def .= " DEFAULT $a{default}" if defined $a{default};
Branch: 1 branch
BranchTrueFalseTotal
if defined $a{"default"} 2 15 17
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
BranchTrueFalseTotal
if defined $a{"on_delete"} 0 1 1
69 1 ▶ $sql .= " ON UPDATE $a{on_update}" if defined $a{on_update};
Branch: 1 branch
BranchTrueFalseTotal
if defined $a{"on_update"} 0 1 1
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
BranchTrueFalseTotal
defined $a{'expression'} ? : 0 8 8
/^\(/ ? : 0 10 10
Condition50%1/2
$a{"columns"} // []
l!l
80
MC/DC0%0/1
$a{"columns"} // []
$a{"columns"}
Truth table50%1/2
A
A= $a{"columns"}
Aresult
11
00
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
BranchTrueFalseTotal
$a{'unique'} ? : 2 6 8
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
BranchTrueFalseTotal
if defined $a{"start"} 1 1 2
99 2 return "$sql;";
100 }
101  
102 13 1;
103  
104 __END__
« blib/lib/DBIO/DuckDB/Extension.pm ↑ Summary blib/lib/DBIO/DuckDB/Arrow.pm »