| 1 | package Data::Generator::FromDDL::Director; | ||||||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 6 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 6 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 6 | ▶ | use List::MoreUtils qw(any); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 6 | ▶ | use Class::Accessor::Lite ( | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 6 | new => 1, | |||||||||||||
| 8 | rw => [qw(builder_class schema include exclude)], | ||||||||||||||
| 9 | 6 | ); | |||||||||||||
| 10 | |||||||||||||||
| 11 | 6 | ▶ | use Data::Generator::FromDDL::Formatter; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 20 | ▶ | sub generate { | ||||||||||||
|
Subroutine
generate: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||
| 14 | 20 | my ($self, $num, $format, $pretty, $bytes_per_sql) = @_; | |||||||||||||
| 15 | 20 | my @tables = $self->_get_right_order_tables; | |||||||||||||
| 16 | 20 | ▶ | croak("Not all tables found: You must specify all tables schema.\n") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 17 | unless @tables; | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 20 | my @recordsets; | |||||||||||||
| 20 | 20 | for my $table (@tables) { | |||||||||||||
| 21 | 21 | my $builder = $self->builder_class->new({ | |||||||||||||
| 22 | table => $table, | ||||||||||||||
| 23 | recordsets => \@recordsets, | ||||||||||||||
| 24 | }); | ||||||||||||||
| 25 | 21 | my $n = $self->_get_num_for_table($num, $table->name); | |||||||||||||
| 26 | |||||||||||||||
| 27 | 21 | ▶ | if ($n) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 28 | 21 | push @recordsets, $builder->generate($n); | |||||||||||||
| 29 | } | ||||||||||||||
| 30 | } | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 20 | return @recordsets; | |||||||||||||
| 33 | } | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 20 | ▶ | sub flush { | ||||||||||||
|
Subroutine
flush: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 36 | 20 | my ($self, $recordsets, $out_fh, $format, $pretty, $bytes_per_sql) = @_; | |||||||||||||
| 37 | |||||||||||||||
| 38 | 20 | my $formatter = Data::Generator::FromDDL::Formatter->new( | |||||||||||||
| 39 | format => $format, | ||||||||||||||
| 40 | pretty => $pretty, | ||||||||||||||
| 41 | bytes_per_sql => $bytes_per_sql, | ||||||||||||||
| 42 | ); | ||||||||||||||
| 43 | |||||||||||||||
| 44 | 20 | for my $recordset (@$recordsets) { | |||||||||||||
| 45 | 21 | ▶ | $recordset->iterate_through_chunks(sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 46 | 21 | my ($table, $fields, $rows) = @_; | |||||||||||||
| 47 | 21 | my $sql = $formatter->to_string($table, $fields, $rows); | |||||||||||||
| 48 | 21 | print $out_fh $sql . "\n"; | |||||||||||||
| 49 | 21 | }); | |||||||||||||
| 50 | } | ||||||||||||||
| 51 | } | ||||||||||||||
| 52 | |||||||||||||||
| 53 | 21 | ▶ | sub _get_num_for_table { | ||||||||||||
|
Subroutine
_get_num_for_table: called CC=3 SCAR=11.0
| |||||||||||||||
| 54 | 21 | my ($self, $num, $table_name) = @_; | |||||||||||||
| 55 | 21 | ▶ | if (ref $num eq 'HASH') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | return exists $num->{tables}{$table_name} | ||||||||||||||
| 57 | ? $num->{tables}{$table_name} | ||||||||||||||
| 58 | 1 | ▶ | : $num->{all}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | } else { | ||||||||||||||
| 60 | 20 | return $num; | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 20 | ▶ | sub _get_right_order_tables { | ||||||||||||
|
Subroutine
_get_right_order_tables: called CC=1 SCAR=0.0
| |||||||||||||||
| 65 | 20 | my $self = shift; | |||||||||||||
| 66 | 20 | my @tables = $self->schema->get_tables; | |||||||||||||
| 67 | 20 | my @filtered = $self->_filter_tables(\@tables); | |||||||||||||
| 68 | |||||||||||||||
| 69 | 20 | return $self->_resolve_data_generation_order(\@filtered); | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | 20 | ▶ | sub _filter_tables { | ||||||||||||
|
Subroutine
_filter_tables: called CC=7 SCAR=20.6
| |||||||||||||||
| 73 | 20 | my ($self, $tables) = @_; | |||||||||||||
| 74 | 20 | my @include = @{$self->include}; | |||||||||||||
| 75 | 20 | my @exclude = @{$self->exclude}; | |||||||||||||
| 76 | |||||||||||||||
| 77 | 20 | my @filtered; | |||||||||||||
| 78 | 20 | ▶ | if (scalar(@include)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 79 | 1 | for my $t (@$tables) { | |||||||||||||
| 80 | 2 | ▶ | push @filtered, $t if any { $t->name eq $_ } @include; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||
| 81 | } | ||||||||||||||
| 82 | } elsif (scalar(@exclude)) { | ||||||||||||||
| 83 | 0 | for my $t (@$tables) { | |||||||||||||
| 84 | 0 | ▶ | push @filtered, $t unless any { $t->name eq $_ } @exclude; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||
| 85 | } | ||||||||||||||
| 86 | } else { | ||||||||||||||
| 87 | 19 | @filtered = @$tables; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | 20 | return @filtered; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | 20 | ▶ | sub _resolve_data_generation_order { | ||||||||||||
|
Subroutine
_resolve_data_generation_order: called CC=3 SCAR=11.1
| |||||||||||||||
| 94 | 20 | my ($self, $tables) = @_; | |||||||||||||
| 95 | 20 | my @unresolved = @$tables; | |||||||||||||
| 96 | 20 | my @resolved; | |||||||||||||
| 97 | |||||||||||||||
| 98 | 20 | ▶ | while (my @before = @unresolved) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | 21 | $self->_resolve_inter_table_reference(\@unresolved, \@resolved); | |||||||||||||
| 100 | |||||||||||||||
| 101 | # can't resolve inter table reference anymore. | ||||||||||||||
| 102 | 21 | ▶ | if (@before == @unresolved) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 0 | croak("Not all tables found: You must specify all tables schema.\n"); | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | } | ||||||||||||||
| 106 | |||||||||||||||
| 107 | 20 | return @resolved; | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | |||||||||||||||
| 110 | 21 | ▶ | sub _resolve_inter_table_reference { | ||||||||||||
|
Subroutine
_resolve_inter_table_reference: called CC=3 SCAR=11.0
| |||||||||||||||
| 111 | 21 | my ($self, $unresolved, $resolved) = @_; | |||||||||||||
| 112 | 21 | my @old_unresolved = @$unresolved; | |||||||||||||
| 113 | 21 | splice @$unresolved, 0; | |||||||||||||
| 114 | |||||||||||||||
| 115 | 21 | for my $ur (@old_unresolved) { | |||||||||||||
| 116 | 22 | ▶ | if ($self->_exist_all_foreign_key_reference($ur, $resolved)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 117 | 21 | push @$resolved, $ur; | |||||||||||||
| 118 | } else { | ||||||||||||||
| 119 | 1 | push @$unresolved, $ur; | |||||||||||||
| 120 | } | ||||||||||||||
| 121 | } | ||||||||||||||
| 122 | } | ||||||||||||||
| 123 | |||||||||||||||
| 124 | 22 | ▶ | sub _exist_all_foreign_key_reference { | ||||||||||||
|
Subroutine
_exist_all_foreign_key_reference: called CC=3 SCAR=11.0
| |||||||||||||||
| 125 | 22 | my ($self, $table, $others) = @_; | |||||||||||||
| 126 | 22 | my @fk_constraints = grep { uc($_->type) eq 'FOREIGN KEY' } | |||||||||||||
| 127 | $table->get_constraints; | ||||||||||||||
| 128 | 22 | for my $constraint (@fk_constraints) { | |||||||||||||
| 129 | 2 | my $ref_table = $constraint->reference_table; | |||||||||||||
| 130 | 2 | ▶ | unless (grep { $ref_table eq $_->name } @$others) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | 1 | return undef; | |||||||||||||
| 132 | } | ||||||||||||||
| 133 | } | ||||||||||||||
| 134 | |||||||||||||||
| 135 | 21 | return 1; | |||||||||||||
| 136 | } | ||||||||||||||
| 137 | |||||||||||||||
| 138 | 6 | 1; | |||||||||||||