include/punk/punk_txn.h

filter: Statementstmt 95.4% 209 / 220 (1 uncoverable) Branchbran 58.8% 132 / 238 (25 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.4% 341 / 458 (26 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_csrf.h ↑ Summary xs/request.xs »
1 /* punk_txn.h - $c->txn: a transaction on a configured database, on either
2 * model backend, with the models that take part bound to it.
3 *
4 * my $order = $c->txn(sub {
5 * my ($tx) = @_;
6 * my $o = $tx->model('Order')->create(\%data);
7 * $tx->model('Stock')->update({ id => $sku, held => $held + 1 });
8 * return $o;
9 * });
10 * $c->txn(analytics => sub { ... });
11 *
12 * The block gets a Punk::Txn. $tx->model($name) is the model bound to the
13 * transaction; what that binding means differs by backend, and the
14 * difference is the whole design:
15 *
16 * - Punk::Model::DBI holds one connection per database per worker, so a
17 * transaction on it is begin_work / commit / rollback on that handle and
18 * every model on the database is inside it whether it came through
19 * $tx->model or $c->model. The binding is a courtesy and a check.
20 *
21 * - Punk::Model::DBIx::Loop runs statements on a pool, and DBIx::Loop pins
22 * a transaction to ONE slot: its own rule is that plain statements during
23 * a transaction run on other slots and never join. So a model reached
24 * through $c->model inside the block is OUTSIDE the transaction, and
25 * $tx->model is the only way in - it hands back a copy of the model whose
26 * backend carries the pinned handle, and every statement that copy runs
27 * goes through it. A worker-wide "current transaction" would be wrong
28 * here: under the event loop the requests served between two of this
29 * block's queries belong to somebody else.
30 *
31 * Both return what the block returns - the value itself on DBI, a
32 * Punk::Future of it on DBIx::Loop, resolved after COMMIT - which is the
33 * rule Punk::Model already states for the contract: shapes, not timing.
34 *
35 * Nested: croaks on DBI (one connection, one transaction, and a silent join
36 * is how a rollback stops covering what the caller thought it covered);
37 * DBIx::Loop's own txn inside a block is an independent transaction on
38 * another slot, and the POD says so rather than hiding it.
39 *
40 * Include after punk_dbil.h (both backends, pdl_start, the bridge) and
41 * punk_future.h, and punk_static.h for punk_closure. */
42  
43 #ifndef PUNK_TXN_H
44 #define PUNK_TXN_H
45  
46 #define PTX_CLS "Punk::Txn"
47  
48 79 static HV *ptx_hv(pTHX_ SV *tx) {
49 /*
50 # uncoverable condition left count:1 class:ignore_covered_err
51 # uncoverable condition left count:3 class:ignore_covered_err
52 # uncoverable condition left count:4 class:ignore_covered_err */
53 79 ▶ if (!(tx && SvROK(tx) && SvTYPE(SvRV(tx)) == SVt_PVHV
Branch: 3 branches
BranchTrueFalseTotal
if (!(tx && SvROK(tx) && SvTYPE(SvRV(tx)) == SVt_PVHV 79 0 79
if (!(tx && SvROK(tx) && SvTYPE(SvRV(tx)) == SVt_PVHV 78 1 79
if (!(tx && SvROK(tx) && SvTYPE(SvRV(tx)) == SVt_PVHV 78 0 78
54 78 ▶ && sv_derived_from(tx, PTX_CLS)))
Branch: 1 branch
BranchTrueFalseTotal
&& sv_derived_from(tx, PTX_CLS))) 0 78 78
55 1 croak(PTX_CLS ": not a transaction object");
56 78 return (HV *)SvRV(tx);
57 }
58  
59 /* ---- the database behind a name -------------------------------------------- */
60  
61 /* The `database` keyword's options for a name (K_DATABASES), and the backend
62 * class they select. `name` NULL or undef is the default database. Returns
63 * the options as an RV (mortal); *class_out is a mortal class-name SV. */
64 25 static SV *ptx_db_opts(pTHX_ SV *app, SV *name, SV **class_out) {
65 25 HV *h = app_hv(aTHX_ app);
66 25 HV *dbs = app_hash(aTHX_ h, K_DATABASES);
67 25 const char *nm = K_DEFAULT; STRLEN nl = 7;
68 25 SV **opts, *ret, *b = NULL;
69 /*
70 # uncoverable condition right count:1 class:ignore_covered_err
71 # uncoverable condition right count:3 class:ignore_covered_err */
72 30 ▶ if (name && SvOK(name) && SvCUR(name)) nm = SvPV_const(name, nl);
Branch: 3 branches
BranchTrueFalseTotal
if (name && SvOK(name) && SvCUR(name)) nm = SvPV_const(name, nl); 25 0 25
if (name && SvOK(name) && SvCUR(name)) nm = SvPV_const(name, nl); 5 20 25
if (name && SvOK(name) && SvCUR(name)) nm = SvPV_const(name, nl); 5 0 5
73 25 opts = hv_fetch(dbs, nm, (I32)nl, 0);
74 /*
75 # uncoverable condition right count:2 class:ignore_covered_err
76 # uncoverable condition right count:3 class:ignore_covered_err
77 # uncoverable condition right count:4 class:ignore_covered_err */
78 25 ▶ if (opts && *opts && SvROK(*opts) && SvTYPE(SvRV(*opts)) == SVt_PVHV) {
Branch: 4 branches
BranchTrueFalseTotal
if (opts && *opts && SvROK(*opts) && SvTYPE(SvRV(*opts)) == SVt_PVHV) { 22 3 25
if (opts && *opts && SvROK(*opts) && SvTYPE(SvRV(*opts)) == SVt_PVHV) { 22 0 22
if (opts && *opts && SvROK(*opts) && SvTYPE(SvRV(*opts)) == SVt_PVHV) { 22 0 22
if (opts && *opts && SvROK(*opts) && SvTYPE(SvRV(*opts)) == SVt_PVHV) { 22 0 22
79 22 ret = sv_2mortal(newSVsv(*opts));
80 22 b = pdbi_get(aTHX_ (HV *)SvRV(*opts), "backend");
81 }
82 3 ▶ else if (nl == 7 && memEQ(nm, K_DEFAULT, 7)) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (nl == 7 && memEQ(nm, K_DEFAULT, 7)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 2 3
else if (nl == 7 && memEQ(nm, K_DEFAULT, 7)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
83 1 croak(PTX_CLS ": no database configured - add a database keyword");
84 else
85 2 croak(PTX_CLS ": no database '%s' configured (the database keyword "
86 "names one as `database %s => { dsn => ... }`)", nm, nm);
87 9 ▶ *class_out = (b && SvOK(b)) ? sv_2mortal(newSVsv(b))
Branch: 1 branch
BranchTrueFalseTotal
*class_out = (b && SvOK(b)) ? sv_2mortal(newSVsv(b)) 9 0 9
88 22 ▶ : sv_2mortal(newSVpvs("Punk::Model::DBI"));
Branch: 1 branch
BranchTrueFalseTotal
: sv_2mortal(newSVpvs("Punk::Model::DBI")); 9 13 22
89 22 return ret;
90 }
91  
92 /* ---- Punk::Txn ------------------------------------------------------------ */
93  
94 20 static SV *ptx_new(pTHX_ SV *c, SV *app, SV *name, SV *class, SV *opts) {
95 20 HV *h = newHV();
96 20 (void)hv_stores(h, "c", newSVsv(c));
97 20 (void)hv_stores(h, "app", newSVsv(app));
98 /*
99 # uncoverable condition right count:1 class:ignore_covered_err
100 # uncoverable condition right count:3 class:ignore_covered_err */
101 20 ▶ (void)hv_stores(h, "name", (name && SvOK(name) && SvCUR(name))
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(h, "name", (name && SvOK(name) && SvCUR(name)) 20 0 20
(void)hv_stores(h, "name", (name && SvOK(name) && SvCUR(name)) 3 17 20
(void)hv_stores(h, "name", (name && SvOK(name) && SvCUR(name)) 3 0 3
102 ? newSVsv(name) : newSVpvs(K_DEFAULT));
103 20 (void)hv_stores(h, "class", newSVsv(class));
104 20 (void)hv_stores(h, "opts", newSVsv(opts));
105 20 (void)hv_stores(h, "handle", newSV(0));
106 20 (void)hv_stores(h, "active", newSViv(0));
107 20 return sv_bless(newRV_noinc((SV *)h), gv_stashpvs(PTX_CLS, GV_ADD));
108 }
109  
110 /* $tx->model($name): the registered model, bound to this transaction.
111 *
112 * A shallow copy of the per-worker instance with a copy of its backend
113 * carrying the transaction handle under `_tx`. The shared instance is never
114 * touched, so nothing leaks out of the block. A model on another database
115 * croaks: it cannot be in this transaction, and pretending would be the
116 * worst of the available bugs. */
117 29 static SV *ptx_model(pTHX_ SV *tx, SV *name) {
118 29 HV *th = ptx_hv(aTHX_ tx);
119 28 SV *app = pdbi_get(aTHX_ th, "app");
120 28 SV *txnm = pdbi_get(aTHX_ th, "name");
121 28 SV *hdl = pdbi_get(aTHX_ th, "handle");
122 28 SV *act = pdbi_get(aTHX_ th, "active");
123 SV *argv[1], *inst, *meta, *backend, *copy_rv, *bcopy_rv;
124 HV *ih, *bh, *copy, *bcopy;
125 28 const char *mdb = K_DEFAULT; STRLEN ml = 7;
126  
127 28 ▶ if (!(act && SvTRUE(act))) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(act && SvTRUE(act))) /* # uncoverable condition left count:1 class:ignore_covered_err */ 28 0 28
if (!(act && SvTRUE(act))) /* # uncoverable condition left count:1 class:ignore_covered_err */ 2 26 28
128 2 croak(PTX_CLS ": the transaction is over - $tx->model is for inside "
129 "the block");
130 26 argv[0] = name;
131 26 inst = pcx_call_meth(aTHX_ app, "model_instance", argv, 1, 1);
132 /*
133 # uncoverable branch true
134 # uncoverable condition left count:1 class:ignore_covered_err
135 # uncoverable condition left count:2 class:ignore_covered_err
136 # uncoverable condition left count:3 class:ignore_covered_err */
137 26 ▶ if (!(inst && SvROK(inst) && SvTYPE(SvRV(inst)) == SVt_PVHV)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(inst && SvROK(inst) && SvTYPE(SvRV(inst)) == SVt_PVHV)) { 26 0 26
if (!(inst && SvROK(inst) && SvTYPE(SvRV(inst)) == SVt_PVHV)) { 26 0 26
if (!(inst && SvROK(inst) && SvTYPE(SvRV(inst)) == SVt_PVHV)) { 0 26 26
138 /*
139 # uncoverable statement
140 # uncoverable branch true
141 # uncoverable branch false
142 # uncoverable condition left count:1
143 # uncoverable condition right count:1 */
144 0 ▶ if (inst) SvREFCNT_dec(inst);
Branch: 1 branch
BranchTrueFalseTotal
if (inst) SvREFCNT_dec(inst); 0 0 0
145 0 croak(PTX_CLS ": no model '%s'", SvPV_nolen(name)); /* # uncoverable statement */
146 }
147 26 sv_2mortal(inst);
148 26 ih = (HV *)SvRV(inst);
149  
150 /* the model's database, from its metadata */
151 26 meta = pdbi_get(aTHX_ ih, "meta");
152 /*
153 # uncoverable branch false
154 # uncoverable condition right count:1 class:ignore_covered_err
155 # uncoverable condition right count:2 class:ignore_covered_err
156 # uncoverable condition right count:3 class:ignore_covered_err */
157 26 ▶ if (meta && SvROK(meta) && SvTYPE(SvRV(meta)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (meta && SvROK(meta) && SvTYPE(SvRV(meta)) == SVt_PVHV) { 26 0 26
if (meta && SvROK(meta) && SvTYPE(SvRV(meta)) == SVt_PVHV) { 26 0 26
if (meta && SvROK(meta) && SvTYPE(SvRV(meta)) == SVt_PVHV) { 26 0 26
158 26 SV *d = pdbi_get(aTHX_ (HV *)SvRV(meta), "database");
159 /*
160 # uncoverable branch false
161 # uncoverable condition right count:1 class:ignore_covered_err
162 # uncoverable condition right count:2 class:ignore_covered_err
163 # uncoverable condition right count:3 class:ignore_covered_err */
164 52 ▶ if (d && SvOK(d) && SvCUR(d)) mdb = SvPV_const(d, ml);
Branch: 3 branches
BranchTrueFalseTotal
if (d && SvOK(d) && SvCUR(d)) mdb = SvPV_const(d, ml); 26 0 26
if (d && SvOK(d) && SvCUR(d)) mdb = SvPV_const(d, ml); 26 0 26
if (d && SvOK(d) && SvCUR(d)) mdb = SvPV_const(d, ml); 26 0 26
165 }
166 26 ▶ if (!(SvCUR(txnm) == ml && memEQ(SvPVX(txnm), mdb, ml))) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvCUR(txnm) == ml && memEQ(SvPVX(txnm), mdb, ml))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 23 3 26
if (!(SvCUR(txnm) == ml && memEQ(SvPVX(txnm), mdb, ml))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 23 23
167 6 ▶ croak(PTX_CLS ": model '%s' lives on database '%.*s', and this "
Branch: 1 branch
BranchTrueFalseTotal
croak(PTX_CLS ": model '%s' lives on database '%.*s', and this " 3 0 3
168 "transaction is on '%s'", SvPV_nolen(name), (int)ml, mdb,
169 SvPV_nolen(txnm));
170  
171 23 backend = pdbi_get(aTHX_ ih, "backend");
172 /*
173 # uncoverable branch true
174 # uncoverable condition left count:1 class:ignore_covered_err
175 # uncoverable condition left count:2 class:ignore_covered_err
176 # uncoverable condition left count:3 class:ignore_covered_err */
177 23 ▶ if (!(backend && SvROK(backend) && SvTYPE(SvRV(backend)) == SVt_PVHV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(backend && SvROK(backend) && SvTYPE(SvRV(backend)) == SVt_PVHV)) 23 0 23
if (!(backend && SvROK(backend) && SvTYPE(SvRV(backend)) == SVt_PVHV)) 23 0 23
if (!(backend && SvROK(backend) && SvTYPE(SvRV(backend)) == SVt_PVHV)) 0 23 23
178 0 croak(PTX_CLS ": model '%s' has no backend", SvPV_nolen(name)); /* # uncoverable statement */
179 23 bh = (HV *)SvRV(backend);
180  
181 23 bcopy = newHVhv(bh);
182 23 ▶ (void)hv_stores(bcopy, "_tx", newSVsv(hdl ? hdl : &PL_sv_undef)); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(bcopy, "_tx", newSVsv(hdl ? hdl : &PL_sv_undef)); /* # uncoverable condition left count:1 class:ignore_covered_err */ 23 0 23
183 23 bcopy_rv = sv_bless(newRV_noinc((SV *)bcopy), SvSTASH(SvRV(backend)));
184  
185 23 copy = newHVhv(ih);
186 23 (void)hv_stores(copy, "backend", bcopy_rv);
187 23 copy_rv = sv_bless(newRV_noinc((SV *)copy), SvSTASH(SvRV(inst)));
188 23 return copy_rv;
189 }
190  
191 /* ---- the DBI backend: one handle, begin_work / commit / rollback ---------- */
192  
193 24 static SV *ptx_dbi_call0(pTHX_ SV *dbh, const char *meth, SV **err) {
194 24 dSP;
195 int count;
196 24 SV *r = NULL;
197 24 ENTER; SAVETMPS;
198 24 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 24 24
199 24 ▶ XPUSHs(dbh);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dbh); 0 24 24
200 24 PUTBACK;
201 24 count = call_method(meth, G_SCALAR | G_EVAL);
202 24 SPAGAIN;
203 /*
204 # uncoverable branch false
205 # uncoverable condition right count:1 class:ignore_covered_err */
206 24 ▶ if (count > 0) r = SvREFCNT_inc(POPs);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) r = SvREFCNT_inc(POPs); 24 0 24
207 24 PUTBACK;
208 /*
209 # uncoverable branch true
210 # uncoverable condition right count:1 class:ignore_covered_err
211 # uncoverable condition left count:2 class:ignore_covered_err
212 # uncoverable condition left count:3 class:ignore_covered_err
213 # uncoverable condition right count:3 class:ignore_covered_err
214 # uncoverable condition left count:4 class:ignore_covered_err
215 # uncoverable condition right count:4 class:ignore_covered_err */
216 24 ▶ if (SvTRUE(ERRSV)) { *err = newSVsv(ERRSV); if (r) SvREFCNT_dec(r); r = NULL; }
Branch: 4 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { *err = newSVsv(ERRSV); if (r) SvREFCNT_dec(r); r = NULL; } 24 0 24
if (SvTRUE(ERRSV)) { *err = newSVsv(ERRSV); if (r) SvREFCNT_dec(r); r = NULL; } 0 24 24
if (SvTRUE(ERRSV)) { *err = newSVsv(ERRSV); if (r) SvREFCNT_dec(r); r = NULL; } 0 0 0
if (SvTRUE(ERRSV)) { *err = newSVsv(ERRSV); if (r) SvREFCNT_dec(r); r = NULL; } 0 0 0
217 24 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 24 0 24
218 24 return r;
219 }
220  
221 /* Punk::Model::DBI->_txn(\%opts, $code, $tx) -> the block's value */
222 13 static SV *ptx_dbi_run(pTHX_ SV *opts, SV *code, SV *tx) {
223 13 ▶ HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 13 0 13
HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 13 0 13
224 26 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 13 0 13
225 13 HV *slot = pdbi_slot_for_opts(aTHX_ o);
226 13 SV *dbh = pdbi_get(aTHX_ slot, "dbh");
227 13 SV *open = pdbi_get(aTHX_ slot, "txn");
228 13 HV *th = ptx_hv(aTHX_ tx);
229 13 SV *err = NULL, *res = NULL, *r;
230  
231 /*
232 # uncoverable branch true
233 # uncoverable condition left count:1 class:ignore_covered_err
234 # uncoverable condition left count:2 class:ignore_covered_err */
235 13 ▶ if (!(dbh && SvROK(dbh)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(dbh && SvROK(dbh))) 13 0 13
if (!(dbh && SvROK(dbh))) 0 13 13
236 0 croak("Punk::Model::DBI: no connection for the transaction"); /* # uncoverable statement */
237 13 ▶ if (open && SvTRUE(open))
Branch: 2 branches
BranchTrueFalseTotal
if (open && SvTRUE(open)) 10 3 13
if (open && SvTRUE(open)) 1 9 10
238 1 croak("Punk::Model::DBI: a transaction is already open on this "
239 "connection - nested transactions are not supported, and a "
240 "silent join would stop a rollback covering what the outer "
241 "block thinks it covers");
242  
243 12 r = ptx_dbi_call0(aTHX_ dbh, "begin_work", &err);
244 /*
245 # uncoverable branch false
246 # uncoverable condition right count:1 class:ignore_covered_err */
247 12 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 12 0 12
248 /*
249 # uncoverable branch true
250 # uncoverable condition left count:1 class:ignore_covered_err */
251 12 ▶ if (err) { sv_2mortal(err); croak_sv(err); }
Branch: 1 branch
BranchTrueFalseTotal
if (err) { sv_2mortal(err); croak_sv(err); } 0 12 12
252  
253 12 (void)hv_stores(slot, "txn", newSViv(1));
254 12 (void)hv_stores(th, "handle", newSVsv(dbh));
255 12 (void)hv_stores(th, "active", newSViv(1));
256  
257 { /* the block, trapped: a die is a rollback and a rethrow */
258 12 dSP;
259 int count;
260 12 ENTER; SAVETMPS;
261 12 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 12 12
262 12 ▶ XPUSHs(tx);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(tx); 0 12 12
263 12 PUTBACK;
264 12 count = call_sv(code, G_SCALAR | G_EVAL);
265 12 SPAGAIN;
266 /*
267 # uncoverable branch false
268 # uncoverable condition right count:1 class:ignore_covered_err */
269 12 ▶ if (count > 0) res = SvREFCNT_inc(POPs);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) res = SvREFCNT_inc(POPs); 12 0 12
270 12 PUTBACK;
271 /*
272 # uncoverable condition right count:1 class:ignore_covered_err
273 # uncoverable condition right count:3 class:ignore_covered_err
274 # uncoverable condition right count:4 class:ignore_covered_err */
275 12 ▶ if (SvTRUE(ERRSV)) { err = newSVsv(ERRSV); if (res) SvREFCNT_dec(res); res = NULL; }
Branch: 4 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { err = newSVsv(ERRSV); if (res) SvREFCNT_dec(res); res = NULL; } 12 0 12
if (SvTRUE(ERRSV)) { err = newSVsv(ERRSV); if (res) SvREFCNT_dec(res); res = NULL; } 5 7 12
if (SvTRUE(ERRSV)) { err = newSVsv(ERRSV); if (res) SvREFCNT_dec(res); res = NULL; } 5 0 5
if (SvTRUE(ERRSV)) { err = newSVsv(ERRSV); if (res) SvREFCNT_dec(res); res = NULL; } 5 0 5
276 12 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 12 0 12
277 }
278  
279 12 ▶ if (!err) {
Branch: 1 branch
BranchTrueFalseTotal
if (!err) { 7 5 12
280 7 r = ptx_dbi_call0(aTHX_ dbh, "commit", &err);
281 /*
282 # uncoverable branch false
283 # uncoverable condition right count:1 class:ignore_covered_err */
284 7 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 7 0 7
285 }
286 12 ▶ if (err) {
Branch: 1 branch
BranchTrueFalseTotal
if (err) { 5 7 12
287 5 SV *rerr = NULL;
288 5 r = ptx_dbi_call0(aTHX_ dbh, "rollback", &rerr);
289 /*
290 # uncoverable branch false
291 # uncoverable condition right count:1 class:ignore_covered_err */
292 5 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 5 0 5
293 /*
294 # uncoverable branch true
295 # uncoverable condition left count:1 class:ignore_covered_err */
296 5 ▶ if (rerr) SvREFCNT_dec(rerr); /* the block's error is the one to report */
Branch: 1 branch
BranchTrueFalseTotal
if (rerr) SvREFCNT_dec(rerr); /* the block's error is the one to report */ 0 5 5
297 5 (void)hv_stores(slot, "txn", newSViv(0));
298 5 (void)hv_stores(th, "active", newSViv(0));
299 5 sv_2mortal(err);
300 5 croak_sv(err);
301 }
302 7 (void)hv_stores(slot, "txn", newSViv(0));
303 7 (void)hv_stores(th, "active", newSViv(0));
304 7 ▶ return res ? res : newSV(0); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return res ? res : newSV(0); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 7 7
305 }
306  
307 /* ---- the DBIx::Loop backend: $db->txn, with the bridge ---------------------- */
308  
309 /* capture layout for the two closures */
310 enum { PTX_CAP_CODE = 0, PTX_CAP_TX, PTX_CAP_DLF };
311  
312 /* A Punk::Future settled -> the DBIx::Loop::Future the txn is waiting on.
313 * Registered as an on_ready reaction, called with the Punk::Future. */
314 XS_INTERNAL(ptx_pf_ready_cb);
315 6 XS_INTERNAL(ptx_pf_ready_cb) {
316 6 dXSARGS;
317 6 AV *cap = punk_clos_cap(aTHX_ cv);
318 6 const dbil_abi *A = punk_dbil(aTHX);
319 6 ▶ SV **dlfp = cap ? av_fetch(cap, PTX_CAP_DLF, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **dlfp = cap ? av_fetch(cap, PTX_CAP_DLF, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 6 0 6
320 6 ▶ SV **txp = cap ? av_fetch(cap, PTX_CAP_TX, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **txp = cap ? av_fetch(cap, PTX_CAP_TX, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 6 0 6
321 punk_future *pf;
322 /*
323 # uncoverable branch true
324 # uncoverable condition left count:1 class:ignore_covered_err
325 # uncoverable condition left count:2 class:ignore_covered_err
326 # uncoverable condition left count:3 class:ignore_covered_err
327 # uncoverable condition left count:4 class:ignore_covered_err */
328 6 ▶ if (!(dlfp && *dlfp) || items < 1 || !pf_is_future(aTHX_ ST(0)))
Branch: 4 branches
BranchTrueFalseTotal
if (!(dlfp && *dlfp) || items < 1 || !pf_is_future(aTHX_ ST(0))) 6 0 6
if (!(dlfp && *dlfp) || items < 1 || !pf_is_future(aTHX_ ST(0))) 6 0 6
if (!(dlfp && *dlfp) || items < 1 || !pf_is_future(aTHX_ ST(0))) 6 0 6
if (!(dlfp && *dlfp) || items < 1 || !pf_is_future(aTHX_ ST(0))) 0 6 6
329 0 XSRETURN_EMPTY; /* # uncoverable statement */
330 /* the block is over once its future settles, whichever way */
331 /*
332 # uncoverable branch false
333 # uncoverable condition right count:1 class:ignore_covered_err
334 # uncoverable condition right count:2 class:ignore_covered_err
335 # uncoverable condition right count:3 class:ignore_covered_err */
336 6 ▶ if (txp && *txp && SvROK(*txp))
Branch: 3 branches
BranchTrueFalseTotal
if (txp && *txp && SvROK(*txp)) 6 0 6
if (txp && *txp && SvROK(*txp)) 6 0 6
if (txp && *txp && SvROK(*txp)) 6 0 6
337 6 (void)hv_stores(ptx_hv(aTHX_ *txp), "active", newSViv(0));
338 6 pf = pf_of(aTHX_ ST(0));
339 6 ▶ if (pf->state == PF_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (pf->state == PF_DONE) { 3 3 6
340 /*
341 # uncoverable condition right count:1 class:ignore_covered_err
342 # uncoverable condition right count:2 class:ignore_covered_err */
343 3 ▶ SV **v = (pf->vals && av_len(pf->vals) >= 0) ? av_fetch(pf->vals, 0, 0) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV **v = (pf->vals && av_len(pf->vals) >= 0) ? av_fetch(pf->vals, 0, 0) : NULL; 3 0 3
SV **v = (pf->vals && av_len(pf->vals) >= 0) ? av_fetch(pf->vals, 0, 0) : NULL; 3 0 3
344 /*
345 # uncoverable condition right count:1 class:ignore_covered_err
346 # uncoverable condition right count:2 class:ignore_covered_err */
347 3 ▶ A->future_done1(aTHX_ *dlfp, (v && *v) ? *v : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
A->future_done1(aTHX_ *dlfp, (v && *v) ? *v : &PL_sv_undef); 3 0 3
A->future_done1(aTHX_ *dlfp, (v && *v) ? *v : &PL_sv_undef); 3 0 3
348 }
349 else {
350 /*
351 # uncoverable condition right count:1 class:ignore_covered_err
352 # uncoverable condition right count:2 class:ignore_covered_err */
353 3 ▶ SV **e = (pf->vals && av_len(pf->vals) >= 0) ? av_fetch(pf->vals, 0, 0) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV **e = (pf->vals && av_len(pf->vals) >= 0) ? av_fetch(pf->vals, 0, 0) : NULL; 3 0 3
SV **e = (pf->vals && av_len(pf->vals) >= 0) ? av_fetch(pf->vals, 0, 0) : NULL; 3 0 3
354 /*
355 # uncoverable condition right count:1 class:ignore_covered_err
356 # uncoverable condition right count:2 class:ignore_covered_err */
357 3 ▶ A->future_fail(aTHX_ *dlfp, (e && *e) ? *e
Branch: 2 branches
BranchTrueFalseTotal
A->future_fail(aTHX_ *dlfp, (e && *e) ? *e 3 0 3
A->future_fail(aTHX_ *dlfp, (e && *e) ? *e 3 0 3
358 0 : sv_2mortal(newSVpvs("transaction block failed"))); /* # uncoverable statement */
359 }
360 6 XSRETURN_EMPTY;
361 }
362  
363 /* The block DBIx::Loop's txn calls with the pinned DBIx::Loop::Txn handle.
364 * Records the handle on the Punk::Txn, runs the application's block, and
365 * hands back its result - a Punk::Future bridged into a DBIx::Loop::Future,
366 * because txn commits as soon as the block returns anything that is not one
367 * of its own futures, and a commit before the model's writes have landed is
368 * exactly the bug a transaction exists to prevent. */
369 XS_INTERNAL(ptx_loop_block_cb);
370 7 XS_INTERNAL(ptx_loop_block_cb) {
371 7 dXSARGS;
372 7 AV *cap = punk_clos_cap(aTHX_ cv);
373 7 const dbil_abi *A = punk_dbil(aTHX);
374 7 SV *code, *tx, *txh, *res = NULL, *err = NULL;
375 HV *th;
376 /*
377 # uncoverable branch true class:ignore_covered_err
378 # uncoverable condition left count:1 class:ignore_covered_err
379 # uncoverable condition left count:2 class:ignore_covered_err */
380 7 ▶ if (!cap || items < 1) croak(PTX_CLS ": the transaction block lost its capture");
Branch: 2 branches
BranchTrueFalseTotal
if (!cap || items < 1) croak(PTX_CLS ": the transaction block lost its capture"); 7 0 7
if (!cap || items < 1) croak(PTX_CLS ": the transaction block lost its capture"); 0 7 7
381 7 code = *av_fetch(cap, PTX_CAP_CODE, 0);
382 7 tx = *av_fetch(cap, PTX_CAP_TX, 0);
383 7 txh = ST(0);
384 7 th = ptx_hv(aTHX_ tx);
385 7 (void)hv_stores(th, "handle", newSVsv(txh));
386 7 (void)hv_stores(th, "active", newSViv(1));
387 {
388 7 dSP;
389 int count;
390 7 ENTER; SAVETMPS;
391 7 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 7 7
392 7 ▶ XPUSHs(tx);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(tx); 0 7 7
393 7 PUTBACK;
394 7 count = call_sv(code, G_SCALAR | G_EVAL);
395 7 SPAGAIN;
396 /*
397 # uncoverable branch false
398 # uncoverable condition right count:1 class:ignore_covered_err */
399 7 ▶ if (count > 0) res = SvREFCNT_inc(POPs);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) res = SvREFCNT_inc(POPs); 7 0 7
400 7 PUTBACK;
401 /*
402 # uncoverable branch true
403 # uncoverable condition right count:1 class:ignore_covered_err
404 # uncoverable condition left count:2 class:ignore_covered_err
405 # uncoverable condition left count:3 class:ignore_covered_err
406 # uncoverable condition right count:3 class:ignore_covered_err
407 # uncoverable condition left count:4 class:ignore_covered_err
408 # uncoverable condition right count:4 class:ignore_covered_err */
409 7 ▶ if (SvTRUE(ERRSV)) { err = newSVsv(ERRSV); if (res) SvREFCNT_dec(res); res = NULL; }
Branch: 4 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { err = newSVsv(ERRSV); if (res) SvREFCNT_dec(res); res = NULL; } 7 0 7
if (SvTRUE(ERRSV)) { err = newSVsv(ERRSV); if (res) SvREFCNT_dec(res); res = NULL; } 0 7 7
if (SvTRUE(ERRSV)) { err = newSVsv(ERRSV); if (res) SvREFCNT_dec(res); res = NULL; } 0 0 0
if (SvTRUE(ERRSV)) { err = newSVsv(ERRSV); if (res) SvREFCNT_dec(res); res = NULL; } 0 0 0
410 7 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 7 0 7
411 }
412 /*
413 # uncoverable branch true
414 # uncoverable condition left count:1 class:ignore_covered_err */
415 7 ▶ if (err) {
Branch: 1 branch
BranchTrueFalseTotal
if (err) { 0 7 7
416 /* DBIx::Loop called this under its own eval: the die is the ROLLBACK */
417 0 (void)hv_stores(th, "active", newSViv(0)); /* # uncoverable statement */
418 0 sv_2mortal(err); /* # uncoverable statement */
419 0 croak_sv(err); /* # uncoverable statement */
420 }
421 7 ▶ if (res && pf_is_future(aTHX_ res)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (res && pf_is_future(aTHX_ res)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
if (res && pf_is_future(aTHX_ res)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 6 1 7
422 6 SV *dlf = A->future_new(aTHX);
423 6 AV *bcap = newAV();
424 SV *cb;
425 6 av_store(bcap, PTX_CAP_DLF, SvREFCNT_inc(dlf));
426 6 av_store(bcap, PTX_CAP_TX, newSVsv(tx));
427 6 cb = punk_closure(aTHX_ ptx_pf_ready_cb, bcap);
428 6 pf_react(aTHX_ pf_of(aTHX_ res), res, PFR_ON_READY, cb);
429 6 SvREFCNT_dec(cb); /* pf_react keeps its own copy */
430 6 SvREFCNT_dec(res);
431 /* `active` stays up until the future settles: ptx_pf_ready_cb */
432 6 ST(0) = sv_2mortal(dlf);
433 6 XSRETURN(1);
434 }
435 1 (void)hv_stores(th, "active", newSViv(0));
436 1 ▶ ST(0) = res ? sv_2mortal(res) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = res ? sv_2mortal(res) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 0 1
437 1 XSRETURN(1);
438 }
439  
440 /* Punk::Model::DBIx::Loop->_txn(\%opts, $code, $tx) -> Punk::Future */
441 7 static SV *ptx_loop_run(pTHX_ SV *opts, SV *code, SV *tx) {
442 7 ▶ HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 7 0 7
HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 7 0 7
443 14 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 7 0 7
444 7 HV *slot = pdl_handle_opts(aTHX_ o);
445 7 SV *db = pdbi_get(aTHX_ slot, "db");
446 7 const dbil_abi *A = punk_dbil(aTHX);
447 7 AV *cap = newAV();
448 SV *block, *f, *out, *argv[1];
449 pdl_pend *p;
450  
451 /*
452 # uncoverable branch true
453 # uncoverable condition left count:1 class:ignore_covered_err
454 # uncoverable condition left count:2 class:ignore_covered_err */
455 7 ▶ if (!(db && SvROK(db)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(db && SvROK(db))) 7 0 7
if (!(db && SvROK(db))) 0 7 7
456 0 croak(PDL_CLS ": no connection for the transaction"); /* # uncoverable statement */
457 7 av_store(cap, PTX_CAP_CODE, newSVsv(code));
458 7 av_store(cap, PTX_CAP_TX, newSVsv(tx));
459 7 block = punk_closure(aTHX_ ptx_loop_block_cb, cap); /* owns cap */
460 7 argv[0] = block;
461 7 f = pcx_call_meth(aTHX_ db, "txn", argv, 1, 1);
462 7 SvREFCNT_dec(block); /* txn holds its own reference */
463 /*
464 # uncoverable branch true
465 # uncoverable condition left count:1 class:ignore_covered_err */
466 7 ▶ if (!f) croak(PDL_CLS ": txn returned no future");
Branch: 1 branch
BranchTrueFalseTotal
if (!f) croak(PDL_CLS ": txn returned no future"); 0 7 7
467  
468 7 p = pdl_start(aTHX_ A, slot, &out);
469 7 p->op = PDL_OP_PASS;
470 7 pdl_attach(aTHX_ f, p);
471 7 return out;
472 }
473  
474 /* ---- $c->txn --------------------------------------------------------------- */
475  
476 /* $c->txn($code) / $c->txn($name => $code) */
477 27 static SV *ptx_context_txn(pTHX_ SV *c, SV *name, SV *code) {
478 27 AV *av = pcx_av(aTHX_ c);
479 27 SV *app = pcx_get(aTHX_ av, PCX_APP);
480 SV *class, *opts, *tx, *argv[3], *r;
481 /*
482 # uncoverable condition left count:1 class:ignore_covered_err
483 # uncoverable condition left count:3 class:ignore_covered_err */
484 27 ▶ if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) 27 0 27
if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) 25 2 27
if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) 0 25 25
485 2 croak(PTX_CLS ": txn takes a coderef - $c->txn(sub { my ($tx) = @_; "
486 "... }) or $c->txn(name => sub { ... })");
487 /*
488 # uncoverable branch true
489 # uncoverable condition left count:1 class:ignore_covered_err
490 # uncoverable condition left count:2 class:ignore_covered_err */
491 25 ▶ if (!(app && SvOK(app)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(app && SvOK(app))) 25 0 25
if (!(app && SvOK(app))) 0 25 25
492 0 croak(PTX_CLS ": this context has no application"); /* # uncoverable statement */
493 25 opts = ptx_db_opts(aTHX_ app, name, &class);
494 { /* the backend class, loaded if a model has not loaded it yet */
495 22 HV *stash = gv_stashsv(class, 0);
496 22 ▶ if (!(stash && gv_fetchmethod_autoload(stash, "_txn", 0))) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(stash && gv_fetchmethod_autoload(stash, "_txn", 0))) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 20 2 22
if (!(stash && gv_fetchmethod_autoload(stash, "_txn", 0))) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 20 20
497 4 ▶ if (!pk_require_once(aTHX_ SvPV_nolen(class), FALSE))
Branch: 1 branch
BranchTrueFalseTotal
if (!pk_require_once(aTHX_ SvPV_nolen(class), FALSE)) 1 1 2
498 3 ▶ croak(PTX_CLS ": backend '%s' failed to load: %s",
Branch: 2 branches
BranchTrueFalseTotal
croak(PTX_CLS ": backend '%s' failed to load: %s", 1 0 1
croak(PTX_CLS ": backend '%s' failed to load: %s", 1 0 1
499 SvPV_nolen(class), SvPV_nolen(ERRSV));
500 1 stash = gv_stashsv(class, 0);
501 /*
502 # uncoverable branch false
503 # uncoverable condition left count:1 class:ignore_covered_err
504 # uncoverable condition right count:1 class:ignore_covered_err
505 # uncoverable condition right count:2 class:ignore_covered_err */
506 1 ▶ if (!(stash && gv_fetchmethod_autoload(stash, "_txn", 0)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(stash && gv_fetchmethod_autoload(stash, "_txn", 0))) 1 0 1
if (!(stash && gv_fetchmethod_autoload(stash, "_txn", 0))) 1 0 1
507 2 croak(PTX_CLS ": backend '%s' does not implement _txn - it "
508 "has no transactions", SvPV_nolen(class));
509 }
510 }
511 20 tx = sv_2mortal(ptx_new(aTHX_ c, app, name, class, opts));
512 20 argv[0] = opts; argv[1] = code; argv[2] = tx;
513 20 r = pcx_call_meth(aTHX_ class, "_txn", argv, 3, 1);
514 14 ▶ return r ? r : newSV(0); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return r ? r : newSV(0); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 14 14
515 }
516  
517 #endif /* PUNK_TXN_H */
« include/punk/punk_csrf.h ↑ Summary xs/request.xs »