include/punk/punk_dbil.h

filter: Statementstmt 87.9% 397 / 463 (10 uncoverable) Branchbran 61.5% 187 / 364 (49 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.3% 584 / 827 (59 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/stream.xs ↑ Summary include/punk/punk_csrf.h »
1 /* punk_dbil.h - Punk::Model::DBIx::Loop, the async model backend, in C.
2 *
3 * The same six-method contract as Punk::Model::DBI, but every method returns a
4 * Punk::Future instead of a value: the statement runs on DBIx::Loop (a worker
5 * pool, or native fd async on Pg) over the Hyperman worker's own loop, so the
6 * worker serves other requests while the database round trip is in flight.
7 * This is the one place a Punk request still blocked.
8 *
9 * Everything goes through DBIx::Loop's C ABI (dbil_abi.h, reached through
10 * ExtUtils::Depends): exec_shaped runs the statement and applies the reshape,
11 * and future_on_ready registers a C continuation, so no Perl closure is
12 * compiled per query and no Perl frame runs when the rows land. The bridge to
13 * Punk::Future is that one continuation: it post-processes per operation and
14 * settles a pf the dispatcher already knows how to await.
15 *
16 * The model tier needs no change for this backend to exist: pm_delegate
17 * passes the future through untouched, and punk_serve.h already awaits any
18 * future a handler returns.
19 *
20 * Must be included after punk_dbi.h (the shared SQL helpers and the keyset
21 * token codec - sharing the codec is a correctness requirement, a `next`
22 * token minted by one backend must decode in the other), punk_wsconn.h
23 * (punk_hm) and punk_future.h (pf_*, pf_wake_hf_cb).
24 */
25  
26 #ifndef PUNK_DBIL_H
27 #define PUNK_DBIL_H
28  
29 #include "dbil_abi.h"
30  
31 #define PDL_CLS "Punk::Model::DBIx::Loop"
32  
33 /* ---- DBIx::Loop's ABI, resolved once (the punk_stencil.h idiom) ---------- */
34  
35 static const dbil_abi *PUNK_DBIL = NULL;
36 static int PUNK_DBIL_TRIED = 0;
37  
38 /* Resolve (once) DBIx::Loop's ABI table, or NULL. PUNK_FAKE_DBIL_BAD
39 * simulates a version mismatch for the guard test. */
40 463 static const dbil_abi *punk_dbil_try(pTHX) {
41 463 ▶ if (!PUNK_DBIL_TRIED) {
Branch: 1 branch
BranchTrueFalseTotal
if (!PUNK_DBIL_TRIED) { 9 454 463
42 9 dSP; int count, ok; IV p = 0;
43 9 PUNK_DBIL_TRIED = 1;
44 9 ok = pk_require_once(aTHX_ "DBIx::Loop", FALSE);
45 9 SPAGAIN; /* the require may have reallocated the value stack */
46 /*
47 # uncoverable branch false
48 # uncoverable condition right count:1 class:ignore_covered_err */
49 9 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 9 0 9
50 9 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 9 9
51 9 count = call_pv("DBIx::Loop::_abi_ptr", G_SCALAR | G_EVAL);
52 9 SPAGAIN;
53 /*
54 # uncoverable branch false
55 # uncoverable condition right count:1 class:ignore_covered_err
56 # uncoverable condition right count:2 class:ignore_covered_err */
57 9 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPi;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 9 0 9
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 9 0 9
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 9 0 9
58 /*
59 # uncoverable statement
60 # uncoverable branch true
61 # uncoverable branch false
62 # uncoverable condition left count:1
63 # uncoverable condition right count:1 */
64 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
65 9 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 9 0 9
66 /*
67 # uncoverable branch false
68 # uncoverable condition right count:1 class:ignore_covered_err */
69 9 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 9 0 9
70 9 const dbil_abi *a = INT2PTR(const dbil_abi *, p);
71 /*
72 # uncoverable condition right count:1 class:ignore_covered_err
73 # uncoverable condition right count:3 class:ignore_covered_err */
74 9 ▶ if (a && !getenv("PUNK_FAKE_DBIL_BAD")
Branch: 2 branches
BranchTrueFalseTotal
if (a && !getenv("PUNK_FAKE_DBIL_BAD") 9 0 9
if (a && !getenv("PUNK_FAKE_DBIL_BAD") 8 1 9
75 8 ▶ && a->abi_version >= DBIL_ABI_VERSION)
Branch: 1 branch
BranchTrueFalseTotal
&& a->abi_version >= DBIL_ABI_VERSION) 8 0 8
76 8 PUNK_DBIL = a;
77 }
78 }
79 }
80 463 return PUNK_DBIL;
81 }
82  
83 /* The table, croaking if it is missing or too old. DBIx::Loop 0.02+ is a hard
84 * prerequisite, so this is a boot-environment error, not a per-request
85 * fallback - there is no synchronous path to fall back to here. */
86 461 static const dbil_abi *punk_dbil(pTHX) {
87 461 const dbil_abi *a = punk_dbil_try(aTHX);
88 /*
89 # uncoverable branch true
90 # uncoverable condition left count:1 class:ignore_covered_err */
91 461 ▶ if (!a)
Branch: 1 branch
BranchTrueFalseTotal
if (!a) 0 461 461
92 0 croak(PDL_CLS ": needs DBIx::Loop with a compatible C ABI " /* # uncoverable statement */
93 "(DBIL_ABI_VERSION %d); upgrade DBIx::Loop to 0.02+",
94 DBIL_ABI_VERSION);
95 461 return a;
96 }
97  
98 /* ---- the per-worker handle -------------------------------------------------
99 *
100 * One DBIx::Loop per distinct connection (dsn + credentials + pool sizes),
101 * shared by every model on it. The pool sizes are in the key on purpose: two
102 * `database` blocks naming one dsn with different worker counts must not
103 * silently share one pool. Built lazily on the first STATEMENT, not at new,
104 * so `punk console` and tests that only instantiate never fork a pool. The
105 * pid rides in the slot, exactly as punk_dbi.h does - DBIx::Loop 0.02 disowns
106 * an inherited pool itself, so this is belt-and-braces, in that order. */
107  
108 static HV *PDL_POOL = NULL;
109  
110 233 static HV *pdl_pool(pTHX) {
111 233 ▶ if (!PDL_POOL) PDL_POOL = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PDL_POOL) PDL_POOL = newHV(); 8 225 233
112 233 return PDL_POOL;
113 }
114  
115 /* The loop adapter for this process. Inside a Hyperman worker it is built on
116 * THE WORKER'S OWN LOOP, named explicitly - never left for the adapter to
117 * choose, because an adapter that picks for itself when no loop is running
118 * quietly constructs an orphan loop the server never pumps, and every future
119 * then hangs. Outside a worker (a script, a test) there is no loop to name,
120 * so the adapter carries its own; backend->await pumps it. */
121 12 static SV *pdl_build_adapter(pTHX_ const dbil_abi *A) {
122 12 const hm_abi *hm = punk_hm(aTHX);
123 12 SV *loop_sv = NULL, *adapter, *err = NULL;
124 /*
125 # uncoverable branch true
126 # uncoverable condition left count:1 class:ignore_covered_err */
127 12 ▶ if (!hm)
Branch: 1 branch
BranchTrueFalseTotal
if (!hm) 0 12 12
128 0 croak(PDL_CLS ": needs Hyperman's C ABI for the loop adapter"); /* # uncoverable statement */
129 {
130 12 void *loop = hm->cur_loop(aTHX);
131 /*
132 # uncoverable branch true
133 # uncoverable condition left count:1 class:ignore_covered_err */
134 12 ▶ if (loop) loop_sv = sv_2mortal(hm->sv_of_loop(aTHX_ loop));
Branch: 1 branch
BranchTrueFalseTotal
if (loop) loop_sv = sv_2mortal(hm->sv_of_loop(aTHX_ loop)); 0 12 12
135 }
136 12 adapter = A->hyperman_adapter(aTHX_ loop_sv, &err);
137 /*
138 # uncoverable branch true
139 # uncoverable condition left count:1 class:ignore_covered_err */
140 12 ▶ if (!adapter) {
Branch: 1 branch
BranchTrueFalseTotal
if (!adapter) { 0 12 12
141 0 SV *e = err ? sv_2mortal(err) /* # uncoverable statement */
142 0 ▶ : sv_2mortal(newSVpvs(PDL_CLS ": could not build the " /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
: sv_2mortal(newSVpvs(PDL_CLS ": could not build the " /* # uncoverable statement */ 0 0 0
143 "DBIx::Loop Hyperman adapter"));
144 0 croak_sv(e); /* # uncoverable statement */
145 }
146 12 return adapter;
147 }
148  
149 /* The slot for this backend's connection: { pid, db, dbh, adapter,
150 * returning }, connected on first use in this process. Borrowed. */
151 233 static HV *pdl_handle_opts(pTHX_ HV *o) {
152 233 const dbil_abi *A = punk_dbil(aTHX);
153 233 ▶ SV *dsn = o ? pdbi_get(aTHX_ o, "dsn") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *dsn = o ? pdbi_get(aTHX_ o, "dsn") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 233 0 233
154 233 ▶ SV *user = o ? pdbi_get(aTHX_ o, "user") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *user = o ? pdbi_get(aTHX_ o, "user") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 233 0 233
155 233 ▶ SV *pass = o ? pdbi_get(aTHX_ o, "password") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *pass = o ? pdbi_get(aTHX_ o, "password") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 233 0 233
156 233 ▶ SV *attr = o ? pdbi_get(aTHX_ o, "attr") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *attr = o ? pdbi_get(aTHX_ o, "attr") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 233 0 233
157 233 ▶ SV *wv = o ? pdbi_get(aTHX_ o, "workers") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *wv = o ? pdbi_get(aTHX_ o, "workers") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 233 0 233
158 233 ▶ SV *mq = o ? pdbi_get(aTHX_ o, "max_queue") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *mq = o ? pdbi_get(aTHX_ o, "max_queue") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 233 0 233
159 /*
160 # uncoverable condition right count:1 class:ignore_covered_err
161 # uncoverable condition right count:2 class:ignore_covered_err */
162 233 ▶ int workers = (wv && SvOK(wv)) ? (int)SvIV(wv) : 0;
Branch: 2 branches
BranchTrueFalseTotal
int workers = (wv && SvOK(wv)) ? (int)SvIV(wv) : 0; 233 0 233
int workers = (wv && SvOK(wv)) ? (int)SvIV(wv) : 0; 233 0 233
163 /*
164 # uncoverable condition left count:1 class:ignore_covered_err
165 # uncoverable condition left count:2 class:ignore_covered_err
166 # uncoverable condition right count:2 class:ignore_covered_err */
167 233 ▶ int max_queue = (mq && SvOK(mq)) ? (int)SvIV(mq) : 0;
Branch: 2 branches
BranchTrueFalseTotal
int max_queue = (mq && SvOK(mq)) ? (int)SvIV(mq) : 0; 0 233 233
int max_queue = (mq && SvOK(mq)) ? (int)SvIV(mq) : 0; 0 0 0
168 SV *key, *slot_sv, *pid;
169 233 HV *pool = pdl_pool(aTHX), *slot;
170  
171 /*
172 # uncoverable condition left count:2 class:ignore_covered_err
173 # uncoverable condition left count:3 class:ignore_covered_err */
174 233 ▶ if (!(dsn && SvOK(dsn) && SvTRUE(dsn)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(dsn && SvOK(dsn) && SvTRUE(dsn))) 232 1 233
if (!(dsn && SvOK(dsn) && SvTRUE(dsn))) 232 0 232
if (!(dsn && SvOK(dsn) && SvTRUE(dsn))) 0 232 232
175 1 croak(PDL_CLS ": no dsn - add a database keyword");
176  
177 /* NUL-joined, sizes included */
178 232 key = sv_2mortal(newSVsv(dsn));
179 232 sv_catpvn(key, "\0", 1);
180 /*
181 # uncoverable branch true
182 # uncoverable condition left count:1 class:ignore_covered_err
183 # uncoverable condition left count:2 class:ignore_covered_err
184 # uncoverable condition right count:2 class:ignore_covered_err */
185 232 ▶ if (user && SvOK(user)) sv_catsv(key, user);
Branch: 2 branches
BranchTrueFalseTotal
if (user && SvOK(user)) sv_catsv(key, user); 0 232 232
if (user && SvOK(user)) sv_catsv(key, user); 0 0 0
186 232 sv_catpvn(key, "\0", 1);
187 /*
188 # uncoverable branch true
189 # uncoverable condition left count:1 class:ignore_covered_err
190 # uncoverable condition left count:2 class:ignore_covered_err
191 # uncoverable condition right count:2 class:ignore_covered_err */
192 232 ▶ if (pass && SvOK(pass)) sv_catsv(key, pass);
Branch: 2 branches
BranchTrueFalseTotal
if (pass && SvOK(pass)) sv_catsv(key, pass); 0 232 232
if (pass && SvOK(pass)) sv_catsv(key, pass); 0 0 0
193 232 sv_catpvn(key, "\0", 1);
194 232 sv_catpvf(key, "%d", workers);
195 232 sv_catpvn(key, "\0", 1);
196 232 sv_catpvf(key, "%d", max_queue);
197  
198 232 slot_sv = pdbi_slot(aTHX_ pool, key);
199 232 slot = (HV *)SvRV(slot_sv);
200 232 pid = pdbi_get(aTHX_ slot, "pid");
201  
202 /*
203 # uncoverable condition left count:1 class:ignore_covered_err
204 # uncoverable condition left count:2 class:ignore_covered_err
205 # uncoverable condition left count:4 class:ignore_covered_err */
206 232 ▶ if (!(pid && SvOK(pid) && SvIV(pid) == (IV)PerlProc_getpid())
Branch: 3 branches
BranchTrueFalseTotal
if (!(pid && SvOK(pid) && SvIV(pid) == (IV)PerlProc_getpid()) 232 0 232
if (!(pid && SvOK(pid) && SvIV(pid) == (IV)PerlProc_getpid()) 232 0 232
if (!(pid && SvOK(pid) && SvIV(pid) == (IV)PerlProc_getpid()) 220 12 232
207 220 ▶ || !pdbi_get(aTHX_ slot, "db")) {
Branch: 1 branch
BranchTrueFalseTotal
|| !pdbi_get(aTHX_ slot, "db")) { 0 220 220
208 12 SV *adapter = pdl_build_adapter(aTHX_ A);
209 12 SV *err = NULL;
210 17 ▶ SV *db = A->connect(aTHX_ dsn,
Branch: 3 branches
BranchTrueFalseTotal
SV *db = A->connect(aTHX_ dsn, 5 7 12
SV *db = A->connect(aTHX_ dsn, 0 12 12
SV *db = A->connect(aTHX_ dsn, 0 12 12
211 0 ▶ (user && SvOK(user)) ? user : NULL, /* # uncoverable statement class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
(user && SvOK(user)) ? user : NULL, /* # uncoverable statement class:ignore_covered_err */ 0 0 0
212 0 ▶ (pass && SvOK(pass)) ? pass : NULL, /* # uncoverable statement class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
(pass && SvOK(pass)) ? pass : NULL, /* # uncoverable statement class:ignore_covered_err */ 0 0 0
213 5 ▶ (attr && SvROK(attr)) ? attr : NULL,
Branch: 1 branch
BranchTrueFalseTotal
(attr && SvROK(attr)) ? attr : NULL, 5 0 5
214 sv_2mortal(adapter), workers, max_queue, &err);
215 SV *dbh;
216 /*
217 # uncoverable branch true
218 # uncoverable condition left count:1 class:ignore_covered_err */
219 12 ▶ if (!db) {
Branch: 1 branch
BranchTrueFalseTotal
if (!db) { 0 12 12
220 0 SV *e = err ? sv_2mortal(err) /* # uncoverable statement */
221 0 ▶ : sv_2mortal(newSVpvs(PDL_CLS ": connect failed")); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
: sv_2mortal(newSVpvs(PDL_CLS ": connect failed")); /* # uncoverable statement */ 0 0 0
222 0 croak_sv(e); /* # uncoverable statement */
223 }
224 12 dbh = pcx_call_meth(aTHX_ db, "dbh", NULL, 0, 1);
225 12 (void)hv_stores(slot, "db", db);
226 12 ▶ (void)hv_stores(slot, "dbh", dbh ? dbh : newSV(0)); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(slot, "dbh", dbh ? dbh : newSV(0)); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 12 12
227 12 (void)hv_stores(slot, "adapter", newSVsv(adapter));
228 12 (void)hv_stores(slot, "pid", newSViv((IV)PerlProc_getpid()));
229 12 ▶ (void)hv_stores(slot, "returning",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(slot, "returning", 12 0 12
230 newSViv(dbh ? pdbi_detect_returning(aTHX_ dbh) : 0));
231 { /* The adapter's underlying hm_loop, kept so this backend's
232 * futures can be LOOP-BACKED even off a worker: get/await then
233 * pump the adapter's own loop - the one the query watchers are
234 * actually on - instead of croaking in block mode. The loop SV
235 * rides in the slot so the pointer cannot dangle. */
236 12 const hm_abi *hm = punk_hm(aTHX);
237 12 SV *loop_sv = pcx_call_meth(aTHX_ adapter, "loop", NULL, 0, 1);
238 /*
239 # uncoverable branch false
240 # uncoverable condition right count:1 class:ignore_covered_err
241 # uncoverable condition right count:2 class:ignore_covered_err
242 # uncoverable condition right count:3 class:ignore_covered_err */
243 24 ▶ if (hm && loop_sv && SvROK(loop_sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (hm && loop_sv && SvROK(loop_sv)) { 12 0 12
if (hm && loop_sv && SvROK(loop_sv)) { 12 0 12
if (hm && loop_sv && SvROK(loop_sv)) { 12 0 12
244 12 void *lp = hm->loop_of_sv(aTHX_ loop_sv);
245 12 (void)hv_stores(slot, "loop_sv", loop_sv);
246 12 (void)hv_stores(slot, "_loop", newSViv(PTR2IV(lp)));
247 }
248 /*
249 # uncoverable statement
250 # uncoverable branch true
251 # uncoverable branch false
252 # uncoverable condition left count:1
253 # uncoverable condition right count:1 */
254 0 ▶ else if (loop_sv) SvREFCNT_dec(loop_sv);
Branch: 1 branch
BranchTrueFalseTotal
else if (loop_sv) SvREFCNT_dec(loop_sv); 0 0 0
255 }
256 }
257 232 return slot;
258 }
259  
260 226 static HV *pdl_handle(pTHX_ SV *self) {
261 226 HV *h = pdbi_hv(aTHX_ self);
262 226 SV *opts = pdbi_get(aTHX_ h, "opts");
263 226 ▶ HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 226 0 226
HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 226 0 226
264 452 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 226 0 226
265 226 HV *slot = pdl_handle_opts(aTHX_ o);
266 { /* keep the instance's slot in step, as the DBI backend does */
267 225 SV *r = pdbi_get(aTHX_ slot, "returning");
268 225 ▶ (void)hv_stores(h, "returning", newSViv(r ? SvIV(r) : 0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, "returning", newSViv(r ? SvIV(r) : 0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 225 0 225
269 }
270 225 return slot;
271 }
272  
273 /* The transaction this backend instance is bound to, or NULL. Set by
274 * Punk::Txn::model on a copy of the instance, never on the shared one: a
275 * "current transaction" on the worker would be wrong under an event loop,
276 * where the requests in flight between two of this request's queries are
277 * somebody else's. */
278 179 static SV *pdl_tx_of(pTHX_ SV *self) {
279 179 SV *tx = pdbi_get(aTHX_ pdbi_hv(aTHX_ self), "_tx");
280 179 ▶ return (tx && SvROK(tx)) ? tx : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return (tx && SvROK(tx)) ? tx : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 169 179
return (tx && SvROK(tx)) ? tx : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 0 10
281 }
282  
283 /* ---- the bridge: one C continuation from DBIx::Loop into Punk::Future ---- */
284  
285 /* what the continuation does with the settled result */
286 enum {
287 PDL_OP_PASS = 0, /* settle with values[0] (row hashref or undef) */
288 PDL_OP_SEARCH, /* rows -> { rows, has_more_data, next } */
289 PDL_OP_COUNT, /* { rows_affected } -> the count */
290 PDL_OP_CREATE_FB, /* do done -> re-get by pk (insert_id fallback) */
291 PDL_OP_UPDATE_FB, /* do done -> re-get by pk */
292 PDL_OP_WRAP /* settle with ALL the values - backend->future */
293 };
294  
295 /* how many settled values PDL_OP_WRAP carries across; every shipped
296 * DBIx::Loop method settles with one, selectrow_array with a row's worth */
297 #define PDL_WRAP_MAX 64
298  
299 typedef struct pdl_pend {
300 const dbil_abi *A;
301 SV *pf; /* the Punk::Future to settle (+1) */
302 SV *db; /* the DBIx::Loop object, for chained follow-ups (+1) */
303 SV *dbh; /* the parent handle, for quote_identifier (+1) */
304 int op;
305 IV limit; /* SEARCH */
306 SV *pk; /* primary key name (+1) or NULL */
307 SV *pkval; /* the key value for a fallback re-get (+1) or NULL */
308 SV *table; /* (+1) for follow-up SQL */
309 HV *slot; /* the pool slot, borrowed - for the quoting cache */
310 SV *data; /* CREATE_FB with no pk: the payload to echo (+1) */
311 AV *cols; /* SEARCH: the resolved ordering, for the token (+1) */
312 AV *desc; /* SEARCH: its directions (+1) */
313 int pk_only; /* SEARCH: no order_by asked - the one-value token */
314 SV *tx; /* the DBIx::Loop::Txn the statement ran on (+1), or NULL */
315 int shape; /* a raw result still to be reshaped (tx path), or 0 */
316 } pdl_pend;
317  
318 187 static void pdl_pend_free(pTHX_ pdl_pend *p) {
319 /*
320 # uncoverable branch false
321 # uncoverable condition right count:1 class:ignore_covered_err */
322 187 ▶ if (p->pf) SvREFCNT_dec(p->pf);
Branch: 1 branch
BranchTrueFalseTotal
if (p->pf) SvREFCNT_dec(p->pf); 187 0 187
323 /*
324 # uncoverable branch true
325 # uncoverable condition left count:1 class:ignore_covered_err */
326 187 ▶ if (p->db) SvREFCNT_dec(p->db);
Branch: 1 branch
BranchTrueFalseTotal
if (p->db) SvREFCNT_dec(p->db); 0 187 187
327 /*
328 # uncoverable branch true
329 # uncoverable condition left count:1 class:ignore_covered_err */
330 187 ▶ if (p->dbh) SvREFCNT_dec(p->dbh);
Branch: 1 branch
BranchTrueFalseTotal
if (p->dbh) SvREFCNT_dec(p->dbh); 0 187 187
331 /*
332 # uncoverable branch true
333 # uncoverable condition left count:1 class:ignore_covered_err */
334 187 ▶ if (p->pk) SvREFCNT_dec(p->pk);
Branch: 1 branch
BranchTrueFalseTotal
if (p->pk) SvREFCNT_dec(p->pk); 0 187 187
335 /*
336 # uncoverable branch true
337 # uncoverable condition left count:1 class:ignore_covered_err */
338 187 ▶ if (p->pkval) SvREFCNT_dec(p->pkval);
Branch: 1 branch
BranchTrueFalseTotal
if (p->pkval) SvREFCNT_dec(p->pkval); 0 187 187
339 /*
340 # uncoverable branch true
341 # uncoverable condition left count:1 class:ignore_covered_err */
342 187 ▶ if (p->table) SvREFCNT_dec(p->table);
Branch: 1 branch
BranchTrueFalseTotal
if (p->table) SvREFCNT_dec(p->table); 0 187 187
343 /*
344 # uncoverable branch true
345 # uncoverable condition left count:1 class:ignore_covered_err */
346 187 ▶ if (p->data) SvREFCNT_dec(p->data);
Branch: 1 branch
BranchTrueFalseTotal
if (p->data) SvREFCNT_dec(p->data); 0 187 187
347 187 ▶ if (p->cols) SvREFCNT_dec((SV *)p->cols);
Branch: 1 branch
BranchTrueFalseTotal
if (p->cols) SvREFCNT_dec((SV *)p->cols); 117 70 187
348 187 ▶ if (p->desc) SvREFCNT_dec((SV *)p->desc);
Branch: 1 branch
BranchTrueFalseTotal
if (p->desc) SvREFCNT_dec((SV *)p->desc); 117 70 187
349 187 ▶ if (p->tx) SvREFCNT_dec(p->tx);
Branch: 1 branch
BranchTrueFalseTotal
if (p->tx) SvREFCNT_dec(p->tx); 10 177 187
350 187 Safefree(p);
351 187 }
352  
353 static void pdl_ready_cb(pTHX_ SV *fut, void *ud); /* below */
354  
355 /* attach the bridge and release our handle on the DBIx::Loop future - its
356 * own chain keeps it alive until it settles */
357 187 static void pdl_attach(pTHX_ SV *fut, pdl_pend *p) {
358 187 p->A->future_on_ready(aTHX_ fut, pdl_ready_cb, p);
359 187 SvREFCNT_dec(fut);
360 187 }
361  
362 /* settle the pending Punk::Future as failed with err (borrowed) */
363 4 static void pdl_fail(pTHX_ pdl_pend *p, SV *err) {
364 4 ▶ SV *e = sv_2mortal(err ? newSVsv(err) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *e = sv_2mortal(err ? newSVsv(err) /* # uncoverable condition right count:1 class:ignore_covered_err */ 4 0 4
365 : newSVpvs(PDL_CLS ": query failed"));
366 4 pf_settle_argv(aTHX_ p->pf, PF_FAILED, &e, 1);
367 4 }
368  
369 /* Run one statement for a pending op and attach the continuation: on the
370 * worker's connection through the ABI, or - when the instance is bound to a
371 * transaction - on the DBIx::Loop::Txn handle pinned to it, through its own
372 * query/do, with the shape recorded for the continuation to apply. */
373 179 static void pdl_exec(pTHX_ pdl_pend *p, SV *db, SV *tx, int is_query,
374 SV *sql, AV *bind, int shape) {
375 179 const dbil_abi *A = p->A;
376 179 ▶ if (tx && SvROK(tx)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (tx && SvROK(tx)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 169 179
if (tx && SvROK(tx)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 0 10
377 10 ▶ SSize_t i, n = bind ? av_len(bind) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = bind ? av_len(bind) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
378 SV **argv, *f;
379 10 ▶ Newx(argv, n + 1, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(argv, n + 1, SV *); 0 10 10
380 10 argv[0] = sql;
381 26 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 16 10 26
382 16 SV **e = av_fetch(bind, i, 0);
383 /*
384 # uncoverable condition right count:1 class:ignore_covered_err
385 # uncoverable condition right count:2 class:ignore_covered_err */
386 16 ▶ argv[i + 1] = (e && *e) ? *e : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
argv[i + 1] = (e && *e) ? *e : &PL_sv_undef; 16 0 16
argv[i + 1] = (e && *e) ? *e : &PL_sv_undef; 16 0 16
387 }
388 { /* Under an eval: this runs from the continuation of the
389 * previous statement, inside DBIx::Loop's settle path, where a
390 * croak would longjmp out of a fire loop. A handle that dies
391 * (the transaction already ended) fails the future instead. */
392 10 dSP;
393 int count;
394 10 ENTER; SAVETMPS;
395 10 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 10 10
396 10 ▶ EXTEND(SP, n + 2);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n + 2); 10 0 10
EXTEND(SP, n + 2); 0 10 10
397 10 PUSHs(tx);
398 36 ▶ for (i = 0; i <= n; i++) PUSHs(argv[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= n; i++) PUSHs(argv[i]); 26 10 36
399 10 PUTBACK;
400 10 ▶ count = call_method(is_query ? "query" : "do", G_SCALAR | G_EVAL); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
count = call_method(is_query ? "query" : "do", G_SCALAR | G_EVAL); /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
401 10 SPAGAIN;
402 10 ▶ f = (count > 0) ? SvREFCNT_inc(POPs) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
f = (count > 0) ? SvREFCNT_inc(POPs) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
403 10 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 10 0 10
404 /*
405 # uncoverable branch true
406 # uncoverable condition right count:1 class:ignore_covered_err
407 # uncoverable condition left count:2 class:ignore_covered_err */
408 10 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 10 0 10
if (SvTRUE(ERRSV)) { 0 10 10
409 /*
410 # uncoverable statement
411 # uncoverable branch true
412 # uncoverable branch false
413 # uncoverable condition left count:1
414 # uncoverable condition right count:1 */
415 0 ▶ if (f) SvREFCNT_dec(f);
Branch: 1 branch
BranchTrueFalseTotal
if (f) SvREFCNT_dec(f); 0 0 0
416 0 Safefree(argv); /* # uncoverable statement */
417 0 ▶ pdl_fail(aTHX_ p, ERRSV); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
pdl_fail(aTHX_ p, ERRSV); /* # uncoverable statement */ 0 0 0
418 0 return; /* # uncoverable statement */
419 }
420 }
421 10 Safefree(argv);
422 /*
423 # uncoverable branch true
424 # uncoverable condition left count:1 class:ignore_covered_err */
425 10 ▶ if (!f) {
Branch: 1 branch
BranchTrueFalseTotal
if (!f) { 0 10 10
426 0 pdl_fail(aTHX_ p, sv_2mortal(newSVpvs( /* # uncoverable statement */
427 PDL_CLS ": the transaction handle returned no future")));
428 0 return; /* # uncoverable statement */
429 }
430 10 ▶ p->shape = is_query ? shape : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
p->shape = is_query ? shape : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
431 /*
432 # uncoverable branch false
433 # uncoverable condition right count:1 class:ignore_covered_err */
434 10 ▶ if (!p->tx) p->tx = newSVsv(tx);
Branch: 1 branch
BranchTrueFalseTotal
if (!p->tx) p->tx = newSVsv(tx); 10 0 10
435 10 pdl_attach(aTHX_ f, p);
436 10 return;
437 }
438 171 ▶ pdl_attach(aTHX_ is_query ? A->exec_shaped(aTHX_ db, sql, bind, shape, NULL)
Branch: 1 branch
BranchTrueFalseTotal
pdl_attach(aTHX_ is_query ? A->exec_shaped(aTHX_ db, sql, bind, shape, NULL) 167 2 169
439 2 : A->exec(aTHX_ db, 0, sql, bind), p);
440 }
441  
442 /* The continuation. This runs inside DBIx::Loop's settle path, so it must
443 * not croak - a longjmp out of a fire loop leaves the queue half-run. Every
444 * failure becomes a failed Punk::Future instead. */
445 187 static void pdl_ready_cb(pTHX_ SV *fut, void *ud) {
446 187 pdl_pend *p = (pdl_pend *)ud;
447 187 const dbil_abi *A = p->A;
448 SV *v[1];
449 SSize_t n;
450  
451 187 ▶ if (A->future_state(aTHX_ fut) != DBIL_ABI_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (A->future_state(aTHX_ fut) != DBIL_ABI_DONE) { 4 183 187
452 4 pdl_fail(aTHX_ p, A->future_error(aTHX_ fut));
453 4 pdl_pend_free(aTHX_ p);
454 4 return;
455 }
456 183 n = A->future_values(aTHX_ fut, v, 1);
457  
458 /* A statement on a transaction handle settles with the raw result -
459 * the pinned handle has no shaped form - so the shape exec_shaped would
460 * have applied is applied here instead, before the op sees it. */
461 183 ▶ if (p->shape && n > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (p->shape && n > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 173 183
if (p->shape && n > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 0 10
462 10 AV *out = (AV *)sv_2mortal((SV *)newAV());
463 10 SV *err = A->reshape(aTHX_ p->shape, v[0], NULL, out);
464 /*
465 # uncoverable branch true
466 # uncoverable condition left count:1 class:ignore_covered_err */
467 10 ▶ if (err) {
Branch: 1 branch
BranchTrueFalseTotal
if (err) { 0 10 10
468 0 pdl_fail(aTHX_ p, err); /* # uncoverable statement */
469 0 SvREFCNT_dec(err); /* # uncoverable statement */
470 0 pdl_pend_free(aTHX_ p); /* # uncoverable statement */
471 0 return; /* # uncoverable statement */
472 }
473 10 n = av_len(out) + 1;
474 10 ▶ v[0] = n > 0 ? *av_fetch(out, 0, 0) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
v[0] = n > 0 ? *av_fetch(out, 0, 0) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
475 /*
476 # uncoverable branch false
477 # uncoverable condition right count:1 class:ignore_covered_err */
478 10 ▶ if (n > 0) n = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) n = 1; 10 0 10
479 10 p->shape = 0;
480 }
481  
482 /*
483 # uncoverable branch true count:5
484 # uncoverable branch true count:6 */
485 183 switch (p->op) {
486  
487 63 case PDL_OP_PASS: {
488 63 ▶ SV *r = (n > 0 && SvOK(v[0])) ? v[0] : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
SV *r = (n > 0 && SvOK(v[0])) ? v[0] : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 63 0 63
SV *r = (n > 0 && SvOK(v[0])) ? v[0] : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 60 3 63
489 63 pf_settle_argv(aTHX_ p->pf, PF_DONE, &r, 1);
490 63 break;
491 }
492  
493 1 case PDL_OP_WRAP: {
494 SV *vs[PDL_WRAP_MAX];
495 1 SSize_t m = A->future_values(aTHX_ fut, vs, PDL_WRAP_MAX);
496 1 pf_settle_argv(aTHX_ p->pf, PF_DONE, vs, (int)m);
497 1 break;
498 }
499  
500 117 case PDL_OP_SEARCH: {
501 /* the rows are borrowed from the DBIx::Loop future; pdbq_page takes
502 * ownership of the reference it is handed, so hand it one of ours */
503 117 ▶ SV *rows_rv = (n > 0 && SvROK(v[0]) && SvTYPE(SvRV(v[0])) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
SV *rows_rv = (n > 0 && SvROK(v[0]) && SvTYPE(SvRV(v[0])) == SVt_PVAV) 117 0 117
SV *rows_rv = (n > 0 && SvROK(v[0]) && SvTYPE(SvRV(v[0])) == SVt_PVAV) 117 0 117
504 234 ▶ ? newRV_inc(SvRV(v[0])) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? newRV_inc(SvRV(v[0])) : NULL; 117 0 117
505 117 SV *out_rv = sv_2mortal(pdbq_page(aTHX_ rows_rv, p->limit,
506 p->cols, p->desc, p->pk_only));
507 117 pf_settle_argv(aTHX_ p->pf, PF_DONE, &out_rv, 1);
508 117 break;
509 }
510  
511 2 case PDL_OP_COUNT: {
512 2 ▶ HV *r = (n > 0 && SvROK(v[0]) && SvTYPE(SvRV(v[0])) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *r = (n > 0 && SvROK(v[0]) && SvTYPE(SvRV(v[0])) == SVt_PVHV) 2 0 2
HV *r = (n > 0 && SvROK(v[0]) && SvTYPE(SvRV(v[0])) == SVt_PVHV) 2 0 2
513 4 ▶ ? (HV *)SvRV(v[0]) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(v[0]) : NULL; 2 0 2
514 2 ▶ SV *ra = r ? pdbi_get(aTHX_ r, "rows_affected") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *ra = r ? pdbi_get(aTHX_ r, "rows_affected") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
515 /*
516 # uncoverable condition right count:1 class:ignore_covered_err
517 # uncoverable condition right count:2 class:ignore_covered_err */
518 2 ▶ SV *c = sv_2mortal(newSViv(ra && SvOK(ra) ? SvIV(ra) : 0));
Branch: 2 branches
BranchTrueFalseTotal
SV *c = sv_2mortal(newSViv(ra && SvOK(ra) ? SvIV(ra) : 0)); 2 0 2
SV *c = sv_2mortal(newSViv(ra && SvOK(ra) ? SvIV(ra) : 0)); 2 0 2
519 2 pf_settle_argv(aTHX_ p->pf, PF_DONE, &c, 1);
520 2 break;
521 }
522  
523 /* the do() half of a write on a driver without RETURNING: chain the
524 * re-get so server-side defaults still come back, exactly as the DBI
525 * backend does synchronously. insert_id comes from the DO RESULT - the
526 * worker's own handle, the connection the INSERT actually ran on -
527 * never from $db->dbh, which is the parent's separate connection. */
528 0 case PDL_OP_CREATE_FB: /* # uncoverable statement */
529 case PDL_OP_UPDATE_FB: {
530 0 SV *id = p->pkval; /* # uncoverable statement */
531 /*
532 # uncoverable statement
533 # uncoverable branch true
534 # uncoverable branch false
535 # uncoverable condition left count:1
536 # uncoverable condition right count:1
537 # uncoverable condition left count:2
538 # uncoverable condition right count:2 */
539 0 ▶ if (!id && p->op == PDL_OP_CREATE_FB) {
Branch: 2 branches
BranchTrueFalseTotal
if (!id && p->op == PDL_OP_CREATE_FB) { 0 0 0
if (!id && p->op == PDL_OP_CREATE_FB) { 0 0 0
540 0 ▶ HV *r = (n > 0 && SvROK(v[0]) && SvTYPE(SvRV(v[0])) == SVt_PVHV) /* # uncoverable statement */
Branch: 2 branches
BranchTrueFalseTotal
HV *r = (n > 0 && SvROK(v[0]) && SvTYPE(SvRV(v[0])) == SVt_PVHV) /* # uncoverable statement */ 0 0 0
HV *r = (n > 0 && SvROK(v[0]) && SvTYPE(SvRV(v[0])) == SVt_PVHV) /* # uncoverable statement */ 0 0 0
541 0 ▶ ? (HV *)SvRV(v[0]) : NULL; /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(v[0]) : NULL; /* # uncoverable statement */ 0 0 0
542 /*
543 # uncoverable statement
544 # uncoverable condition left count:1
545 # uncoverable condition right count:1 */
546 0 ▶ id = r ? pdbi_get(aTHX_ r, "insert_id") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
id = r ? pdbi_get(aTHX_ r, "insert_id") : NULL; 0 0 0
547 }
548 /*
549 # uncoverable statement
550 # uncoverable branch true
551 # uncoverable branch false
552 # uncoverable condition left count:1
553 # uncoverable condition right count:1
554 # uncoverable condition left count:2
555 # uncoverable condition right count:2
556 # uncoverable condition left count:3
557 # uncoverable condition right count:3 */
558 0 ▶ if (!p->pk || !id || !SvOK(id)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!p->pk || !id || !SvOK(id)) { 0 0 0
if (!p->pk || !id || !SvOK(id)) { 0 0 0
if (!p->pk || !id || !SvOK(id)) { 0 0 0
559 /* no key to re-fetch by: echo the payload, as the DBI backend
560 * does for a keyless create */
561 0 ▶ SV *r = p->data && SvROK(p->data) /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
SV *r = p->data && SvROK(p->data) /* # uncoverable statement */ 0 0 0
562 0 ? sv_2mortal(newRV_noinc((SV *)newHVhv((HV *)SvRV(p->data)))) /* # uncoverable statement */
563 0 ▶ : &PL_sv_undef; /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
: &PL_sv_undef; /* # uncoverable statement */ 0 0 0
564 0 pf_settle_argv(aTHX_ p->pf, PF_DONE, &r, 1); /* # uncoverable statement */
565 0 break; /* # uncoverable statement */
566 }
567 {
568 0 AV *bind = (AV *)sv_2mortal((SV *)newAV()); /* # uncoverable statement */
569 0 SV *sql = sv_2mortal(newSVpvs("SELECT * FROM ")); /* # uncoverable statement */
570 SV *nf;
571 0 sv_catsv(sql, pdbi_qi_slot(aTHX_ p->slot, p->dbh, p->table)); /* # uncoverable statement */
572 0 sv_catpvs(sql, " WHERE "); /* # uncoverable statement */
573 0 sv_catsv(sql, pdbi_qi_slot(aTHX_ p->slot, p->dbh, p->pk)); /* # uncoverable statement */
574 0 sv_catpvs(sql, " = ? LIMIT 1"); /* # uncoverable statement */
575 0 av_push(bind, newSVsv(id)); /* # uncoverable statement */
576 0 p->op = PDL_OP_PASS; /* # uncoverable statement */
577 /* inside a transaction the re-get must run on the pinned
578 * handle, or it runs on another slot and cannot see the row */
579 0 pdl_exec(aTHX_ p, p->db, p->tx, 1, sql, bind, /* # uncoverable statement */
580 DBIL_ABI_ROW_HASHREF); /* the same pend rides along */
581 PERL_UNUSED_VAR(nf);
582 0 return; /* not freed: the follow-up owns it */ /* # uncoverable statement */
583 }
584 }
585 }
586  
587 183 pdl_pend_free(aTHX_ p);
588 }
589  
590 /* a pending Punk::Future and its bridge record. The blessed SV is returned
591 * to the caller; the pend holds its own reference until the bridge fires.
592 *
593 * On a worker, pf_new already bound the future to the worker's loop. Off a
594 * worker it came back block-mode, and a block-mode future settled by a loop
595 * callback would hang (or croak) on get - so it is bound to the ADAPTER'S
596 * loop instead, the loop the query watchers are on. get/await then pump the
597 * right loop wherever they run, and then-chains inherit the binding. */
598 187 static pdl_pend *pdl_start(pTHX_ const dbil_abi *A, HV *slot, SV **pf_out) {
599 187 punk_future *pf = pf_new(aTHX);
600 SV *self;
601 pdl_pend *p;
602 187 ▶ if (!pf->is_loop) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pf->is_loop) { 176 11 187
603 176 const hm_abi *hm = punk_hm(aTHX);
604 176 SV *lp = pdbi_get(aTHX_ slot, "_loop");
605 /*
606 # uncoverable branch false
607 # uncoverable condition right count:1 class:ignore_covered_err
608 # uncoverable condition right count:2 class:ignore_covered_err
609 # uncoverable condition right count:3 class:ignore_covered_err */
610 176 ▶ if (hm && lp && SvIOK(lp)) {
Branch: 3 branches
BranchTrueFalseTotal
if (hm && lp && SvIOK(lp)) { 176 0 176
if (hm && lp && SvIOK(lp)) { 176 0 176
if (hm && lp && SvIOK(lp)) { 176 0 176
611 176 pf->is_loop = 1;
612 176 pf->abi = hm;
613 176 pf->loop = INT2PTR(void *, SvIV(lp));
614 }
615 }
616 187 self = pf_bless(aTHX_ pf, "Punk::Future");
617 187 p = NULL;
618 187 Newxz(p, 1, pdl_pend);
619 187 p->A = A;
620 187 p->slot = slot; /* borrowed: the pool holds it for the process */
621 187 p->pf = SvREFCNT_inc(self);
622 187 *pf_out = self;
623 187 return p;
624 }
625  
626 /* ---- the six operations --------------------------------------------------- */
627  
628 /* get(%key) -> future of the row hashref, or undef */
629 15 static SV *pdl_get(pTHX_ SV *self, SV **st, I32 items) {
630 HV *key;
631 15 AV *keys = pdbi_key_args(aTHX_ st, items, &key, "get", PDL_CLS);
632 15 HV *slot = pdl_handle(aTHX_ self);
633 15 SV *db = pdbi_get(aTHX_ slot, "db");
634 15 SV *dbh = pdbi_get(aTHX_ slot, "dbh");
635 15 AV *bind = (AV *)sv_2mortal((SV *)newAV());
636 15 SV *where = pdbi_where_eq_slot(aTHX_ slot, dbh, key, keys, bind);
637 15 SV *table = pdbi_get(aTHX_ pdbi_hv(aTHX_ self), "table");
638 15 SV *sql = sv_2mortal(newSVpvs("SELECT * FROM "));
639 15 const dbil_abi *A = punk_dbil(aTHX);
640 SV *out;
641 pdl_pend *p;
642  
643 15 pdbq_check_keys(aTHX_ self, keys, PDL_CLS, "get");
644  
645 13 sv_catsv(sql, pdbi_qi_slot(aTHX_ slot, dbh, table));
646 13 sv_catpvs(sql, " WHERE ");
647 13 sv_catsv(sql, where);
648 13 sv_catpvs(sql, " LIMIT 1");
649  
650 13 p = pdl_start(aTHX_ A, slot, &out);
651 13 p->op = PDL_OP_PASS;
652 13 pdl_exec(aTHX_ p, db, pdl_tx_of(aTHX_ self), 1, sql, bind, DBIL_ABI_ROW_HASHREF);
653 13 return out;
654 }
655  
656 /* search(\%filter, \%opts) -> future of { rows, has_more_data, next } */
657 136 static SV *pdl_search(pTHX_ SV *self, SV *filter, SV *opts) {
658 136 HV *h = pdbi_hv(aTHX_ self);
659 136 ▶ HV *f = (SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *f = (SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV) 136 0 136
660 272 ▶ ? (HV *)SvRV(filter) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(filter) : NULL; 136 0 136
661 109 ▶ HV *o = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *o = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 109 0 109
662 245 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 109 27 136
663 136 SV *pk = pdbi_get(aTHX_ h, "primary");
664 136 SV *table = pdbi_get(aTHX_ h, "table");
665 136 SV *colsv = pdbi_get(aTHX_ h, "col");
666 /*
667 # uncoverable condition right count:1 class:ignore_covered_err
668 # uncoverable condition right count:2 class:ignore_covered_err */
669 136 ▶ HV *col = (colsv && SvROK(colsv)) ? (HV *)SvRV(colsv) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *col = (colsv && SvROK(colsv)) ? (HV *)SvRV(colsv) : NULL; 136 0 136
HV *col = (colsv && SvROK(colsv)) ? (HV *)SvRV(colsv) : NULL; 136 0 136
670 136 HV *slot = pdl_handle(aTHX_ self);
671 136 SV *db = pdbi_get(aTHX_ slot, "db");
672 136 SV *dbh = pdbi_get(aTHX_ slot, "dbh");
673 136 AV *bind = (AV *)sv_2mortal((SV *)newAV());
674 136 const dbil_abi *A = punk_dbil(aTHX);
675 AV *cols, *desc;
676 IV limit;
677 int pk_only;
678 SV *sql, *out;
679 pdl_pend *p;
680  
681 /* synchronous, before the exec: a bad filter, option or token is a
682 * caller error and croaks here, exactly as the DBI backend's does */
683 136 sql = pdbq_search_sql(aTHX_ slot, dbh, col, table, pk, f, o, PDL_CLS,
684 bind, &limit, &cols, &desc, &pk_only);
685  
686 117 p = pdl_start(aTHX_ A, slot, &out);
687 117 p->op = PDL_OP_SEARCH;
688 117 p->limit = limit;
689 117 p->cols = (AV *)SvREFCNT_inc((SV *)cols);
690 117 p->desc = (AV *)SvREFCNT_inc((SV *)desc);
691 117 p->pk_only = pk_only;
692 117 pdl_exec(aTHX_ p, db, pdl_tx_of(aTHX_ self), 1, sql, bind, DBIL_ABI_ALL_ROWHASH);
693 117 return out;
694 }
695  
696 /* count(\%filter) -> future of the matching row count */
697 18 static SV *pdl_count(pTHX_ SV *self, SV *filter) {
698 18 HV *h = pdbi_hv(aTHX_ self);
699 16 ▶ HV *f = (SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *f = (SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV) 16 0 16
700 34 ▶ ? (HV *)SvRV(filter) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(filter) : NULL; 16 2 18
701 18 SV *table = pdbi_get(aTHX_ h, "table");
702 18 SV *colsv = pdbi_get(aTHX_ h, "col");
703 /*
704 # uncoverable condition right count:1 class:ignore_covered_err
705 # uncoverable condition right count:2 class:ignore_covered_err */
706 18 ▶ HV *col = (colsv && SvROK(colsv)) ? (HV *)SvRV(colsv) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *col = (colsv && SvROK(colsv)) ? (HV *)SvRV(colsv) : NULL; 18 0 18
HV *col = (colsv && SvROK(colsv)) ? (HV *)SvRV(colsv) : NULL; 18 0 18
707 18 HV *slot = pdl_handle(aTHX_ self);
708 18 SV *db = pdbi_get(aTHX_ slot, "db");
709 18 SV *dbh = pdbi_get(aTHX_ slot, "dbh");
710 18 AV *bind = (AV *)sv_2mortal((SV *)newAV());
711 18 const dbil_abi *A = punk_dbil(aTHX);
712 18 SV *sql = pdbq_count_sql(aTHX_ slot, dbh, col, table, f, PDL_CLS, bind);
713 SV *out;
714 pdl_pend *p;
715  
716 17 p = pdl_start(aTHX_ A, slot, &out);
717 /* the first row as a list, and the first value of that list is the
718 * count: the PASS continuation settles with values[0] */
719 17 p->op = PDL_OP_PASS;
720 17 pdl_exec(aTHX_ p, db, pdl_tx_of(aTHX_ self), 1, sql, bind, DBIL_ABI_ROW_ARRAY);
721 17 return out;
722 }
723  
724 /* the INSERT/UPDATE SQL builders, shared shape with xs/dbi.xs but quoting on
725 * the handle from the slot */
726  
727 /* create(\%data) -> future of the stored row */
728 28 static SV *pdl_create(pTHX_ SV *self, SV *data) {
729 28 HV *h = pdbi_hv(aTHX_ self);
730 28 ▶ HV *d = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *d = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) 28 0 28
731 56 ▶ ? (HV *)SvRV(data) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(data) : NULL; 28 0 28
732 28 SV *colset = pdbi_get(aTHX_ h, "col");
733 /*
734 # uncoverable condition right count:1 class:ignore_covered_err
735 # uncoverable condition right count:2 class:ignore_covered_err */
736 28 ▶ HV *known = (colset && SvROK(colset)) ? (HV *)SvRV(colset) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *known = (colset && SvROK(colset)) ? (HV *)SvRV(colset) : NULL; 28 0 28
HV *known = (colset && SvROK(colset)) ? (HV *)SvRV(colset) : NULL; 28 0 28
737 28 SV *table = pdbi_get(aTHX_ h, "table");
738 28 SV *pk = pdbi_get(aTHX_ h, "primary");
739 28 HV *slot = pdl_handle(aTHX_ self);
740 28 SV *db = pdbi_get(aTHX_ slot, "db");
741 28 SV *dbh = pdbi_get(aTHX_ slot, "dbh");
742 28 SV *ret = pdbi_get(aTHX_ slot, "returning");
743 28 AV *all = pdbi_sorted_keys(aTHX_ d);
744 28 AV *cols = (AV *)sv_2mortal((SV *)newAV());
745 28 AV *bind = (AV *)sv_2mortal((SV *)newAV());
746 28 const dbil_abi *A = punk_dbil(aTHX);
747 SV *sql, *cl, *ph, *out;
748 pdl_pend *p;
749 28 SSize_t i, n = av_len(all) + 1;
750  
751 82 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 54 28 82
752 54 SV *k = *av_fetch(all, i, 0);
753 54 ▶ HE *he = d ? hv_fetch_ent(d, k, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
HE *he = d ? hv_fetch_ent(d, k, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 54 0 54
754 54 ▶ if (!(known && hv_exists_ent(known, k, 0))) continue; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(known && hv_exists_ent(known, k, 0))) continue; /* # uncoverable condition left count:1 class:ignore_covered_err */ 54 0 54
if (!(known && hv_exists_ent(known, k, 0))) continue; /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 53 54
755 /*
756 # uncoverable branch true
757 # uncoverable condition left count:1 class:ignore_covered_err
758 # uncoverable condition left count:2 class:ignore_covered_err */
759 53 ▶ if (!(he && SvOK(HeVAL(he)))) continue; /* defined values only */
Branch: 2 branches
BranchTrueFalseTotal
if (!(he && SvOK(HeVAL(he)))) continue; /* defined values only */ 53 0 53
if (!(he && SvOK(HeVAL(he)))) continue; /* defined values only */ 0 53 53
760 53 av_push(cols, newSVsv(k));
761 53 av_push(bind, newSVsv(HeVAL(he)));
762 }
763 28 ▶ if (av_len(cols) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(cols) < 0) 1 27 28
764 1 croak(PDL_CLS ": create with no known columns");
765  
766 27 cl = sv_2mortal(newSVpvs(""));
767 27 ph = sv_2mortal(newSVpvs(""));
768 27 n = av_len(cols) + 1;
769 80 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 53 27 80
770 53 ▶ if (i) { sv_catpvs(cl, ", "); sv_catpvs(ph, ", "); }
Branch: 1 branch
BranchTrueFalseTotal
if (i) { sv_catpvs(cl, ", "); sv_catpvs(ph, ", "); } 26 27 53
771 53 sv_catsv(cl, pdbi_qi_slot(aTHX_ slot, dbh, *av_fetch(cols, i, 0)));
772 53 sv_catpvs(ph, "?");
773 }
774 27 sql = sv_2mortal(newSVpvs("INSERT INTO "));
775 27 sv_catsv(sql, pdbi_qi_slot(aTHX_ slot, dbh, table));
776 27 sv_catpvs(sql, " ("); sv_catsv(sql, cl);
777 27 sv_catpvs(sql, ") VALUES ("); sv_catsv(sql, ph);
778 27 sv_catpvs(sql, ")");
779  
780 27 p = pdl_start(aTHX_ A, slot, &out);
781  
782 /*
783 # uncoverable branch false
784 # uncoverable condition right count:1 class:ignore_covered_err
785 # uncoverable condition right count:2 class:ignore_covered_err */
786 27 ▶ if (ret && SvIV(ret)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ret && SvIV(ret)) { 27 0 27
if (ret && SvIV(ret)) { 27 0 27
787 /* RETURNING goes through the QUERY path, not do - do discards rows.
788 * The row arrives already reshaped. */
789 27 sv_catpvs(sql, " RETURNING *");
790 27 p->op = PDL_OP_PASS;
791 27 pdl_exec(aTHX_ p, db, pdl_tx_of(aTHX_ self), 1, sql, bind, DBIL_ABI_ROW_HASHREF);
792 27 return out;
793 }
794  
795 0 p->op = PDL_OP_CREATE_FB; /* # uncoverable statement */
796 0 p->db = newSVsv(db); /* # uncoverable statement */
797 0 p->dbh = newSVsv(dbh); /* # uncoverable statement */
798 0 p->table = newSVsv(table); /* # uncoverable statement */
799 /*
800 # uncoverable statement
801 # uncoverable branch true
802 # uncoverable branch false
803 # uncoverable condition left count:1
804 # uncoverable condition right count:1
805 # uncoverable condition left count:2
806 # uncoverable condition right count:2 */
807 0 ▶ if (pk && SvOK(pk)) {
Branch: 2 branches
BranchTrueFalseTotal
if (pk && SvOK(pk)) { 0 0 0
if (pk && SvOK(pk)) { 0 0 0
808 /*
809 # uncoverable statement
810 # uncoverable condition left count:1
811 # uncoverable condition right count:1 */
812 0 ▶ HE *he = d ? hv_fetch_ent(d, pk, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HE *he = d ? hv_fetch_ent(d, pk, 0, 0) : NULL; 0 0 0
813 0 p->pk = newSVsv(pk); /* # uncoverable statement */
814 /*
815 # uncoverable statement
816 # uncoverable branch true
817 # uncoverable branch false
818 # uncoverable condition left count:1
819 # uncoverable condition right count:1
820 # uncoverable condition left count:2
821 # uncoverable condition right count:2 */
822 0 ▶ if (he && SvOK(HeVAL(he))) p->pkval = newSVsv(HeVAL(he));
Branch: 2 branches
BranchTrueFalseTotal
if (he && SvOK(HeVAL(he))) p->pkval = newSVsv(HeVAL(he)); 0 0 0
if (he && SvOK(HeVAL(he))) p->pkval = newSVsv(HeVAL(he)); 0 0 0
823 }
824 /*
825 # uncoverable statement
826 # uncoverable branch true
827 # uncoverable branch false
828 # uncoverable condition left count:1
829 # uncoverable condition right count:1 */
830 0 ▶ if (d) p->data = newSVsv(data);
Branch: 1 branch
BranchTrueFalseTotal
if (d) p->data = newSVsv(data); 0 0 0
831 0 pdl_exec(aTHX_ p, db, pdl_tx_of(aTHX_ self), 0, sql, bind, 0); /* # uncoverable statement */
832 0 return out; /* # uncoverable statement */
833 }
834  
835 /* update(\%key_and_changes) -> future of the stored row */
836 5 static SV *pdl_update(pTHX_ SV *self, SV *data) {
837 5 HV *h = pdbi_hv(aTHX_ self);
838 5 ▶ HV *d = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *d = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) 5 0 5
839 10 ▶ ? (HV *)SvRV(data) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(data) : NULL; 5 0 5
840 5 SV *colset = pdbi_get(aTHX_ h, "col");
841 /*
842 # uncoverable condition right count:1 class:ignore_covered_err
843 # uncoverable condition right count:2 class:ignore_covered_err */
844 5 ▶ HV *known = (colset && SvROK(colset)) ? (HV *)SvRV(colset) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *known = (colset && SvROK(colset)) ? (HV *)SvRV(colset) : NULL; 5 0 5
HV *known = (colset && SvROK(colset)) ? (HV *)SvRV(colset) : NULL; 5 0 5
845 5 SV *table = pdbi_get(aTHX_ h, "table");
846 5 SV *pk = pdbi_get(aTHX_ h, "primary");
847 HV *slot, *key;
848 SV *db, *dbh, *ret;
849 AV *all, *cols, *bind;
850 5 const dbil_abi *A = punk_dbil(aTHX);
851 SV *sql, *set, *id, *out;
852 pdl_pend *p;
853 HE *pke;
854 SSize_t i, n;
855 PERL_UNUSED_VAR(key);
856  
857 /*
858 # uncoverable branch true
859 # uncoverable condition left count:1 class:ignore_covered_err
860 # uncoverable condition left count:2 class:ignore_covered_err */
861 5 ▶ if (!(pk && SvOK(pk)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(pk && SvOK(pk))) 5 0 5
if (!(pk && SvOK(pk))) 0 5 5
862 0 croak(PDL_CLS ": update needs a primary key"); /* # uncoverable statement */
863 5 ▶ pke = d ? hv_fetch_ent(d, pk, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
pke = d ? hv_fetch_ent(d, pk, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 0 5
864 5 ▶ if (!(pke && SvOK(HeVAL(pke)))) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(pke && SvOK(HeVAL(pke)))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 4 1 5
if (!(pke && SvOK(HeVAL(pke)))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 4 4
865 1 croak(PDL_CLS ": update needs the primary key in the data");
866 4 id = sv_2mortal(newSVsv(HeVAL(pke)));
867  
868 4 slot = pdl_handle(aTHX_ self);
869 4 db = pdbi_get(aTHX_ slot, "db");
870 4 dbh = pdbi_get(aTHX_ slot, "dbh");
871 4 ret = pdbi_get(aTHX_ slot, "returning");
872  
873 4 all = pdbi_sorted_keys(aTHX_ d);
874 4 cols = (AV *)sv_2mortal((SV *)newAV());
875 4 bind = (AV *)sv_2mortal((SV *)newAV());
876 4 n = av_len(all) + 1;
877 11 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7 4 11
878 7 SV *k = *av_fetch(all, i, 0);
879 HE *he;
880 7 ▶ if (sv_eq(k, pk)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (sv_eq(k, pk)) continue; 4 3 7
881 /*
882 # uncoverable branch true
883 # uncoverable condition left count:1 class:ignore_covered_err
884 # uncoverable condition left count:2 class:ignore_covered_err */
885 3 ▶ if (!(known && hv_exists_ent(known, k, 0))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(known && hv_exists_ent(known, k, 0))) continue; 3 0 3
if (!(known && hv_exists_ent(known, k, 0))) continue; 0 3 3
886 3 he = hv_fetch_ent(d, k, 0, 0);
887 3 av_push(cols, newSVsv(k));
888 3 ▶ av_push(bind, newSVsv(he ? HeVAL(he) : &PL_sv_undef)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
av_push(bind, newSVsv(he ? HeVAL(he) : &PL_sv_undef)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 0 3
889 }
890 4 ▶ if (av_len(cols) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(cols) < 0) 1 3 4
891 1 croak(PDL_CLS ": update with no columns to change");
892  
893 3 set = sv_2mortal(newSVpvs(""));
894 3 n = av_len(cols) + 1;
895 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3 3 6
896 /*
897 # uncoverable branch true
898 # uncoverable condition left count:1 class:ignore_covered_err */
899 3 ▶ if (i) sv_catpvs(set, ", ");
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpvs(set, ", "); 0 3 3
900 3 sv_catsv(set, pdbi_qi_slot(aTHX_ slot, dbh, *av_fetch(cols, i, 0)));
901 3 sv_catpvs(set, " = ?");
902 }
903 3 sql = sv_2mortal(newSVpvs("UPDATE "));
904 3 sv_catsv(sql, pdbi_qi_slot(aTHX_ slot, dbh, table));
905 3 sv_catpvs(sql, " SET "); sv_catsv(sql, set);
906 3 sv_catpvs(sql, " WHERE "); sv_catsv(sql, pdbi_qi_slot(aTHX_ slot, dbh, pk));
907 3 sv_catpvs(sql, " = ?");
908 3 av_push(bind, newSVsv(id)); /* the key binds last */
909  
910 3 p = pdl_start(aTHX_ A, slot, &out);
911  
912 /*
913 # uncoverable branch false
914 # uncoverable condition right count:1 class:ignore_covered_err
915 # uncoverable condition right count:2 class:ignore_covered_err */
916 3 ▶ if (ret && SvIV(ret)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ret && SvIV(ret)) { 3 0 3
if (ret && SvIV(ret)) { 3 0 3
917 3 sv_catpvs(sql, " RETURNING *");
918 3 p->op = PDL_OP_PASS;
919 3 pdl_exec(aTHX_ p, db, pdl_tx_of(aTHX_ self), 1, sql, bind, DBIL_ABI_ROW_HASHREF);
920 3 return out;
921 }
922  
923 0 p->op = PDL_OP_UPDATE_FB; /* # uncoverable statement */
924 0 p->db = newSVsv(db); /* # uncoverable statement */
925 0 p->dbh = newSVsv(dbh); /* # uncoverable statement */
926 0 p->table = newSVsv(table); /* # uncoverable statement */
927 0 p->pk = newSVsv(pk); /* # uncoverable statement */
928 0 p->pkval = newSVsv(id); /* # uncoverable statement */
929 0 pdl_exec(aTHX_ p, db, pdl_tx_of(aTHX_ self), 0, sql, bind, 0); /* # uncoverable statement */
930 0 return out; /* # uncoverable statement */
931 }
932  
933 /* delete(%key) -> future of the affected row count */
934 4 static SV *pdl_delete(pTHX_ SV *self, SV **st, I32 items) {
935 HV *key;
936 4 AV *keys = pdbi_key_args(aTHX_ st, items, &key, "delete", PDL_CLS);
937 4 HV *slot = pdl_handle(aTHX_ self);
938 4 SV *db = pdbi_get(aTHX_ slot, "db");
939 4 SV *dbh = pdbi_get(aTHX_ slot, "dbh");
940 4 AV *bind = (AV *)sv_2mortal((SV *)newAV());
941 4 SV *where = pdbi_where_eq_slot(aTHX_ slot, dbh, key, keys, bind);
942 4 SV *table = pdbi_get(aTHX_ pdbi_hv(aTHX_ self), "table");
943 4 SV *sql = sv_2mortal(newSVpvs("DELETE FROM "));
944 4 const dbil_abi *A = punk_dbil(aTHX);
945 SV *out;
946 pdl_pend *p;
947  
948 4 pdbq_check_keys(aTHX_ self, keys, PDL_CLS, "delete");
949  
950 2 sv_catsv(sql, pdbi_qi_slot(aTHX_ slot, dbh, table));
951 2 sv_catpvs(sql, " WHERE ");
952 2 sv_catsv(sql, where);
953  
954 2 p = pdl_start(aTHX_ A, slot, &out);
955 2 p->op = PDL_OP_COUNT;
956 2 pdl_exec(aTHX_ p, db, pdl_tx_of(aTHX_ self), 0, sql, bind, 0);
957 2 return out;
958 }
959  
960 /* future($dbil_future) -> Punk::Future: the public bridge, for custom model
961 * methods that run their own SQL on $backend->db and want to hand back the
962 * framework's own future type. All the settled values carry across. */
963 2 static SV *pdl_wrap(pTHX_ SV *self, SV *f) {
964 2 const dbil_abi *A = punk_dbil(aTHX);
965 2 HV *slot = pdl_handle(aTHX_ self);
966 SV *out;
967 pdl_pend *p;
968 2 ▶ if (!A->is_future(aTHX_ f))
Branch: 1 branch
BranchTrueFalseTotal
if (!A->is_future(aTHX_ f)) 1 1 2
969 1 croak(PDL_CLS "->future: need a DBIx::Loop::Future");
970 1 p = pdl_start(aTHX_ A, slot, &out);
971 1 p->op = PDL_OP_WRAP;
972 1 pdl_attach(aTHX_ SvREFCNT_inc(f), p); /* attach takes ownership */
973 1 return out;
974 }
975  
976 /* ---- await: pump the adapter's loop until a Punk::Future settles ---------
977 *
978 * On a worker, $c->await already works - the future is loop-backed and
979 * pf_await pumps the worker's loop, which is the same loop the adapter's
980 * watchers are on. This is for scripts and tests, where the adapter carries
981 * its own loop and nothing else will ever pump it. */
982 8 static void pdl_await(pTHX_ SV *self, SV *f) {
983 8 punk_future *pf = pf_of(aTHX_ f); /* croaks unless a Punk::Future */
984 8 ▶ if (pf->state != PF_PENDING) return;
Branch: 1 branch
BranchTrueFalseTotal
if (pf->state != PF_PENDING) return; 1 7 8
985 7 ▶ if (pf->is_loop) { pf_await(aTHX_ f); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (pf->is_loop) { pf_await(aTHX_ f); return; } 6 1 7
986 {
987 1 const hm_abi *hm = punk_hm(aTHX);
988 1 HV *slot = pdl_handle(aTHX_ self);
989 1 SV *adapter = pdbi_get(aTHX_ slot, "adapter");
990 SV *loop_sv, *hf, *clos;
991 void *loop;
992 AV *cap;
993 /*
994 # uncoverable branch true
995 # uncoverable condition left count:1 class:ignore_covered_err
996 # uncoverable condition left count:2 class:ignore_covered_err */
997 1 ▶ if (!hm || !adapter)
Branch: 2 branches
BranchTrueFalseTotal
if (!hm || !adapter) 1 0 1
if (!hm || !adapter) 0 1 1
998 0 croak(PDL_CLS ": await with no loop adapter to pump"); /* # uncoverable statement */
999 1 loop_sv = pcx_call_meth(aTHX_ adapter, "loop", NULL, 0, 1);
1000 /*
1001 # uncoverable branch true
1002 # uncoverable condition left count:1 class:ignore_covered_err
1003 # uncoverable condition left count:2 class:ignore_covered_err */
1004 1 ▶ if (!(loop_sv && SvROK(loop_sv)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(loop_sv && SvROK(loop_sv))) 1 0 1
if (!(loop_sv && SvROK(loop_sv))) 0 1 1
1005 0 croak(PDL_CLS ": the adapter has no loop to pump"); /* # uncoverable statement */
1006 1 sv_2mortal(loop_sv);
1007 1 loop = hm->loop_of_sv(aTHX_ loop_sv);
1008 1 hf = hm->future_new(aTHX);
1009 1 cap = newAV();
1010 1 av_push(cap, newSVsv(hf));
1011 1 clos = sv_2mortal(punk_closure(aTHX_ pf_wake_hf_cb, cap));
1012 1 pf_react(aTHX_ pf, f, PFR_ON_READY, clos);
1013 /*
1014 # uncoverable branch false
1015 # uncoverable condition right count:1 class:ignore_covered_err */
1016 1 ▶ if (pf->state == PF_PENDING)
Branch: 1 branch
BranchTrueFalseTotal
if (pf->state == PF_PENDING) 1 0 1
1017 1 hm->run_until(aTHX_ loop, hf);
1018 1 SvREFCNT_dec(hf);
1019 }
1020 }
1021  
1022 #endif /* PUNK_DBIL_H */
« xs/stream.xs ↑ Summary include/punk/punk_csrf.h »