include/punk/punk_dbil.h

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