blib/lib/SQL/Translator/Schema.pm

filter: Statementstmt 80.5% 207 / 257 Branchbran 63.3% 76 / 120 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 92.8% 26 / 28 Podpod 100% 19 / 19 totaltotal 75.9% 328 / 432 CC 73 SCAR 38.7
CC
73
Cov
74%
make_natural_joins
272.0
as_graph_pm
20.0
get_table
8.7
CRAP 48.1
« blib/lib/SQL/Translator/Parser/DBI/Oracle.pm ↑ Summary blib/lib/SQL/Translator/Producer/Oracle.pm »
1 package SQL::Translator::Schema;
2  
3 =pod
4  
5 =head1 NAME
6  
7 SQL::Translator::Schema - SQL::Translator schema object
8  
9 =head1 SYNOPSIS
10  
11 use SQL::Translator::Schema;
12 my $schema = SQL::Translator::Schema->new(
13 name => 'Foo',
14 database => 'MySQL',
15 );
16 my $table = $schema->add_table( name => 'foo' );
17 my $view = $schema->add_view( name => 'bar', sql => '...' );
18  
19  
20 =head1 DESCSIPTION
21  
22 C<SQL::Translator::Schema> is the object that accepts, validates, and
23 returns the database structure.
24  
25 =head1 METHODS
26  
27 =cut
28  
29 73 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 73 ▶use SQL::Translator::Schema::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 73 ▶use SQL::Translator::Schema::Procedure;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 73 ▶use SQL::Translator::Schema::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 73 ▶use SQL::Translator::Schema::Trigger;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 73 ▶use SQL::Translator::Schema::View;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 73 ▶use Sub::Quote qw(quote_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 73 ▶use SQL::Translator::Utils 'parse_list_arg';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 73 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 73 extends 'SQL::Translator::Schema::Object';
41  
42 73 our $VERSION = '1.66';
43  
44 73 has _order => (
45 is => 'ro',
46 default => quote_sub(
47 q{ +{ map { $_ => 0 } qw/
48 table
49 view
50 trigger
51 proc
52 /} }
53 ),
54 );
55  
56 0 ▶sub as_graph_pm {
Subroutine as_graph_pm: not called CC=4 SCAR=30.0
Pod documented
57  
58 =pod
59  
60 =head2 as_graph_pm
61  
62 Returns a Graph::Directed object with the table names for nodes.
63  
64 =cut
65  
66 0 require Graph::Directed;
67  
68 0 my $self = shift;
69 0 my $g = Graph::Directed->new;
70  
71 0 for my $table ($self->get_tables) {
72 0 my $tname = $table->name;
73 0 $g->add_vertex($tname);
74  
75 0 for my $field ($table->get_fields) {
76 0 ▶ if ($field->is_foreign_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->is_foreign_key) 0 0 0
77 0 my $fktable = $field->foreign_key_reference->reference_table;
78  
79 0 $g->add_edge($fktable, $tname);
80 }
81 }
82 }
83  
84 0 return $g;
85 }
86  
87 73 has _tables => (is => 'ro', init_arg => undef, default => quote_sub(q{ +{} }));
88  
89 359 ▶sub add_table {
Subroutine add_table: called CC=5 SCAR=16.1
Pod documented
90  
91 =pod
92  
93 =head2 add_table
94  
95 Add a table object. Returns the new L<SQL::Translator::Schema::Table> object.
96 The "name" parameter is required. If you try to create a table with the
97 same name as an existing table, you will get an error and the table will
98 not be created.
99  
100 my $t1 = $schema->add_table( name => 'foo' ) or die $schema->error;
101 my $t2 = SQL::Translator::Schema::Table->new( name => 'bar' );
102 $t2 = $schema->add_table( $table_bar ) or die $schema->error;
103  
104 =cut
105  
106 359 my $self = shift;
107 359 my $table_class = 'SQL::Translator::Schema::Table';
108 359 my $table;
109  
110 359 ▶ if (UNIVERSAL::isa($_[0], $table_class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], $table_class)) { } 19 340 359
111 19 $table = shift;
112 19 $table->schema($self);
113 } else {
114 0 ▶ my %args = ref $_[0] eq 'HASH' ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 340 340
115 340 $args{'schema'} = $self;
116 340 ▶ $table = $table_class->new(\%args)
Branch: 1 branch
BranchTrueFalseTotal
$table = $table_class->new(\%args) or return $self->error($table_class->error) 339 1 340
117 or return $self->error($table_class->error);
118 }
119  
120 358 $table->order(++$self->_order->{table});
121  
122 # We know we have a name as the Table->new above errors if none given.
123 358 my $table_name = $table->name;
124  
125 358 ▶ if (defined $self->_tables->{$table_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_tables->{$table_name}) { } 1 357 358
126 1 return $self->error(qq[Can't use table name "$table_name": table exists]);
127 } else {
128 357 $self->_tables->{$table_name} = $table;
129 }
130  
131 357 return $table;
132 }
133  
134 5 ▶sub drop_table {
Subroutine drop_table: called CC=5 SCAR=16.1
Pod documented
135  
136 =pod
137  
138 =head2 drop_table
139  
140 Remove a table from the schema. Returns the table object if the table was found
141 and removed, an error otherwise. The single parameter can be either a table
142 name or an L<SQL::Translator::Schema::Table> object. The "cascade" parameter
143 can be set to 1 to also drop all triggers on the table, default is 0.
144  
145 $schema->drop_table('mytable');
146 $schema->drop_table('mytable', cascade => 1);
147  
148 =cut
149  
150 5 my $self = shift;
151 5 my $table_class = 'SQL::Translator::Schema::Table';
152 5 my $table_name;
153  
154 5 ▶ if (UNIVERSAL::isa($_[0], $table_class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], $table_class)) { } 2 3 5
155 2 $table_name = shift->name;
156 } else {
157 3 $table_name = shift;
158 }
159 5 my %args = @_;
160 5 my $cascade = $args{'cascade'};
161  
162 5 ▶ if (!exists $self->_tables->{$table_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_tables->{$table_name}) 1 4 5
163 1 return $self->error(qq[Can't drop table: "$table_name" doesn't exist]);
164 }
165  
166 4 my $table = delete $self->_tables->{$table_name};
167  
168 4 ▶ if ($cascade) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cascade) 2 2 4
169  
170 # Drop all triggers on this table
171 0 $self->drop_trigger() for (grep { $_->on_table eq $table_name } values %{ $self->_triggers });
172 }
173 4 return $table;
174 }
175  
176 73 has _procedures => (is => 'ro', init_arg => undef, default => quote_sub(q{ +{} }));
177  
178 34 ▶sub add_procedure {
Subroutine add_procedure: called CC=7 SCAR=20.4
Pod documented
179  
180 =pod
181  
182 =head2 add_procedure
183  
184 Add a procedure object. Returns the new L<SQL::Translator::Schema::Procedure>
185 object. The "name" parameter is required. If you try to create a procedure
186 with the same name as an existing procedure, you will get an error and the
187 procedure will not be created.
188  
189 my $p1 = $schema->add_procedure( name => 'foo' );
190 my $p2 = SQL::Translator::Schema::Procedure->new( name => 'bar' );
191 $p2 = $schema->add_procedure( $procedure_bar ) or die $schema->error;
192  
193 =cut
194  
195 34 my $self = shift;
196 34 my $procedure_class = 'SQL::Translator::Schema::Procedure';
197 34 my $procedure;
198  
199 34 ▶ if (UNIVERSAL::isa($_[0], $procedure_class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], $procedure_class)) { } 2 32 34
200 2 $procedure = shift;
201 2 $procedure->schema($self);
202 } else {
203 0 ▶ my %args = ref $_[0] eq 'HASH' ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 32 32
204 32 $args{'schema'} = $self;
205 32 ▶ return $self->error('No procedure name') unless $args{'name'};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 0 32 32
206 32 ▶ $procedure = $procedure_class->new(\%args)
Branch: 1 branch
BranchTrueFalseTotal
$procedure = $procedure_class->new(\%args) or return $self->error($procedure_class->error) 32 0 32
207 or return $self->error($procedure_class->error);
208 }
209  
210 34 $procedure->order(++$self->_order->{proc});
211 34 ▶ my $procedure_name = $procedure->name
Branch: 1 branch
BranchTrueFalseTotal
my $procedure_name = $procedure->name or return $self->error("No procedure name") 34 0 34
212 or return $self->error('No procedure name');
213  
214 34 ▶ if (defined $self->_procedures->{$procedure_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_procedures->{$procedure_name}) { } 0 34 34
215 0 return $self->error(qq[Can't create procedure: "$procedure_name" exists]);
216 } else {
217 34 $self->_procedures->{$procedure_name} = $procedure;
218 }
219  
220 34 return $procedure;
221 }
222  
223 3 ▶sub drop_procedure {
Subroutine drop_procedure: called CC=3 SCAR=11.0
Pod documented
224  
225 =pod
226  
227 =head2 drop_procedure
228  
229 Remove a procedure from the schema. Returns the procedure object if the
230 procedure was found and removed, an error otherwise. The single parameter
231 can be either a procedure name or an L<SQL::Translator::Schema::Procedure>
232 object.
233  
234 $schema->drop_procedure('myprocedure');
235  
236 =cut
237  
238 3 my $self = shift;
239 3 my $proc_class = 'SQL::Translator::Schema::Procedure';
240 3 my $proc_name;
241  
242 3 ▶ if (UNIVERSAL::isa($_[0], $proc_class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], $proc_class)) { } 1 2 3
243 1 $proc_name = shift->name;
244 } else {
245 2 $proc_name = shift;
246 }
247  
248 3 ▶ if (!exists $self->_procedures->{$proc_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_procedures->{$proc_name}) 1 2 3
249 1 return $self->error(qq[Can't drop procedure: "$proc_name" doesn't exist]);
250 }
251  
252 2 my $proc = delete $self->_procedures->{$proc_name};
253  
254 2 return $proc;
255 }
256  
257 73 has _triggers => (is => 'ro', init_arg => undef, default => quote_sub(q{ +{} }));
258  
259 79 ▶sub add_trigger {
Subroutine add_trigger: called CC=7 SCAR=20.4
Pod documented
260  
261 =pod
262  
263 =head2 add_trigger
264  
265 Add a trigger object. Returns the new L<SQL::Translator::Schema::Trigger> object.
266 The "name" parameter is required. If you try to create a trigger with the
267 same name as an existing trigger, you will get an error and the trigger will
268 not be created.
269  
270 my $t1 = $schema->add_trigger( name => 'foo' );
271 my $t2 = SQL::Translator::Schema::Trigger->new( name => 'bar' );
272 $t2 = $schema->add_trigger( $trigger_bar ) or die $schema->error;
273  
274 =cut
275  
276 79 my $self = shift;
277 79 my $trigger_class = 'SQL::Translator::Schema::Trigger';
278 79 my $trigger;
279  
280 79 ▶ if (UNIVERSAL::isa($_[0], $trigger_class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], $trigger_class)) { } 2 77 79
281 2 $trigger = shift;
282 2 $trigger->schema($self);
283 } else {
284 0 ▶ my %args = ref $_[0] eq 'HASH' ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 77 77
285 77 $args{'schema'} = $self;
286 77 ▶ return $self->error('No trigger name') unless $args{'name'};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 0 77 77
287 77 ▶ $trigger = $trigger_class->new(\%args)
Branch: 1 branch
BranchTrueFalseTotal
$trigger = $trigger_class->new(\%args) or return $self->error($trigger_class->error) 77 0 77
288 or return $self->error($trigger_class->error);
289 }
290  
291 79 $trigger->order(++$self->_order->{trigger});
292  
293 79 ▶ my $trigger_name = $trigger->name or return $self->error('No trigger name');
Branch: 1 branch
BranchTrueFalseTotal
my $trigger_name = $trigger->name or return $self->error("No trigger name") 79 0 79
294 79 ▶ if (defined $self->_triggers->{$trigger_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_triggers->{$trigger_name}) { } 0 79 79
295 0 return $self->error(qq[Can't create trigger: "$trigger_name" exists]);
296 } else {
297 79 $self->_triggers->{$trigger_name} = $trigger;
298 }
299  
300 79 return $trigger;
301 }
302  
303 3 ▶sub drop_trigger {
Subroutine drop_trigger: called CC=3 SCAR=11.0
Pod documented
304  
305 =pod
306  
307 =head2 drop_trigger
308  
309 Remove a trigger from the schema. Returns the trigger object if the trigger was
310 found and removed, an error otherwise. The single parameter can be either a
311 trigger name or an L<SQL::Translator::Schema::Trigger> object.
312  
313 $schema->drop_trigger('mytrigger');
314  
315 =cut
316  
317 3 my $self = shift;
318 3 my $trigger_class = 'SQL::Translator::Schema::Trigger';
319 3 my $trigger_name;
320  
321 3 ▶ if (UNIVERSAL::isa($_[0], $trigger_class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], $trigger_class)) { } 1 2 3
322 1 $trigger_name = shift->name;
323 } else {
324 2 $trigger_name = shift;
325 }
326  
327 3 ▶ if (!exists $self->_triggers->{$trigger_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_triggers->{$trigger_name}) 1 2 3
328 1 return $self->error(qq[Can't drop trigger: "$trigger_name" doesn't exist]);
329 }
330  
331 2 my $trigger = delete $self->_triggers->{$trigger_name};
332  
333 2 return $trigger;
334 }
335  
336 73 has _views => (is => 'ro', init_arg => undef, default => quote_sub(q{ +{} }));
337  
338 43 ▶sub add_view {
Subroutine add_view: called CC=7 SCAR=19.8
Pod documented
339  
340 =pod
341  
342 =head2 add_view
343  
344 Add a view object. Returns the new L<SQL::Translator::Schema::View> object.
345 The "name" parameter is required. If you try to create a view with the
346 same name as an existing view, you will get an error and the view will
347 not be created.
348  
349 my $v1 = $schema->add_view( name => 'foo' );
350 my $v2 = SQL::Translator::Schema::View->new( name => 'bar' );
351 $v2 = $schema->add_view( $view_bar ) or die $schema->error;
352  
353 =cut
354  
355 43 my $self = shift;
356 43 my $view_class = 'SQL::Translator::Schema::View';
357 43 my $view;
358  
359 43 ▶ if (UNIVERSAL::isa($_[0], $view_class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], $view_class)) { } 3 40 43
360 3 $view = shift;
361 3 $view->schema($self);
362 } else {
363 0 ▶ my %args = ref $_[0] eq 'HASH' ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 40 40
364 40 $args{'schema'} = $self;
365 40 ▶ return $self->error('No view name') unless $args{'name'};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 0 40 40
366 40 ▶ $view = $view_class->new(\%args) or return $view_class->error;
Branch: 1 branch
BranchTrueFalseTotal
$view = $view_class->new(\%args) or return $view_class->error 40 0 40
367 }
368  
369 43 $view->order(++$self->_order->{view});
370 43 ▶ my $view_name = $view->name or return $self->error('No view name');
Branch: 1 branch
BranchTrueFalseTotal
my $view_name = $view->name or return $self->error("No view name") 43 0 43
371  
372 43 ▶ if (defined $self->_views->{$view_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_views->{$view_name}) { } 1 42 43
373 1 return $self->error(qq[Can't create view: "$view_name" exists]);
374 } else {
375 42 $self->_views->{$view_name} = $view;
376 }
377  
378 42 return $view;
379 }
380  
381 3 ▶sub drop_view {
Subroutine drop_view: called CC=3 SCAR=11.0
Pod documented
382  
383 =pod
384  
385 =head2 drop_view
386  
387 Remove a view from the schema. Returns the view object if the view was found
388 and removed, an error otherwise. The single parameter can be either a view
389 name or an L<SQL::Translator::Schema::View> object.
390  
391 $schema->drop_view('myview');
392  
393 =cut
394  
395 3 my $self = shift;
396 3 my $view_class = 'SQL::Translator::Schema::View';
397 3 my $view_name;
398  
399 3 ▶ if (UNIVERSAL::isa($_[0], $view_class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], $view_class)) { } 1 2 3
400 1 $view_name = shift->name;
401 } else {
402 2 $view_name = shift;
403 }
404  
405 3 ▶ if (!exists $self->_views->{$view_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_views->{$view_name}) 1 2 3
406 1 return $self->error(qq[Can't drop view: "$view_name" doesn't exist]);
407 }
408  
409 2 my $view = delete $self->_views->{$view_name};
410  
411 2 return $view;
412 }
413  
414 =head2 database
415  
416 Get or set the schema's database. (optional)
417  
418 my $database = $schema->database('PostgreSQL');
419  
420 =cut
421  
422 73 has database => (is => 'rw', default => quote_sub(q{ '' }));
423  
424 17 ▶sub is_valid {
Subroutine is_valid: called CC=4 SCAR=13.9
Pod documented
425  
426 =pod
427  
428 =head2 is_valid
429  
430 Returns true if all the tables and views are valid.
431  
432 my $ok = $schema->is_valid or die $schema->error;
433  
434 =cut
435  
436 17 my $self = shift;
437  
438 17 ▶ return $self->error('No tables') unless $self->get_tables;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_tables 1 16 17
439  
440 16 for my $object ($self->get_tables, $self->get_views) {
441 62 ▶ return $object->error unless $object->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $object->is_valid 0 62 62
442 }
443  
444 16 return 1;
445 }
446  
447 1 ▶sub get_procedure {
Subroutine get_procedure: called CC=3 SCAR=11.4
Pod documented
448  
449 =pod
450  
451 =head2 get_procedure
452  
453 Returns a procedure by the name provided.
454  
455 my $procedure = $schema->get_procedure('foo');
456  
457 =cut
458  
459 1 my $self = shift;
460 1 ▶ my $procedure_name = shift or return $self->error('No procedure name');
Branch: 1 branch
BranchTrueFalseTotal
my $procedure_name = shift() or return $self->error("No procedure name") 1 0 1
461 return $self->error(qq[Table "$procedure_name" does not exist])
462 1 ▶ unless exists $self->_procedures->{$procedure_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->_procedures->{$procedure_name} 0 1 1
463 1 return $self->_procedures->{$procedure_name};
464 }
465  
466 23 ▶sub get_procedures {
Subroutine get_procedures: called CC=3 SCAR=11.0
Pod documented
467  
468 =pod
469  
470 =head2 get_procedures
471  
472 Returns all the procedures as an array or array reference.
473  
474 my @procedures = $schema->get_procedures;
475  
476 =cut
477  
478 23 my $self = shift;
479 23 my @procedures = map { $_->[1] }
480 25 sort { $a->[0] <=> $b->[0] }
481 23 map { [ $_->order, $_ ] } values %{ $self->_procedures };
482  
483 23 ▶ if (@procedures) {
Branch: 1 branch
BranchTrueFalseTotal
if (@procedures) { } 13 10 23
484 13 ▶ return wantarray ? @procedures : \@procedures;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 13 0 13
485 } else {
486 10 $self->error('No procedures');
487 10 return;
488 }
489 }
490  
491 449 ▶sub get_table {
Subroutine get_table: called CC=6 SCAR=21.6
Pod documented
492  
493 =pod
494  
495 =head2 get_table
496  
497 Returns a table by the name provided.
498  
499 my $table = $schema->get_table('foo');
500  
501 =cut
502  
503 449 my $self = shift;
504 449 ▶ my $table_name = shift or return $self->error('No table name');
Branch: 1 branch
BranchTrueFalseTotal
my $table_name = shift() or return $self->error("No table name") 448 1 449
505 448 my $case_insensitive = shift;
506 448 ▶ if ($case_insensitive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($case_insensitive) 0 448 448
507 0 $table_name = uc($table_name);
508 0 foreach my $table (keys %{ $self->_tables }) {
509 0 ▶ return $self->_tables->{$table} if $table_name eq uc($table);
Branch: 1 branch
BranchTrueFalseTotal
if $table_name eq uc $table 0 0 0
510 }
511 0 return $self->error(qq[Table "$table_name" does not exist]);
512 }
513 return $self->error(qq[Table "$table_name" does not exist])
514 448 ▶ unless exists $self->_tables->{$table_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->_tables->{$table_name} 49 399 448
515 399 return $self->_tables->{$table_name};
516 }
517  
518 277 ▶sub get_tables {
Subroutine get_tables: called CC=3 SCAR=11.0
Pod documented
519  
520 =pod
521  
522 =head2 get_tables
523  
524 Returns all the tables as an array or array reference.
525  
526 my @tables = $schema->get_tables;
527  
528 =cut
529  
530 277 my $self = shift;
531 820 my @tables = map { $_->[1] }
532 1048 sort { $a->[0] <=> $b->[0] }
533 277 map { [ $_->order, $_ ] } values %{ $self->_tables };
534  
535 277 ▶ if (@tables) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tables) { } 273 4 277
536 273 ▶ return wantarray ? @tables : \@tables;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 257 16 273
537 } else {
538 4 $self->error('No tables');
539 4 return;
540 }
541 }
542  
543 2 ▶sub get_trigger {
Subroutine get_trigger: called CC=3 SCAR=11.4
Pod documented
544  
545 =pod
546  
547 =head2 get_trigger
548  
549 Returns a trigger by the name provided.
550  
551 my $trigger = $schema->get_trigger('foo');
552  
553 =cut
554  
555 2 my $self = shift;
556 2 ▶ my $trigger_name = shift or return $self->error('No trigger name');
Branch: 1 branch
BranchTrueFalseTotal
my $trigger_name = shift() or return $self->error("No trigger name") 2 0 2
557 return $self->error(qq[Trigger "$trigger_name" does not exist])
558 2 ▶ unless exists $self->_triggers->{$trigger_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->_triggers->{$trigger_name} 0 2 2
559 2 return $self->_triggers->{$trigger_name};
560 }
561  
562 43 ▶sub get_triggers {
Subroutine get_triggers: called CC=3 SCAR=11.0
Pod documented
563  
564 =pod
565  
566 =head2 get_triggers
567  
568 Returns all the triggers as an array or array reference.
569  
570 my @triggers = $schema->get_triggers;
571  
572 =cut
573  
574 43 my $self = shift;
575 61 my @triggers = map { $_->[1] }
576 40 sort { $a->[0] <=> $b->[0] }
577 43 map { [ $_->order, $_ ] } values %{ $self->_triggers };
578  
579 43 ▶ if (@triggers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@triggers) { } 26 17 43
580 26 ▶ return wantarray ? @triggers : \@triggers;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 26 0 26
581 } else {
582 17 $self->error('No triggers');
583 17 return;
584 }
585 }
586  
587 4 ▶sub get_view {
Subroutine get_view: called CC=3 SCAR=11.0
Pod documented
588  
589 =pod
590  
591 =head2 get_view
592  
593 Returns a view by the name provided.
594  
595 my $view = $schema->get_view('foo');
596  
597 =cut
598  
599 4 my $self = shift;
600 4 ▶ my $view_name = shift or return $self->error('No view name');
Branch: 1 branch
BranchTrueFalseTotal
my $view_name = shift() or return $self->error("No view name") 3 1 4
601 return $self->error('View "$view_name" does not exist')
602 3 ▶ unless exists $self->_views->{$view_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->_views->{$view_name} 1 2 3
603 2 return $self->_views->{$view_name};
604 }
605  
606 75 ▶sub get_views {
Subroutine get_views: called CC=3 SCAR=11.0
Pod documented
607  
608 =pod
609  
610 =head2 get_views
611  
612 Returns all the views as an array or array reference.
613  
614 my @views = $schema->get_views;
615  
616 =cut
617  
618 75 my $self = shift;
619 44 my @views = map { $_->[1] }
620 7 sort { $a->[0] <=> $b->[0] }
621 75 map { [ $_->order, $_ ] } values %{ $self->_views };
622  
623 75 ▶ if (@views) {
Branch: 1 branch
BranchTrueFalseTotal
if (@views) { } 39 36 75
624 39 ▶ return wantarray ? @views : \@views;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 39 0 39
625 } else {
626 36 $self->error('No views');
627 36 return;
628 }
629 }
630  
631 0 ▶sub make_natural_joins {
Subroutine make_natural_joins: not called CC=16 SCAR=56.1
Pod documented
632  
633 =pod
634  
635 =head2 make_natural_joins
636  
637 Creates foreign key relationships among like-named fields in different
638 tables. Accepts the following arguments:
639  
640 =over 4
641  
642 =item * join_pk_only
643  
644 A True or False argument which determines whether or not to perform
645 the joins from primary keys to fields of the same name in other tables
646  
647 =item * skip_fields
648  
649 A list of fields to skip in the joins
650  
651 =back
652  
653 $schema->make_natural_joins(
654 join_pk_only => 1,
655 skip_fields => 'name,department_id',
656 );
657  
658 =cut
659  
660 0 my $self = shift;
661 0 my %args = @_;
662 0 ▶ my $join_pk_only = $args{'join_pk_only'} || 0;
Condition0%0/2
$args{'join_pk_only'} || 0
l!l
00
MC/DC0%0/1
$args{'join_pk_only'} || 0
$args{'join_pk_only'}
Truth table0%0/2
A
A= $args{'join_pk_only'}
Aresult
11
00
663 0 my %skip_fields = map { s/^\s+|\s+$//g; $_, 1 } @{ parse_list_arg($args{'skip_fields'}) };
664  
665 0 my (%common_keys, %pk);
666 0 for my $table ($self->get_tables) {
667 0 for my $field ($table->get_fields) {
668 0 ▶ my $field_name = $field->name or next;
Branch: 1 branch
BranchTrueFalseTotal
my $field_name = $field->name or next 0 0 0
669 0 ▶ next if $skip_fields{$field_name};
Branch: 1 branch
BranchTrueFalseTotal
if $skip_fields{$field_name} 0 0 0
670 0 ▶ $pk{$field_name} = 1 if $field->is_primary_key;
Branch: 1 branch
BranchTrueFalseTotal
if $field->is_primary_key 0 0 0
671 0 push @{ $common_keys{$field_name} }, $table->name;
672 }
673 }
674  
675 0 for my $field (keys %common_keys) {
676 0 ▶ next if $join_pk_only and !defined $pk{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $join_pk_only and not defined $pk{$field} 0 0 0
Condition0%0/3
$join_pk_only and not defined $pk{$field}
!ll&&!rl&&r
000
MC/DC0%0/2
$join_pk_only and not defined $pk{$field}
$join_pk_only not defined $pk{$field}
Truth table0%0/3
A and not(B)
A= $join_pk_onlyB= not defined $pk{$field}
ABresult
0X0
100
111
677  
678 0 my @table_names = @{ $common_keys{$field} };
679 0 ▶ next unless scalar @table_names > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @table_names > 1 0 0 0
680  
681 0 for my $i (0 .. $#table_names) {
682 0 ▶ my $table1 = $self->get_table($table_names[$i]) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $table1 = $self->get_table($table_names[$i]) or next 0 0 0
683  
684 0 for my $j (1 .. $#table_names) {
685 0 ▶ my $table2 = $self->get_table($table_names[$j]) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $table2 = $self->get_table($table_names[$j]) or next 0 0 0
686 0 ▶ next if $table1->name eq $table2->name;
Branch: 1 branch
BranchTrueFalseTotal
if $table1->name eq $table2->name 0 0 0
687  
688 0 $table1->add_constraint(
689 type => FOREIGN_KEY,
690 fields => $field,
691 reference_table => $table2->name,
692 reference_fields => $field,
693 );
694 }
695 }
696 }
697  
698 0 return 1;
699 }
700  
701 =head2 name
702  
703 Get or set the schema's name. (optional)
704  
705 my $schema_name = $schema->name('Foo Database');
706  
707 =cut
708  
709 73 has name => (is => 'rw', default => quote_sub(q{ '' }));
710  
711 =pod
712  
713 =head2 translator
714  
715 Get the SQL::Translator instance that instantiated the parser.
716  
717 =cut
718  
719 73 has translator => (is => 'rw', weak_ref => 1);
720  
721 73 1;
722  
723 =pod
724  
725 =head1 AUTHOR
726  
727 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
728  
729 =cut
730  
« blib/lib/SQL/Translator/Parser/DBI/Oracle.pm ↑ Summary blib/lib/SQL/Translator/Producer/Oracle.pm »