blib/lib/DBIx/QuickORM/Role/Type.pm

filter: Statementstmt 100% 34 / 34 Branchbran 100% 6 / 6 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 100% 59 / 59 CC 5 SCAR 6.2
CC
5
Cov
100%
clone_inflated
3.0
_for_field
3.0
BEGIN
1.0
CRAP 1.9
« blib/lib/DBIx/QuickORM/Dialect/PostgreSQL.pm ↑ Summary blib/lib/DBIx/QuickORM/WhereBuilder/SQLAbstract.pm »
1 package DBIx::QuickORM::Role::Type;
2 51 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 51 our $VERSION = '0.000031';
6  
7 51 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 51 ▶use Storable();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 51 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =pod
13  
14 =encoding UTF-8
15  
16 =head1 NAME
17  
18 DBIx::QuickORM::Role::Type - The interface for field types.
19  
20 =head1 DESCRIPTION
21  
22 A type converts between the raw value a database stores and the inflated
23 value Perl code works with. A type is a class used through class methods. The
24 map names it by class (a field's C<orm_type>) and never holds objects. A type
25 needs C<inflate> and C<deflate>. This role supplies defaults for its other
26 methods.
27  
28 The CRUD operations deflate the values they send (see
29 L<DBIx::QuickORM/"CRUD operations">), and row objects inflate field values
30 when they are read (see L<DBIx::QuickORM::Role::Row>). NULL is never passed to
31 C<inflate> or C<deflate>.
32  
33 C<inflate> and C<deflate> get the map's record for the field and the
34 L<DBIx::QuickORM::Connection>, which gives the dialect
35 (C<< $conn->dialect >>) and the time zone (C<< $conn->time_zone >>).
36  
37 A field record has C<table> and C<name> (the field's own), C<affinity>,
38 C<db_type>, C<type>, C<size>, C<orm_type> (this type's class), the hints
39 C<format> and C<generate>, and the scan facts C<db_fills> and
40 C<db_refuses_value>.
41 Any key may be missing. A hand-written record may have only some of them.
42 C<_for_field> turns C<table> and C<name> into text for croaks.
43  
44 For C<autotype>, a type claims fields with C<base_types>, the base C<type>
45 names it takes, or C<matches($field_name, \%field, $dialect)>, for fields
46 known by name. It needs at least one of the two. A type used only through
47 C<types> needs neither. This role provides neither.
48  
49 =head2 Cached inflated values
50  
51 A row caches each value it inflates and hands a reader either the cached
52 value itself or a copy, so code changing a value it read cannot change what
53 the next reader gets. The type says which with
54 C<inflated_is_immutable_for_cache> and C<clone_inflated>.
55  
56 A custom type takes one of two paths:
57  
58 =over 4
59  
60 =item *
61  
62 It inflates to values nobody can change (plain strings, or objects that
63 hand out copies to change, as L<DBIx::QuickORM::Type::DateTime::Lazy>
64 does) and declares C<inflated_is_immutable_for_cache> true.
65  
66 =item *
67  
68 It leaves that false (the default), and provides C<clone_inflated> when
69 C<dclone> (L<Storable>) cannot copy its values.
70  
71 =back
72  
73 With it false, every read of a field of that type deep-copies the value (JSON
74 today). Code reading large values often may prefer C<raw_field>, or a type
75 with immutable values.
76  
77 A type class that does not consume this role must provide
78 C<inflated_is_immutable_for_cache>, and C<clone_inflated> unless it is true.
79  
80 =head2 Primary key fields
81  
82 A type on a primary key field needs C<canonical_key_value> (see the
83 SYNOPSIS), which this role does not provide. Building the map croaks for a
84 key field whose type lacks it.
85  
86 It returns the string the row cache files the key value under. It gets every
87 form a value can arrive in: database text, an inflated value, or what
88 C<deflate> returned. Every form of one value must give the same string. Keep
89 it fast: it runs for every row read from a table whose key it covers.
90  
91 A field with no type is keyed by its value as it is.
92  
93 =head2 Generated key values
94  
95 A type may provide C<generate_key_value($field, $conn)>, which this role does
96 not. It returns a new inflated value for a primary key field an insert left
97 out. The insert deflates it like any given value. The field's C<generate>
98 hint decides when it is called.
99  
100 L<DBIx::QuickORM::Type::UUID> provides one.
101  
102 =head2 Field hints
103  
104 A field may carry a C<format> code reference beside its type. After the type
105 deflates a value to plain text, the ORM sends what C<format> returns for it.
106 Binary fields are never formatted. Reads and the row cache are unchanged.
107  
108 # CREATE TABLE tokens (token CHAR(36) PRIMARY KEY, note TEXT)
109 # -- a legacy table: UUIDs in upper case
110 my $orm = DBIx::QuickORM->new(
111 credentials => \%credentials,
112 types => {
113 tokens => {
114 token => {
115 class => 'UUID',
116 format => sub { uc $_[0] },
117 },
118 },
119 },
120 );
121  
122 my $id = '0190a6e8-1d2c-7c3b-9f00-123456789abc';
123  
124 # stores 0190A6E8-...
125 my $token = $orm->insert(
126 tokens => {
127 token => $id,
128 note => 'legacy',
129 },
130 );
131  
132 # looks up 0190A6E8-...
133 my $same = $orm->find(
134 tokens => $id,
135 cache => 0,
136 );
137  
138 That gives one field a format. C<type_options> gives one to every field of a
139 type, and a field's own wins:
140  
141 type_options => {UUID => {format => sub { uc $_[0] }}},
142  
143 C<format> must be a code reference, on a field that has a type. C<lc> and
144 C<uc> are built-in functions, so C<\&lc> does not work: write
145 C<sub { lc $_[0] }>, or C<\&CORE::lc> on Perl 5.16 and later.
146  
147 C<generate> says when a primary key field an insert leaves out gets a value
148 from its type's C<generate_key_value> (see L</Generated key values>):
149  
150 =over 4
151  
152 =item C<prefer_db> (the default)
153  
154 When the database does not fill the column itself, or fills it on a backend
155 without C<RETURNING> on insert (MySQL), which could not return the value.
156 Auto-increment integer keys are unaffected: the last insert id reads them.
157  
158 =item C<prefer_orm>
159  
160 Always, even over a database default. Use it when the database's own values
161 are not the ones you want, such as UUIDs of another version.
162  
163 =item C<none>
164  
165 Never. On a backend without C<RETURNING>, a key that is not auto-increment
166 is then unknown after the insert, so call C<insert> in void context or pass
167 the key.
168  
169 =back
170  
171 It is given like C<format>: per field in C<types>, or per type in
172 C<type_options>, such as C<< {UUID => {generate => 'prefer_orm'}} >>.
173  
174 C<prefer_orm> on a column that refuses values (PostgreSQL C<GENERATED ALWAYS
175 AS IDENTITY>) croaks when the map is built. A type without
176 C<generate_key_value> ignores the hint.
177  
178 Whether the database fills a column comes from the scan. A map passed as
179 C<db_map> without those facts reads as "it does not", so C<prefer_db>
180 generates, and the C<prefer_orm> croak cannot fire.
181  
182 =head1 SYNOPSIS
183  
184 package My::Type::Email;
185 use Role::Tiny::With qw/with/;
186 with 'DBIx::QuickORM::Role::Type';
187  
188 # claims fields named email
189 sub matches { my ($class, $name, $field, $dialect) = @_; $name eq 'email' }
190  
191 sub inflate { my ($class, $raw, $field, $conn) = @_; $raw }
192 sub deflate { my ($class, $value, $field, $conn) = @_; lc $value }
193  
194 # needed on a primary key field
195 sub canonical_key_value { my ($class, $value, $field, $conn) = @_; lc $value }
196  
197 sub is_inflated { 1 } # deflate every plain value sent
198 sub inflated_is_immutable_for_cache { 1 } # plain strings: nothing to copy
199  
200 package main;
201  
202 my $orm = DBIx::QuickORM->new(
203 credentials => \%credentials,
204 append_autotype => ['+My::Type::Email'],
205 );
206  
207 my $ann = $orm->insert(
208 users => {
209 name => 'Ann',
210 email => 'Ann@Example.COM',
211 },
212 );
213  
214 my $email = $ann->field('email'); # ann@example.com
215  
216 =head1 REQUIRED METHODS
217  
218 =over 4
219  
220 =item $value = $type->inflate($raw, \%field, $conn)
221  
222 The inflated value for a raw value the database returned.
223  
224 =item $raw = $type->deflate($value, \%field, $conn)
225  
226 The raw value to send to the database for an inflated value.
227  
228 =back
229  
230 =cut
231  
232 51 requires qw/inflate deflate/;
233  
234 =head1 PUBLIC METHODS
235  
236 =over 4
237  
238 =item $bool = $type->is_inflated($value)
239  
240 Whether a plain (non-reference) value is in inflated form and must be
241 deflated before it is sent. Undef by default.
242  
243 True, false, or undef when the type cannot tell. The ORM deflates a plain
244 value only when this is true. A reference is always deflated.
245  
246 =back
247  
248 =cut
249  
250 1 ▶sub is_inflated { undef }
Subroutine is_inflated: called CC=1 SCAR=0.0
Pod documented
251  
252 =pod
253  
254 =over 4
255  
256 =item $bool = $type->inflated_is_immutable_for_cache
257  
258 True when nothing can change an inflated value, so a row may hand every reader
259 the one value it cached. False by default.
260  
261 False means a row must hand out a copy made by C<clone_inflated>.
262  
263 =back
264  
265 =cut
266  
267 20 ▶sub inflated_is_immutable_for_cache { 0 }
Subroutine inflated_is_immutable_for_cache: called CC=1 SCAR=0.0
Pod documented
268  
269 =pod
270  
271 =over 4
272  
273 =item $bool = $type->inflate_is_identity(\%field)
274  
275 True when C<inflate> returns its input unchanged for the field. False by
276 default.
277  
278 A subclass that overrides C<inflate> must override C<inflate_is_identity> too.
279  
280 The ORM asks once per field when it builds the map and marks such fields
281 C<passthrough> (see L<DBIx::QuickORM::Map>). Reading one then returns the raw
282 value with no C<inflate> call and nothing cached.
283  
284 =back
285  
286 =cut
287  
288 218 ▶sub inflate_is_identity { 0 }
Subroutine inflate_is_identity: called CC=1 SCAR=0.0
Pod documented
289  
290 =pod
291  
292 =over 4
293  
294 =item $copy = $type->clone_inflated($value)
295  
296 A copy of an inflated value that shares nothing changeable with it.
297  
298 Called on the type class with the value, which need not be an object. By
299 default a plain value is returned as it is and a reference is copied with
300 C<dclone> (L<Storable>), croaking when that cannot copy it (code references, for
301 instance).
302  
303 =back
304  
305 =cut
306  
307 24 ▶sub clone_inflated {
Subroutine clone_inflated: called CC=3 SCAR=11.0
Pod documented
308 24 my $class = shift;
309 24 my ($value) = @_;
310 24 ▶ return $value unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 4 20 24
311  
312 20 my $copy;
313 20 ▶ eval { $copy = Storable::dclone($value); 1 } or croak "$class cannot copy an inflated value with Storable::dclone, so it needs its own clone_inflated() or a true inflated_is_immutable_for_cache(): $@";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $copy = &Storable::dclone($value); 1 } } or croak("$class cannot copy an inflated value with Storable::dclone, so it needs its own clone_inflated() or a true inflated_is_immutable_for_cache(): $@") 19 1 20
314 19 return $copy;
315 }
316  
317 =head1 PRIVATE METHODS
318  
319 =over 4
320  
321 =item $text = $type->_for_field(\%field)
322  
323 The C<" for table.field"> suffix for a croak about a field: C<" for name">
324 when the record has a C<name> but no C<table>, and empty without a C<name>.
325 Use it as C<< croak "Bad value" . $class->_for_field($field) >>.
326  
327 =back
328  
329 =cut
330  
331 14 ▶sub _for_field {
Subroutine _for_field: called CC=3 SCAR=11.0
332 14 my $class = shift;
333 14 my ($field) = @_;
334 14 ▶ return '' unless $field && defined $field->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $field and defined $field->{"name"} 8 6 14
Condition100%3/3
$field and defined $field->{"name"}
!ll&&!rl&&r
176
MC/DC100%2/2
$field and defined $field->{"name"}
$field defined $field->{"name"}
Truth table100%3/3
A and B
A= $fieldB= defined $field->{"name"}
ABresult
0X0
100
111
335 6 return ' for ' . join('.' => grep { defined } $field->{table}, $field->{name});
336 }
337  
338 51 1;
339  
340 __END__
« blib/lib/DBIx/QuickORM/Dialect/PostgreSQL.pm ↑ Summary blib/lib/DBIx/QuickORM/WhereBuilder/SQLAbstract.pm »