| 1 | package Cheater::AST; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 7 | ▶ | use 5.010000; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 7 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 7 | ▶ | use Clone qw(clone); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | sub process_table ($$$$$$$); | ||||||||||||||
| 8 | |||||||||||||||
| 9 | 7 | has 'goals' => (is => 'ro', isa => 'HashRef'); | |||||||||||||
| 10 | 7 | has 'cols' => (is => 'ro', isa => 'HashRef'); | |||||||||||||
| 11 | 7 | has 'deps' => (is => 'ro', isa => 'HashRef'); | |||||||||||||
| 12 | 7 | has 'types' => (is => 'ro', isa => 'HashRef'); | |||||||||||||
| 13 | 7 | has 'tables' => (is => 'ro', isa => 'HashRef'); | |||||||||||||
| 14 | 7 | has 'col_clones' => (is => 'ro', isa => 'HashRef'); | |||||||||||||
| 15 | |||||||||||||||
| 16 | 30 | ▶ | around BUILDARGS => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.3
| |||||||||||||||
| 17 | 30 | my $orig = shift; | |||||||||||||
| 18 | 30 | my $class = shift; | |||||||||||||
| 19 | |||||||||||||||
| 20 | 30 | my $parse_tree = shift; | |||||||||||||
| 21 | |||||||||||||||
| 22 | #warn "BUILDARGS"; | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 30 | my (%tables, %cols, %deps, %goals, %types, %col_clones); | |||||||||||||
| 25 | |||||||||||||||
| 26 | 30 | my %cols_visited; | |||||||||||||
| 27 | |||||||||||||||
| 28 | 30 | %types = ( | |||||||||||||
| 29 | integer => 1, | ||||||||||||||
| 30 | text => 1, | ||||||||||||||
| 31 | serial => 1, | ||||||||||||||
| 32 | real => 1, | ||||||||||||||
| 33 | date => 1, | ||||||||||||||
| 34 | time => 1, | ||||||||||||||
| 35 | datetime => 1, | ||||||||||||||
| 36 | ); | ||||||||||||||
| 37 | |||||||||||||||
| 38 | 30 | my $n; | |||||||||||||
| 39 | 30 | ▶ | do {{ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | 31 | $n = @$parse_tree; | |||||||||||||
| 41 | @$parse_tree = map { | ||||||||||||||
| 42 | 31 | ▶ | $_->[0] eq 'include' ? @{ $_->[1] } : $_ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 43 | } @$parse_tree; | ||||||||||||||
| 44 | }} while (@$parse_tree > $n); | ||||||||||||||
| 45 | |||||||||||||||
| 46 | 30 | for my $stmt (@$parse_tree) { | |||||||||||||
| 47 | #say $stmt->[0]; | ||||||||||||||
| 48 | 65 | my $typ = $stmt->[0]; | |||||||||||||
| 49 | |||||||||||||||
| 50 | 65 | given ($typ) { | |||||||||||||
| 51 | 65 | when ('type') { | |||||||||||||
| 52 | 0 | my $typname = $stmt->[1]; | |||||||||||||
| 53 | 0 | my $def = $stmt->[2]; | |||||||||||||
| 54 | 0 | $types{$typname} = $def; | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | 65 | when ('rows') { | |||||||||||||
| 57 | 32 | my $table = $stmt->[2]; | |||||||||||||
| 58 | 32 | my $rows = $goals{$table}; | |||||||||||||
| 59 | 32 | ▶ | if ($rows) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 60 | 0 | die "table $table was configured to generate $rows rows.\n"; | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | 32 | $rows = $stmt->[1]; | |||||||||||||
| 63 | 32 | $goals{$table} = $rows; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | 33 | when ('table') { | |||||||||||||
| 66 | 32 | my $table_name = $stmt->[1]; | |||||||||||||
| 67 | 32 | my $table = $stmt->[2]; | |||||||||||||
| 68 | 32 | process_table($table_name, $table, \%tables, \%deps, \%cols, \%types, \%col_clones); | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | 1 | when ('table_assign') { | |||||||||||||
| 71 | 1 | my $lhs = $stmt->[1]; | |||||||||||||
| 72 | 1 | my $rhs = $stmt->[2]; | |||||||||||||
| 73 | |||||||||||||||
| 74 | 1 | my $table = $tables{$rhs}; | |||||||||||||
| 75 | |||||||||||||||
| 76 | 1 | ▶ | if (!defined $table) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 0 | die "ERROR: $lhs = $rhs: Table $rhs not defined yet.\n"; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | 1 | my $new_table = clone($table); | |||||||||||||
| 81 | |||||||||||||||
| 82 | 1 | process_table($lhs, $new_table, \%tables, \%deps, \%cols, \%types, \%col_clones); | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | 0 | default { | |||||||||||||
| 85 | 0 | warn "WARN: Unknown statement type: $typ\n"; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | } | ||||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | return { | ||||||||||||||
| 91 | 30 | tables => \%tables, | |||||||||||||
| 92 | cols => \%cols, | ||||||||||||||
| 93 | deps => \%deps, | ||||||||||||||
| 94 | goals => \%goals, | ||||||||||||||
| 95 | types => \%types, | ||||||||||||||
| 96 | col_clones => \%col_clones, | ||||||||||||||
| 97 | }; | ||||||||||||||
| 98 | 7 | }; | |||||||||||||
| 99 | |||||||||||||||
| 100 | 33 | ▶ | sub process_table ($$$$$$$) { | ||||||||||||
|
Subroutine
process_table: called CC=6 SCAR=18.4
Pod
undocumented
| |||||||||||||||
| 101 | 33 | my ($table_name, $table, $tables, $deps, $cols, $types, $col_clones) = @_; | |||||||||||||
| 102 | |||||||||||||||
| 103 | 33 | $tables->{$table_name} = $table; | |||||||||||||
| 104 | 33 | for my $col (@$table) { | |||||||||||||
| 105 | #say "col: ", $col->[0]; | ||||||||||||||
| 106 | 36 | my $name = $col->[0]; | |||||||||||||
| 107 | 36 | my $type = $col->[1]; | |||||||||||||
| 108 | |||||||||||||||
| 109 | 36 | my ($domain, $attrs); | |||||||||||||
| 110 | |||||||||||||||
| 111 | 36 | ▶ | if ($type eq 'refs') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 112 | 0 | my $target = $col->[2]; | |||||||||||||
| 113 | 0 | $deps->{"$table_name.$name"} = | |||||||||||||
| 114 | $target->[0] . '.' . $target->[1]; | ||||||||||||||
| 115 | 0 | $attrs = $col->[3]; | |||||||||||||
| 116 | } elsif ($type eq '=') { | ||||||||||||||
| 117 | 1 | my $target = $col->[2]; | |||||||||||||
| 118 | 1 | $deps->{"$table_name.$name"} = | |||||||||||||
| 119 | "$table_name.$target"; | ||||||||||||||
| 120 | 1 | $col_clones->{"$table_name.$name"} = "$table_name.$target"; | |||||||||||||
| 121 | } else { | ||||||||||||||
| 122 | 35 | ▶ | if (! $types->{$type}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | 0 | die "column type $type not defined.\n"; | |||||||||||||
| 124 | } | ||||||||||||||
| 125 | |||||||||||||||
| 126 | 35 | $domain = $col->[2]; | |||||||||||||
| 127 | 35 | ▶ | if (@$domain == 0) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 128 | 22 | $domain = undef; | |||||||||||||
| 129 | } else { | ||||||||||||||
| 130 | 13 | $domain = $domain->[0]; | |||||||||||||
| 131 | } | ||||||||||||||
| 132 | |||||||||||||||
| 133 | 35 | $attrs = $col->[3]; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | 36 | $cols->{"$table_name.$name"} = { | |||||||||||||
| 137 | type => $type, | ||||||||||||||
| 138 | domain => $domain, | ||||||||||||||
| 139 | attrs => $attrs, | ||||||||||||||
| 140 | }; | ||||||||||||||
| 141 | } | ||||||||||||||
| 142 | } | ||||||||||||||
| 143 | |||||||||||||||
| 144 | 7 | 1; | |||||||||||||