| 1 | package DBIx::QuickORM::Role::Type; | ||||||||||||||||||||||||||||
| 2 | 85 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 85 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 85 | our $VERSION = '0.000033'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 85 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 85 | ▶ | use Storable(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 85 | ▶ | 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 (the SYNOPSIS shows C<matches>): | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # claims every inet column | ||||||||||||||||||||||||||||
| 49 | sub base_types { 'inet' } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | It needs at least one of the two. A type used only through | ||||||||||||||||||||||||||||
| 52 | C<types> needs neither. This role provides neither. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head2 Cached inflated values | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | A row hands every reader a value of its own, so code changing a value it read | ||||||||||||||||||||||||||||
| 57 | cannot change what the next reader gets. The type says how with | ||||||||||||||||||||||||||||
| 58 | C<inflated_is_immutable_for_cache>, C<inflate_on_each_read>, and | ||||||||||||||||||||||||||||
| 59 | C<clone_inflated>. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | A custom type takes one of three paths: | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =over 4 | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =item * | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | It inflates to values nobody can change (plain strings, or objects that | ||||||||||||||||||||||||||||
| 68 | hand out copies to change, as L<DBIx::QuickORM::Type::DateTime::Lazy> | ||||||||||||||||||||||||||||
| 69 | does) and declares C<inflated_is_immutable_for_cache> true. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =item * | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | It declares C<inflate_on_each_read> true when inflating the raw value again | ||||||||||||||||||||||||||||
| 74 | is cheaper than copying an inflated one. Rows then inflate on every read and | ||||||||||||||||||||||||||||
| 75 | cache nothing for the field. L<DBIx::QuickORM::Type::JSON> does this: decoding | ||||||||||||||||||||||||||||
| 76 | JSON text was faster than deep-copying the decoded data at every size | ||||||||||||||||||||||||||||
| 77 | measured, from 23 bytes to 6 KB. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =item * | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | It leaves both false (the default). Rows cache the inflated value and hand out | ||||||||||||||||||||||||||||
| 82 | C<clone_inflated> copies. It provides C<clone_inflated> when C<dclone> | ||||||||||||||||||||||||||||
| 83 | (L<Storable>) cannot copy its values. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =back | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | C<inflated_is_immutable_for_cache> wins over C<inflate_on_each_read>. To pick | ||||||||||||||||||||||||||||
| 88 | between copying and inflating again, time both on realistic values. Code | ||||||||||||||||||||||||||||
| 89 | reading large values often may prefer C<raw_field>, or a type with immutable | ||||||||||||||||||||||||||||
| 90 | values. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | A type class that does not consume this role must provide | ||||||||||||||||||||||||||||
| 93 | C<inflated_is_immutable_for_cache>, and C<clone_inflated> unless it is true or | ||||||||||||||||||||||||||||
| 94 | the class provides a true C<inflate_on_each_read>. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head2 Primary key fields | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | A type on a primary key field needs C<canonical_key_value> (see the | ||||||||||||||||||||||||||||
| 99 | SYNOPSIS), which this role does not provide. Building the map croaks for a | ||||||||||||||||||||||||||||
| 100 | key field whose type lacks it. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | It returns the string the row cache files the key value under. It gets every | ||||||||||||||||||||||||||||
| 103 | form a value can arrive in: database text, an inflated value, or what | ||||||||||||||||||||||||||||
| 104 | C<deflate> returned. Every form of one value must give the same string. Keep | ||||||||||||||||||||||||||||
| 105 | it fast: it runs for every row read from a table whose key it covers. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | A field with no type is keyed by its value as it is. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head2 Generated key values | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | A type may provide C<generate_key_value(\%field, $conn)>, which this role does | ||||||||||||||||||||||||||||
| 112 | not. It returns a new inflated value for a primary key field an insert left | ||||||||||||||||||||||||||||
| 113 | out. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | package My::Type::RequestId; # 32 hex digits, stored in lower case | ||||||||||||||||||||||||||||
| 116 | use Role::Tiny::With qw/with/; | ||||||||||||||||||||||||||||
| 117 | with 'DBIx::QuickORM::Role::Type'; | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub inflate { my ($class, $raw, $field, $conn) = @_; $raw } | ||||||||||||||||||||||||||||
| 120 | sub deflate { my ($class, $value, $field, $conn) = @_; lc $value } | ||||||||||||||||||||||||||||
| 121 | sub canonical_key_value { my ($class, $value, $field, $conn) = @_; lc $value } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | sub is_inflated { 1 } # deflate every plain value sent, given or generated | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # an id, not a secret: rand is predictable | ||||||||||||||||||||||||||||
| 126 | sub generate_key_value { | ||||||||||||||||||||||||||||
| 127 | my ($class, $field, $conn) = @_; | ||||||||||||||||||||||||||||
| 128 | return join '' => map { sprintf('%02X', int rand 256) } 1 .. 16; | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | package main; | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # CREATE TABLE requests (id CHAR(32) PRIMARY KEY, note TEXT) | ||||||||||||||||||||||||||||
| 134 | my $orm = DBIx::QuickORM->new( | ||||||||||||||||||||||||||||
| 135 | credentials => \%credentials, | ||||||||||||||||||||||||||||
| 136 | types => {requests => {id => '+My::Type::RequestId'}}, | ||||||||||||||||||||||||||||
| 137 | ); | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | my $row = $orm->insert(requests => {note => 'ci'}); | ||||||||||||||||||||||||||||
| 140 | my $id = $row->field('id'); # 32 new hex digits, in lower case | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | my $given = $orm->insert( | ||||||||||||||||||||||||||||
| 143 | requests => { | ||||||||||||||||||||||||||||
| 144 | id => 'ABCDEF0123456789ABCDEF0123456789', | ||||||||||||||||||||||||||||
| 145 | note => 'given', | ||||||||||||||||||||||||||||
| 146 | }, | ||||||||||||||||||||||||||||
| 147 | ); # stored in lower case | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | The insert deflates it like any given value. The field's C<generate> hint | ||||||||||||||||||||||||||||
| 150 | decides when it is called. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | L<DBIx::QuickORM::Type::UUID> provides one. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head2 Field hints | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | A field may carry a C<format> code reference beside its type. After the type | ||||||||||||||||||||||||||||
| 157 | deflates a value to plain text, the ORM sends what C<format> returns for it. | ||||||||||||||||||||||||||||
| 158 | Binary fields are never formatted. Reads and the row cache are unchanged. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # CREATE TABLE tokens (token CHAR(36) PRIMARY KEY, note TEXT) | ||||||||||||||||||||||||||||
| 161 | # -- a legacy table: UUIDs in upper case | ||||||||||||||||||||||||||||
| 162 | my $orm = DBIx::QuickORM->new( | ||||||||||||||||||||||||||||
| 163 | credentials => \%credentials, | ||||||||||||||||||||||||||||
| 164 | types => { | ||||||||||||||||||||||||||||
| 165 | tokens => { | ||||||||||||||||||||||||||||
| 166 | token => { | ||||||||||||||||||||||||||||
| 167 | class => 'UUID', | ||||||||||||||||||||||||||||
| 168 | format => sub { uc $_[0] }, | ||||||||||||||||||||||||||||
| 169 | }, | ||||||||||||||||||||||||||||
| 170 | }, | ||||||||||||||||||||||||||||
| 171 | }, | ||||||||||||||||||||||||||||
| 172 | ); | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | my $id = '0190a6e8-1d2c-7c3b-9f00-123456789abc'; | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # stores 0190A6E8-... | ||||||||||||||||||||||||||||
| 177 | my $token = $orm->insert( | ||||||||||||||||||||||||||||
| 178 | tokens => { | ||||||||||||||||||||||||||||
| 179 | token => $id, | ||||||||||||||||||||||||||||
| 180 | note => 'legacy', | ||||||||||||||||||||||||||||
| 181 | }, | ||||||||||||||||||||||||||||
| 182 | ); | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # looks up 0190A6E8-... | ||||||||||||||||||||||||||||
| 185 | my $same = $orm->find( | ||||||||||||||||||||||||||||
| 186 | tokens => $id, | ||||||||||||||||||||||||||||
| 187 | cache => 0, | ||||||||||||||||||||||||||||
| 188 | ); | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | That gives one field a format. C<type_options> gives one to every field of a | ||||||||||||||||||||||||||||
| 191 | type, and a field's own wins: | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | type_options => {UUID => {format => sub { uc $_[0] }}}, | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | C<format> must be a code reference, on a field that has a type. C<lc> and | ||||||||||||||||||||||||||||
| 196 | C<uc> are built-in functions, so C<\&lc> does not work: write | ||||||||||||||||||||||||||||
| 197 | C<sub { lc $_[0] }>, or C<\&CORE::lc> on Perl 5.16 and later. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | C<generate> says when a primary key field an insert leaves out gets a value | ||||||||||||||||||||||||||||
| 200 | from its type's C<generate_key_value> (see L</Generated key values>): | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =over 4 | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =item C<prefer_db> (the default) | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | When the database does not fill the column itself, or fills it on a backend | ||||||||||||||||||||||||||||
| 207 | without C<RETURNING> on insert (MySQL), which could not return the value. | ||||||||||||||||||||||||||||
| 208 | Auto-increment integer keys are unaffected: the last insert id reads them. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =item C<prefer_orm> | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | Always, even over a database default. Use it when the database's own values | ||||||||||||||||||||||||||||
| 213 | are not the ones you want, such as UUIDs of another version. | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =item C<none> | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | Never. On a backend without C<RETURNING>, a key that is not auto-increment | ||||||||||||||||||||||||||||
| 218 | is then unknown after the insert, so call C<insert> in void context or pass | ||||||||||||||||||||||||||||
| 219 | the key. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =back | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | It is given like C<format>: per field in C<types>, or per type in | ||||||||||||||||||||||||||||
| 224 | C<type_options>, such as C<< {UUID => {generate => 'prefer_orm'}} >>. | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | C<prefer_orm> on a column that refuses values (PostgreSQL C<GENERATED ALWAYS | ||||||||||||||||||||||||||||
| 227 | AS IDENTITY>) croaks when the map is built. A type without | ||||||||||||||||||||||||||||
| 228 | C<generate_key_value> ignores the hint. | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | Whether the database fills a column comes from the scan. A map passed as | ||||||||||||||||||||||||||||
| 231 | C<db_map> without those facts reads as "it does not", so C<prefer_db> | ||||||||||||||||||||||||||||
| 232 | generates, and the C<prefer_orm> croak cannot fire. | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | package My::Type::Email; | ||||||||||||||||||||||||||||
| 237 | use Role::Tiny::With qw/with/; | ||||||||||||||||||||||||||||
| 238 | with 'DBIx::QuickORM::Role::Type'; | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | # claims fields named email | ||||||||||||||||||||||||||||
| 241 | sub matches { my ($class, $name, $field, $dialect) = @_; $name eq 'email' } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | sub inflate { my ($class, $raw, $field, $conn) = @_; $raw } | ||||||||||||||||||||||||||||
| 244 | sub deflate { my ($class, $value, $field, $conn) = @_; lc $value } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | # needed on a primary key field | ||||||||||||||||||||||||||||
| 247 | sub canonical_key_value { my ($class, $value, $field, $conn) = @_; lc $value } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | sub is_inflated { 1 } # deflate every plain value sent | ||||||||||||||||||||||||||||
| 250 | sub inflated_is_immutable_for_cache { 1 } # plain strings: nothing to copy | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | package main; | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | my $orm = DBIx::QuickORM->new( | ||||||||||||||||||||||||||||
| 255 | credentials => \%credentials, | ||||||||||||||||||||||||||||
| 256 | append_autotype => ['+My::Type::Email'], | ||||||||||||||||||||||||||||
| 257 | ); | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | my $ann = $orm->insert( | ||||||||||||||||||||||||||||
| 260 | users => { | ||||||||||||||||||||||||||||
| 261 | name => 'Ann', | ||||||||||||||||||||||||||||
| 262 | email => 'Ann@Example.COM', | ||||||||||||||||||||||||||||
| 263 | }, | ||||||||||||||||||||||||||||
| 264 | ); | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | my $email = $ann->field('email'); # ann@example.com | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | =head1 REQUIRED METHODS | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | =over 4 | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | =item $value = $type->inflate($raw, \%field, $conn) | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | The inflated value for a raw value the database returned. | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =item $raw = $type->deflate($value, \%field, $conn) | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | The raw value to send to the database for an inflated value. | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | =back | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | =cut | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 85 | requires qw/inflate deflate/; | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | =head1 PUBLIC METHODS | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =over 4 | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | =item $bool = $type->is_inflated($value) | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | Whether a plain (non-reference) value is in inflated form and must be | ||||||||||||||||||||||||||||
| 293 | deflated before it is sent. | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | # dates written 2024-02-03 are inflated, stored ones (20240203) are not | ||||||||||||||||||||||||||||
| 296 | sub is_inflated { | ||||||||||||||||||||||||||||
| 297 | my ($class, $value) = @_; | ||||||||||||||||||||||||||||
| 298 | return $value =~ m/^\d{4}-\d\d-\d\d\z/ ? 1 : 0; | ||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | Undef by default, so C<deflate> never sees an unmarked plain value unless | ||||||||||||||||||||||||||||
| 302 | this says so. A value marked C<inflated($value)> (L<DBIx::QuickORM::Value>) | ||||||||||||||||||||||||||||
| 303 | is deflated whatever this says, even under C<< raw => 1 >>. | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | True, false, or undef when the type cannot tell. The ORM deflates a plain | ||||||||||||||||||||||||||||
| 306 | value only when this is true. A reference is always deflated. | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =back | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | =cut | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 1 | ▶ | sub is_inflated { undef } | ||||||||||||||||||||||||||
|
Subroutine
is_inflated: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | =pod | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | =over 4 | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | =item $db_type = $type->db_type_for($dialect) | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | The column type a temporary table or schema building declares for a field | ||||||||||||||||||||||||||||
| 321 | of this type, when the field gives no C<db_type> of its own. | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | # text, but a column MySQL and MariaDB can index whole | ||||||||||||||||||||||||||||
| 324 | sub db_type_for { | ||||||||||||||||||||||||||||
| 325 | my ($class, $dialect) = @_; | ||||||||||||||||||||||||||||
| 326 | return $dialect->name =~ m/^(?:MySQL|MariaDB)\z/ ? 'varchar(255)' : 'text'; | ||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | Undef by default, which leaves the field needing one. | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | C<$dialect> is a L<DBIx::QuickORM::Dialect> object: the Connection's, or | ||||||||||||||||||||||||||||
| 332 | the one schema building writes for. The answer can depend on its C<name> | ||||||||||||||||||||||||||||
| 333 | and C<server_version>. | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | =back | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | =cut | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 2 | ▶ | sub db_type_for { undef } | ||||||||||||||||||||||||||
|
Subroutine
db_type_for: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | =pod | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | =over 4 | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | =item $bool = $type->inflated_is_immutable_for_cache | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | True when nothing can change an inflated value, so a row may hand every reader | ||||||||||||||||||||||||||||
| 348 | the one value it cached. False by default. | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | False means a row must hand out a value of its own: inflated again when | ||||||||||||||||||||||||||||
| 351 | C<inflate_on_each_read> is true, otherwise a copy made by C<clone_inflated>. | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | =back | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | =cut | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 43 | ▶ | sub inflated_is_immutable_for_cache { 0 } | ||||||||||||||||||||||||||
|
Subroutine
inflated_is_immutable_for_cache: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =pod | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | =over 4 | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | =item $bool = $type->inflate_on_each_read | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | True when inflating the raw value again is cheaper than copying an inflated | ||||||||||||||||||||||||||||
| 366 | one, so a row inflates on every read and caches nothing for the field. False | ||||||||||||||||||||||||||||
| 367 | by default. | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | Rows ask it only when C<inflated_is_immutable_for_cache> is false. | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | =back | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | =cut | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 0 | ▶ | sub inflate_on_each_read { 0 } | ||||||||||||||||||||||||||
|
Subroutine
inflate_on_each_read: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | =pod | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =over 4 | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | =item $bool = $type->inflate_is_identity(\%field) | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | True when C<inflate> returns its input unchanged for the field. | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | # as UUID does: inflate changes binary values only | ||||||||||||||||||||||||||||
| 386 | sub inflate_is_identity { | ||||||||||||||||||||||||||||
| 387 | my ($class, $field) = @_; | ||||||||||||||||||||||||||||
| 388 | return ($field->{affinity} // '') ne 'binary'; | ||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | False by default. | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | A subclass that overrides C<inflate> must override C<inflate_is_identity> too. | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | The ORM asks once per field when it builds the map and marks such fields | ||||||||||||||||||||||||||||
| 396 | C<passthrough> (see L<DBIx::QuickORM::Map>). Reading one then returns the raw | ||||||||||||||||||||||||||||
| 397 | value with no C<inflate> call and nothing cached. | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | =back | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =cut | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | 319 | ▶ | sub inflate_is_identity { 0 } | ||||||||||||||||||||||||||
|
Subroutine
inflate_is_identity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | =pod | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | =over 4 | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | =item $copy = $type->clone_inflated($value) | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | A copy of an inflated value that shares nothing changeable with it. | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | package My::Type::Tags; # 'a,b' text as ['a', 'b'] | ||||||||||||||||||||||||||||
| 414 | use Carp qw/croak/; | ||||||||||||||||||||||||||||
| 415 | use Role::Tiny::With qw/with/; | ||||||||||||||||||||||||||||
| 416 | with 'DBIx::QuickORM::Role::Type'; | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | sub inflate { my ($class, $raw, $field, $conn) = @_; [split /,/ => $raw] } | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | # a tag must be non-empty and hold no comma, or it would not read back | ||||||||||||||||||||||||||||
| 421 | sub deflate { | ||||||||||||||||||||||||||||
| 422 | my ($class, $value, $field, $conn) = @_; | ||||||||||||||||||||||||||||
| 423 | croak "A tag must be non-empty and hold no comma" if grep { !length || m/,/ } @$value; | ||||||||||||||||||||||||||||
| 424 | return join ',' => @$value; | ||||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | # an array of plain strings: a shallow copy is enough, and faster than dclone | ||||||||||||||||||||||||||||
| 428 | sub clone_inflated { my ($class, $value) = @_; [@$value] } | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | package main; | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | my $temp = $orm->temp_table(sub { | ||||||||||||||||||||||||||||
| 433 | my $t = shift; | ||||||||||||||||||||||||||||
| 434 | $t->add_column('id', { | ||||||||||||||||||||||||||||
| 435 | db_type => 'integer', | ||||||||||||||||||||||||||||
| 436 | primary_key => 1, | ||||||||||||||||||||||||||||
| 437 | }); | ||||||||||||||||||||||||||||
| 438 | $t->add_column('tags', { | ||||||||||||||||||||||||||||
| 439 | db_type => 'text', | ||||||||||||||||||||||||||||
| 440 | type => '+My::Type::Tags', | ||||||||||||||||||||||||||||
| 441 | }); | ||||||||||||||||||||||||||||
| 442 | }); | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | my $row = $temp->handle->insert({ | ||||||||||||||||||||||||||||
| 445 | id => 1, | ||||||||||||||||||||||||||||
| 446 | tags => ['a', 'b'], | ||||||||||||||||||||||||||||
| 447 | }); | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | push @{$row->field('tags')} => 'c'; # changes a copy | ||||||||||||||||||||||||||||
| 450 | my $tags = $row->field('tags'); # still ['a', 'b'] | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | Called on the type class with the value, which need not be an object. By | ||||||||||||||||||||||||||||
| 453 | default a plain value is returned as it is and a reference is copied with | ||||||||||||||||||||||||||||
| 454 | C<dclone> (L<Storable>), croaking when that cannot copy it (code references, for | ||||||||||||||||||||||||||||
| 455 | instance). | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | =back | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | =cut | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 8 | ▶ | sub clone_inflated { | ||||||||||||||||||||||||||
|
Subroutine
clone_inflated: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 462 | 8 | my $class = shift; | |||||||||||||||||||||||||||
| 463 | 8 | my ($value) = @_; | |||||||||||||||||||||||||||
| 464 | 8 | ▶ | return $value unless ref $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | 6 | my $copy; | |||||||||||||||||||||||||||
| 467 | 6 | ▶ | 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
| |||||||||||||||||||||||||||||
| 468 | 5 | return $copy; | |||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | =head1 PRIVATE METHODS | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | =over 4 | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | =item $text = $type->_for_field(\%field) | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | The C<" for table.field"> suffix for a croak about a field: C<" for name"> | ||||||||||||||||||||||||||||
| 478 | when the record has a C<name> but no C<table>, and empty without a C<name>. | ||||||||||||||||||||||||||||
| 479 | Use it as C<< croak "Bad value" . $class->_for_field($field) >>. | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | =back | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | =cut | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | 14 | ▶ | sub _for_field { | ||||||||||||||||||||||||||
|
Subroutine
_for_field: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 486 | 14 | my $class = shift; | |||||||||||||||||||||||||||
| 487 | 14 | my ($field) = @_; | |||||||||||||||||||||||||||
| 488 | 14 | ▶ | return '' unless $field && defined $field->{name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$field and defined $field->{"name"}
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"}
| |||||||||||||||||||||||||||||
| 489 | 6 | return ' for ' . join('.' => grep { defined } $field->{table}, $field->{name}); | |||||||||||||||||||||||||||
| 490 | } | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | 85 | 1; | |||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | __END__ | ||||||||||||||||||||||||||||