| 1 | package Mojolicious::Plugin::Tables::Model; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use base qw/DBIx::Class::Schema/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use DBIx::Class::Schema::Loader::Dynamic; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | __PACKAGE__->mk_group_accessors(inherited => qw/log connect_info model/); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | sub setup { | ||||||||||||||||||||||||||
|
Subroutine
setup: called CC=4 SCAR=14.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 3 | my ($class, $conf) = @_; | |||||||||||||||||||||||||||
| 13 | 3 | ▶ | if (my $connect_info = $conf->{connect_info}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 3 | $class->connect_info($connect_info) | |||||||||||||||||||||||||||
| 15 | } else { | ||||||||||||||||||||||||||||
| 16 | 0 | ▶ | die "Provide connect_info either as a config value or an override" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | unless $class->connect_info | ||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # to return a schema object-ref here say 'connect' instead of 'connection'. | ||||||||||||||||||||||||||||
| 21 | 3 | my $schema = $class->connection(@{$class->connect_info}); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | DBIx::Class::Schema::Loader::Dynamic->new( | ||||||||||||||||||||||||||||
| 24 | left_base_classes => $class->row_base, | ||||||||||||||||||||||||||||
| 25 | rel_name_map => $class->rel_name_map, | ||||||||||||||||||||||||||||
| 26 | 27 | ▶ | custom_column_info => sub { $class->custom_column_info(@_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | naming => 'v8', | ||||||||||||||||||||||||||||
| 28 | use_namespaces => 0, | ||||||||||||||||||||||||||||
| 29 | schema => $schema, | ||||||||||||||||||||||||||||
| 30 | 3 | ▶ | %{$conf->{loader_opts}||{}}, | ||||||||||||||||||||||||||
|
Condition50%1/2
$conf->{'loader_opts'} || {}
MC/DC0%0/1
$conf->{'loader_opts'} || {}
$conf->{'loader_opts'}
Truth table50%1/2
A
A= $conf->{'loader_opts'}
| |||||||||||||||||||||||||||||
| 31 | )->load; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 3 | $schema->model($schema->_model); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | return $schema; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 3 | ▶ | sub row_base { 'Mojolicious::Plugin::Tables::Model::Row' } | ||||||||||||||||||||||||||
|
Subroutine
row_base: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 48 | ▶ | sub glossary { +{ id => 'Identifier' } } | ||||||||||||||||||||||||||
|
Subroutine
glossary: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 27 | ▶ | sub input_attrs { +{ name => { size=>80 } } } | ||||||||||||||||||||||||||
|
Subroutine
input_attrs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 48 | ▶ | sub make_label { | ||||||||||||||||||||||||||
|
Subroutine
make_label: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 48 | my $self = shift; | |||||||||||||||||||||||||||
| 46 | 48 | my $name = shift; | |||||||||||||||||||||||||||
| 47 | 48 | my @label = split '_', $name; | |||||||||||||||||||||||||||
| 48 | 48 | for (@label) { | |||||||||||||||||||||||||||
| 49 | 48 | ▶ | $_ = $self->glossary->{$_}, next if $self->glossary->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 48 | $_ = ucfirst | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 48 | join(' ', @label) | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 27 | ▶ | sub custom_column_info { | ||||||||||||||||||||||||||
|
Subroutine
custom_column_info: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 27 | my ($class, $table, $column, $column_info) = @_; | |||||||||||||||||||||||||||
| 57 | 27 | my $info = { label => $class->make_label($column) }; | |||||||||||||||||||||||||||
| 58 | 27 | my $attrs1; | |||||||||||||||||||||||||||
| 59 | 27 | for ($column_info->{data_type}) { | |||||||||||||||||||||||||||
| 60 | 27 | ▶ | $attrs1 = | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 61 | /numeric|integer/ ? {type=>'number'} : | ||||||||||||||||||||||||||||
| 62 | /timestamp/ ? {type=>'datetime-local'} : | ||||||||||||||||||||||||||||
| 63 | /date|time/ ? {type=>$_} : | ||||||||||||||||||||||||||||
| 64 | {}; | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 27 | ▶ | my $attrs2 = $class->input_attrs->{$column} || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$class->input_attrs->{$column} || {}
MC/DC100%1/1
$class->input_attrs->{$column} || {}
$class->input_attrs->{$column}
Truth table100%2/2
A
A= $class->input_attrs->{$column}
| |||||||||||||||||||||||||||||
| 67 | 27 | ▶ | $info->{input_attrs} = {%$attrs1, %$attrs2} if keys(%$attrs1) || keys(%$attrs2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
keys %$attrs1 or keys %$attrs2
MC/DC100%2/2
keys %$attrs1 or keys %$attrs2
keys %$attrs1
keys %$attrs2
Truth table100%3/3
A or B
A= keys %$attrs1B= keys %$attrs2
| |||||||||||||||||||||||||||||
| 68 | 27 | $info | |||||||||||||||||||||||||||
| 69 | }; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 3 | ▶ | sub rel_name_map { +{} } | ||||||||||||||||||||||||||
|
Subroutine
rel_name_map: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 3 | ▶ | sub _model { | ||||||||||||||||||||||||||
|
Subroutine
_model: called CC=15 SCAR=28.4
| |||||||||||||||||||||||||||||
| 74 | 3 | my $schema = shift; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 3 | my @tablist = (); | |||||||||||||||||||||||||||
| 77 | 3 | my %bytable = (); | |||||||||||||||||||||||||||
| 78 | #my $log = $schema->log; | ||||||||||||||||||||||||||||
| 79 | #$log->debug("$schema is building its model"); | ||||||||||||||||||||||||||||
| 80 | 3 | for my $source (sort $schema->sources) { | |||||||||||||||||||||||||||
| 81 | 9 | my $s = $schema->source($source); | |||||||||||||||||||||||||||
| 82 | 9 | my @has_a; | |||||||||||||||||||||||||||
| 83 | my %has_many; | ||||||||||||||||||||||||||||
| 84 | 9 | for my $rel ($s->relationships) { | |||||||||||||||||||||||||||
| 85 | 12 | my $info = $s->relationship_info($rel); | |||||||||||||||||||||||||||
| 86 | 12 | my $ftable = $info->{class}->table; | |||||||||||||||||||||||||||
| 87 | 12 | my $attrs = $info->{attrs}; | |||||||||||||||||||||||||||
| 88 | 12 | my $card = $attrs->{accessor}; | |||||||||||||||||||||||||||
| 89 | 12 | ▶ | if ($card eq 'single') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 90 | 6 | my $fks = $attrs->{fk_columns}; | |||||||||||||||||||||||||||
| 91 | 6 | my @fks = keys %$fks; | |||||||||||||||||||||||||||
| 92 | 6 | ▶ | push @has_a, { fkey=>$fks[0], parent=>$rel, label=>$schema->make_label($rel), ptable=>$ftable } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | if @fks == 1 | ||||||||||||||||||||||||||||
| 94 | } elsif ($card eq 'filter') { | ||||||||||||||||||||||||||||
| 95 | 0 | my @ffkeys = keys %{$info->{cond}}; | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | if (@ffkeys == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | (my $cfkey = $ffkeys[0]) =~ s/^foreign\.//; | |||||||||||||||||||||||||||
| 98 | 0 | push @has_a, { fkey=>$cfkey, parent=>$rel, label=>$schema->make_label($rel), ptable=>$ftable } | |||||||||||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||||||||||
| 100 | 0 | warn __PACKAGE__." model: $source: $rel: multi-barrelled M-1 keys not supported\n" | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } elsif ($card eq 'multi') { | ||||||||||||||||||||||||||||
| 103 | 6 | my $fsource_name = $info->{source}; | |||||||||||||||||||||||||||
| 104 | 6 | my $fsource = $schema->source($fsource_name); | |||||||||||||||||||||||||||
| 105 | 6 | my $fpkey = join(',', $fsource->primary_columns); | |||||||||||||||||||||||||||
| 106 | 6 | my @ffkeys = keys %{$info->{cond}}; | |||||||||||||||||||||||||||
| 107 | 6 | ▶ | if (@ffkeys == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 6 | (my $cfkey = $ffkeys[0]) =~ s/^foreign\.//; | |||||||||||||||||||||||||||
| 109 | 6 | $has_many{$rel} = {ctable=>$ftable, cpkey=>$fpkey, cfkey=>$cfkey, label=>$schema->make_label($rel)}; | |||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||
| 111 | 0 | warn __PACKAGE__." model: $source: $rel: multi-barrelled 1-M keys not supported\n" | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||||||||||
| 114 | 0 | warn __PACKAGE__." model: $source: $rel: strange cardinality: $card\n"; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | my %bycolumn = map { | ||||||||||||||||||||||||||||
| 118 | 9 | my %info = %{$s->column_info($_)}; | |||||||||||||||||||||||||||
| 119 | 27 | delete $info{name}; | |||||||||||||||||||||||||||
| 120 | 27 | ▶ | /^_/ && delete $info{$_} for keys %info; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 27 | ( $_ => \%info ) | |||||||||||||||||||||||||||
| 122 | } $s->columns; | ||||||||||||||||||||||||||||
| 123 | 9 | for (@has_a) { | |||||||||||||||||||||||||||
| 124 | 6 | my $fkey = $_->{fkey}; | |||||||||||||||||||||||||||
| 125 | 6 | my $parent = delete $_->{parent}; | |||||||||||||||||||||||||||
| 126 | 6 | ▶ | $bycolumn{$fkey}->{parent} = $parent if $bycolumn{$fkey}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 6 | $bycolumn{$parent} = $_; # gets {fkey=>, label=>, ptable=>,} | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 9 | my $pkeys = [$s->primary_columns]; | |||||||||||||||||||||||||||
| 130 | 9 | my $pknum = 0; | |||||||||||||||||||||||||||
| 131 | 9 | for (@$pkeys) { | |||||||||||||||||||||||||||
| 132 | 9 | $bycolumn{$_}{is_primary_key} = ++$pknum | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 9 | ▶ | my @columns = map { $_, $bycolumn{$_}{parent}? ($bycolumn{$_}{parent}): () } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | $s->columns; | ||||||||||||||||||||||||||||
| 136 | 9 | my $label = $schema->make_label($s->name); | |||||||||||||||||||||||||||
| 137 | 9 | my $tabinfo = { | |||||||||||||||||||||||||||
| 138 | source => $source, | ||||||||||||||||||||||||||||
| 139 | columns => \@columns, | ||||||||||||||||||||||||||||
| 140 | bycolumn => \%bycolumn, | ||||||||||||||||||||||||||||
| 141 | has_many => \%has_many, | ||||||||||||||||||||||||||||
| 142 | label => $label, | ||||||||||||||||||||||||||||
| 143 | pkeys => $pkeys, | ||||||||||||||||||||||||||||
| 144 | }; | ||||||||||||||||||||||||||||
| 145 | 9 | push @tablist, $s->name; | |||||||||||||||||||||||||||
| 146 | 9 | $bytable{$s->name} = $tabinfo; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 3 | return {schema=>$schema, tablist=>\@tablist, bytable=>\%bytable}; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 3 | 1; | |||||||||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||||||||||