blib/lib/DBIx/QuickORM/Manual/Background.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/Aggregates.pm ↑ Summary blib/lib/DBIx/QuickORM/Manual/BulkInsert.pm »
1 package DBIx::QuickORM::Manual::Background;
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::Background - Run queries in the background with async, aside, and forked, by example.
14  
15 =head1 DESCRIPTION
16  
17 C<async>, C<aside>, and C<forked> are handle modifiers. The first read
18 (C<iter>, C<ready>, or another) then starts the query without waiting.
19 Reading the rows waits for them.
20  
21 =head1 SCHEMA
22  
23 The examples use PostgreSQL and this schema:
24  
25 =for highlighter language=sql
26  
27 CREATE TABLE users (
28 id SERIAL PRIMARY KEY,
29 name TEXT NOT NULL,
30 email TEXT UNIQUE,
31 prefs JSON,
32 referred_by INTEGER REFERENCES users (id),
33 created TIMESTAMP NOT NULL DEFAULT now()
34 );
35  
36 CREATE TABLE posts (
37 id SERIAL PRIMARY KEY,
38 user_id INTEGER NOT NULL REFERENCES users (id),
39 title TEXT NOT NULL,
40 views INTEGER NOT NULL DEFAULT 0
41 );
42  
43 =head1 ASYNC
44  
45 =for highlighter language=perl
46  
47 my $it = $orm->handle('posts')
48 ->order_by('id')
49 ->async
50 ->iter; # the query starts, and iter returns at once
51  
52 do_something_else() until $it->ready; # polls, and collects the result when ready
53  
54 my @posts = $it->all;
55  
56 The query runs on the ORM's own connection. Until the iterator collects its
57 rows (C<ready> finding them, or any reading method), anything else that sends
58 SQL on that connection croaks:
59  
60 my $pending = $orm->handle('posts')->async->iter;
61  
62 # croaks: an async query is pending on this connection
63 my $count = eval { $orm->handle('users')->count };
64  
65 my @rows = $pending->all; # collected, and the connection is free again
66  
67 A C<find> answered from the canonical copy still works meanwhile
68 (L<DBIx::QuickORM::Manual::Caching>).
69  
70 Discard the handle and every iterator over its query before the rows are
71 collected and the query is given up: PostgreSQL is asked to cancel it, while
72 MySQL and MariaDB wait for the result and discard it
73 (L<DBIx::QuickORM::Manual::Dialects>). On PostgreSQL, giving a query up inside
74 a transaction aborts that transaction.
75  
76 =head1 ASIDE
77  
78 C<aside> is like C<async>, but runs on a second connection, so the ORM's own
79 connection stays free. It cannot be used inside a transaction. You can start
80 it, run a whole transaction on the main connection, then read its rows once
81 the transaction is done (L</AND TRANSACTIONS>).
82  
83 my $aside = $orm->handle('posts')->aside->iter; # on a second connection
84 my $count = $orm->handle('users')->count; # the ORM's own connection is free meanwhile
85 my @posts = $aside->all; # the second connection closes
86  
87 =head1 FORKED
88  
89 C<forked> is like C<async> and C<aside>, but runs in a separate child
90 process, on that process's own connection.
91  
92 # in a child process, on its own connection
93 my $forked = $orm->handle('posts')->forked->iter;
94 my $count = $orm->handle('users')->count;
95 my @posts = $forked->all; # the child sent the rows and exited
96  
97 C<forked> needs no async support from the database, only a second
98 connection, so it also works on a SQLite file.
99  
100 =head1 ON THE HANDLE OR A NEW ITERATOR
101  
102 my $h = $orm->handle('users')->async;
103 do_something_else() until $h->ready; # the first call starts the query
104 my @users = $h->all; # that same query's rows
105  
106 my $it = $h->iter; # a new iterator over the same query
107 $h->clear; # the next read starts another query
108  
109 A handle runs one query, started by its first read (C<iter> included), and
110 sends it once. Its reading methods and every iterator share that one
111 response, and collecting through any of them collects for all. Each iterator
112 keeps its own position. Each read builds a new row object, but the row
113 objects for one row all wrap the same row data hash. (With C<data_only> or
114 C<row_class(undef)>, every read hands out that hash itself.) C<clear> forgets
115 the query.
116  
117 With C<async>, starting a query croaks while another is pending on the
118 connection.
119  
120 =head1 AND TRANSACTIONS
121  
122 # aside and forked: start before the transaction, read after it
123 my $posts = $orm->handle('posts')->aside->iter;
124  
125 $orm->txn(sub {
126 $orm->insert(users => {name => 'Zoe'}); # another table meanwhile
127  
128 my @mine = $orm->handle('posts')
129 ->async
130 ->iter
131 ->all; # async: the transaction's own connection, sees its writes
132 });
133  
134 my @posts = $posts->all;
135  
136 # croaks: aside and forked cannot start, or be read, inside a transaction
137 my $ok = eval {
138 $orm->txn(sub { $orm->handle('posts')->aside->iter });
139 1;
140 };
141  
142 C<async> runs on the ORM's own connection, so inside a transaction it works
143 like any query: it sees the transaction's writes.
144  
145 C<aside> and C<forked> run on other connections. Starting one, or reading its
146 rows (C<ready> included), croaks while a transaction is open. One started
147 before a transaction may keep running during it. Read it after the
148 transaction ends.
149  
150 Rows read then are ingested like any others, and may be stale if the
151 transaction changed the same rows. Reading one table in the background while
152 a short transaction writes another is fine.
153  
154 Table rows with a primary key become canonical when collected, which
155 ordinary updates and deletes check by default
156 (L<DBIx::QuickORM::Row/"Keys and canonical data">,
157 L<DBIx::QuickORM::Manual::Caching>).
158  
159 An explicit C<rollback> croaks while an async query is pending. When the
160 transaction unwinds by itself (its block died, or its object went out of
161 scope) the query is given up, and later reads from the iterator croak.
162  
163 =head1 ERRORS
164  
165 # a scalar reference: a handle over SQL you write
166 my $bad = $orm->handle(\'SELECT * FROM no_such_table')->async->iter;
167  
168 # the database error is thrown here, and again on every later read
169 my $rows = eval { [$bad->all] };
170  
171 A forked child's error arrives the same way, as C<The forked query failed: ...>.
172 L<DBIx::QuickORM::Manual::Errors> covers database errors.
173  
174 =head1 FORKING YOUR OWN PROCESS
175  
176 my $pid = fork // die "Could not fork: $!";
177 unless ($pid) {
178 my $count = $orm->handle('users')->count; # the ORM reconnects in the child by itself
179 exit($count ? 0 : 1);
180 }
181 waitpid($pid, 0);
182  
183 Rows the child inherited are not canonical there: refresh one before writing
184 it (L<DBIx::QuickORM::Manual::Caching>). A pending iterator from the parent
185 cannot be collected in the child. On an ORM given a C<dbh>, on DuckDB, or on
186 an in-memory SQLite database the child cannot reconnect, and croaks.
187  
188 =head1 BACKEND SUPPORT
189  
190 my $it = $orm->handle('posts')
191 ->order_by('id')
192 ->maybe_async
193 ->iter; # async where the database can, otherwise an ordinary query
194  
195 do_something_else() until $it->ready; # the same iterator API either way
196 my @rows = $it->all;
197  
198 When the mode cannot run, C<maybe_async>, C<maybe_aside>, and C<maybe_forked>
199 leave the handle as it was: an ordinary handle runs an ordinary query on the
200 ORM's connection. Inside a transaction that query sees the
201 transaction's own uncommitted writes, where C<aside> and C<forked> croak.
202 C<can_async>, C<can_aside>, and C<can_forked> ask first.
203 L<DBIx::QuickORM::Manual::Dialects> has the other differences.
204  
205 =over 4
206  
207 =item C<async> and C<aside>
208  
209 PostgreSQL, MySQL, and MariaDB. SQLite and DuckDB croak.
210  
211 =item C<forked>
212  
213 Any database a second connection can reach: not DuckDB, and not an in-memory
214 SQLite database.
215  
216 =item An ORM given a C<dbh>
217  
218 It cannot open a second connection, so C<aside> and C<forked> croak, and a
219 forked child cannot use the ORM.
220  
221 =back
222  
223 =cut
224  
225 2 1;
226  
227 __END__
« blib/lib/DBIx/QuickORM/Manual/Aggregates.pm ↑ Summary blib/lib/DBIx/QuickORM/Manual/BulkInsert.pm »