| 1 | package DBIx::QuickORM::Role::Row; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 258 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 258 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 258 | our $VERSION = '0.000033'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 258 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 258 | ▶ | use Hash::Util::FieldHash qw/fieldhash/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 258 | ▶ | use DBIx::QuickORM::RowState(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 258 | ▶ | use Role::Tiny; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 258 | requires qw/orm table row_data set_row_data/; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | DBIx::QuickORM::Role::Row - The logic of a row object. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | A row object wraps one row's data (a hashref, as the ORM's CRUD operations | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | return it) with the ORM and the table it came from. C<raw_field> gives a value | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | as the database returned it. C<inflated_field>, and C<field>, give it | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | inflated through the field's type. A row in the database changes only through | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | C<update>, C<cas_update>, and C<delete>. There are no setters. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | A row need not be in the database: one built by the ORM's C<vivify>, one | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | deleted, or one whose insert was rolled back. C<is_in_db> and C<was_deleted> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | tell. Such a row is changed with C<modify> and written with C<insert>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | C<update>, C<cas_update>, C<delete>, and C<refresh> croak on it. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | Inflated values are cached per field, except for types that inflate on each | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | read. When the row data is replaced (by | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | C<update>, C<refresh>, a field fetch that finds the row changed, or any call | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | of C<set_row_data>), cached values of fields whose raw value is unchanged are | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | kept and the rest are dropped. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | A read hands out the cached value itself, a fresh inflation, or a copy of the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | cached value, as the type says (see | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | L<DBIx::QuickORM::Role::Type/"Cached inflated values">), so changing a value | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | read from a row never changes the row or the next read. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | A field missing from the row data was not fetched: it was omitted, or the row | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | came back from a write without C<RETURNING>. Reading it, in either form, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | fetches every field asked for with one C<refresh_row>. When that shows the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | row changed, the row takes the new row data, as C<refresh> would. Reading an | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | omitted field from each row in a loop can run one query per row. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | A row that is not canon (from a C<no_cache> handle, say) fetches its missing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | fields without the row cache: they are added to its own row data, nothing the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | ORM holds changes, and the row stays not canon. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | A row with no table (a row of a query, or a computed row given a row class) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | returns the row data's values as they are from every field reader: no field | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | check, no inflation, and no fetch. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | C<update>, C<cas_update>, and C<refresh> call the ORM's CRUD operations and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | take the row data they return. C<delete> calls C<delete_row>. Options mean | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | what L<DBIx::QuickORM/"CRUD operations"> says. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | Any class providing the required methods can consume the role. It keeps | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | nothing in the object. L<DBIx::QuickORM::Row> is the default row class. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | =head2 Overriding | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | A row class changes what C<field> and C<fields> return by overriding them | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | (say, to give the raw form). It changes the cache policy by overriding | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | C<_inflation_cache> and C<_inflated_value>, the two supported override points. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | Nothing else about the cache is. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | package My::Row; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | use Object::HashBase qw{<orm <table row_data}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | use Role::Tiny::With qw/with/; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | with 'DBIx::QuickORM::Role::Row'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # Raw values by default, but inflated_field and inflated_fields still inflate. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | sub field { my $self = shift; $self->raw_field(@_) } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | sub fields { my $self = shift; $self->raw_fields(@_) } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | package main; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | my $orm = DBIx::QuickORM->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | credentials => \%credentials, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | row_class => 'My::Row', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | my $ann = $orm->insert( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | users => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | name => 'Ann', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | prefs => {theme => 'dark'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | my $raw = $ann->field('prefs'); # the JSON text | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | my $all = $ann->inflated_field('prefs'); # {theme => 'dark'} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | =head1 REQUIRED METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | =item $orm = $row->orm | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | The L<DBIx::QuickORM> the row came from. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | =item $table = $row->table | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | The name of the row's table, or undef for a row with no table. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | =item \%data = $row->row_data | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | The row data the row wraps. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | =item $row->set_row_data(\%data) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | Replaces the row data. The role calls it whenever the ORM returns new row | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | data for the row. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # Inside-out, keyed by row object: {data => the row data the values came from, values => {field => inflated}}. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 258 | fieldhash my %CACHE; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | =head1 PUBLIC METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | =item $value = $row->field($name) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | =item @values = $row->fields(\@names) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | =item @values = $row->fields(@names) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | The inflated form: C<inflated_field> and C<inflated_fields>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | my $name = $row->field('name'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | my ($nick, $mail) = $row->fields('name', 'email'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | my @both = $row->fields(['name', 'email']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | Override these to give a row class another default form. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | A row whose class keeps this role's C<inflated_field> and C<_inflated_value> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | reads a field it holds without going through C<inflated_field>, which is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | faster. The value is the same. The class is checked on its first read, as | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | C<fast_bless> is, so methods replaced at run time after that are not seen. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | # Classes whose field() may skip inflated_field: they keep the role's inflated_field and _inflated_value. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 258 | my %DIRECT; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 38 | ▶ | sub _direct_field { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_direct_field: called CC=5 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | my $class = ref($_[0]) || $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_direct_field: not called CC=5 SCAR=23.0
Condition33%2/6
ref $_[0] || $_[0]
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | return $DIRECT{$class} //= ($class->can('inflated_field') == \&inflated_field && $class->can('_inflated_value') == \&_inflated_value) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition40%4/10
$DIRECT{$class} //= $class->can("inflated_field") == \&DBIx::QuickORM::Role::Row::inflated_field && $class->can("_inflated_value") == \&DBIx::QuickORM::Role::Row::_inflated_value ? 1 : 0
$class->can("inflated_field") == \&DBIx::QuickORM::Role::Row::inflated_field and $class->can("_inflated_value") == \&DBIx::QuickORM::Role::Row::_inflated_value
$DIRECT{$class} //= $class->can("inflated_field") == \&DBIx::QuickORM::Role::Row::inflated_field && $class->can("_inflated_value") == \&DBIx::QuickORM::Role::Row::_inflated_value ? 1 : 0
$class->can("inflated_field") == \&DBIx::QuickORM::Role::Row::inflated_field and $class->can("_inflated_value") == \&DBIx::QuickORM::Role::Row::_inflated_value
MC/DC0%0/1
$DIRECT{$class} //= $class->can("inflated_field") == \&DBIx::QuickORM::Role::Row::inflated_field && $class->can("_inflated_value") == \&DBIx::QuickORM::Role::Row::_inflated_value ? 1 : 0
$DIRECT{$class}
MC/DC0%0/1
$DIRECT{$class} //= $class->can("inflated_field") == \&DBIx::QuickORM::Role::Row::inflated_field && $class->can("_inflated_value") == \&DBIx::QuickORM::Role::Row::_inflated_value ? 1 : 0
$DIRECT{$class}
Truth table50%1/2
A
A= $DIRECT{$class}
Truth table0%0/2
A
A= $DIRECT{$class}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # A present field is read from the row data, through _inflated_value only when typed. Anything else (no table, an | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | # unknown or missing field) goes through inflated_field. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 328 | ▶ | sub field { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
field: called CC=7 SCAR=27.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
field: not called CC=7 SCAR=27.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | return $self->inflated_field(@_) unless $DIRECT{ref $self} // $self->_direct_field; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
$DIRECT{ref $self} // $self->_direct_field
$DIRECT{ref $self} // $self->_direct_field
MC/DC0%0/2
$DIRECT{ref $self} // $self->_direct_field
$DIRECT{ref $self}
$self->_direct_field
MC/DC0%0/2
$DIRECT{ref $self} // $self->_direct_field
$DIRECT{ref $self}
$self->_direct_field
Truth table66%2/3
A // B
A= $DIRECT{ref $self}B= $self->_direct_field
Truth table0%0/3
A // B
A= $DIRECT{ref $self}B= $self->_direct_field
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | my $table = $self->table; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | my $data = $self->row_data; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | return $self->inflated_field($name) unless defined $table && exists $data->{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%3/6
defined $table and exists $data->{$name}
defined $table and exists $data->{$name}
MC/DC100%2/2
defined $table and exists $data->{$name}
defined $table
exists $data->{$name}
MC/DC0%0/2
defined $table and exists $data->{$name}
defined $table
exists $data->{$name}
Truth table100%3/3
A and B
A= defined $tableB= exists $data->{$name}
Truth table0%0/3
A and B
A= defined $tableB= exists $data->{$name}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | my $field = $self->orm->_table_fields($table)->{$name} or return $self->inflated_field($name); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | return $data->{$name} unless $field->{orm_type}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | return $self->_inflated_value($name, $field); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 10 | ▶ | sub fields { my $self = shift; $self->inflated_fields(@_) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fields: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | =item $value = $row->inflated_field($name) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | =item @values = $row->inflated_fields(\@names) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | =item @values = $row->inflated_fields(@names) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | Values inflated through each field's type. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | my $prefs = $row->inflated_field('prefs'); # {theme => 'dark'} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | $prefs->{theme} = 'light'; # yours alone: the row still holds dark | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | my ($name, $settings) = $row->inflated_fields('name', 'prefs'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | Each value is the cached value, a fresh | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | inflation, or a copy of the cached value, as the type says (see | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | L</DESCRIPTION>). The raw value for an untyped field, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | undef for NULL, and undef for an undef inflated value (a JSON C<null>). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | Missing fields are fetched with one C<refresh_row> (one C<no_cache> read for a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | row that is not canon). A row built by C<vivify> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | returns undef for a field it was not given. So does a row whose insert | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | was rolled back. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | Croaks for a field the table does not have, and for a missing field that | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | cannot be fetched (no primary key, or the row is deleted or no longer in the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | database). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | # One field-record lookup. It calls _fetch_missing (which croaks for an unknown field) only when a field is unknown or missing. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 40 | ▶ | sub inflated_field { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
inflated_field: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 40 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 40 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 40 | my $table = $self->table; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 40 | ▶ | return $self->row_data->{$name} unless defined $table; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 36 | my $field = $self->orm->_table_fields($table)->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 36 | ▶ | $self->_fetch_missing($name) unless $field && exists $self->row_data->{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$field and exists $self->row_data->{$name}
MC/DC100%2/2
$field and exists $self->row_data->{$name}
$field
exists $self->row_data->{$name}
Truth table100%3/3
A and B
A= $fieldB= exists $self->row_data->{$name}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 30 | return $self->_inflated_value($name, $field); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 13 | ▶ | sub inflated_fields { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
inflated_fields: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 13 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 13 | ▶ | my @names = ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 13 | my $table = $self->table; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 13 | ▶ | return @{$self->row_data}{@names} unless defined $table; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 11 | my $fields = $self->orm->_table_fields($table); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 11 | my $data = $self->row_data; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 11 | ▶ | $self->_fetch_missing(@names) if grep { !$fields->{$_} || !exists $data->{$_} } @names; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$fields->{$_} && exists $data->{$_}
MC/DC100%2/2
$fields->{$_} && exists $data->{$_}
$fields->{$_}
exists $data->{$_}
Truth table100%3/3
A && B
A= $fields->{$_}B= exists $data->{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 10 | return map { $self->_inflated_value($_, $fields->{$_}) } @names; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | =item $raw = $row->raw_field($name) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | =item @raw = $row->raw_fields(\@names) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | =item @raw = $row->raw_fields(@names) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | Values as the database returned them, never inflated. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | my $json = $row->raw_field('prefs'); # '{"theme":"dark"}' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | my ($id, $text) = $row->raw_fields(['id', 'prefs']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | Missing fields are | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | fetched and unknown ones croak, as for C<inflated_fields>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 21 | ▶ | sub raw_field { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
raw_field: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 21 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 21 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 21 | return ($self->raw_fields($name))[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | # Like inflated_fields, it calls _fetch_missing (which croaks for an unknown field) only when a field is unknown or missing. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 23 | ▶ | sub raw_fields { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
raw_fields: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 23 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 23 | ▶ | my @names = ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 23 | my $table = $self->table; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 23 | ▶ | if (defined $table) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 22 | my $fields = $self->orm->_table_fields($table); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 22 | my $data = $self->row_data; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 22 | ▶ | $self->_fetch_missing(@names) if grep { !$fields->{$_} || !exists $data->{$_} } @names; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$fields->{$_} && exists $data->{$_}
MC/DC100%2/2
$fields->{$_} && exists $data->{$_}
$fields->{$_}
exists $data->{$_}
Truth table100%3/3
A && B
A= $fields->{$_}B= exists $data->{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 21 | return @{$self->row_data}{@names}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | =item \%pk = $row->pk | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | The primary key fields and their values from the row data. Undef when the row | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | has no table or the table has no primary key. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 243 | ▶ | sub pk { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pk: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 243 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 243 | ▶ | my $table = $self->table // return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->table // (return undef)
MC/DC100%1/1
$self->table // (return undef)
$self->table
Truth table100%2/2
A
A= $self->table
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 230 | ▶ | my $pk = $self->orm->db_map->pk($table) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 221 | my $data = $self->row_data; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 221 | return {map { $_ => $data->{$_} } @$pk}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | =item $row = $row->update(\%changes, %update_row_options) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | Writes the changes with C<update_row> and takes the new row data. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | $row->update({name => 'Ann Smith'}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | # Only while email still holds what this row read. Undef otherwise. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | my $ok = $row->update({name => 'Ann Jones'}, cas => ['email']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | # Only while each changed field the row holds still has the value it read. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | $ok = $row->update({email => 'ann@example.org'}, auto_cas => 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | Returns the row, or undef when no row matched (a compare-and-swap failed, or | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | the row is gone), leaving the row as it was. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | Each change is deflated through its field's type. The options are those of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | C<update_row>: C<enforce_canon>, C<cas>, C<auto_cas>. A row that is not | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | canonical croaks under C<enforce_canon>. C<cas_update> writes one safely. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | Croaks when the changes are not a hashref, when given C<data> (it always | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | returns the row), when the row is not in the database, or when it has no | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | usable primary key. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 70 | ▶ | sub update { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 70 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 70 | my ($changes, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 70 | ▶ | croak "update() needs a hashref of changes" unless ref($changes) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 69 | ▶ | croak "update() always returns the row; use the ORM's update_row() for row data" if exists $params{data}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 68 | ▶ | croak "update() always returns the row itself, so it takes no row_class" if exists $params{row_class}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 67 | $self->_require_in_db('update'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 64 | my $table = $self->_require_pk('update'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 60 | my $orm = $self->orm; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 60 | my $debug = $orm->debug; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 60 | ▶ | local $debug->{caller} = [(caller(0))[0 .. 3]] if $debug && !$debug->{caller}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$debug and not $debug->{"caller"}
MC/DC100%2/2
$debug and not $debug->{"caller"}
$debug
not $debug->{"caller"}
Truth table100%3/3
A and not(B)
A= $debugB= not $debug->{"caller"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 60 | return $orm->update_row($table, $self, $orm->connection->deflate($table, $changes), %params, raw => 1); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | =item $row = $row->cas_update(\%changes) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | =item $row = $row->cas_update(\%changes, cas => \@fields) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | Writes the changes only if the database still holds the row's values for the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | changed fields (or the C<cas> fields). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | my $ann = $orm->find(users => $id); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | $orm->txn(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | # Read before the transaction, so update() would croak here. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | $ann->cas_update({name => 'Ann B'}) or die "Ann changed since we read her\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | # JSON cannot be compared, so compare the name: the prefs write goes through | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | # only while the name is still what this row read. A change to prefs alone | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | # since then is not caught, and is overwritten. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | my $saved = $ann->cas_update({prefs => {theme => 'light'}}, cas => ['name']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | warn "Ann was renamed, prefs not saved\n" unless $saved; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | Returns the row with the new data, or undef on a conflict, leaving the row as | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | it was. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | The row need not be canonical, even inside a transaction, so this is how | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | to write a row read before the transaction began. The changes are deflated as | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | C<update> deflates them. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | It compares the changed fields with C<=>, which does not suit floats, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | PostgreSQL C<json>, or MySQL C<JSON>: pass C<cas> to compare other fields. On | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | MySQL and MariaDB a successful update leaves only the primary key in the row | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | data, so another C<cas_update> croaks until the row is refreshed (or the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | compared fields are read again). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | Croaks when the changes are not a hashref of at least one change, on any | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | option but C<cas>, on an empty C<cas> list (an unconditional write), when the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | row is not in the database, when it has no usable primary key, or when it does | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | not hold a field it compares. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | It calls the ORM's C<cas_update> (L<DBIx::QuickORM/"CRUD operations">), which | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | does the same for row data, a C<bare> hashref included. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | A field left out of the row's select (C<omit>, C<default_fields>, or | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | C<auto_omit>) must be read first (C<< $row->field($name) >>), or selected | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | with C<fields>, before C<cas_update> compares it. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 19 | ▶ | sub cas_update { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cas_update: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 19 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 19 | my ($changes, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 19 | ▶ | croak "cas_update() needs a hashref of changes" unless ref($changes) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 18 | ▶ | croak "Odd number of options to cas_update()" if @args % 2; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 17 | my %params = @args; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 17 | ▶ | croak "cas_update() takes only the 'cas' option" if grep { $_ ne 'cas' } keys %params; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 16 | $self->_require_in_db('cas_update'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 15 | my $table = $self->_require_pk('cas_update'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 14 | my $orm = $self->orm; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 14 | my $debug = $orm->debug; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 14 | ▶ | local $debug->{caller} = [(caller(0))[0 .. 3]] if $debug && !$debug->{caller}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$debug and not $debug->{"caller"}
MC/DC0%0/2
$debug and not $debug->{"caller"}
$debug
not $debug->{"caller"}
Truth table33%1/3
A and not(B)
A= $debugB= not $debug->{"caller"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 14 | return $orm->cas_update($table, $self, $changes, %params); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | =item $count = $row->delete | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | =item $count = $row->delete(enforce_canon => $bool) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | Deletes the row with C<delete_row> and returns how many rows went. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | my $count = $row->delete; # 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | my $gone = $row->was_deleted; # 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | my $in_db = $row->is_in_db; # 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | # Another user, read around the cache: not canonical, so a plain delete croaks. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | my $copy = $orm->handle('users') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | ->no_cache | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | ->where({id => $other_id}) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | ->only; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | $count = $copy->delete(enforce_canon => 0); # deleted by its key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | The row, and every row holding the same row data, is then marked deleted. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | C<enforce_canon> is as for C<update_row>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | A delete that matches no row also marks it deleted (it is not in the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | database) and returns 0. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | Croaks when the row is not in the database or has no usable primary key. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 29 | ▶ | sub delete { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | 29 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 29 | my (%params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 29 | $self->_require_in_db('delete'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 28 | my $table = $self->_require_pk('delete'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 24 | my $debug = $self->orm->debug; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 24 | ▶ | local $debug->{caller} = [(caller(0))[0 .. 3]] if $debug && !$debug->{caller}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$debug and not $debug->{"caller"}
MC/DC50%1/2
$debug and not $debug->{"caller"}
$debug
not $debug->{"caller"}
Truth table66%2/3
A and not(B)
A= $debugB= not $debug->{"caller"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 24 | return $self->orm->connection->delete_row($table, $self->row_data, %params, raw => 1); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | =item $row = $row->refresh | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | Reads the row from the database and takes the new row data. Takes no | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | arguments. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | Croaks when given any, when the row is not in the database (as this ORM last | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | saw it), or has no usable primary key. Croaks too when the row is no longer | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | there, marking it deleted. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 43 | ▶ | sub refresh { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
refresh: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 43 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 43 | ▶ | croak "refresh() takes no arguments" if @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | 42 | $self->_require_in_db('refresh'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | 39 | my $table = $self->_require_pk('refresh'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 33 | my $debug = $self->orm->debug; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | 33 | ▶ | local $debug->{caller} = [(caller(0))[0 .. 3]] if $debug && !$debug->{caller}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$debug and not $debug->{"caller"}
MC/DC50%1/2
$debug and not $debug->{"caller"}
$debug
not $debug->{"caller"}
Truth table66%2/3
A and not(B)
A= $debugB= not $debug->{"caller"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | 33 | ▶ | return $self->orm->refresh_row($table, $self, cache => 0, raw => 1) // croak "The row is no longer in the table '$table'"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->orm->refresh_row($table, $self, "cache", 0, "raw", 1) // croak("The row is no longer in the table '${table}'")
MC/DC0%0/1
$self->orm->refresh_row($table, $self, "cache", 0, "raw", 1) // croak("The row is no longer in the table '${table}'")
$self->orm->refresh_row($table, $self, "cache", 0, "raw", 1)
Truth table50%1/2
A
A= $self->orm->refresh_row($table, $self, "cache", 0, "raw", 1)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | =item $bool = $row->is_canon | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | True when the row data is the ORM's canonical copy of the row. False for a row | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | not in the database, even one without its key. Croaks when a row in the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | database has no usable primary key. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | 60 | ▶ | sub is_canon { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_canon: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | 60 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 60 | ▶ | return 0 unless $self->is_in_db; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | 56 | my $table = $self->_require_pk('is_canon'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | 53 | return $self->orm->connection->is_canon($table, $self->row_data); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | =item $status = $row->canon_status | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | Undef while the row is canonical. Otherwise a hashref saying why not: its | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | C<cause>, and the call behind it (C<by>, or C<last_clear>) when the ORM was | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | built with C<< debug => 1 >>. This is the ORM's C<canon_status> for the row, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | which lists the causes. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | my $why = $row->canon_status; # {cause => 'replaced', by => [$pkg, $file, $line, $sub]} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | Croaks for a row with no table, and as C<is_canon> does for a row in the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | database without a usable primary key. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | 20 | ▶ | sub canon_status { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
canon_status: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | 20 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | 20 | return $self->orm->canon_status($self->_require_table('canon_status'), $self); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | =item $bool = $row->is_in_db | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | True unless the row was built by C<vivify> and not yet inserted, or was | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | marked deleted (see C<was_deleted>). This is what this ORM last saw, not a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | check of the database. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | C<insert> sets it. A rollback of the inserting transaction or savepoint | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | clears it again, so the row can be inserted again. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | An update or refresh of that row in the same transaction carries this along. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | A plain re-read that gives the row changed data does not: after a rollback | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | such a row may still report C<is_in_db>, so C<refresh> it to check. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | =item $bool = $row->was_deleted | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | True when the row was marked deleted. Three things mark it: a row | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | C<delete>, a C<delete_row>, and a C<refresh> or C<find> that finds nothing in | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | the database. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | A vivified row is neither in the database nor deleted. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | A bulk C<delete> on a handle marks nothing unless given | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | C<< mark_deleted => 1 >>. Otherwise rows already held keep C<is_in_db> true, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | so refresh them to check. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | my $dee = $orm->find(users => $dee_id); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | $orm->handle('users')->where({id => $dee_id})->delete(mark_deleted => 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | my $gone = $dee->was_deleted; # 1, but 0 without mark_deleted | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | A delete inside a transaction or savepoint that rolls back is undone here too. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | 290 | ▶ | sub is_in_db { DBIx::QuickORM::RowState->state($_[0]->row_data) ? 0 : 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_in_db: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | 30 | ▶ | sub was_deleted { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
was_deleted: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | 30 | ▶ | my $state = DBIx::QuickORM::RowState->state($_[0]->row_data) // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
"DBIx::QuickORM::RowState"->state($_[0]->row_data) // ""
MC/DC100%1/1
"DBIx::QuickORM::RowState"->state($_[0]->row_data) // ""
"DBIx::QuickORM::RowState"->state($_[0]->row_data)
Truth table100%2/2
A
A= "DBIx::QuickORM::RowState"->state($_[0]->row_data)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | 30 | ▶ | return $state eq 'deleted' ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | =item $row = $row->insert | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | Inserts a row that is not in the database, with the ORM's C<insert>, and takes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | the new row data. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | my $draft = $orm->vivify(users => {name => 'Draft'}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | $draft->modify({email => 'draft@example.com'}); # nothing written yet | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | my $in_db = $draft->is_in_db; # 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | $draft->insert; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | $in_db = $draft->is_in_db; # 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | my $id = $draft->field('id'); # filled in by the database | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | The row is then in the database, and canonical when the table has a primary | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | key. Returns the row. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | Croaks when given arguments, for a row with no table, and for a row already | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | in the database. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | 6 | ▶ | sub insert { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | 6 | ▶ | croak "insert() takes no arguments" if @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | 6 | my $table = $self->_require_table('insert'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | 6 | ▶ | croak "Cannot insert a row that is already in the database (table '$table')" if $self->is_in_db; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | 5 | my $debug = $self->orm->debug; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | 5 | ▶ | local $debug->{caller} = [(caller(0))[0 .. 3]] if $debug && !$debug->{caller}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$debug and not $debug->{"caller"}
MC/DC0%0/2
$debug and not $debug->{"caller"}
$debug
not $debug->{"caller"}
Truth table33%1/3
A and not(B)
A= $debugB= not $debug->{"caller"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 5 | $self->set_row_data($self->orm->insert($table => $self->row_data, data => 1, raw => 1)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 5 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | =item $row = $row->modify(\%changes) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | Changes the fields of a row that is not in the database, before its | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | C<insert>. Nothing is written. Returns the row. The example under C<insert> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | shows it. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | The changes are deflated as C<update> deflates them. The row takes a changed | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | copy of its row data, so other rows holding the old row data are untouched. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | Croaks when the changes are not a hashref, for a row with no table, for a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | field the table does not have, and for a row in the database: use C<update>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | 8 | ▶ | sub modify { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
modify: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | 8 | my ($changes) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | 8 | ▶ | croak "modify() needs a hashref of changes" unless ref($changes) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | 8 | my $table = $self->_require_table('modify'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | 8 | ▶ | croak "Cannot modify a row that is in the database; use update()" if $self->is_in_db; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | 7 | my $old = $self->row_data; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | 7 | my $new = {%$old, %{$self->orm->connection->deflate($table, $changes)}}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | 6 | DBIx::QuickORM::RowState->copy($old, $new); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | 6 | $self->set_row_data($new); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | 6 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | =item $h = $row->follow_fk($other_table) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | =item $h = $row->follow_fk($other_table, link => \%link) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | =item $h = $row->follow_fk($other_table, via => \%via) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | The ORM's C<follow_fk> from this row: a handle on the rows it links to. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | my $author = $post->follow_fk('users')->only; # the user this post's user_id points at | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | # The same key from the other side: posts whose user_id is this user's id. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | my @posts = $author->follow_fk('posts')->all; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | # users.referred_by points at users too, so give the direction. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | # link is {this row's field => the other row's field}. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | my $referrer = $author->follow_fk('users', link => {referred_by => 'id'})->only; # the user who referred this one | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | my @referred = $author->follow_fk('users', link => {id => 'referred_by'})->all; # the users this one referred | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | # posts.editor_id holds a user's id, but no key declares it. link only follows | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | # keys in the ORM's map, so it croaks here. via takes the same mapping and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | # needs no key. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | my $editor = $post->follow_fk('users', via => {editor_id => 'id'})->only; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | Croaks for a row with no table. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | 31 | ▶ | sub follow_fk { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
follow_fk: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | 31 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | 31 | my ($other, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | 31 | my $table = $self->table; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | 31 | ▶ | croak "Cannot follow a foreign key from a row with no table" unless defined $table; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | 30 | ▶ | return $self->orm->_follow_fk($self->orm->debug ? [(caller(0))[0 .. 3]] : [caller], $table, $self, $other, %params); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | =item $new = $row->insert_fk($other_table, %opts, \%fields) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | =item \@new = $row->insert_many_fk($other_table, %opts, @fields) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | The ORM's C<insert_fk> and C<insert_many_fk> from this row: insert rows that | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | point at it. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | my $post = $alice->insert_fk('posts', {title => 'One'}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | my $posts = $alice->insert_many_fk('posts', link => {id => 'user_id'}, {title => 'A'}, {title => 'B'}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | Croaks for a row with no table. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | 21 | ▶ | sub insert_fk { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert_fk: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | 21 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | 21 | my $table = $self->table; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | 21 | ▶ | croak "Cannot insert rows linked to a row with no table" unless defined $table; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | 21 | ▶ | return $self->orm->_insert_fk($self->orm->debug ? [(caller(0))[0 .. 3]] : [caller], insert => $table, $self, @_); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | 8 | ▶ | sub insert_many_fk { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert_many_fk: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | 8 | my $table = $self->table; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | 8 | ▶ | croak "Cannot insert rows linked to a row with no table" unless defined $table; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | 8 | ▶ | return $self->orm->_insert_fk($self->orm->debug ? [(caller(0))[0 .. 3]] : [caller], insert_many => $table, $self, @_); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | =item $bool = $class->fast_bless | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | True when the ORM may build rows of the class by blessing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | C<< {orm => $orm, table => $table, row_data => \%data} >> directly, skipping | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | C<new>. False here. L<DBIx::QuickORM::Row> says when it is true. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | The ORM takes the answer once per row class per ORM, not once per row. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | 9 | ▶ | sub fast_bless { 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fast_bless: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | =head1 PRIVATE METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | =item $table = $row->_require_pk($action) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | The row's table. Croaks naming the action unless the table has a primary key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | and the row data holds all of it. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | 224 | ▶ | sub _require_pk { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_require_pk: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | 224 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | 224 | my ($action) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | 224 | my $table = $self->table; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | 224 | my $pk = $self->pk; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | 224 | ▶ | my $ok = $pk && !grep { !defined } values %$pk; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$pk && !grep({not defined $_;} values %$pk)
MC/DC100%2/2
$pk && !grep({not defined $_;} values %$pk)
$pk
!grep({not defined $_;} values %$pk)
Truth table100%3/3
A && not(B)
A= $pkB= !grep({not defined $_;} values %$pk)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | 224 | ▶ | croak "Cannot $action a row without a primary key" . (defined($table) ? " (table '$table')" : '') unless $ok; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | 205 | return $table; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | =item $row->_require_in_db($action) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | Croaks naming the action unless the row is in the database. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | =item $table = $row->_require_table($action) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | The row's table. Croaks naming the action for a row with no table. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | 177 | ▶ | sub _require_in_db { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_require_in_db: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | 177 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | 177 | my ($action) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | 177 | ▶ | croak "Cannot $action a row that is not in the database; insert it first" unless $self->is_in_db; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | 34 | ▶ | sub _require_table { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_require_table: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | 34 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | 34 | my ($action) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | 34 | my $table = $self->table; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | 34 | ▶ | croak "Cannot $action a row with no table" unless defined $table; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | 34 | return $table; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | =item $row->_fetch_missing(@names) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | Fetches the missing fields, all of them with one C<refresh_row> that also | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | asks for the fields already present, and takes the row data it returns. A vivified row fetches nothing: its missing fields | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | read as undef. Croaks for a field the table does not have, and for a deleted | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | row. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | A row that is not canon reads just the missing fields through a C<no_cache> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | handle and adds them to its own row data: the row cache is not touched, and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | the row stays not canon. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | 32 | ▶ | sub _fetch_missing { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fetch_missing: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | 32 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | 32 | my (@names) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | 32 | my $table = $self->table; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | 32 | my $fields = $self->orm->_table_fields($table); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | 32 | for my $name (@names) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | 42 | ▶ | croak "The table '$table' has no field '$name'" unless $fields->{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | 26 | my $data = $self->row_data; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | 26 | ▶ | return unless grep { !exists $data->{$_} } @names; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | 26 | ▶ | return if (DBIx::QuickORM::RowState->state($data) // '') eq 'vivified'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
"DBIx::QuickORM::RowState"->state($data) // ""
MC/DC100%1/1
"DBIx::QuickORM::RowState"->state($data) // ""
"DBIx::QuickORM::RowState"->state($data)
Truth table100%2/2
A
A= "DBIx::QuickORM::RowState"->state($data)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | 23 | $self->_require_in_db('fetch missing fields of'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | 22 | $self->_require_pk('fetch missing fields of'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | # The reader that called this is the public call, one frame up. When it was called through another reader of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | # this role (field() through inflated_field()), that frame is ours, so none is recorded. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | 21 | my $debug = $self->orm->debug; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | 21 | ▶ | my @frame = $debug ? (caller(1))[0 .. 3] : (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | 21 | ▶ | local $debug->{caller} = ($frame[0] eq __PACKAGE__ ? undef : \@frame) if $debug && !$debug->{caller}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$debug and not $debug->{"caller"}
MC/DC50%1/2
$debug and not $debug->{"caller"}
$debug
not $debug->{"caller"}
Truth table66%2/3
A and not(B)
A= $debugB= not $debug->{"caller"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | # A row that is not canon reads around the row cache and keeps its own data. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | 21 | ▶ | unless ($self->is_canon) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | 11 | my @missing = grep { !exists $data->{$_} } @names; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | 11 | ▶ | my $got = $self->orm->handle($table)->no_cache->fields(\@missing)->where($self->pk)->data_only->first or croak "Cannot fetch missing fields: the row is no longer in the table '$table'"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | 11 | $data->{$_} = $got->{$_} for @missing; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | 11 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | # The present fields are asked for too, so a changed row's new data holds them as well. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 852 | 10 | my @want = ((grep { $fields->{$_} } keys %$data), @names); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 853 | 10 | ▶ | my $new = $self->orm->connection->refresh_row($table, $data, fields => \@want, raw => 1) or croak "Cannot fetch missing fields: the row is no longer in the table '$table'"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 854 | 9 | ▶ | $self->set_row_data($new) unless $new == $data; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 855 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 856 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 857 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 859 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 860 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 861 | =item \%values = $row->_inflation_cache | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 862 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 863 | The cache of inflated values (field name to value) for the current row data. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 864 | A supported override point. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 866 | When the row data has been replaced since the cache was built, a new cache is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 867 | started, keeping the values of fields present in both whose raw values do not | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 868 | differ by the lookup table's C<differ>, so a lookup table that overrides it | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 869 | decides here too. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 870 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 871 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 872 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 873 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 874 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 875 | 51 | ▶ | sub _inflation_cache { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_inflation_cache: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 876 | 51 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 877 | 51 | my $data = $self->row_data; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 878 | 51 | my $old = $CACHE{$self}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 879 | 51 | ▶ | return $old->{values} if $old && $old->{data} == $data; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$old and $old->{"data"} == $data
MC/DC100%2/2
$old and $old->{"data"} == $data
$old
$old->{"data"} == $data
Truth table100%3/3
A and B
A= $oldB= $old->{"data"} == $data
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 880 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 881 | 30 | my %keep; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 882 | 30 | ▶ | if ($old) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 883 | 8 | my $was = $old->{data}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 884 | # The ORM's Connection as it stands, without the reconnect connection() does in a forked child: no query follows. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 885 | 8 | my $lookup = $self->orm->{connection}->lookup; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 886 | 8 | for my $name (keys %{$old->{values}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 887 | 6 | ▶ | next unless exists $data->{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 888 | 6 | ▶ | next if $lookup->differ($was->{$name}, $data->{$name}); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 889 | 3 | $keep{$name} = $old->{values}->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 890 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 891 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 892 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 893 | 30 | $CACHE{$self} = {data => $data, values => \%keep}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 894 | 30 | return \%keep; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 895 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 896 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 897 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 898 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 899 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 900 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 901 | =item $value = $row->_inflated_value($name, \%field) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 902 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 903 | The value to hand out for a present field. A supported override point. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 904 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 905 | The raw value when it is NULL, the field is untyped, or the field is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 906 | C<passthrough> (its type would return it unchanged). Otherwise, by the type: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 907 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 908 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 909 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 910 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 911 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 912 | C<inflated_is_immutable_for_cache> true: the value cached by | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 913 | C<_inflation_cache>, inflated on first read, handed out as it is. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 914 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 915 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 917 | Else C<inflate_on_each_read> true: the raw value inflated again for this read, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 918 | with nothing cached. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 919 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 920 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 921 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 922 | Else: the cached value, copied by C<clone_inflated>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 923 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 924 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 925 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 926 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 927 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 928 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 929 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 930 | 141 | ▶ | sub _inflated_value { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_inflated_value: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 931 | 141 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 932 | 141 | my ($name, $field) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 933 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 934 | 141 | my $raw = $self->row_data->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 935 | 141 | my $type = $field->{orm_type}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 936 | 141 | ▶ | return $raw unless defined($raw) && $type && !$field->{passthrough}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $raw and $type and not $field->{"passthrough"}
defined $raw and $type
MC/DC100%3/3
defined $raw and $type and not $field->{"passthrough"}
defined $raw
$type
not $field->{"passthrough"}
Truth table100%4/4
A and B and not(C)
A= defined $rawB= $typeC= not $field->{"passthrough"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 937 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 938 | 92 | my $immutable = $type->inflated_is_immutable_for_cache; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 939 | 92 | ▶ | return scalar $type->inflate($raw, $field, $self->orm->connection) if !$immutable && $type->can('inflate_on_each_read') && $type->inflate_on_each_read; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not $immutable and $type->can("inflate_on_each_read") and $type->inflate_on_each_read
not $immutable and $type->can("inflate_on_each_read")
MC/DC66%2/3
not $immutable and $type->can("inflate_on_each_read") and $type->inflate_on_each_read
not $immutable
$type->can("inflate_on_each_read")
$type->inflate_on_each_read
Truth table75%3/4
not(A) and B and C
A= not $immutableB= $type->can("inflate_on_each_read")C= $type->inflate_on_each_read
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 940 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 941 | 49 | my $cache = $self->_inflation_cache; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 942 | 49 | ▶ | $cache->{$name} = $type->inflate($raw, $field, $self->orm->connection) unless exists $cache->{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 943 | 49 | ▶ | return $immutable ? $cache->{$name} : $type->clone_inflated($cache->{$name}); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 944 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 945 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 946 | 258 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 947 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 948 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||