DuckDB/DDL/Emit.pm

filter: Statementstmt 100% 42 / 42 Branchbran 100% 8 / 8 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 7 totaltotal 87.8% 65 / 74 CC 7 SCAR 7.8
CC
7
Cov
98%
column_def
4.0
create_index
3.0
create_sequence
2.0
CRAP 2.2
« DuckDB/Diff/Index.pm ↑ Summary DuckDB/Deploy.pm »
1 package DBIO::DuckDB::DDL::Emit;
2 # ABSTRACT: Single source of DuckDB DDL statement shape
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 DBIO::SQL::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our @EXPORT_OK = qw(
11 column_def pk_clause unique_clause fk_clause
12 create_table create_index create_sequence
13 );
14  
15  
16  
17 7 ▶sub column_def {
Subroutine column_def: called CC=4 SCAR=13.9
Pod undocumented
18 7 my (%a) = @_;
19 7 ▶ my $def = sprintf '%s %s', _quote_ident($a{name}), ($a{type} // 'VARCHAR');
Condition100%2/2
$a{"type"} // "VARCHAR"
l!l
61
MC/DC100%1/1
$a{"type"} // "VARCHAR"
$a{"type"}
Truth table100%2/2
A
A= $a{"type"}
Aresult
11
00
20 7 ▶ $def .= ' NOT NULL' if $a{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"not_null"} 2 5 7
21 7 ▶ $def .= " DEFAULT $a{default}" if defined $a{default};
Branch: 1 branch
BranchTrueFalseTotal
if defined $a{"default"} 2 5 7
22 7 return $def;
23 }
24  
25  
26 2 ▶sub pk_clause {
Subroutine pk_clause: called CC=1 SCAR=0.0
Pod undocumented
27 2 my (@cols) = @_;
28 2 return sprintf 'PRIMARY KEY (%s)', join ', ', map { _quote_ident($_) } @cols;
29 }
30  
31  
32 1 ▶sub unique_clause {
Subroutine unique_clause: called CC=1 SCAR=0.0
Pod undocumented
33 1 my (@cols) = @_;
34 1 return sprintf 'UNIQUE (%s)', join ', ', map { _quote_ident($_) } @cols;
35 }
36  
37  
38 1 ▶sub fk_clause {
Subroutine fk_clause: called CC=1 SCAR=0.0
Pod undocumented
39 1 my (%a) = @_;
40 return sprintf 'FOREIGN KEY (%s) REFERENCES %s(%s)',
41 1 join(', ', map { _quote_ident($_) } @{ $a{from} }),
42 _quote_ident($a{to_table}),
43 1 join(', ', map { _quote_ident($_) } @{ $a{to} });
44 }
45  
46  
47 1 ▶sub create_table {
Subroutine create_table: called CC=1 SCAR=0.0
Pod undocumented
48 1 my ($name, @defs) = @_;
49 return sprintf "CREATE TABLE %s (\n%s\n);",
50 1 _quote_ident($name), join ",\n", map { " $_" } @defs;
51 }
52  
53  
54 2 ▶sub create_index {
Subroutine create_index: called CC=3 SCAR=11.0
Pod undocumented
55 2 my (%a) = @_;
56 return sprintf 'CREATE %sINDEX %s ON %s (%s);',
57 ($a{unique} ? 'UNIQUE ' : ''),
58 _quote_ident($a{name}), _quote_ident($a{table}),
59 2 ▶ join(', ', map { _quote_ident($_) } @{ $a{columns} // [] });
Branch: 1 branch
BranchTrueFalseTotal
$a{'unique'} ? : 1 1 2
Condition50%1/2
$a{"columns"} // []
l!l
20
MC/DC0%0/1
$a{"columns"} // []
$a{"columns"}
Truth table50%1/2
A
A= $a{"columns"}
Aresult
11
00
60 }
61  
62  
63 2 ▶sub create_sequence {
Subroutine create_sequence: called CC=2 SCAR=6.9
Pod undocumented
64 2 my (%a) = @_;
65 2 my $sql = sprintf 'CREATE SEQUENCE IF NOT EXISTS %s', _quote_ident($a{name});
66 2 ▶ $sql .= " START $a{start}" if defined $a{start};
Branch: 1 branch
BranchTrueFalseTotal
if defined $a{"start"} 1 1 2
67 2 return "$sql;";
68 }
69  
70 5 1;
71  
72 __END__
73  
74 =pod
75  
76 =encoding UTF-8
77  
78 =head1 NAME
79  
80 DBIO::DuckDB::DDL::Emit - Single source of DuckDB DDL statement shape
81  
82 =head1 VERSION
83  
84 version 0.900001
85  
86 =head1 DESCRIPTION
87  
88 The DuckDB DDL string templates -- column definitions, table/index/sequence
89 statements, and the inline PK/UNIQUE/FK clauses -- live here so that the two
90 producers of DDL agree on syntax char-for-char:
91  
92 =over 4
93  
94 =item * L<DBIO::DuckDB::DDL> renders the desired schema from DBIO Result
95 classes (C<column_info> hashrefs).
96  
97 =item * L<DBIO::DuckDB::Diff::Table> / L<DBIO::DuckDB::Diff::Column> /
98 L<DBIO::DuckDB::Diff::Index> render CREATE / ALTER statements from an
99 introspected model.
100  
101 =back
102  
103 Each caller owns the field-extraction adapter for its own input shape
104 (DBIO metadata vs introspected model) and feeds normalized pieces here.
105 These functions are pure: identifiers are quoted via L<DBIO::SQL::Util>,
106 nothing else is interpreted. Clauses are returned B<without> indentation;
107 L</create_table> indents the body uniformly.
108  
109 =func column_def
110  
111 column_def(name => $n, type => $t, not_null => $bool, default => $sql);
112  
113 Renders C<"name" TYPE [NOT NULL] [DEFAULT $sql]>. C<default> is emitted
114 verbatim -- the caller is responsible for quoting/escaping it (the two
115 producers derive defaults very differently). Pass C<undef> for no default.
116  
117 =func pk_clause
118  
119 pk_clause(@columns);
120  
121 =func unique_clause
122  
123 unique_clause(@columns);
124  
125 =func fk_clause
126  
127 fk_clause(from => \@local, to_table => $t, to => \@remote);
128  
129 =func create_table
130  
131 create_table($table_name, @clause_strings);
132  
133 Wraps the clauses (column defs, PK, UNIQUE, FK -- all unindented) in a
134 C<CREATE TABLE> statement, indenting each clause by two spaces.
135  
136 =func create_index
137  
138 create_index(name => $n, table => $t, columns => \@cols, unique => $bool);
139  
140 =func create_sequence
141  
142 create_sequence(name => $n);
143 create_sequence(name => $n, start => 1000000);
144  
145 =head1 AUTHOR
146  
147 DBIO Authors
148  
149 =head1 COPYRIGHT AND LICENSE
150  
151 Copyright (C) 2026 DBIO Authors
152  
153 This is free software; you can redistribute it and/or modify it under
154 the same terms as the Perl 5 programming language system itself.
155  
156 =cut
« DuckDB/Diff/Index.pm ↑ Summary DuckDB/Deploy.pm »