| 1 | package DBIx::QuickORM::Manual::Values; | ||
| 2 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 2 | our $VERSION = '0.000032'; | |
| 6 | |||
| 7 | =pod | ||
| 8 | |||
| 9 | =encoding UTF-8 | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | DBIx::QuickORM::Manual::Values - How values move between Perl and the database: deflation, inflation, and the raw, inflated, binary, and sql markers, by example. | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | A value sent to the database by an insert or update is deflated through its | ||
| 18 | field's type: a hashref on a JSON field becomes JSON text, a DateTime becomes | ||
| 19 | the database's text (L<DBIx::QuickORM::Manual::Types>). The values of a | ||
| 20 | C<where> are sent as given, not deflated: pass database form, or wrap one in | ||
| 21 | C<inflated()>. A marker from L<DBIx::QuickORM::Value> changes the rule for one | ||
| 22 | value. | ||
| 23 | |||
| 24 | A value read back is inflated through the same type (L</INFLATION>). | ||
| 25 | |||
| 26 | =head1 SCHEMA | ||
| 27 | |||
| 28 | The examples use PostgreSQL and this schema: | ||
| 29 | |||
| 30 | =for highlighter language=sql | ||
| 31 | |||
| 32 | CREATE TABLE users ( | ||
| 33 | id SERIAL PRIMARY KEY, | ||
| 34 | name TEXT NOT NULL, | ||
| 35 | email TEXT UNIQUE, | ||
| 36 | prefs JSON, | ||
| 37 | referred_by INTEGER REFERENCES users (id), | ||
| 38 | created TIMESTAMP NOT NULL DEFAULT now() | ||
| 39 | ); | ||
| 40 | |||
| 41 | CREATE TABLE posts ( | ||
| 42 | id SERIAL PRIMARY KEY, | ||
| 43 | user_id INTEGER NOT NULL REFERENCES users (id), | ||
| 44 | title TEXT NOT NULL, | ||
| 45 | views INTEGER NOT NULL DEFAULT 0 | ||
| 46 | ); | ||
| 47 | |||
| 48 | CREATE TABLE files (id SERIAL PRIMARY KEY, name TEXT, data BYTEA); | ||
| 49 | CREATE TABLE tags (id SERIAL PRIMARY KEY, names TEXT[]); | ||
| 50 | |||
| 51 | =head1 DEFLATION | ||
| 52 | |||
| 53 | =for highlighter language=perl | ||
| 54 | |||
| 55 | use DateTime; | ||
| 56 | |||
| 57 | my $ann = $orm->insert(users => { | ||
| 58 | name => 'Ann', | ||
| 59 | prefs => {theme => 'dark'}, | ||
| 60 | created => DateTime->new( | ||
| 61 | year => 2024, | ||
| 62 | month => 1, | ||
| 63 | day => 2, | ||
| 64 | ), | ||
| 65 | }); | ||
| 66 | my $json = $ann->raw_field('prefs'); # '{"theme":"dark"}': what the database holds | ||
| 67 | my $text = $ann->raw_field('created'); # '2024-01-02 00:00:00' | ||
| 68 | |||
| 69 | my $bob = $orm->insert(users => { | ||
| 70 | name => 'Bob', | ||
| 71 | prefs => '{"theme":"light"}', # a plain value is sent as given: already JSON text | ||
| 72 | }); | ||
| 73 | |||
| 74 | A reference on a typed field is always deflated. A plain value is deflated | ||
| 75 | only when its type recognises it as an inflated value (the UUID type knows a | ||
| 76 | UUID string). A plain value on a JSON field is taken as JSON text already (is | ||
| 77 | C<123> a number or JSON?), so it goes as given. To store a Perl string as a | ||
| 78 | JSON string, encode it yourself (L<DBIx::QuickORM::Type::JSON>). | ||
| 79 | |||
| 80 | =head1 INFLATION | ||
| 81 | |||
| 82 | my $prefs = $ann->field('prefs'); # {theme => 'dark'}: inflated by the JSON type | ||
| 83 | my $day = $ann->field('created')->datetime_ref->day; # 2 | ||
| 84 | |||
| 85 | C<field> returns a typed field's value inflated by its type | ||
| 86 | (C<inflated_field>). A row class may change that default. C<raw_field> | ||
| 87 | returns it as the database returned it (L</DEFLATION>). An untyped field | ||
| 88 | gives the same from both. | ||
| 89 | |||
| 90 | See L<DBIx::QuickORM::Role::Row> for the row methods, and | ||
| 91 | L<DBIx::QuickORM::Manual::Types> to write a type. | ||
| 92 | |||
| 93 | =head1 SQL | ||
| 94 | |||
| 95 | use DBIx::QuickORM::Value qw/raw inflated binary sql/; | ||
| 96 | |||
| 97 | my $cy = $orm->insert(users => { | ||
| 98 | name => 'Cy', | ||
| 99 | created => sql('now() - ?::interval', '1 day'), # literal SQL, with binds | ||
| 100 | }); | ||
| 101 | |||
| 102 | # the value the database computed, read back through RETURNING | ||
| 103 | my $when = $cy->field('created'); | ||
| 104 | |||
| 105 | $cy->update({name => sql('upper(name)')}); | ||
| 106 | |||
| 107 | my $post = $orm->insert(posts => { | ||
| 108 | user_id => $cy->field('id'), | ||
| 109 | title => 'Hi', | ||
| 110 | }); | ||
| 111 | |||
| 112 | my $bumped = $orm->handle('posts') | ||
| 113 | ->where({user_id => $cy->field('id')}) | ||
| 114 | ->update({views => sql('views + 1')}); # 1: one row changed | ||
| 115 | |||
| 116 | my @recent = $orm->handle('users') | ||
| 117 | ->where({created => {'>=' => sql('now() - ?::interval', '1 hour')}}) | ||
| 118 | ->all; | ||
| 119 | |||
| 120 | Write the SQL yourself: nothing in it is quoted, so pass values through C<?> | ||
| 121 | placeholders. Their binds are sent without a declared type, so the database | ||
| 122 | decides. | ||
| 123 | |||
| 124 | C<sql()> is rejected in primary key lookups (L<DBIx::QuickORM::Manual::Keys>) | ||
| 125 | and compare-and-swap comparisons. In an insert or update it can set any field, | ||
| 126 | and C<RETURNING> reads the value back. Without it (MySQL, and MariaDB for | ||
| 127 | updates, see L<DBIx::QuickORM::Manual::Dialects/RETURNING>) the field is | ||
| 128 | fetched when first read, and setting a primary key with C<sql()> in an update | ||
| 129 | croaks, since the new key could not be known. | ||
| 130 | |||
| 131 | On MySQL, an insert that computes a single-field primary key with C<sql()> | ||
| 132 | runs, then croaks, since the key cannot be read back. An C<AUTO_INCREMENT> | ||
| 133 | key is the exception: MySQL reports the value it stored. | ||
| 134 | |||
| 135 | =head1 RAW | ||
| 136 | |||
| 137 | my $eve = $orm->insert( | ||
| 138 | users => { | ||
| 139 | name => 'Eve', | ||
| 140 | prefs => '{}', | ||
| 141 | created => '2024-01-02 03:04:05', | ||
| 142 | }, | ||
| 143 | raw => 1, # values are already in database form | ||
| 144 | ); | ||
| 145 | |||
| 146 | # a PostgreSQL array: a reference on a field without a type, sent as is | ||
| 147 | my $tag = $orm->insert(tags => {names => raw(['perl', 'sql'])}); | ||
| 148 | |||
| 149 | C<raw> sends a value as it is, whatever its field's type would do with it. | ||
| 150 | Without a marker or C<< raw => 1 >>, a reference on a field without a type | ||
| 151 | croaks, since nothing can deflate it. | ||
| 152 | |||
| 153 | C<< raw => 1 >> is an option of C<insert>, C<insert_many> | ||
| 154 | (L<DBIx::QuickORM::Manual::BulkInsert>), and the CRUD operations. | ||
| 155 | A row's C<update> takes none: mark each value instead. | ||
| 156 | |||
| 157 | $eve->update({created => raw('2024-02-03 04:05:06')}); | ||
| 158 | |||
| 159 | =head1 INFLATED | ||
| 160 | |||
| 161 | my $fay = $orm->insert( | ||
| 162 | users => { | ||
| 163 | name => 'Fay', | ||
| 164 | created => '2024-01-02 03:04:05', | ||
| 165 | prefs => inflated({theme => 'dark'}), # deflate this one anyway | ||
| 166 | }, | ||
| 167 | raw => 1, | ||
| 168 | ); | ||
| 169 | |||
| 170 | use DateTime; | ||
| 171 | |||
| 172 | my $cutoff = DateTime->new( | ||
| 173 | year => 2024, | ||
| 174 | month => 6, | ||
| 175 | day => 1, | ||
| 176 | ); | ||
| 177 | |||
| 178 | my @before = $orm->handle('users') | ||
| 179 | # a where sends values as given: mark one to deflate | ||
| 180 | ->where({created => {'<' => inflated($cutoff)}}) | ||
| 181 | ->order_by('id') | ||
| 182 | ->all; | ||
| 183 | |||
| 184 | =head1 BINARY | ||
| 185 | |||
| 186 | my $bytes = pack('C*', 0, 255, 10, 13); | ||
| 187 | my $file = $orm->insert(files => { | ||
| 188 | name => 'blob', | ||
| 189 | data => $bytes, # a BYTEA field is bound as binary automatically | ||
| 190 | }); | ||
| 191 | |||
| 192 | # so is a where on the table | ||
| 193 | my $same = $orm->handle('files')->where({data => $bytes})->only; | ||
| 194 | |||
| 195 | # raw SQL has no column information, so mark the bytes | ||
| 196 | my $found = $orm->handle(\'SELECT * FROM files')->where({data => binary($bytes)})->only; | ||
| 197 | |||
| 198 | =head1 WHICH ONE | ||
| 199 | |||
| 200 | =over 4 | ||
| 201 | |||
| 202 | =item * | ||
| 203 | |||
| 204 | On insert or update, pass a hashref for a JSON field or a DateTime for a | ||
| 205 | timestamp: the type deflates them. Text already in database form goes | ||
| 206 | directly. | ||
| 207 | |||
| 208 | =item * | ||
| 209 | |||
| 210 | A value the database should compute: C<sql>. | ||
| 211 | |||
| 212 | =item * | ||
| 213 | |||
| 214 | A reference on a field without a type, or a value its type must not touch: | ||
| 215 | C<raw>. | ||
| 216 | |||
| 217 | =item * | ||
| 218 | |||
| 219 | Inflated data under C<< raw => 1 >>, or in a C<where>: C<inflated>. | ||
| 220 | |||
| 221 | =item * | ||
| 222 | |||
| 223 | Bytes in a query over raw SQL: C<binary>. | ||
| 224 | |||
| 225 | =back | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | 2 | 1; | |
| 230 | |||
| 231 | __END__ | ||