| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Aniki::Schema::Table; | 
| 2 | 29 |  |  | 29 |  | 457 | use 5.014002; | 
|  | 29 |  |  |  |  | 101 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 29 |  |  | 29 |  | 141 | use namespace::autoclean; | 
|  | 29 |  |  |  |  | 56 |  | 
|  | 29 |  |  |  |  | 168 |  | 
| 5 | 29 |  |  | 29 |  | 1976 | use Mouse v2.4.5; | 
|  | 29 |  |  |  |  | 358 |  | 
|  | 29 |  |  |  |  | 184 |  | 
| 6 | 29 |  |  | 29 |  | 9689 | use Carp qw/croak/; | 
|  | 29 |  |  |  |  | 70 |  | 
|  | 29 |  |  |  |  | 1226 |  | 
| 7 | 29 |  |  | 29 |  | 9573 | use Aniki::Schema::Relationships; | 
|  | 29 |  |  |  |  | 2823 |  | 
|  | 29 |  |  |  |  | 957 |  | 
| 8 | 29 |  |  | 29 |  | 12396 | use Aniki::Schema::Table::Field; | 
|  | 29 |  |  |  |  | 2495 |  | 
|  | 29 |  |  |  |  | 969 |  | 
| 9 | 29 |  |  | 29 |  | 10731 | use Aniki::Schema::Table::PrimaryKey; | 
|  | 29 |  |  |  |  | 2385 |  | 
|  | 29 |  |  |  |  | 1102 |  | 
| 10 | 29 |  |  | 29 |  | 212 | use SQL::Translator::Schema::Constants; | 
|  | 29 |  |  |  |  | 63 |  | 
|  | 29 |  |  |  |  | 24000 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | has _schema => ( | 
| 13 |  |  |  |  |  |  | is       => 'ro', | 
| 14 |  |  |  |  |  |  | required => 1, | 
| 15 |  |  |  |  |  |  | weak_ref => 1, | 
| 16 |  |  |  |  |  |  | ); | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | has _table => ( | 
| 19 |  |  |  |  |  |  | is       => 'ro', | 
| 20 |  |  |  |  |  |  | required => 1, | 
| 21 |  |  |  |  |  |  | ); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | has name => ( | 
| 24 |  |  |  |  |  |  | is      => 'ro', | 
| 25 |  |  |  |  |  |  | default => sub { shift->_table->name }, | 
| 26 |  |  |  |  |  |  | ); | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | has relationships => ( | 
| 29 |  |  |  |  |  |  | is      => 'ro', | 
| 30 |  |  |  |  |  |  | default => \&_setup_relationships, | 
| 31 |  |  |  |  |  |  | ); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | has primary_key => ( | 
| 34 |  |  |  |  |  |  | is      => 'ro', | 
| 35 |  |  |  |  |  |  | default => sub { | 
| 36 |  |  |  |  |  |  | my $self = shift; | 
| 37 |  |  |  |  |  |  | if (my $primary_key = $self->_table->primary_key) { | 
| 38 |  |  |  |  |  |  | return Aniki::Schema::Table::PrimaryKey->new($primary_key); | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  | return undef; | 
| 41 |  |  |  |  |  |  | }, | 
| 42 |  |  |  |  |  |  | ); | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | has _fields_cache => ( | 
| 45 |  |  |  |  |  |  | is      => 'ro', | 
| 46 |  |  |  |  |  |  | default => sub { | 
| 47 |  |  |  |  |  |  | my $self = shift; | 
| 48 |  |  |  |  |  |  | return [ | 
| 49 |  |  |  |  |  |  | map { Aniki::Schema::Table::Field->new($_) } $self->_table->get_fields | 
| 50 |  |  |  |  |  |  | ] | 
| 51 |  |  |  |  |  |  | }, | 
| 52 |  |  |  |  |  |  | ); | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | has _field_names => ( | 
| 55 |  |  |  |  |  |  | is      => 'ro', | 
| 56 |  |  |  |  |  |  | default => sub { | 
| 57 |  |  |  |  |  |  | my $self = shift; | 
| 58 |  |  |  |  |  |  | return [map { $_->name } @{ $self->_fields_cache }]; | 
| 59 |  |  |  |  |  |  | }, | 
| 60 |  |  |  |  |  |  | ); | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | has _fields_map_cache => ( | 
| 63 |  |  |  |  |  |  | is      => 'ro', | 
| 64 |  |  |  |  |  |  | default => sub { | 
| 65 |  |  |  |  |  |  | my $self = shift; | 
| 66 |  |  |  |  |  |  | return { | 
| 67 |  |  |  |  |  |  | map { $_->name => $_ } @{ $self->_fields_cache } | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | }, | 
| 70 |  |  |  |  |  |  | ); | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | sub BUILDARGS { | 
| 73 | 89 |  |  | 89 | 1 | 13227 | my ($class, $table, $schema) = @_; | 
| 74 | 89 |  |  |  |  | 1167 | return $class->SUPER::BUILDARGS(_table => $table, _schema => $schema); | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 8 |  |  | 8 | 0 | 18 | sub get_fields { @{ shift->_fields_cache } } | 
|  | 8 |  |  |  |  | 59 |  | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 78 | 100 |  | 78 | 0 | 225 | sub field_names { wantarray ? @{ shift->_field_names } : [@{ shift->_field_names }] } | 
|  | 4 |  |  |  |  | 36 |  | 
|  | 74 |  |  |  |  | 467 |  | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | sub get_field { | 
| 82 | 131 |  |  | 131 | 0 | 326 | my ($self, $name) = @_; | 
| 83 | 131 | 50 |  |  |  | 560 | return unless exists $self->_fields_map_cache->{$name}; | 
| 84 | 131 |  |  |  |  | 624 | return $self->_fields_map_cache->{$name} | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 248 |  |  | 248 | 0 | 1006 | sub get_relationships { shift->relationships } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | sub _setup_relationships { | 
| 90 | 89 |  |  | 89 |  | 9817 | my $self = shift; | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 89 |  |  |  |  | 807 | my @constraints = grep { $_->type eq FOREIGN_KEY } $self->get_constraints; | 
|  | 94 |  |  |  |  | 5223 |  | 
| 93 | 89 |  |  |  |  | 10994 | for my $table ($self->_schema->context->schema->get_tables) { | 
| 94 | 265 |  |  |  |  | 31867 | for my $constraint ($table->get_constraints) { | 
| 95 | 279 | 100 |  |  |  | 12724 | next if $constraint->type            ne FOREIGN_KEY; | 
| 96 | 9 | 100 |  |  |  | 968 | next if $constraint->reference_table ne $self->name; | 
| 97 | 3 |  |  |  |  | 11 | push @constraints => $constraint; | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 89 |  |  |  |  | 9515 | my $relationships = Aniki::Schema::Relationships->new(schema => $self->_schema, table => $self); | 
| 102 | 89 |  |  |  |  | 334 | for my $constraint (@constraints) { | 
| 103 | 6 |  |  |  |  | 28 | $relationships->add_by_constraint($constraint); | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 89 | 100 |  |  |  | 1173 | if ($self->_schema->schema_class->can('relationship_rules')) { | 
| 107 | 86 |  |  |  |  | 528 | my $rules = $self->_schema->schema_class->relationship_rules; | 
| 108 | 86 |  |  |  |  | 277 | for my $rule (@$rules) { | 
| 109 | 336 | 100 |  |  |  | 25209 | next if $rule->{src_table_name} ne $self->_table->name; | 
| 110 | 112 |  |  |  |  | 11697 | $relationships->add(%$rule); | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 89 |  |  |  |  | 6538 | return $relationships; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | our $AUTOLOAD; | 
| 118 |  |  |  |  |  |  | sub AUTOLOAD { | 
| 119 | 90 |  |  | 90 |  | 256 | my $self = shift; | 
| 120 | 90 |  |  |  |  | 585 | my $method = $AUTOLOAD =~ s/^.*://r; | 
| 121 | 90 | 50 |  |  |  | 729 | if ($self->_table->can($method)) { | 
| 122 | 90 |  |  |  |  | 489 | return $self->_table->$method(@_); | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 | 0 |  |  |  |  |  | my $class = ref $self; | 
| 126 | 0 |  |  |  |  |  | croak qq{Can't locate object method "$method" via package "$class"}; | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable(); | 
| 130 |  |  |  |  |  |  | __END__ |