blib/lib/DBIx/QuickORM/Manual/Values.pm

filter: Statementstmt 100% 8 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 100% 10 / 10 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/DBIx/QuickORM/Manual/Types.pm ↑ Summary blib/lib/DBIx/QuickORM/Role.pm »
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.000031';
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 my $ann = $orm->insert(users => {
56 name => 'Ann',
57 prefs => {theme => 'dark'},
58 created => DateTime->new(
59 year => 2024,
60 month => 1,
61 day => 2,
62 ),
63 });
64 my $json = $ann->raw_field('prefs'); # '{"theme":"dark"}': what the database holds
65 my $text = $ann->raw_field('created'); # '2024-01-02 00:00:00'
66  
67 my $bob = $orm->insert(users => {
68 name => 'Bob',
69 prefs => '{"theme":"light"}', # a plain value is sent as given: already JSON text
70 });
71  
72 A reference on a typed field is always deflated. A plain value is deflated
73 only when its type recognises it as an inflated value (the UUID type knows a
74 UUID string). A plain value on a JSON field is taken as JSON text already (is
75 C<123> a number or JSON?), so it goes as given. To store a Perl string as a
76 JSON string, encode it yourself (L<DBIx::QuickORM::Type::JSON>).
77  
78 =head1 INFLATION
79  
80 my $prefs = $ann->field('prefs'); # {theme => 'dark'}: inflated by the JSON type
81 my $day = $ann->field('created')->datetime_ref->day; # 2
82  
83 C<field> returns a typed field's value inflated by its type
84 (C<inflated_field>). A row class may change that default. C<raw_field>
85 returns it as the database returned it (L</DEFLATION>). An untyped field
86 gives the same from both.
87  
88 See L<DBIx::QuickORM::Role::Row> for the row methods, and
89 L<DBIx::QuickORM::Manual::Types> to write a type.
90  
91 =head1 SQL
92  
93 use DBIx::QuickORM::Value qw/raw inflated binary sql/;
94  
95 my $cy = $orm->insert(users => {
96 name => 'Cy',
97 created => sql('now() - ?::interval', '1 day'), # literal SQL, with binds
98 });
99  
100 # the value the database computed, read back through RETURNING
101 my $when = $cy->field('created');
102  
103 $cy->update({name => sql('upper(name)')});
104  
105 my $post = $orm->insert(posts => {
106 user_id => $cy->field('id'),
107 title => 'Hi',
108 });
109  
110 my $bumped = $orm->handle('posts')
111 ->where({user_id => $cy->field('id')})
112 ->update({views => sql('views + 1')}); # 1: one row changed
113  
114 my @recent = $orm->handle('users')
115 ->where({created => {'>=' => sql('now() - ?::interval', '1 hour')}})
116 ->all;
117  
118 Write the SQL yourself: nothing in it is quoted, so pass values through C<?>
119 placeholders. Their binds are sent without a declared type, so the database
120 decides.
121  
122 C<sql()> is rejected in primary key lookups (L<DBIx::QuickORM::Manual::Keys>)
123 and compare-and-swap comparisons. In an insert or update it can set any field,
124 and C<RETURNING> reads the value back. Without it (MySQL, and MariaDB for
125 updates, see L<DBIx::QuickORM::Manual::Dialects/RETURNING>) the field is
126 fetched when first read, and setting a primary key with C<sql()> in an update
127 croaks, since the new key could not be known.
128  
129 On MySQL, an insert that computes a single-field primary key with C<sql()>
130 runs, then croaks, since the key cannot be read back. An C<AUTO_INCREMENT>
131 key is the exception: MySQL reports the value it stored.
132  
133 =head1 RAW
134  
135 my $eve = $orm->insert(
136 users => {
137 name => 'Eve',
138 prefs => '{}',
139 created => '2024-01-02 03:04:05',
140 },
141 raw => 1, # values are already in database form
142 );
143  
144 # a PostgreSQL array: a reference on a field without a type, sent as is
145 my $tag = $orm->insert(tags => {names => raw(['perl', 'sql'])});
146  
147 C<raw> sends a value as it is, whatever its field's type would do with it.
148 Without a marker or C<< raw => 1 >>, a reference on a field without a type
149 croaks, since nothing can deflate it.
150  
151 C<< raw => 1 >> is an option of C<insert>, C<insert_many>
152 (L<DBIx::QuickORM::Manual::BulkInsert>), and the CRUD operations.
153 A row's C<update> takes none: mark each value instead.
154  
155 $eve->update({created => raw('2024-02-03 04:05:06')});
156  
157 =head1 INFLATED
158  
159 my $fay = $orm->insert(
160 users => {
161 name => 'Fay',
162 created => '2024-01-02 03:04:05',
163 prefs => inflated({theme => 'dark'}), # deflate this one anyway
164 },
165 raw => 1,
166 );
167  
168 my $cutoff = DateTime->new(
169 year => 2024,
170 month => 6,
171 day => 1,
172 );
173  
174 my @before = $orm->handle('users')
175 # a where sends values as given: mark one to deflate
176 ->where({created => {'<' => inflated($cutoff)}})
177 ->order_by('id')
178 ->all;
179  
180 =head1 BINARY
181  
182 my $bytes = pack('C*', 0, 255, 10, 13);
183 my $file = $orm->insert(files => {
184 name => 'blob',
185 data => $bytes, # a BYTEA field is bound as binary automatically
186 });
187  
188 # so is a where on the table
189 my $same = $orm->handle('files')->where({data => $bytes})->only;
190  
191 # raw SQL has no column information, so mark the bytes
192 my $found = $orm->handle(\'SELECT * FROM files')->where({data => binary($bytes)})->only;
193  
194 =head1 WHICH ONE
195  
196 =over 4
197  
198 =item *
199  
200 On insert or update, pass a hashref for a JSON field or a DateTime for a
201 timestamp: the type deflates them. Text already in database form goes
202 directly.
203  
204 =item *
205  
206 A value the database should compute: C<sql>.
207  
208 =item *
209  
210 A reference on a field without a type, or a value its type must not touch:
211 C<raw>.
212  
213 =item *
214  
215 Inflated data under C<< raw => 1 >>, or in a C<where>: C<inflated>.
216  
217 =item *
218  
219 Bytes in a query over raw SQL: C<binary>.
220  
221 =back
222  
223 =cut
224  
225 2 1;
226  
227 __END__
« blib/lib/DBIx/QuickORM/Manual/Types.pm ↑ Summary blib/lib/DBIx/QuickORM/Role.pm »