| 1 | package DBIx::QuickORM::Map; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 265 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 265 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 265 | our $VERSION = '0.000033'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 265 | ▶ | use Carp qw/croak confess/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 265 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 265 | ▶ | use Object::HashBase qw{ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | <tables | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 265 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | DBIx::QuickORM::Map - The database map: tables, fields, and keys as plain data. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | One object over plain nested data (strings, hashes, and arrays only, plus a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | field's C<format> code reference), keyed by table name. The ORM builds it in | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | its constructor. After that, treat the map and everything its methods return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | as read-only. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | users => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | fields => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | id => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | affinity => 'numeric', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | db_type => 'integer', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | type => 'integer', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | name => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | affinity => 'string', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | db_type => 'varchar(255)', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | type => 'varchar', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | size => 255, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | prefs => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | affinity => 'string', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | db_type => 'jsonb', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | type => 'jsonb', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | orm_type => 'DBIx::QuickORM::Type::JSON', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | pk => ['id'], # undef when the table has none | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # each sorted, and the primary key is not repeated | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | unique => [['email']], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | fk => {orgs => [{org_id => 'id'}]}, # several links may name one table | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | default_fields => ['id', 'name'], # absent: every field | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # absent: the ORM's default, undef: hashrefs | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | row_class => 'My::User', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | C<db_type> is the field's type as the database reports it, C<type> its | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | lowercased base name, C<size> its declared length or precision when it has | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | one, and C<affinity> one of C<string>, C<numeric>, C<binary>, or C<boolean>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | C<orm_type> is the type class chosen for the field, if any, and C<format> and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | C<generate> its hints (see L<DBIx::QuickORM::Role::Type/Field hints>). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | C<passthrough> is 1 when that type's C<inflate> returns the raw value | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | unchanged for the field (C<inflate_is_identity>). The ORM sets it, and rows | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | then skip inflating the field. C<db_fills> is 1 when the database fills the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | column when an insert leaves it out, and C<db_refuses_value> is 1 when it | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | rejects an explicit value for it. Both come from the scan and are absent | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | otherwise. A hand-written map without them reads as "no", so C<prefer_db> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | generates and the C<prefer_orm> build croak cannot fire. C<pk_types> holds | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | the C<orm_type> of each primary key field, in key order, for the lookup | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | table. The ORM sets it only when a key field has a type. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | A scanned field record also holds its own C<table> and C<name> (left out | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | above), so messages can name the field. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | A view's record has C<view> set to 1, and a PostgreSQL materialized view's | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | also C<materialized>. A temporary table's or view's record has C<temp> set to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 1. Temporary records live only in a Connection's own map (see | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | L<DBIx::QuickORM::Connection>). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | Names are exact and case-sensitive, and a table or field named C<0> works. A | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | foreign key may name a table that is not in the map. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | my $map = $orm->db_map; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | if (my $field = $map->field(users => 'name')) { ... } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | my $pk = $map->pk('users'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | my $copy = $map->clone; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | my $own = $map->shallow_copy; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | =item \%tables = $map->tables | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | The data, as shown above. Required. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 994 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=9.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: not called CC=2 SCAR=9.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | croak "'tables' must be a hashref" unless ref($self->{+TABLES}) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | =head1 PUBLIC METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | =item $map = DBIx::QuickORM::Map->new(tables => \%tables) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | Holds C<\%tables> as given, without copying it. Croaks unless it is a hashref. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | =item $copy = $map->clone | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | A new map over a deep copy of the data. Code references (a field's | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | C<format>) are kept as they are. Any other reference but a plain hash or | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | array croaks. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 201 | ▶ | sub clone { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clone: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clone: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | return ref($self)->new(tables => $self->_copy($self->{+TABLES})); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | =item $copy = $map->shallow_copy | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | A new map over a copy of the table hash only. The table records are shared | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | with this map, so adding or removing a table in one does not touch the other, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | but nothing may change a shared record. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 405 | ▶ | sub shallow_copy { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
shallow_copy: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
shallow_copy: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | return ref($self)->new(tables => {%{$self->{+TABLES}}}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | =item $map->add_table($name, \%record) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | =item $map->remove_table($name) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | Add or remove one table. Only a Connection's own copy changes this way, for | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | its temporary tables and views. C<add_table> croaks when the name is taken. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 57 | ▶ | sub add_table { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_table: called CC=2 SCAR=10.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_table: not called CC=2 SCAR=10.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | my ($name, $record) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | croak "The table '$name' is already in the database map" if $self->{+TABLES}->{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | $self->{+TABLES}->{$name} = $record; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 60 | ▶ | sub remove_table { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove_table: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove_table: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | delete $self->{+TABLES}->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | =item \%table = $map->table($name) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | =item \%field = $map->field($table, $name) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | The record, or undef when there is none. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 22366 | ▶ | sub table { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
table: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
table: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | return $self->{+TABLES}->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 665 | ▶ | sub field { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
field: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
field: not called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | my ($table, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | my $t = $self->{+TABLES}->{$table} or return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | return $t->{fields}->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | =item \@fields = $map->pk($table) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | The primary key fields in key order, or undef when the table has none. Croaks | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | when there is no such table. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 1627 | ▶ | sub pk { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pk: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pk: not called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | my ($table) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | ▶ | my $t = $self->{+TABLES}->{$table} or croak "The table '$table' is not in the database map"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | return $t->{pk}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | =item \@values = $map->normalize_pk($table, $pk) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | The primary key values of C<$table>, in key order, from any key form. It | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | judges the form, not the values: they come back as given, and deflating them | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | is the caller's job (the ORM's key arguments do it). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | A hashref holding the key fields. Other fields are ignored, so row data works. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | A row object (any object with C<row_data>), taken as its row data. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | An arrayref of the values, in the primary key's declared order. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | A single value, for a single-field key. Any blessed object that is not a row | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | is a value, such as a C<DateTime> or a value marker. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | Croaks when the table has no primary key, on a single value for a compound | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | key, on an arrayref of the wrong length, on a missing or undef key value, and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | on an unblessed reference that is not a hash or an array. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 468 | ▶ | sub normalize_pk { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
normalize_pk: called CC=13 SCAR=35.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
normalize_pk: not called CC=13 SCAR=35.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | my ($table, $pk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | my $fields = $self->pk($table); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | confess "The table '$table' has no primary key" unless $fields && @$fields; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
$fields and @$fields
$fields and @$fields
MC/DC50%1/2
$fields and @$fields
$fields
@$fields
MC/DC0%0/2
$fields and @$fields
$fields
@$fields
Truth table66%2/3
A and B
A= $fieldsB= @$fields
Truth table0%0/3
A and B
A= $fieldsB= @$fields
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | my $names = join(', ' => @$fields); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | $pk = $pk->row_data if blessed($pk) && $pk->can('row_data'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%3/6
builtin::blessed($pk) and $pk->can("row_data")
builtin::blessed($pk) and $pk->can("row_data")
MC/DC100%2/2
builtin::blessed($pk) and $pk->can("row_data")
builtin::blessed($pk)
$pk->can("row_data")
MC/DC0%0/2
builtin::blessed($pk) and $pk->can("row_data")
builtin::blessed($pk)
$pk->can("row_data")
Truth table100%3/3
A and B
A= builtin::blessed($pk)B= $pk->can("row_data")
Truth table0%0/3
A and B
A= builtin::blessed($pk)B= $pk->can("row_data")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | my @values; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | ▶ | if (ref($pk) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | @values = map { $pk->{$_} } @$fields; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | elsif (ref($pk) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | confess "The table '$table' has " . @$fields . " primary key field(s) ($names), but the arrayref holds " . @$pk . " value(s)" unless @$pk == @$fields; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | @values = @$pk; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | ▶ | confess "Primary key must be a value, an arrayref, a hashref, or a row; got $pk" if ref($pk) && !blessed($pk); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%3/6
ref $pk and not builtin::blessed($pk)
ref $pk and not builtin::blessed($pk)
MC/DC100%2/2
ref $pk and not builtin::blessed($pk)
ref $pk
not builtin::blessed($pk)
MC/DC0%0/2
ref $pk and not builtin::blessed($pk)
ref $pk
not builtin::blessed($pk)
Truth table100%3/3
A and not(B)
A= ref $pkB= not builtin::blessed($pk)
Truth table0%0/3
A and not(B)
A= ref $pkB= not builtin::blessed($pk)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | ▶ | confess "The table '$table' has a compound primary key ($names); give a hashref of its fields or an arrayref of its values in that order" if @$fields > 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | @values = ($pk); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | for my $i (0 .. $#$fields) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | ▶ | confess "The primary key field '$fields->[$i]' of the table '$table' is missing or undefined" unless defined $values[$i]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | return \@values; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | =head1 PRIVATE METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | =item $copy = $map->_copy($data) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | A deep copy of map data, with the croak C<clone> documents. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 13117 | ▶ | sub _copy { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_copy: called CC=5 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_copy: not called CC=5 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | my ($data) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | ▶ | my $type = ref($data) or return $data; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | ▶ | return $data if $type eq 'CODE'; # a field's format, which nothing changes | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | return {map { $_ => $self->_copy($data->{$_}) } keys %$data} if $type eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | ▶ | return [map { $self->_copy($_) } @$data] if $type eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | croak "The database map holds only strings, hashes, arrays, and code references, found a '$type' reference"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 265 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||