| 1 | package DBIx::QuickORM::Row; | ||||||||||||||||||||||||||||||||||||||
| 2 | 257 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 3 | 257 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||
| 5 | 257 | our $VERSION = '0.000031'; | |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||
| 7 | 257 | ▶ | use Object::HashBase qw{ | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | <orm | ||||||||||||||||||||||||||||||||||||||
| 9 | <table | ||||||||||||||||||||||||||||||||||||||
| 10 | row_data | ||||||||||||||||||||||||||||||||||||||
| 11 | 257 | }; | |||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||
| 13 | 257 | ▶ | use Role::Tiny::With qw/with/; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 14 | 257 | with 'DBIx::QuickORM::Role::Row'; | |||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | =pod | ||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||
| 18 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||
| 22 | DBIx::QuickORM::Row - The default row class. | ||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||
| 26 | L<DBIx::QuickORM::Role::Row> applied to a class, and nothing more. The ORM | ||||||||||||||||||||||||||||||||||||||
| 27 | builds rows with this class unless told otherwise (C<row_class> in | ||||||||||||||||||||||||||||||||||||||
| 28 | L<DBIx::QuickORM>). Subclass it to add methods to a table's rows. | ||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||
| 30 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||
| 32 | my $bob = $orm->insert(users => {name => 'Bob'}); | ||||||||||||||||||||||||||||||||||||||
| 33 | my $name = $bob->field('name'); | ||||||||||||||||||||||||||||||||||||||
| 34 | $bob->update({name => 'Robert'}); | ||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||
| 36 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||
| 38 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||
| 40 | =item $orm = $row->orm | ||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||
| 42 | The L<DBIx::QuickORM> the row came from. | ||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||
| 44 | =item $table = $row->table | ||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||
| 46 | The name of the row's table, or undef for a row with no table. | ||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||
| 48 | =item \%data = $row->row_data | ||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||
| 50 | =item $row->set_row_data(\%data) | ||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||
| 52 | The row data the row wraps. C<update>, C<refresh>, and field fetches replace | ||||||||||||||||||||||||||||||||||||||
| 53 | it. Setting it directly is supported but rarely needed. | ||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||
| 55 | =back | ||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||
| 57 | =head1 USING ROWS | ||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||
| 59 | The methods below come from L<DBIx::QuickORM::Role::Row>, which documents | ||||||||||||||||||||||||||||||||||||||
| 60 | each in full. | ||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||
| 62 | =head2 Read fields | ||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||
| 64 | my $user = $orm->find(users => $id); | ||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||
| 66 | my $name = $user->field('name'); | ||||||||||||||||||||||||||||||||||||||
| 67 | my ($nick, $email) = $user->fields('name', 'email'); | ||||||||||||||||||||||||||||||||||||||
| 68 | my $theme = $user->field('prefs')->{theme}; # inflated: JSON as Perl data | ||||||||||||||||||||||||||||||||||||||
| 69 | my $json = $user->raw_field('prefs'); # as the database returned it | ||||||||||||||||||||||||||||||||||||||
| 70 | my $year = $user->field('created')->datetime_ref->year; # a DateTime, built on first use | ||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||
| 72 | C<field> gives the inflated form, as does C<inflated_field>. C<raw_field> | ||||||||||||||||||||||||||||||||||||||
| 73 | never inflates. Each has a plural form taking several names. | ||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||
| 75 | =head2 Change a row | ||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||
| 77 | Rows have no setters. Change a row with C<update>, which writes to the | ||||||||||||||||||||||||||||||||||||||
| 78 | database and takes the new data: | ||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||
| 80 | # a copy: changing it changes neither the row nor the database | ||||||||||||||||||||||||||||||||||||||
| 81 | my $prefs = $user->field('prefs'); | ||||||||||||||||||||||||||||||||||||||
| 82 | $prefs->{theme} = 'light'; | ||||||||||||||||||||||||||||||||||||||
| 83 | $user->update({ | ||||||||||||||||||||||||||||||||||||||
| 84 | prefs => $prefs, | ||||||||||||||||||||||||||||||||||||||
| 85 | name => 'Alice Smith', | ||||||||||||||||||||||||||||||||||||||
| 86 | }); | ||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||
| 88 | # Only while email still holds what this row read. Undef otherwise. | ||||||||||||||||||||||||||||||||||||||
| 89 | my $ok = $user->update({email => 'alice@example.org'}, cas => ['email']); | ||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||
| 91 | $user->refresh; # read it again from the database | ||||||||||||||||||||||||||||||||||||||
| 92 | my $deleted = $user->delete; # 1 | ||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||
| 94 | =head2 Fields not fetched yet | ||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||
| 96 | A row can lack fields: the handle asked for only some, or the database could | ||||||||||||||||||||||||||||||||||||||
| 97 | not return the whole row after a write. Reading one fetches every field asked | ||||||||||||||||||||||||||||||||||||||
| 98 | for with one C<refresh_row>. | ||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||
| 100 | my $slim = $orm->handle('users') | ||||||||||||||||||||||||||||||||||||||
| 101 | ->fields(['name']) | ||||||||||||||||||||||||||||||||||||||
| 102 | ->where({name => 'Bob'}) | ||||||||||||||||||||||||||||||||||||||
| 103 | ->only; | ||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||
| 105 | my $email = $slim->field('email'); # fetched now | ||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||
| 107 | In a loop that is one query per row, so select the fields up front instead. | ||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||
| 109 | =head2 Follow foreign keys | ||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||
| 111 | my $post = $orm->handle('posts')->where({title => 'Hello'})->only; | ||||||||||||||||||||||||||||||||||||||
| 112 | my $author = $post->follow_fk('users')->only; # one row | ||||||||||||||||||||||||||||||||||||||
| 113 | my @posts = $author->follow_fk('posts')->all; # many rows | ||||||||||||||||||||||||||||||||||||||
| 114 | my @busy = $author->follow_fk('posts')->and({views => {'>' => 100}})->all; | ||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||
| 116 | =head2 Insert linked rows | ||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||
| 118 | my $reply = $author->insert_fk('posts', {title => 'Reply'}); # user_id is filled in | ||||||||||||||||||||||||||||||||||||||
| 119 | my $more = $author->insert_many_fk('posts', {title => 'A'}, {title => 'B'}); | ||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||
| 121 | =head2 Keys and canonical data | ||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||
| 123 | my $pk = $post->pk; # {id => 1} | ||||||||||||||||||||||||||||||||||||||
| 124 | my $data = $post->row_data; # the row's data as a hashref: read it, never change it | ||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||
| 126 | unless ($post->is_canon) { | ||||||||||||||||||||||||||||||||||||||
| 127 | $post->refresh; | ||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||
| 130 | C<is_canon> is true while the row's data is the ORM's canonical copy. It | ||||||||||||||||||||||||||||||||||||||
| 131 | stops being so when another row object or a bulk write changes the row, when | ||||||||||||||||||||||||||||||||||||||
| 132 | the ORM's cache is cleared, and inside a transaction begun after the row was | ||||||||||||||||||||||||||||||||||||||
| 133 | read. Writing (an update or a delete) a row that is not canonical croaks, | ||||||||||||||||||||||||||||||||||||||
| 134 | unless C<enforce_canon> is off: refresh it first, or update it with | ||||||||||||||||||||||||||||||||||||||
| 135 | C<cas_update>, which writes only while the database still holds what the row | ||||||||||||||||||||||||||||||||||||||
| 136 | read. | ||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | =head2 Add your own methods | ||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||
| 140 | Subclass this class and name it for a table: | ||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||
| 142 | package My::User; | ||||||||||||||||||||||||||||||||||||||
| 143 | use parent 'DBIx::QuickORM::Row'; | ||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||
| 145 | sub display_name { | ||||||||||||||||||||||||||||||||||||||
| 146 | my $self = shift; | ||||||||||||||||||||||||||||||||||||||
| 147 | my $email = $self->field('email'); | ||||||||||||||||||||||||||||||||||||||
| 148 | return $self->field('name') . (defined($email) ? " <$email>" : ''); | ||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||
| 151 | package main; | ||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||
| 153 | my $orm = DBIx::QuickORM->new( | ||||||||||||||||||||||||||||||||||||||
| 154 | credentials => \%credentials, | ||||||||||||||||||||||||||||||||||||||
| 155 | row_classes => {users => 'My::User'}, | ||||||||||||||||||||||||||||||||||||||
| 156 | ); | ||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||
| 158 | my $who = $orm->insert(users => {name => 'Carol'})->display_name; | ||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||
| 160 | =head1 PUBLIC METHODS | ||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||
| 162 | A row has every method of L<DBIx::QuickORM::Role::Row>, which documents them: | ||||||||||||||||||||||||||||||||||||||
| 163 | C<orm>, C<table>, C<row_data>, C<set_row_data>, C<field>, C<fields>, | ||||||||||||||||||||||||||||||||||||||
| 164 | C<inflated_field>, C<inflated_fields>, C<raw_field>, C<raw_fields>, C<pk>, | ||||||||||||||||||||||||||||||||||||||
| 165 | C<update>, C<cas_update>, C<delete>, C<refresh>, C<is_canon>, C<is_in_db>, | ||||||||||||||||||||||||||||||||||||||
| 166 | C<was_deleted>, C<insert>, C<modify>, C<follow_fk>, C<insert_fk>, and | ||||||||||||||||||||||||||||||||||||||
| 167 | C<insert_many_fk>. It overrides one, C<fast_bless>: | ||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||
| 169 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||
| 171 | =item $bool = $class->fast_bless | ||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||
| 173 | True when the class builds rows exactly as a plain bless would: it has no | ||||||||||||||||||||||||||||||||||||||
| 174 | C<init> (nor HashBase pre- or post-init hooks), and its C<new> is still this | ||||||||||||||||||||||||||||||||||||||
| 175 | class's. The ORM then blesses rows directly, which is faster. | ||||||||||||||||||||||||||||||||||||||
| 176 | The ORM takes the answer once per row class per ORM. | ||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||
| 178 | A subclass that adds an C<init> or its own C<new> gets false, so its rows go | ||||||||||||||||||||||||||||||||||||||
| 179 | through C<new>. | ||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||
| 181 | =back | ||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||
| 183 | =cut | ||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||
| 185 | 47 | ▶ | sub fast_bless { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
fast_bless: called CC=6 SCAR=26.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
fast_bless: not called CC=6 SCAR=26.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | $class = ref($class) || $class; | ||||||||||||||||||||||||||||||||||||
|
Condition16%1/6
ref $class || $class
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | return 0 if $class->can('init'); | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | return 0 unless ($class->can('new') // 0) == \&DBIx::QuickORM::Row::new; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition25%1/4
$class->can("new") // 0
$class->can("new") // 0
MC/DC0%0/1
$class->can("new") // 0
$class->can("new")
MC/DC0%0/1
$class->can("new") // 0
$class->can("new")
Truth table50%1/2
A
A= $class->can("new")
Truth table0%0/2
A
A= $class->can("new")
| |||||||||||||||||||||||||||||||||||||||
| 191 | 0 | my @hooks = ($class->_pre_init, $class->_post_init); | |||||||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | return @hooks ? 0 : 1; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||
| 195 | 257 | 1; | |||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||
| 197 | __END__ | ||||||||||||||||||||||||||||||||||||||