include/punk/punk_txn.h

filter: Statementstmt 91.8% 202 / 220 Branchbran 51.2% 122 / 238 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.7% 324 / 458 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_auth.h ↑ Summary xs/dbi.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 75 static HV *ptx_hv(pTHX_ SV *tx) {
49 75 ▶ if (!(tx && SvROK(tx) && SvTYPE(SvRV(tx)) == SVt_PVHV
Branch: 3 branches
BranchTrueFalseTotal
if (!(tx && SvROK(tx) && SvTYPE(SvRV(tx)) == SVt_PVHV 75 0 75
if (!(tx && SvROK(tx) && SvTYPE(SvRV(tx)) == SVt_PVHV 75 0 75
if (!(tx && SvROK(tx) && SvTYPE(SvRV(tx)) == SVt_PVHV 75 0 75
50 75 ▶ && sv_derived_from(tx, PTX_CLS)))
Branch: 1 branch
BranchTrueFalseTotal
&& sv_derived_from(tx, PTX_CLS))) 0 75 75
51 0 croak(PTX_CLS ": not a transaction object");
52 75 return (HV *)SvRV(tx);
53 }
54  
55 /* ---- the database behind a name -------------------------------------------- */
56  
57 /* The `database` keyword's options for a name (K_DATABASES), and the backend
58 * class they select. `name` NULL or undef is the default database. Returns
59 * the options as an RV (mortal); *class_out is a mortal class-name SV. */
60 19 static SV *ptx_db_opts(pTHX_ SV *app, SV *name, SV **class_out) {
61 19 HV *h = app_hv(aTHX_ app);
62 19 HV *dbs = app_hash(aTHX_ h, K_DATABASES);
63 19 const char *nm = K_DEFAULT; STRLEN nl = 7;
64 19 SV **opts, *ret, *b = NULL;
65 22 ▶ 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); 19 0 19
if (name && SvOK(name) && SvCUR(name)) nm = SvPV_const(name, nl); 3 16 19
if (name && SvOK(name) && SvCUR(name)) nm = SvPV_const(name, nl); 3 0 3
66 19 opts = hv_fetch(dbs, nm, (I32)nl, 0);
67 19 ▶ if (opts && *opts && SvROK(*opts) && SvTYPE(SvRV(*opts)) == SVt_PVHV) {
Branch: 4 branches
BranchTrueFalseTotal
if (opts && *opts && SvROK(*opts) && SvTYPE(SvRV(*opts)) == SVt_PVHV) { 18 1 19
if (opts && *opts && SvROK(*opts) && SvTYPE(SvRV(*opts)) == SVt_PVHV) { 18 0 18
if (opts && *opts && SvROK(*opts) && SvTYPE(SvRV(*opts)) == SVt_PVHV) { 18 0 18
if (opts && *opts && SvROK(*opts) && SvTYPE(SvRV(*opts)) == SVt_PVHV) { 18 0 18
68 18 ret = sv_2mortal(newSVsv(*opts));
69 18 b = pdbi_get(aTHX_ (HV *)SvRV(*opts), "backend");
70 }
71 1 ▶ else if (nl == 7 && memEQ(nm, K_DEFAULT, 7))
Branch: 2 branches
BranchTrueFalseTotal
else if (nl == 7 && memEQ(nm, K_DEFAULT, 7)) 0 1 1
else if (nl == 7 && memEQ(nm, K_DEFAULT, 7)) 0 0 0
72 0 croak(PTX_CLS ": no database configured - add a database keyword");
73 else
74 1 croak(PTX_CLS ": no database '%s' configured (the database keyword "
75 "names one as `database %s => { dsn => ... }`)", nm, nm);
76 7 ▶ *class_out = (b && SvOK(b)) ? sv_2mortal(newSVsv(b))
Branch: 1 branch
BranchTrueFalseTotal
*class_out = (b && SvOK(b)) ? sv_2mortal(newSVsv(b)) 7 0 7
77 18 ▶ : sv_2mortal(newSVpvs("Punk::Model::DBI"));
Branch: 1 branch
BranchTrueFalseTotal
: sv_2mortal(newSVpvs("Punk::Model::DBI")); 7 11 18
78 18 return ret;
79 }
80  
81 /* ---- Punk::Txn ------------------------------------------------------------ */
82  
83 18 static SV *ptx_new(pTHX_ SV *c, SV *app, SV *name, SV *class, SV *opts) {
84 18 HV *h = newHV();
85 18 (void)hv_stores(h, "c", newSVsv(c));
86 18 (void)hv_stores(h, "app", newSVsv(app));
87 18 ▶ (void)hv_stores(h, "name", (name && SvOK(name) && SvCUR(name))
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(h, "name", (name && SvOK(name) && SvCUR(name)) 18 0 18
(void)hv_stores(h, "name", (name && SvOK(name) && SvCUR(name)) 2 16 18
(void)hv_stores(h, "name", (name && SvOK(name) && SvCUR(name)) 2 0 2
88 ? newSVsv(name) : newSVpvs(K_DEFAULT));
89 18 (void)hv_stores(h, "class", newSVsv(class));
90 18 (void)hv_stores(h, "opts", newSVsv(opts));
91 18 (void)hv_stores(h, "handle", newSV(0));
92 18 (void)hv_stores(h, "active", newSViv(0));
93 18 return sv_bless(newRV_noinc((SV *)h), gv_stashpvs(PTX_CLS, GV_ADD));
94 }
95  
96 /* $tx->model($name): the registered model, bound to this transaction.
97 *
98 * A shallow copy of the per-worker instance with a copy of its backend
99 * carrying the transaction handle under `_tx`. The shared instance is never
100 * touched, so nothing leaks out of the block. A model on another database
101 * croaks: it cannot be in this transaction, and pretending would be the
102 * worst of the available bugs. */
103 27 static SV *ptx_model(pTHX_ SV *tx, SV *name) {
104 27 HV *th = ptx_hv(aTHX_ tx);
105 27 SV *app = pdbi_get(aTHX_ th, "app");
106 27 SV *txnm = pdbi_get(aTHX_ th, "name");
107 27 SV *hdl = pdbi_get(aTHX_ th, "handle");
108 27 SV *act = pdbi_get(aTHX_ th, "active");
109 SV *argv[1], *inst, *meta, *backend, *copy_rv, *bcopy_rv;
110 HV *ih, *bh, *copy, *bcopy;
111 27 const char *mdb = K_DEFAULT; STRLEN ml = 7;
112  
113 27 ▶ if (!(act && SvTRUE(act)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(act && SvTRUE(act))) 27 0 27
if (!(act && SvTRUE(act))) 2 25 27
114 2 croak(PTX_CLS ": the transaction is over - $tx->model is for inside "
115 "the block");
116 25 argv[0] = name;
117 25 inst = pcx_call_meth(aTHX_ app, "model_instance", argv, 1, 1);
118 25 ▶ if (!(inst && SvROK(inst) && SvTYPE(SvRV(inst)) == SVt_PVHV)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(inst && SvROK(inst) && SvTYPE(SvRV(inst)) == SVt_PVHV)) { 25 0 25
if (!(inst && SvROK(inst) && SvTYPE(SvRV(inst)) == SVt_PVHV)) { 25 0 25
if (!(inst && SvROK(inst) && SvTYPE(SvRV(inst)) == SVt_PVHV)) { 0 25 25
119 0 ▶ if (inst) SvREFCNT_dec(inst);
Branch: 1 branch
BranchTrueFalseTotal
if (inst) SvREFCNT_dec(inst); 0 0 0
120 0 croak(PTX_CLS ": no model '%s'", SvPV_nolen(name));
121 }
122 25 sv_2mortal(inst);
123 25 ih = (HV *)SvRV(inst);
124  
125 /* the model's database, from its metadata */
126 25 meta = pdbi_get(aTHX_ ih, "meta");
127 25 ▶ if (meta && SvROK(meta) && SvTYPE(SvRV(meta)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (meta && SvROK(meta) && SvTYPE(SvRV(meta)) == SVt_PVHV) { 25 0 25
if (meta && SvROK(meta) && SvTYPE(SvRV(meta)) == SVt_PVHV) { 25 0 25
if (meta && SvROK(meta) && SvTYPE(SvRV(meta)) == SVt_PVHV) { 25 0 25
128 25 SV *d = pdbi_get(aTHX_ (HV *)SvRV(meta), "database");
129 50 ▶ 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); 25 0 25
if (d && SvOK(d) && SvCUR(d)) mdb = SvPV_const(d, ml); 25 0 25
if (d && SvOK(d) && SvCUR(d)) mdb = SvPV_const(d, ml); 25 0 25
130 }
131 25 ▶ if (!(SvCUR(txnm) == ml && memEQ(SvPVX(txnm), mdb, ml)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvCUR(txnm) == ml && memEQ(SvPVX(txnm), mdb, ml))) 23 2 25
if (!(SvCUR(txnm) == ml && memEQ(SvPVX(txnm), mdb, ml))) 0 23 23
132 4 ▶ 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 " 2 0 2
133 "transaction is on '%s'", SvPV_nolen(name), (int)ml, mdb,
134 SvPV_nolen(txnm));
135  
136 23 backend = pdbi_get(aTHX_ ih, "backend");
137 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
138 0 croak(PTX_CLS ": model '%s' has no backend", SvPV_nolen(name));
139 23 bh = (HV *)SvRV(backend);
140  
141 23 bcopy = newHVhv(bh);
142 23 ▶ (void)hv_stores(bcopy, "_tx", newSVsv(hdl ? hdl : &PL_sv_undef));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(bcopy, "_tx", newSVsv(hdl ? hdl : &PL_sv_undef)); 23 0 23
143 23 bcopy_rv = sv_bless(newRV_noinc((SV *)bcopy), SvSTASH(SvRV(backend)));
144  
145 23 copy = newHVhv(ih);
146 23 (void)hv_stores(copy, "backend", bcopy_rv);
147 23 copy_rv = sv_bless(newRV_noinc((SV *)copy), SvSTASH(SvRV(inst)));
148 23 return copy_rv;
149 }
150  
151 /* ---- the DBI backend: one handle, begin_work / commit / rollback ---------- */
152  
153 20 static SV *ptx_dbi_call0(pTHX_ SV *dbh, const char *meth, SV **err) {
154 20 dSP;
155 int count;
156 20 SV *r = NULL;
157 20 ENTER; SAVETMPS;
158 20 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 20 20
159 20 ▶ XPUSHs(dbh);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dbh); 0 20 20
160 20 PUTBACK;
161 20 count = call_method(meth, G_SCALAR | G_EVAL);
162 20 SPAGAIN;
163 20 ▶ if (count > 0) r = SvREFCNT_inc(POPs);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) r = SvREFCNT_inc(POPs); 20 0 20
164 20 PUTBACK;
165 20 ▶ 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; } 20 0 20
if (SvTRUE(ERRSV)) { *err = newSVsv(ERRSV); if (r) SvREFCNT_dec(r); r = NULL; } 0 20 20
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
166 20 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 20 0 20
167 20 return r;
168 }
169  
170 /* Punk::Model::DBI->_txn(\%opts, $code, $tx) -> the block's value */
171 11 static SV *ptx_dbi_run(pTHX_ SV *opts, SV *code, SV *tx) {
172 11 ▶ HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 11 0 11
HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 11 0 11
173 22 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 11 0 11
174 11 HV *slot = pdbi_slot_for_opts(aTHX_ o);
175 11 SV *dbh = pdbi_get(aTHX_ slot, "dbh");
176 11 SV *open = pdbi_get(aTHX_ slot, "txn");
177 11 HV *th = ptx_hv(aTHX_ tx);
178 11 SV *err = NULL, *res = NULL, *r;
179  
180 11 ▶ if (!(dbh && SvROK(dbh)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(dbh && SvROK(dbh))) 11 0 11
if (!(dbh && SvROK(dbh))) 0 11 11
181 0 croak("Punk::Model::DBI: no connection for the transaction");
182 11 ▶ if (open && SvTRUE(open))
Branch: 2 branches
BranchTrueFalseTotal
if (open && SvTRUE(open)) 9 2 11
if (open && SvTRUE(open)) 1 8 9
183 1 croak("Punk::Model::DBI: a transaction is already open on this "
184 "connection - nested transactions are not supported, and a "
185 "silent join would stop a rollback covering what the outer "
186 "block thinks it covers");
187  
188 10 r = ptx_dbi_call0(aTHX_ dbh, "begin_work", &err);
189 10 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 10 0 10
190 10 ▶ if (err) { sv_2mortal(err); croak_sv(err); }
Branch: 1 branch
BranchTrueFalseTotal
if (err) { sv_2mortal(err); croak_sv(err); } 0 10 10
191  
192 10 (void)hv_stores(slot, "txn", newSViv(1));
193 10 (void)hv_stores(th, "handle", newSVsv(dbh));
194 10 (void)hv_stores(th, "active", newSViv(1));
195  
196 { /* the block, trapped: a die is a rollback and a rethrow */
197 10 dSP;
198 int count;
199 10 ENTER; SAVETMPS;
200 10 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 10 10
201 10 ▶ XPUSHs(tx);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(tx); 0 10 10
202 10 PUTBACK;
203 10 count = call_sv(code, G_SCALAR | G_EVAL);
204 10 SPAGAIN;
205 10 ▶ if (count > 0) res = SvREFCNT_inc(POPs);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) res = SvREFCNT_inc(POPs); 10 0 10
206 10 PUTBACK;
207 10 ▶ 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; } 10 0 10
if (SvTRUE(ERRSV)) { err = newSVsv(ERRSV); if (res) SvREFCNT_dec(res); res = NULL; } 4 6 10
if (SvTRUE(ERRSV)) { err = newSVsv(ERRSV); if (res) SvREFCNT_dec(res); res = NULL; } 4 0 4
if (SvTRUE(ERRSV)) { err = newSVsv(ERRSV); if (res) SvREFCNT_dec(res); res = NULL; } 4 0 4
208 10 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 10 0 10
209 }
210  
211 10 ▶ if (!err) {
Branch: 1 branch
BranchTrueFalseTotal
if (!err) { 6 4 10
212 6 r = ptx_dbi_call0(aTHX_ dbh, "commit", &err);
213 6 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 6 0 6
214 }
215 10 ▶ if (err) {
Branch: 1 branch
BranchTrueFalseTotal
if (err) { 4 6 10
216 4 SV *rerr = NULL;
217 4 r = ptx_dbi_call0(aTHX_ dbh, "rollback", &rerr);
218 4 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 4 0 4
219 4 ▶ 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 4 4
220 4 (void)hv_stores(slot, "txn", newSViv(0));
221 4 (void)hv_stores(th, "active", newSViv(0));
222 4 sv_2mortal(err);
223 4 croak_sv(err);
224 }
225 6 (void)hv_stores(slot, "txn", newSViv(0));
226 6 (void)hv_stores(th, "active", newSViv(0));
227 6 ▶ return res ? res : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
return res ? res : newSV(0); 0 6 6
228 }
229  
230 /* ---- the DBIx::Loop backend: $db->txn, with the bridge ---------------------- */
231  
232 /* capture layout for the two closures */
233 enum { PTX_CAP_CODE = 0, PTX_CAP_TX, PTX_CAP_DLF };
234  
235 /* A Punk::Future settled -> the DBIx::Loop::Future the txn is waiting on.
236 * Registered as an on_ready reaction, called with the Punk::Future. */
237 XS_INTERNAL(ptx_pf_ready_cb);
238 6 XS_INTERNAL(ptx_pf_ready_cb) {
239 6 dXSARGS;
240 6 AV *cap = punk_clos_cap(aTHX_ cv);
241 6 const dbil_abi *A = punk_dbil(aTHX);
242 6 ▶ SV **dlfp = cap ? av_fetch(cap, PTX_CAP_DLF, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **dlfp = cap ? av_fetch(cap, PTX_CAP_DLF, 0) : NULL; 6 0 6
243 6 ▶ SV **txp = cap ? av_fetch(cap, PTX_CAP_TX, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **txp = cap ? av_fetch(cap, PTX_CAP_TX, 0) : NULL; 6 0 6
244 punk_future *pf;
245 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
246 0 XSRETURN_EMPTY;
247 /* the block is over once its future settles, whichever way */
248 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
249 6 (void)hv_stores(ptx_hv(aTHX_ *txp), "active", newSViv(0));
250 6 pf = pf_of(aTHX_ ST(0));
251 6 ▶ if (pf->state == PF_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (pf->state == PF_DONE) { 3 3 6
252 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
253 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
254 }
255 else {
256 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
257 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
258 0 : sv_2mortal(newSVpvs("transaction block failed")));
259 }
260 6 XSRETURN_EMPTY;
261 }
262  
263 /* The block DBIx::Loop's txn calls with the pinned DBIx::Loop::Txn handle.
264 * Records the handle on the Punk::Txn, runs the application's block, and
265 * hands back its result - a Punk::Future bridged into a DBIx::Loop::Future,
266 * because txn commits as soon as the block returns anything that is not one
267 * of its own futures, and a commit before the model's writes have landed is
268 * exactly the bug a transaction exists to prevent. */
269 XS_INTERNAL(ptx_loop_block_cb);
270 7 XS_INTERNAL(ptx_loop_block_cb) {
271 7 dXSARGS;
272 7 AV *cap = punk_clos_cap(aTHX_ cv);
273 7 const dbil_abi *A = punk_dbil(aTHX);
274 7 SV *code, *tx, *txh, *res = NULL, *err = NULL;
275 HV *th;
276 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
277 7 code = *av_fetch(cap, PTX_CAP_CODE, 0);
278 7 tx = *av_fetch(cap, PTX_CAP_TX, 0);
279 7 txh = ST(0);
280 7 th = ptx_hv(aTHX_ tx);
281 7 (void)hv_stores(th, "handle", newSVsv(txh));
282 7 (void)hv_stores(th, "active", newSViv(1));
283 {
284 7 dSP;
285 int count;
286 7 ENTER; SAVETMPS;
287 7 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 7 7
288 7 ▶ XPUSHs(tx);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(tx); 0 7 7
289 7 PUTBACK;
290 7 count = call_sv(code, G_SCALAR | G_EVAL);
291 7 SPAGAIN;
292 7 ▶ if (count > 0) res = SvREFCNT_inc(POPs);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) res = SvREFCNT_inc(POPs); 7 0 7
293 7 PUTBACK;
294 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
295 7 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 7 0 7
296 }
297 7 ▶ if (err) {
Branch: 1 branch
BranchTrueFalseTotal
if (err) { 0 7 7
298 /* DBIx::Loop called this under its own eval: the die is the ROLLBACK */
299 0 (void)hv_stores(th, "active", newSViv(0));
300 0 sv_2mortal(err);
301 0 croak_sv(err);
302 }
303 7 ▶ if (res && pf_is_future(aTHX_ res)) {
Branch: 2 branches
BranchTrueFalseTotal
if (res && pf_is_future(aTHX_ res)) { 7 0 7
if (res && pf_is_future(aTHX_ res)) { 6 1 7
304 6 SV *dlf = A->future_new(aTHX);
305 6 AV *bcap = newAV();
306 SV *cb;
307 6 av_store(bcap, PTX_CAP_DLF, SvREFCNT_inc(dlf));
308 6 av_store(bcap, PTX_CAP_TX, newSVsv(tx));
309 6 cb = punk_closure(aTHX_ ptx_pf_ready_cb, bcap);
310 6 pf_react(aTHX_ pf_of(aTHX_ res), res, PFR_ON_READY, cb);
311 6 SvREFCNT_dec(cb); /* pf_react keeps its own copy */
312 6 SvREFCNT_dec(res);
313 /* `active` stays up until the future settles: ptx_pf_ready_cb */
314 6 ST(0) = sv_2mortal(dlf);
315 6 XSRETURN(1);
316 }
317 1 (void)hv_stores(th, "active", newSViv(0));
318 1 ▶ ST(0) = res ? sv_2mortal(res) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = res ? sv_2mortal(res) : &PL_sv_undef; 1 0 1
319 1 XSRETURN(1);
320 }
321  
322 /* Punk::Model::DBIx::Loop->_txn(\%opts, $code, $tx) -> Punk::Future */
323 7 static SV *ptx_loop_run(pTHX_ SV *opts, SV *code, SV *tx) {
324 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
325 14 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 7 0 7
326 7 HV *slot = pdl_handle_opts(aTHX_ o);
327 7 SV *db = pdbi_get(aTHX_ slot, "db");
328 7 const dbil_abi *A = punk_dbil(aTHX);
329 7 AV *cap = newAV();
330 SV *block, *f, *out, *argv[1];
331 pdl_pend *p;
332  
333 7 ▶ if (!(db && SvROK(db)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(db && SvROK(db))) 7 0 7
if (!(db && SvROK(db))) 0 7 7
334 0 croak(PDL_CLS ": no connection for the transaction");
335 7 av_store(cap, PTX_CAP_CODE, newSVsv(code));
336 7 av_store(cap, PTX_CAP_TX, newSVsv(tx));
337 7 block = punk_closure(aTHX_ ptx_loop_block_cb, cap); /* owns cap */
338 7 argv[0] = block;
339 7 f = pcx_call_meth(aTHX_ db, "txn", argv, 1, 1);
340 7 SvREFCNT_dec(block); /* txn holds its own reference */
341 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
342  
343 7 p = pdl_start(aTHX_ A, slot, &out);
344 7 p->op = PDL_OP_PASS;
345 7 pdl_attach(aTHX_ f, p);
346 7 return out;
347 }
348  
349 /* ---- $c->txn --------------------------------------------------------------- */
350  
351 /* $c->txn($code) / $c->txn($name => $code) */
352 20 static SV *ptx_context_txn(pTHX_ SV *c, SV *name, SV *code) {
353 20 AV *av = pcx_av(aTHX_ c);
354 20 SV *app = pcx_get(aTHX_ av, PCX_APP);
355 SV *class, *opts, *tx, *argv[3], *r;
356 20 ▶ if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) 20 0 20
if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) 19 1 20
if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) 0 19 19
357 1 croak(PTX_CLS ": txn takes a coderef - $c->txn(sub { my ($tx) = @_; "
358 "... }) or $c->txn(name => sub { ... })");
359 19 ▶ if (!(app && SvOK(app)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(app && SvOK(app))) 19 0 19
if (!(app && SvOK(app))) 0 19 19
360 0 croak(PTX_CLS ": this context has no application");
361 19 opts = ptx_db_opts(aTHX_ app, name, &class);
362 { /* the backend class, loaded if a model has not loaded it yet */
363 18 HV *stash = gv_stashsv(class, 0);
364 18 ▶ if (!(stash && gv_fetchmethod_autoload(stash, "_txn", 0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(stash && gv_fetchmethod_autoload(stash, "_txn", 0))) { 18 0 18
if (!(stash && gv_fetchmethod_autoload(stash, "_txn", 0))) { 0 18 18
365 0 ▶ if (!pk_require_once(aTHX_ SvPV_nolen(class), FALSE))
Branch: 1 branch
BranchTrueFalseTotal
if (!pk_require_once(aTHX_ SvPV_nolen(class), FALSE)) 0 0 0
366 0 ▶ croak(PTX_CLS ": backend '%s' failed to load: %s",
Branch: 2 branches
BranchTrueFalseTotal
croak(PTX_CLS ": backend '%s' failed to load: %s", 0 0 0
croak(PTX_CLS ": backend '%s' failed to load: %s", 0 0 0
367 SvPV_nolen(class), SvPV_nolen(ERRSV));
368 0 stash = gv_stashsv(class, 0);
369 0 ▶ if (!(stash && gv_fetchmethod_autoload(stash, "_txn", 0)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(stash && gv_fetchmethod_autoload(stash, "_txn", 0))) 0 0 0
if (!(stash && gv_fetchmethod_autoload(stash, "_txn", 0))) 0 0 0
370 0 croak(PTX_CLS ": backend '%s' does not implement _txn - it "
371 "has no transactions", SvPV_nolen(class));
372 }
373 }
374 18 tx = sv_2mortal(ptx_new(aTHX_ c, app, name, class, opts));
375 18 argv[0] = opts; argv[1] = code; argv[2] = tx;
376 18 r = pcx_call_meth(aTHX_ class, "_txn", argv, 3, 1);
377 13 ▶ return r ? r : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
return r ? r : newSV(0); 0 13 13
378 }
379  
380 #endif /* PUNK_TXN_H */
« include/punk/punk_auth.h ↑ Summary xs/dbi.xs »