PostgreSQL/DDL.pm

filter: Statementstmt 17.2% 14 / 81 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 30.7% 4 / 13 Podpod 50.0% 4 / 8 totaltotal 16.0% 22 / 137 CC 19 SCAR 36.4
CC
19
Cov
12%
create_table
90.0
create_type
42.0
create_schema
20.0
CRAP 38.0
« PostgreSQL.pm ↑ Summary
1 package Database::Async::Engine::PostgreSQL::DDL;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '1.005'; # VERSION
7  
8 =head1 NAME
9  
10 Database::Async::Engine::PostgreSQL::DDL - support for DDL required for
11 L<Database::Async::ORM>
12  
13 =head1 DESCRIPTION
14  
15 This class is not intended to be used directly. L<Database::Async> will
16 locate it based on the provided engine.
17  
18 This class deals with the mechanics of applying schemata to a database,
19 or reading data back from an existing database.
20  
21 =cut
22  
23 1 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
28 0 my ($class, %args) = @_;
29 0 bless \%args, $class;
30 }
31  
32 =head1 METHODS
33  
34 =cut
35  
36 0 ▶sub table_info {
Subroutine table_info: not called CC=1 SCAR=6.9
Pod undocumented
37 0 my ($self, $tbl) = @_;
38 0 return (<<'EOS', 'schema', 'table');
39 select a.attname as "name",
40 t.typname as "type",
41 not(a.attnotnull) as "nullable",
42 (
43 select substring(
44 pg_catalog.pg_get_expr(d.adbin, d.adrelid),
45 E'\'(.*)\''
46 )
47 from pg_catalog.pg_attrdef d
48 where d.adrelid = a.attrelid
49 and d.adnum = a.attnum
50 and a.atthasdef
51 ) as "default"
52 from pg_class c
53 inner join pg_namespace n on n.oid = c.relnamespace
54 inner join pg_attribute a on a.attrelid = c.oid
55 inner join pg_type t on a.atttypid = t.oid
56 where n.nspname = $1
57 and c.relname = $2
58 and a.attnum > 0
59 order by a.attnum
60 EOS
61 }
62  
63 0 ▶sub schema_info {
Subroutine schema_info: not called CC=1 SCAR=6.9
Pod undocumented
64 0 my ($self, $tbl) = @_;
65 0 return (<<'EOS', 'schema');
66 select n.nspname as "name"
67 from pg_namespace n
68 where n.nspname = $1
69 EOS
70 }
71  
72 0 ▶sub type_info {
Subroutine type_info: not called CC=1 SCAR=6.9
Pod undocumented
73 0 my ($self, $tbl) = @_;
74 0 return (<<'EOS', 'schema', 'type');
75 select *
76 from pg_catalog.pg_type t
77 inner join pg_namespace n on n.oid = t.typnamespace
78 where n.nspname = $1
79 and t.typname = $2
80 EOS
81 }
82  
83 =head2 create_type
84  
85 Creates a type, assuming that it does not currently exist.
86  
87 Returns a list of SQL commands to run.
88  
89 =cut
90  
91 0 ▶sub create_type {
Subroutine create_type: not called CC=6 SCAR=37.4
Pod documented
92 0 my ($self, $type) = @_;
93 0 my $tt = $self->tt;
94 my $data = {
95 type => {
96 defined_in => $type->defined_in,
97 name => $type->name,
98 schema => $type->schema->name,
99 type => $type->type,
100 basis => $type->basis,
101 description => $type->description,
102 ($type->type eq 'enum' ? (data => [ $type->values ]) : ()),
103 ($type->type eq 'composite'
104 ? (
105 fields => [
106 0 ▶ (map {;
Branch: 2 branches
BranchTrueFalseTotal
$type->type eq 'enum' ? : 0 0 0
$type->type eq 'composite' ? : 0 0 0
107 +{
108 0 name => $_->name,
109 type => {
110 schema => $_->type->schema,
111 name => $_->type->name,
112 is_builtin => $_->type->is_builtin,
113 },
114 nullable => $_->nullable,
115 default => $_->default,
116 attributes => $_->attributes,
117 }
118 } $type->fields)
119 ]) : ())
120 }
121 };
122  
123 0 my @pending = do {
124 0 ▶ $tt->process(
Branch: 1 branch
BranchTrueFalseTotal
$tt->process(\"[% IF type.defined_in -%]\n-- Defined in [% type.defined_in %]\n[% END -%]\n[% SWITCH type.type -%]\n[% CASE 'enum' -%]\ncreate type \"[% type.schema %]\".\"[% type.name %]\" as enum (\n[% FOREACH item IN type.data -%]\n '[% item %]'[% UNLESS loop.last %],[% END %]\n[% END -%]\n)\n[% CASE 'domain' -%]\ncreate domain \"[% type.schema %]\".\"[% type.name %]\" as [% type.basis %]\n[% CASE 'composite' -%]\ncreate type \"[% type.schema %]\".\"[% type.name %]\" as (\n[% FOREACH field IN type.fields -%]\n \"[% field.name %]\" [% IF field.type.schema %]\"[% field.type.schema.name %]\".[% END %][% IF field.type.is_builtin; field.type.name; ELSE %]\"[% field.type.name %]\"[% END %][% IF !field.nullable %] not null[% END %][% IF field.default.size %] default [% field.default %][% END %][% UNLESS loop.last %],[% END %]\n[% END -%]\n)\n[% END -%]\n", $data, \my $out) or die $tt->error 0 0 0
125 \<<'TEMPLATE',
126 [% IF type.defined_in -%]
127 -- Defined in [% type.defined_in %]
128 [% END -%]
129 [% SWITCH type.type -%]
130 [% CASE 'enum' -%]
131 create type "[% type.schema %]"."[% type.name %]" as enum (
132 [% FOREACH item IN type.data -%]
133 '[% item %]'[% UNLESS loop.last %],[% END %]
134 [% END -%]
135 )
136 [% CASE 'domain' -%]
137 create domain "[% type.schema %]"."[% type.name %]" as [% type.basis %]
138 [% CASE 'composite' -%]
139 create type "[% type.schema %]"."[% type.name %]" as (
140 [% FOREACH field IN type.fields -%]
141 "[% field.name %]" [% IF field.type.schema %]"[% field.type.schema.name %]".[% END %][% IF field.type.is_builtin; field.type.name; ELSE %]"[% field.type.name %]"[% END %][% IF !field.nullable %] not null[% END %][% IF field.default.size %] default [% field.default %][% END %][% UNLESS loop.last %],[% END %]
142 [% END -%]
143 )
144 [% END -%]
145 TEMPLATE
146 $data,
147 \my $out
148 ) or die $tt->error;
149 0 $log->tracef('Type %s definition would be: %s', $type->name, $out);
150 0 $out
151 };
152 0 ▶ push @pending, do {
Branch: 1 branch
BranchTrueFalseTotal
if defined $type->description 0 0 0
153 0 ▶ $tt->process(
Branch: 1 branch
BranchTrueFalseTotal
$tt->process(\"[% SWITCH type.type -%]\n[% CASE 'enum' -%]\ncomment on type \"[% type.schema %]\".\"[% type.name %]\" is '[% type.description | pg_text %]'\n[% CASE 'domain' -%]\ncomment on domain \"[% type.schema %]\".\"[% type.name %]\" is '[% type.description | pg_text %]'\n[% END -%]\n", $data, \my $out) or die $tt->error 0 0 0
154 \<<'TEMPLATE',
155 [% SWITCH type.type -%]
156 [% CASE 'enum' -%]
157 comment on type "[% type.schema %]"."[% type.name %]" is '[% type.description | pg_text %]'
158 [% CASE 'domain' -%]
159 comment on domain "[% type.schema %]"."[% type.name %]" is '[% type.description | pg_text %]'
160 [% END -%]
161 TEMPLATE
162 $data,
163 \my $out
164 ) or die $tt->error;
165 0 $log->tracef('Type %s description would be: %s', $type->name, $out);
166 0 $out
167 } if defined $type->description;
168 @pending
169 0 }
170  
171 =head2 create_table
172  
173 Creates a table, assuming that it does not already exist.
174  
175 Returns a list of SQL commands to run.
176  
177 =cut
178  
179 0 ▶sub create_table {
Subroutine create_table: not called CC=9 SCAR=45.0
Pod documented
180 0 my ($self, $tbl) = @_;
181 0 my $tt = $self->tt;
182 0 $log->tracef('Creating table %s', $tbl->name);
183 my $data = {
184 table => {
185 defined_in => $tbl->defined_in,
186 schema => $tbl->schema->name,
187 0 (map {; $_ => $tbl->$_ } grep { defined $tbl->$_ } qw(name tablespace)),
188 0 primary_keys => [ map { $_->name } $tbl->primary_keys ],
189 constraints => [
190 map +{
191 type => $_->type,
192 references => {
193 name => $_->references->name,
194 schema => $_->references->schema->name,
195 },
196 0 fields => [ map { $_->name } $_->fields ],
197 }, $tbl->foreign_keys
198 ],
199 parents => [
200 map {;
201 +{
202 0 name => $_->name,
203 schema => $_->schema->name
204 }
205 } $tbl->parents
206 ],
207 fields => [
208 0 (map {;
209 +{
210 0 ▶ name => $_->name,
Branch: 2 branches
BranchTrueFalseTotal
$_->type->is_builtin ? : 0 0 0
$_->type->schema ? : 0 0 0
211 type => {
212 schema => ($_->type->is_builtin
213 ? undef
214 : $_->type->schema
215 ? $_->type->schema->name
216 : $tbl->schema->name
217 ),
218 name => $_->type->name,
219 is_builtin => $_->type->is_builtin,
220 },
221 nullable => $_->nullable,
222 attributes => $_->attributes,
223 default => $_->default,
224 }
225 } $tbl->fields)
226 ]
227 }
228 };
229 0 my @pending = do {
230 0 ▶ $tt->process(
Branch: 1 branch
BranchTrueFalseTotal
$tt->process(\"[% IF table.defined_in -%]\n-- Defined in [% table.defined_in %]\n[% END -%]\ncreate [% IF table.temporary %]temporary [% END %][% IF table.unlogged %]unlogged [% END %]table if not exists \"[% table.schema %]\".\"[% table.name %]\" (\n[% FOREACH field IN table.fields -%]\n \"[% field.name %]\" [% IF field.type.schema.defined %]\"[% field.type.schema %]\".[% END %][% IF field.type.is_builtin; field.type.name; ELSE %]\"[% field.type.name %]\"[% END %][% IF field.attributes.size %] [% field.attributes %][% END %][% IF !field.nullable %] not null[% END %][% IF field.default.size %] default [% field.default %][% END %][% IF table.primary_keys.size > 0 || !loop.last %],[% END %]\n[% END -%]\n[% IF table.primary_keys.size -%]\n primary key ([% FOR pk IN table.primary_keys %]\"[% pk %]\"[% UNLESS loop.last %], [% END %][% END %])\n[% END -%]\n)[% IF table.parents.size %] inherits (\n[% FOREACH parent IN table.parents -%]\n \"[% parent.schema %]\".\"[% parent.name %]\"[% UNLESS loop.last %],[% END %]\n[% END -%]\n)[% END %][% IF table.tablespace %] tablespace \"[% table.tablespace %]\"[% END %]\n", $data, \my $out) or die $tt->error 0 0 0
231 \<<'TEMPLATE',
232 [% IF table.defined_in -%]
233 -- Defined in [% table.defined_in %]
234 [% END -%]
235 create [% IF table.temporary %]temporary [% END %][% IF table.unlogged %]unlogged [% END %]table if not exists "[% table.schema %]"."[% table.name %]" (
236 [% FOREACH field IN table.fields -%]
237 "[% field.name %]" [% IF field.type.schema.defined %]"[% field.type.schema %]".[% END %][% IF field.type.is_builtin; field.type.name; ELSE %]"[% field.type.name %]"[% END %][% IF field.attributes.size %] [% field.attributes %][% END %][% IF !field.nullable %] not null[% END %][% IF field.default.size %] default [% field.default %][% END %][% IF table.primary_keys.size > 0 || !loop.last %],[% END %]
238 [% END -%]
239 [% IF table.primary_keys.size -%]
240 primary key ([% FOR pk IN table.primary_keys %]"[% pk %]"[% UNLESS loop.last %], [% END %][% END %])
241 [% END -%]
242 )[% IF table.parents.size %] inherits (
243 [% FOREACH parent IN table.parents -%]
244 "[% parent.schema %]"."[% parent.name %]"[% UNLESS loop.last %],[% END %]
245 [% END -%]
246 )[% END %][% IF table.tablespace %] tablespace "[% table.tablespace %]"[% END %]
247 TEMPLATE
248 $data,
249 \my $out
250 ) or die $tt->error;
251 0 $log->tracef('Table %s definition would be: %s', $tbl->name, $out);
252 0 $out
253 };
254 0 ▶ push @pending, do {
Branch: 1 branch
BranchTrueFalseTotal
if defined $tbl->description 0 0 0
255 0 ▶ $tt->process(
Branch: 1 branch
BranchTrueFalseTotal
$tt->process(\"comment on table \"[% table.schema %]\".\"[% table.name %]\" is '[% table.description | pg_text %]'\n", $data, \my $out) or die $tt->error 0 0 0
256 \<<'TEMPLATE',
257 comment on table "[% table.schema %]"."[% table.name %]" is '[% table.description | pg_text %]'
258 TEMPLATE
259 $data,
260 \my $out
261 ) or die $tt->error;
262 0 $log->tracef('Table %s description would be: %s', $tbl->name, $out);
263 0 $out
264 } if defined $tbl->description;
265  
266 0 for my $constraint ($data->{table}{constraints}->@*) {
267 0 ▶ unless($constraint->{type} eq 'foreign_key') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($constraint->{"type"} eq "foreign_key") 0 0 0
268 0 $log->warnf('unsupported constraint %s on table %s', $constraint->{type}, $data->{table}{name});
269 0 next;
270 }
271 0 push @pending, do {
272 0 ▶ $tt->process(
Branch: 1 branch
BranchTrueFalseTotal
$tt->process(\"alter table \"[% table.schema %]\".\"[% table.name %]\" add foreign key ([% constraint.fields.join(',') %]) references \"[% constraint.references.schema %]\".\"[% constraint.references.name %]\"\n", {%$data, "constraint", $constraint}, \my $out) or die $tt->error 0 0 0
273 \<<'TEMPLATE',
274 alter table "[% table.schema %]"."[% table.name %]" add foreign key ([% constraint.fields.join(',') %]) references "[% constraint.references.schema %]"."[% constraint.references.name %]"
275 TEMPLATE
276 { %$data, constraint => $constraint },
277 \my $out
278 ) or die $tt->error;
279 0 $log->tracef('Table %s FK on (%s) to %s', $tbl->name, join(',', $constraint->{fields}->@*), $constraint->{references});
280 0 $out
281 };
282 }
283 0 @pending;
284 }
285  
286 =head2 create_schema
287  
288 Creates a schema, assuming that it does not currently exist.
289  
290 Returns a list of SQL commands to run.
291  
292 =cut
293  
294 0 ▶sub create_schema {
Subroutine create_schema: not called CC=4 SCAR=30.0
Pod documented
295 0 my ($self, $schema) = @_;
296 0 my $tt = $self->tt;
297 0 my $data = {
298 schema => {
299 name => $schema->name,
300 defined_in => $schema->defined_in,
301 }
302 };
303 0 my @pending = do {
304 0 ▶ $tt->process(
Branch: 1 branch
BranchTrueFalseTotal
$tt->process(\"[% IF schema.defined_in -%]\n-- Defined in [% schema.defined_in %]\n[% END -%]\ncreate schema if not exists \"[% schema.name %]\"\n", $data, \my $out) or die $tt->error 0 0 0
305 \<<'TEMPLATE',
306 [% IF schema.defined_in -%]
307 -- Defined in [% schema.defined_in %]
308 [% END -%]
309 create schema if not exists "[% schema.name %]"
310 TEMPLATE
311 $data,
312 \my $out
313 ) or die $tt->error;
314 0 $log->tracef('Schema %s definition would be: %s', $schema->name, $out);
315 0 $out
316 };
317 0 ▶ push @pending, do {
Branch: 1 branch
BranchTrueFalseTotal
if defined $schema->description 0 0 0
318 0 ▶ $tt->process(
Branch: 1 branch
BranchTrueFalseTotal
$tt->process(\"comment on schema \"[% schema.name %]\" is '[% schema.description | pg_text %]'\n", $data, \my $out) or die $tt->error 0 0 0
319 \<<'TEMPLATE',
320 comment on schema "[% schema.name %]" is '[% schema.description | pg_text %]'
321 TEMPLATE
322 $data,
323 \my $out
324 ) or die $tt->error;
325 0 $log->tracef('Schema %s description would be: %s', $schema->name, $out);
326 0 $out
327 } if defined $schema->description;
328 0 @pending;
329 }
330  
331 =head1 METHODS - Internal
332  
333 =cut
334  
335 =head2 tt
336  
337 Returns a L<Template> instance.
338  
339 =cut
340  
341 0 ▶sub tt {
Subroutine tt: not called CC=2 SCAR=17.9
Pod documented
342 shift->{tt} //= Template->new(
343 UNICODE => 1,
344 FILTERS => {
345 0 ▶ pg_text => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
346 0 my ($txt) = @_;
347 0 for($txt) {
348 0 s{^\s+}{};
349 0 s{\s+$}{};
350 0 s{'}{''}g;
351 }
352 0 return $txt
353 }
354 }
355 0 ▶ );
Condition0%0/3
+(shift())->{"tt"} //= "Template"->new("UNICODE", 1, "FILTERS", {"pg_text", sub { my($txt) = @_; foreach $_ ($txt) { s/^\s+//; s/\s+$//; s/'/''/g; } ; return $txt; } })
l!l&&r!l&&!r
000
MC/DC0%0/2
+(shift())->{"tt"} //= "Template"->new("UNICODE", 1, "FILTERS", {"pg_text", sub { my($txt) = @_; foreach $_ ($txt) { s/^\s+//; s/\s+$//; s/'/''/g; } ; return $txt; } })
+(shift())->{"tt"} "Template"->new("UNICODE", 1, "FILTERS", {"pg_text", sub { my($txt) = @_; foreach $_ ($txt) { s/^\s+//; s/\s+$//; s/'/''/g; } ; return $txt; } })
Truth table0%0/3
A //= B
A= +(shift())->{"tt"}B= "Template"->new("UNICODE", 1, "FILTERS", {"pg_text", sub { my($txt) = @_; foreach $_ ($txt) { s/^\s+//; s/\s+$//; s/'/''/g; } ; return $txt; } })
ABresult
1X1
011
000
356 }
357  
358 1 1;
359  
360 __END__
« PostgreSQL.pm ↑ Summary