| 1 | package DBIx::QuickORM::Source::Table; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 260 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 260 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 260 | our $VERSION = '0.000033'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 260 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 260 | ▶ | use Object::HashBase qw{ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | <table | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | <db_map | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | +field_records | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 260 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 260 | ▶ | use Role::Tiny::With qw/with/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 260 | with 'DBIx::QuickORM::Role::Source'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | DBIx::QuickORM::Source::Table - A table as a source. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | A table in the map, as L<DBIx::QuickORM::Role::Source> describes. It holds | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | the table name and the map, and answers from the map. It is immutable: the ORM | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | builds one per table and C<< $orm->handle($table) >> reuses it. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | my $source = DBIx::QuickORM::Source::Table->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | table => 'users', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | db_map => $orm->db_map, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | my $h = $orm->handle($source); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | =item $name = $source->table | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | Required. Croaks when the map has no such table. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | =item $map = $source->db_map | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | Required: the L<DBIx::QuickORM::Map>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 192 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=5 SCAR=21.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: not called CC=5 SCAR=21.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my ($table, $map) = @{$self}{+TABLE, +DB_MAP}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | croak "A 'db_map' is required" unless $map; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | croak "A 'table' is required" unless defined($table) && length($table); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
defined $table and length $table
defined $table and length $table
MC/DC50%1/2
defined $table and length $table
defined $table
length $table
MC/DC0%0/2
defined $table and length $table
defined $table
length $table
Truth table66%2/3
A and B
A= defined $tableB= length $table
Truth table0%0/3
A and B
A= defined $tableB= length $table
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | croak "The table '$table' is not in the database map" unless $map->table($table); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | =head1 PUBLIC METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | =item \@fields = $source->pk | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | The table's primary key fields from the map, or undef when it has none. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | =item () = $source->query_sql | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | An empty list: a table source has no SQL of its own to run. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 306 | ▶ | sub pk { $_[0]->{+DB_MAP}->pk($_[0]->{+TABLE}) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pk: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | ▶ | sub query_sql { () } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
query_sql: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | =item \%fields = $source->field_records | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | The table's fields in the map, by name and qualified as C<table.field>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | Built on first use, then the same shared hash every call: do not change it. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 787 | ▶ | sub field_records { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
field_records: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 787 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 787 | ▶ | return $self->{+FIELD_RECORDS} //= do { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"field_records"} //= do {
my $table = $self->{"table"};
my $fields = $self->{"db_map"}->table($table)->{"fields"};
+{%$fields, map({"$table.$_", $fields->{$_};} keys %$fields)}
}
MC/DC0%0/2
$self->{"field_records"} //= do {
my $table = $self->{"table"};
my $fields = $self->{"db_map"}->table($table)->{"fields"};
+{%$fields, map({"$table.$_", $fields->{$_};} keys %$fields)}
}
$self->{"field_records"}
do {
my $table = $self->{"table"};
my $fields = $self->{"db_map"}->table($table)->{"fields"};
+{%$fields, map({"$table.$_", $fields->{$_};} keys %$fields)}
}
Truth table66%2/3
A //= B
A= $self->{"field_records"}B= do {
my $table = $self->{"table"};
my $fields = $self->{"db_map"}->table($table)->{"fields"};
+{%$fields, map({"$table.$_", $fields->{$_};} keys %$fields)}
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 114 | my $table = $self->{+TABLE}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 114 | my $fields = $self->{+DB_MAP}->table($table)->{fields}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 114 | +{%$fields, map { ("$table.$_" => $fields->{$_}) } keys %$fields}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | =item \@fields = $source->select_fields($given) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | The fields to select. With no C<$given>, the table's C<default_fields>, or | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | every field sorted. For an arrayref, those fields with the primary key added, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | without duplicates. Computed fields (a hashref) come back as they are. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | Croaks for a name in the arrayref that is not a field of the table. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 338 | ▶ | sub select_fields { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
select_fields: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 338 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 338 | my ($given) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 338 | ▶ | return $given if ref($given) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 287 | my $t = $self->{+DB_MAP}->table($self->{+TABLE}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 287 | ▶ | if ($given) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 61 | ▶ | $t->{fields}->{$_} or croak "The table '$self->{+TABLE}' has no field '$_'" for @$given; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 60 | my %seen; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 60 | ▶ | return [grep { !$seen{$_}++ } @$given, @{$self->pk // []}]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->pk // []
MC/DC0%0/1
$self->pk // []
$self->pk
Truth table50%1/2
A
A= $self->pk
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 226 | ▶ | return $t->{default_fields} // [sort keys %{$t->{fields}}]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$t->{"default_fields"} // [sort(keys %{$$t{"fields"};})]
MC/DC100%1/1
$t->{"default_fields"} // [sort(keys %{$$t{"fields"};})]
$t->{"default_fields"}
Truth table100%2/2
A
A= $t->{"default_fields"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | =item ($sql, \@binds) = $source->from_sql($dialect) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | The quoted table name, with no binds. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 409 | ▶ | sub from_sql { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_sql: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 409 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 409 | my ($dialect) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 409 | return ($dialect->quote_identifier($self->{+TABLE}), []); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | =item (\@rows, \&build) = $source->results($orm, \@raw, $ingest, %opts) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | The rows, built as the ORM builds rows of the table, with the handle's | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | C<row_class> when given. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | With a primary key and C<$ingest>, each row is ingested into the Connection's | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | lookup table and the canon row data kept. Otherwise the rows are kept as | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | fetched. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 244 | ▶ | sub results { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
results: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 244 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 244 | my ($orm, $raw, $ingest, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 244 | my $table = $self->{+TABLE}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 244 | my $pk = $self->pk; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 244 | my $rows = $raw; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 244 | ▶ | if ($ingest && $pk && @$pk) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$ingest and $pk and @$pk
$ingest and $pk
MC/DC66%2/3
$ingest and $pk and @$pk
$ingest
$pk
@$pk
Truth table75%3/4
A and B and C
A= $ingestB= $pkC= @$pk
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 202 | my $lookup = $orm->connection->lookup; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 202 | $rows = $lookup->ingest_many($table, $raw); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 244 | return ($rows, $orm->_row_builder($table, %opts)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 260 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||