include/punk/punk_dbi.h

filter: Statementstmt 96.7% 294 / 307 (3 uncoverable) Branchbran 66.6% 215 / 342 (29 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.8% 509 / 649 (32 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_computed.h ↑ Summary include/punk/punk_obs_selftest.h »
1 /* punk_dbi.h - the shipped Punk::Model backend, in C.
2 *
3 * What this port does and does not buy is worth stating, because the shape of
4 * the win is unusual here. Every database operation is still a DBI method
5 * call - connect, prepare_cached, execute, fetchrow_hashref, quote_identifier,
6 * last_insert_id - and those are Perl-level calls into DBI's own XS that no
7 * amount of C on this side removes. The round trip to the database dominates
8 * either way.
9 *
10 * What moves into C is the glue around them: assembling the SQL, sorting the
11 * column lists, the connection pool bookkeeping, and the keyset token codec
12 * (base64url over the File::Raw::JSON ABI, so MIME::Base64 and a Perl eval
13 * leave the pagination path entirely). Punk::Model's contract methods are
14 * already XSUBs that call_method into the backend, so with this the whole
15 * model tier is one C frame calling DBI rather than two Perl frames.
16 *
17 * The object stays a blessed hash with the same slots the Perl used
18 * (opts/table/primary/columns/col/returning): t/0510-model-dbi.t reaches
19 * through ->backend->dbh, and keeping the shape means a hand-written backend
20 * can still subclass this one.
21 *
22 * Must be included after punk_context.h (pcx_call_meth) and the punk_frj
23 * resolver.
24 */
25  
26 #ifndef PUNK_DBI_H
27 #define PUNK_DBI_H
28  
29 1417 static HV *pdbi_hv(pTHX_ SV *self) {
30 1417 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1417 0 1417
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 1416 1417
31 1 croak("Punk::Model::DBI: not a backend object");
32 1416 return (HV *)SvRV(self);
33 }
34  
35 13134 static SV *pdbi_get(pTHX_ HV *h, const char *k) {
36 13134 SV **e = hv_fetch(h, k, (I32)strlen(k), 0);
37 13134 ▶ return (e && *e) ? *e : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? *e : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 10822 2312 13134
return (e && *e) ? *e : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 10822 0 10822
38 }
39  
40 /* ---- the connection pool ---------------------------------------------------
41 *
42 * One handle per distinct connection (dsn + credentials), shared by every
43 * model that uses it, so a hundred models on one database open one connection
44 * per worker rather than a hundred. The pid lives in the reconnect check, so a
45 * fork gets a fresh handle instead of a corrupted shared one. */
46  
47 static HV *PDBI_POOL = NULL;
48  
49 413 static HV *pdbi_pool(pTHX) {
50 413 ▶ if (!PDBI_POOL) PDBI_POOL = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PDBI_POOL) PDBI_POOL = newHV(); 10 403 413
51 413 return PDBI_POOL;
52 }
53  
54 /* The pool slot for a connection key, created empty on first use. */
55 645 static SV *pdbi_slot(pTHX_ HV *pool, SV *key) {
56 645 HE *he = hv_fetch_ent(pool, key, 0, 0);
57 645 ▶ if (!he) {
Branch: 1 branch
BranchTrueFalseTotal
if (!he) { 23 622 645
58 23 HV *slot = newHV();
59 23 (void)hv_stores(slot, "pid", newSViv(-1));
60 23 he = hv_store_ent(pool, key, newRV_noinc((SV *)slot), 0);
61 }
62 645 return HeVAL(he);
63 }
64  
65 /* An attribute off a DBI handle's hash ($dbh->{Driver}), or NULL. DBI handles
66 * are magical hashes, so this goes through hv_fetch, which runs the magic. */
67 85 static SV *pdbi_attr(pTHX_ SV *h, const char *name) {
68 SV **e;
69 /*
70 # uncoverable branch true
71 # uncoverable condition left count:1 class:ignore_covered_err
72 # uncoverable condition left count:2 class:ignore_covered_err
73 # uncoverable condition left count:3 class:ignore_covered_err */
74 85 ▶ if (!(h && SvROK(h) && SvTYPE(SvRV(h)) == SVt_PVHV)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(h && SvROK(h) && SvTYPE(SvRV(h)) == SVt_PVHV)) return NULL; 85 0 85
if (!(h && SvROK(h) && SvTYPE(SvRV(h)) == SVt_PVHV)) return NULL; 85 0 85
if (!(h && SvROK(h) && SvTYPE(SvRV(h)) == SVt_PVHV)) return NULL; 0 85 85
75 85 e = hv_fetch((HV *)SvRV(h), name, (I32)strlen(name), 0);
76 /*
77 # uncoverable branch true
78 # uncoverable condition left count:1 class:ignore_covered_err
79 # uncoverable condition left count:2 class:ignore_covered_err */
80 85 ▶ if (!(e && *e)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && *e)) return NULL; 85 0 85
if (!(e && *e)) return NULL; 0 85 85
81 85 SvGETMAGIC(*e);
82 85 return *e;
83 }
84  
85 /* RETURNING support, feature-detected once per connection: PostgreSQL always,
86 * SQLite from 3.35. Anything else re-fetches the row after a write. Detection
87 * failing is not fatal - a 0 here only costs an extra SELECT. */
88 27 static int pdbi_detect_returning(pTHX_ SV *dbh) {
89 27 SV *drv = pdbi_attr(aTHX_ dbh, "Driver");
90 27 ▶ SV *name = drv ? pdbi_attr(aTHX_ drv, "Name") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *name = drv ? pdbi_attr(aTHX_ drv, "Name") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 27 0 27
91 const char *n;
92 STRLEN nl;
93 /*
94 # uncoverable branch true
95 # uncoverable condition left count:1 class:ignore_covered_err
96 # uncoverable condition left count:2 class:ignore_covered_err */
97 27 ▶ if (!(name && SvOK(name))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(name && SvOK(name))) return 0; 27 0 27
if (!(name && SvOK(name))) return 0; 0 27 27
98 27 n = SvPV_const(name, nl);
99 /*
100 # uncoverable branch true
101 # uncoverable condition left count:1 class:ignore_covered_err
102 # uncoverable condition left count:2 class:ignore_covered_err
103 # uncoverable condition right count:2 class:ignore_covered_err */
104 27 ▶ if (nl == 2 && memEQ(n, "Pg", 2)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (nl == 2 && memEQ(n, "Pg", 2)) return 1; 0 27 27
if (nl == 2 && memEQ(n, "Pg", 2)) return 1; 0 0 0
105 /*
106 # uncoverable branch false
107 # uncoverable condition right count:1 class:ignore_covered_err
108 # uncoverable condition right count:2 class:ignore_covered_err */
109 27 ▶ if (nl == 6 && memEQ(n, "SQLite", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
if (nl == 6 && memEQ(n, "SQLite", 6)) { 27 0 27
if (nl == 6 && memEQ(n, "SQLite", 6)) { 27 0 27
110 27 SV *v = pdbi_attr(aTHX_ dbh, "sqlite_version");
111 /*
112 # uncoverable branch false
113 # uncoverable condition right count:1 class:ignore_covered_err
114 # uncoverable condition right count:2 class:ignore_covered_err */
115 27 ▶ if (v && SvOK(v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (v && SvOK(v)) { 27 0 27
if (v && SvOK(v)) { 27 0 27
116 STRLEN vl;
117 27 const char *s = SvPV_const(v, vl);
118 27 int maj = 0, min = 0;
119 27 STRLEN i = 0;
120 54 ▶ while (i < vl && isDIGIT((U8)s[i])) maj = maj * 10 + (s[i++] - '0'); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (i < vl && isDIGIT((U8)s[i])) maj = maj * 10 + (s[i++] - '0'); /* # uncoverable condition right count:1 class:ignore_covered_err */ 54 0 54
while (i < vl && isDIGIT((U8)s[i])) maj = maj * 10 + (s[i++] - '0'); /* # uncoverable condition right count:1 class:ignore_covered_err */ 27 27 54
121 /*
122 # uncoverable branch false
123 # uncoverable condition right count:1 class:ignore_covered_err
124 # uncoverable condition right count:2 class:ignore_covered_err */
125 27 ▶ if (i < vl && s[i] == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (i < vl && s[i] == '.') { 27 0 27
if (i < vl && s[i] == '.') { 27 0 27
126 27 i++;
127 81 ▶ while (i < vl && isDIGIT((U8)s[i])) min = min * 10 + (s[i++] - '0'); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (i < vl && isDIGIT((U8)s[i])) min = min * 10 + (s[i++] - '0'); /* # uncoverable condition right count:1 class:ignore_covered_err */ 81 0 81
while (i < vl && isDIGIT((U8)s[i])) min = min * 10 + (s[i++] - '0'); /* # uncoverable condition right count:1 class:ignore_covered_err */ 54 27 81
128 }
129 /*
130 # uncoverable branch false
131 # uncoverable condition left count:1 class:ignore_covered_err
132 # uncoverable condition right count:1 class:ignore_covered_err
133 # uncoverable condition right count:2 class:ignore_covered_err
134 # uncoverable condition right count:3 class:ignore_covered_err */
135 27 ▶ if (maj > 3 || (maj == 3 && min >= 35)) return 1;
Branch: 3 branches
BranchTrueFalseTotal
if (maj > 3 || (maj == 3 && min >= 35)) return 1; 27 0 27
if (maj > 3 || (maj == 3 && min >= 35)) return 1; 27 0 27
if (maj > 3 || (maj == 3 && min >= 35)) return 1; 27 0 27
136 }
137 }
138 0 return 0; /* # uncoverable statement */
139 }
140  
141 /* The pool slot for a set of connection options, connected on first use in
142 * this process and shared with every backend on the same ones. Borrowed.
143 * Split from pdbi_slot_for so a transaction, which belongs to a connection
144 * and not to any one model, can reach the slot from the database options
145 * the `database` keyword recorded. */
146 413 static HV *pdbi_slot_for_opts(pTHX_ HV *o) {
147 413 ▶ SV *dsn = o ? pdbi_get(aTHX_ o, "dsn") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *dsn = o ? pdbi_get(aTHX_ o, "dsn") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 413 0 413
148 413 ▶ SV *user = o ? pdbi_get(aTHX_ o, "user") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *user = o ? pdbi_get(aTHX_ o, "user") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 413 0 413
149 413 ▶ SV *pass = o ? pdbi_get(aTHX_ o, "password") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *pass = o ? pdbi_get(aTHX_ o, "password") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 413 0 413
150 SV *key, *slot_sv, *dbh;
151 413 HV *pool = pdbi_pool(aTHX), *slot;
152 SV *pid;
153  
154 /*
155 # uncoverable branch true
156 # uncoverable condition left count:1 class:ignore_covered_err
157 # uncoverable condition left count:2 class:ignore_covered_err
158 # uncoverable condition left count:3 class:ignore_covered_err */
159 413 ▶ if (!(dsn && SvOK(dsn) && SvTRUE(dsn)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(dsn && SvOK(dsn) && SvTRUE(dsn))) 413 0 413
if (!(dsn && SvOK(dsn) && SvTRUE(dsn))) 413 0 413
if (!(dsn && SvOK(dsn) && SvTRUE(dsn))) 0 413 413
160 0 croak("Punk::Model::DBI: no dsn - add a database keyword"); /* # uncoverable statement */
161  
162 /* NUL-joined so a dsn containing the separator cannot collide */
163 413 key = sv_2mortal(newSVsv(dsn));
164 413 sv_catpvn(key, "\0", 1);
165 /*
166 # uncoverable branch true
167 # uncoverable condition left count:1 class:ignore_covered_err
168 # uncoverable condition left count:2 class:ignore_covered_err
169 # uncoverable condition right count:2 class:ignore_covered_err */
170 413 ▶ if (user && SvOK(user)) sv_catsv(key, user);
Branch: 2 branches
BranchTrueFalseTotal
if (user && SvOK(user)) sv_catsv(key, user); 0 413 413
if (user && SvOK(user)) sv_catsv(key, user); 0 0 0
171 413 sv_catpvn(key, "\0", 1);
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 right count:2 class:ignore_covered_err */
177 413 ▶ if (pass && SvOK(pass)) sv_catsv(key, pass);
Branch: 2 branches
BranchTrueFalseTotal
if (pass && SvOK(pass)) sv_catsv(key, pass); 0 413 413
if (pass && SvOK(pass)) sv_catsv(key, pass); 0 0 0
178  
179 413 slot_sv = pdbi_slot(aTHX_ pool, key);
180 413 slot = (HV *)SvRV(slot_sv);
181 413 pid = pdbi_get(aTHX_ slot, "pid");
182 413 dbh = pdbi_get(aTHX_ slot, "dbh");
183  
184 /*
185 # uncoverable condition left count:1 class:ignore_covered_err
186 # uncoverable condition left count:2 class:ignore_covered_err
187 # uncoverable condition left count:4 class:ignore_covered_err
188 # uncoverable condition left count:5 class:ignore_covered_err */
189 413 ▶ if (!(pid && SvOK(pid) && SvIV(pid) == (IV)PerlProc_getpid())
Branch: 3 branches
BranchTrueFalseTotal
if (!(pid && SvOK(pid) && SvIV(pid) == (IV)PerlProc_getpid()) 413 0 413
if (!(pid && SvOK(pid) && SvIV(pid) == (IV)PerlProc_getpid()) 413 0 413
if (!(pid && SvOK(pid) && SvIV(pid) == (IV)PerlProc_getpid()) 399 14 413
190 399 ▶ || !(dbh && SvROK(dbh))) {
Branch: 2 branches
BranchTrueFalseTotal
|| !(dbh && SvROK(dbh))) { 399 0 399
|| !(dbh && SvROK(dbh))) { 0 399 399
191 SV *argv[4], *attr_rv, *conn;
192 14 HV *attr = newHV();
193 14 ▶ SV *user_attr = o ? pdbi_get(aTHX_ o, "attr") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *user_attr = o ? pdbi_get(aTHX_ o, "attr") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 14 0 14
194 14 (void)hv_stores(attr, "RaiseError", newSViv(1));
195 14 (void)hv_stores(attr, "AutoCommit", newSViv(1));
196 14 (void)hv_stores(attr, "PrintError", newSViv(0));
197 /* A handle inherited across a fork is freed when the child connects
198 * for itself, and freeing it must not say goodbye on a socket the
199 * parent is still using. */
200 14 (void)hv_stores(attr, "AutoInactiveDestroy", newSViv(1));
201 /*
202 # uncoverable condition right count:2 class:ignore_covered_err
203 # uncoverable condition right count:3 class:ignore_covered_err */
204 14 ▶ if (user_attr && SvROK(user_attr)
Branch: 2 branches
BranchTrueFalseTotal
if (user_attr && SvROK(user_attr) 2 12 14
if (user_attr && SvROK(user_attr) 2 0 2
205 2 ▶ && SvTYPE(SvRV(user_attr)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(user_attr)) == SVt_PVHV) { 2 0 2
206 2 HV *ua = (HV *)SvRV(user_attr);
207 HE *he;
208 2 hv_iterinit(ua);
209 4 ▶ while ((he = hv_iternext(ua)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(ua))) 2 2 4
210 2 ▶ (void)hv_store_ent(attr, HeSVKEY_force(he),
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_store_ent(attr, HeSVKEY_force(he), 2 0 2
(void)hv_store_ent(attr, HeSVKEY_force(he), 0 2 2
211 newSVsv(HeVAL(he)), 0);
212 }
213 /* Observed statements. The six methods below are not the only way an
214 * application runs one - it reaches through to this handle for an OR,
215 * a UNION, a FOR UPDATE, an upsert - and those never touch this file.
216 * Punk::DBI wraps the handle instead, so they are seen too.
217 *
218 * Only when somebody is listening, so an uninstrumented application
219 * pays no Perl frame per statement; and never over an application's
220 * own RootClass, which it would be losing to get this. */
221 14 ▶ if (PK_OBS_WANT_QUERY && !hv_exists(attr, "RootClass", 9)) {
Branch: 2 branches
BranchTrueFalseTotal
if (PK_OBS_WANT_QUERY && !hv_exists(attr, "RootClass", 9)) { 3 11 14
if (PK_OBS_WANT_QUERY && !hv_exists(attr, "RootClass", 9)) { 2 1 3
222 2 (void)pk_require_once(aTHX_ "Punk::DBI", TRUE);
223 2 (void)hv_stores(attr, "RootClass", newSVpvs("Punk::DBI"));
224 }
225 14 attr_rv = sv_2mortal(newRV_noinc((SV *)attr));
226  
227 14 (void)pk_require_once(aTHX_ "DBI", TRUE);
228 14 argv[0] = dsn;
229 /*
230 # uncoverable condition right count:1 class:ignore_covered_err
231 # uncoverable condition left count:2 class:ignore_covered_err
232 # uncoverable condition right count:2 class:ignore_covered_err */
233 14 ▶ argv[1] = (user && SvOK(user)) ? user : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
argv[1] = (user && SvOK(user)) ? user : &PL_sv_undef; 0 14 14
argv[1] = (user && SvOK(user)) ? user : &PL_sv_undef; 0 0 0
234 /*
235 # uncoverable condition right count:1 class:ignore_covered_err
236 # uncoverable condition left count:2 class:ignore_covered_err
237 # uncoverable condition right count:2 class:ignore_covered_err */
238 14 ▶ argv[2] = (pass && SvOK(pass)) ? pass : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
argv[2] = (pass && SvOK(pass)) ? pass : &PL_sv_undef; 0 14 14
argv[2] = (pass && SvOK(pass)) ? pass : &PL_sv_undef; 0 0 0
239 14 argv[3] = attr_rv;
240 14 conn = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("DBI")), "connect",
241 argv, 4, 1);
242 /*
243 # uncoverable branch true
244 # uncoverable condition left count:1 class:ignore_covered_err */
245 14 ▶ if (!conn) conn = newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!conn) conn = newSV(0); 0 14 14
246 14 (void)hv_stores(slot, "dbh", conn);
247 14 (void)hv_stores(slot, "pid", newSViv((IV)PerlProc_getpid()));
248 14 (void)hv_stores(slot, "returning",
249 newSViv(pdbi_detect_returning(aTHX_ conn)));
250 /* Both caches belong to the connection: the quoted identifiers
251 * because quoting is the driver's, and the assembled statements
252 * because they have those identifiers baked into them. A reconnect
253 * (or a fork) starts fresh ones. */
254 14 (void)hv_delete(slot, "qi", 2, G_DISCARD);
255 14 (void)hv_delete(slot, "sqlc", 4, G_DISCARD);
256 /* a transaction open on the old handle died with it */
257 14 (void)hv_delete(slot, "txn", 3, G_DISCARD);
258 }
259 413 return slot;
260 }
261  
262 /* The pool slot for this backend's dsn. Borrowed. */
263 401 static HV *pdbi_slot_for(pTHX_ SV *self) {
264 401 HV *h = pdbi_hv(aTHX_ self);
265 400 SV *opts = pdbi_get(aTHX_ h, "opts");
266 400 ▶ HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 400 0 400
HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 400 0 400
267 800 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 400 0 400
268 400 HV *slot = pdbi_slot_for_opts(aTHX_ o);
269 /* create/update read this off the instance; keep it in step with the
270 * pooled slot, which is where the detection actually lives */
271 {
272 400 SV *r = pdbi_get(aTHX_ slot, "returning");
273 400 ▶ (void)hv_stores(h, "returning", newSViv(r ? SvIV(r) : 0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, "returning", newSViv(r ? SvIV(r) : 0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 400 0 400
274 }
275 400 return slot;
276 }
277  
278 /* The live handle for this backend's dsn. Borrowed. */
279 65 static SV *pdbi_dbh(pTHX_ SV *self) {
280 65 SV *dbh = pdbi_get(aTHX_ pdbi_slot_for(aTHX_ self), "dbh");
281 65 ▶ return dbh ? dbh : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return dbh ? dbh : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */ 65 0 65
282 }
283  
284 /* $dbh->quote_identifier($name), on a handle the caller already holds - the
285 * DBIx::Loop backend quotes against its parent handle, which is not in this
286 * pool. Mortal. */
287 75 static SV *pdbi_qi_dbh(pTHX_ SV *dbh, SV *name) {
288 SV *argv[1], *q;
289 75 argv[0] = name;
290 75 q = pcx_call_meth(aTHX_ dbh, "quote_identifier", argv, 1, 1);
291 75 ▶ return q ? sv_2mortal(q) : sv_2mortal(newSVsv(name)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return q ? sv_2mortal(q) : sv_2mortal(newSVsv(name)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 75 0 75
292 }
293  
294 /* quote_identifier, memoised on the pool slot.
295 *
296 * It is a Perl method call into DBI, and on a small statement it dominates:
297 * a get quotes the table and one key column, and those two calls were 1.5us
298 * of a 3.6us round trip - more than the query itself. The answer depends only
299 * on the driver and the name, and a model's identifiers are a fixed, tiny set
300 * (its table and its columns), so the first call per name pays for the rest.
301 *
302 * The cache lives on the SLOT rather than the instance because quoting is a
303 * property of the connection: every model on one dsn shares it, and a
304 * reconnect drops it (see pdbi_slot_for). The returned SV is borrowed from
305 * the cache - callers concatenate it into the SQL immediately. */
306 1746 static SV *pdbi_qi_slot(pTHX_ HV *slot, SV *dbh, SV *name) {
307 1746 SV **e = hv_fetchs(slot, "qi", 0);
308 HV *cache;
309 HE *he;
310 /*
311 # uncoverable condition right count:2 class:ignore_covered_err
312 # uncoverable condition right count:3 class:ignore_covered_err
313 # uncoverable condition right count:4 class:ignore_covered_err */
314 1746 ▶ if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 1728 18 1746
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 1728 0 1728
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 1728 0 1728
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 1728 0 1728
315 1728 cache = (HV *)SvRV(*e);
316 else {
317 18 cache = newHV();
318 18 (void)hv_stores(slot, "qi", newRV_noinc((SV *)cache));
319 }
320 1746 he = hv_fetch_ent(cache, name, 0, 0);
321 /*
322 # uncoverable condition right count:2 class:ignore_covered_err
323 # uncoverable condition right count:3 class:ignore_covered_err */
324 1746 ▶ if (he && HeVAL(he) && SvOK(HeVAL(he))) return HeVAL(he);
Branch: 3 branches
BranchTrueFalseTotal
if (he && HeVAL(he) && SvOK(HeVAL(he))) return HeVAL(he); 1671 75 1746
if (he && HeVAL(he) && SvOK(HeVAL(he))) return HeVAL(he); 1671 0 1671
if (he && HeVAL(he) && SvOK(HeVAL(he))) return HeVAL(he); 1671 0 1671
325 75 he = hv_store_ent(cache, name,
326 newSVsv(pdbi_qi_dbh(aTHX_ dbh, name)), 0);
327 /*
328 # uncoverable condition right count:1 class:ignore_covered_err
329 # uncoverable condition right count:2 class:ignore_covered_err */
330 75 ▶ return (he && HeVAL(he)) ? HeVAL(he) : name;
Branch: 2 branches
BranchTrueFalseTotal
return (he && HeVAL(he)) ? HeVAL(he) : name; 75 0 75
return (he && HeVAL(he)) ? HeVAL(he) : name; 75 0 75
331 }
332  
333 /* $self->dbh->quote_identifier($name), memoised. Borrowed. */
334 70 static SV *pdbi_qi(pTHX_ SV *self, SV *name) {
335 70 HV *slot = pdbi_slot_for(aTHX_ self);
336 69 SV *dbh = pdbi_get(aTHX_ slot, "dbh");
337 69 ▶ return pdbi_qi_slot(aTHX_ slot, dbh ? dbh : &PL_sv_undef, name); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return pdbi_qi_slot(aTHX_ slot, dbh ? dbh : &PL_sv_undef, name); /* # uncoverable condition left count:1 class:ignore_covered_err */ 69 0 69
338 }
339  
340 /*
341 # uncoverable statement
342 # uncoverable subroutine */
343 0 static SV *pdbi_qi_pv(pTHX_ SV *self, const char *name) {
344 0 return pdbi_qi(aTHX_ self, sv_2mortal(newSVpv(name, 0))); /* # uncoverable statement */
345 }
346  
347 /* prepare_cached: one statement handle per distinct SQL string on the
348 * connection, which is what keeps repeated queries off the parser (and what
349 * t/0510 checks through $dbh->{CachedKids}). Mortal. */
350 255 static SV *pdbi_sth_dbh(pTHX_ SV *dbh, SV *sql) {
351 SV *argv[3], *sth;
352 255 argv[0] = sql;
353 255 argv[1] = &PL_sv_undef;
354 255 argv[2] = sv_2mortal(newSViv(3));
355 255 sth = pcx_call_meth(aTHX_ dbh, "prepare_cached", argv, 3, 1);
356 /*
357 # uncoverable branch true
358 # uncoverable condition left count:1 class:ignore_covered_err */
359 255 ▶ if (!sth) croak("Punk::Model::DBI: prepare_cached returned nothing");
Branch: 1 branch
BranchTrueFalseTotal
if (!sth) croak("Punk::Model::DBI: prepare_cached returned nothing"); 0 255 255
360 255 return sv_2mortal(sth);
361 }
362  
363 26 static SV *pdbi_sth(pTHX_ SV *self, SV *sql) {
364 26 return pdbi_sth_dbh(aTHX_ pdbi_dbh(aTHX_ self), sql);
365 }
366  
367 /* $sth->execute(@bind)
368 *
369 * The query observers used to fire HERE, which was the wrong altitude: these
370 * six methods are a layer above the handle, and an application reaching
371 * through to $model->backend->dbh for an OR, a UNION, a FOR UPDATE or an
372 * upsert ran statements no observer could see. Punk::DBI wraps the handle
373 * instead, so this path is covered by Punk::DBI::st::execute along with every
374 * other, and firing again here would report each of these twice.
375 *
376 * `sql` stays in the signature: the callers have it, a future observer at
377 * this altitude would want it, and threading it back through eight call sites
378 * later is worse than an unused argument now. */
379 253 static void pdbi_execute_sql(pTHX_ SV *sth, AV *bind, SV *sql) {
380 253 ▶ SSize_t n = bind ? av_len(bind) + 1 : 0, i; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SSize_t n = bind ? av_len(bind) + 1 : 0, i; /* # uncoverable condition right count:1 class:ignore_covered_err */ 253 0 253
381 253 ▶ SV **argv = n ? (SV **)safemalloc(sizeof(SV *) * (size_t)n) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **argv = n ? (SV **)safemalloc(sizeof(SV *) * (size_t)n) : NULL; 250 3 253
382 SV *r;
383 3350 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3097 253 3350
384 3097 SV **e = av_fetch(bind, i, 0);
385 /*
386 # uncoverable condition right count:1 class:ignore_covered_err
387 # uncoverable condition right count:2 class:ignore_covered_err */
388 3097 ▶ argv[i] = (e && *e) ? *e : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
argv[i] = (e && *e) ? *e : &PL_sv_undef; 3097 0 3097
argv[i] = (e && *e) ? *e : &PL_sv_undef; 3097 0 3097
389 }
390 PERL_UNUSED_ARG(sql);
391 253 r = pcx_call_meth(aTHX_ sth, "execute", argv, (int)n, 1);
392 253 ▶ if (argv) safefree(argv);
Branch: 1 branch
BranchTrueFalseTotal
if (argv) safefree(argv); 250 3 253
393 /*
394 # uncoverable branch false
395 # uncoverable condition right count:1 class:ignore_covered_err */
396 253 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 253 0 253
397 253 }
398  
399 310 static SV *pdbi_meth0(pTHX_ SV *obj, const char *meth) {
400 310 return pcx_call_meth(aTHX_ obj, meth, NULL, 0, 1);
401 }
402  
403 /* The hash keys of an unblessed hashref, sorted the way `sort keys %$h` would
404 * sort them - the generated SQL has to be stable, or prepare_cached sees a
405 * different statement for every permutation of the same filter. Mortal AV. */
406 1169 static AV *pdbi_sorted_keys(pTHX_ HV *h) {
407 1169 AV *out = (AV *)sv_2mortal((SV *)newAV());
408 HE *he;
409 SSize_t n;
410 1169 ▶ if (!h) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return out; 4 1165 1169
411 1165 hv_iterinit(h);
412 2159 ▶ while ((he = hv_iternext(h)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(h))) 994 1165 2159
413 994 ▶ av_push(out, newSVsv(HeSVKEY_force(he)));
Branch: 2 branches
BranchTrueFalseTotal
av_push(out, newSVsv(HeSVKEY_force(he))); 994 0 994
av_push(out, newSVsv(HeSVKEY_force(he))); 0 994 994
414 1165 n = av_len(out) + 1;
415 1165 ▶ if (n > 1) sortsv(AvARRAY(out), (size_t)n, Perl_sv_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (n > 1) sortsv(AvARRAY(out), (size_t)n, Perl_sv_cmp); 106 1059 1165
416 1165 return out;
417 }
418  
419 /* ---- the opaque keyset token ----------------------------------------------
420 *
421 * base64url of {"k":<primary key>}: url-safe, unpadded, and opaque enough
422 * that nobody builds one by hand and depends on the shape. */
423  
424 static const char PDBI_B64U[] =
425 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
426  
427 147 static SV *pdbi_b64u_encode(pTHX_ const unsigned char *s, STRLEN len) {
428 147 SV *out = newSV(((len + 2) / 3) * 4 + 1);
429 char *d;
430 147 STRLEN i = 0;
431 147 SvPOK_on(out);
432 147 d = SvPVX(out);
433 1971 ▶ while (i + 2 < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i + 2 < len) { 1824 147 1971
434 1824 unsigned v = ((unsigned)s[i] << 16) | ((unsigned)s[i+1] << 8) | s[i+2];
435 1824 *d++ = PDBI_B64U[(v >> 18) & 0x3F];
436 1824 *d++ = PDBI_B64U[(v >> 12) & 0x3F];
437 1824 *d++ = PDBI_B64U[(v >> 6) & 0x3F];
438 1824 *d++ = PDBI_B64U[ v & 0x3F];
439 1824 i += 3;
440 }
441 147 ▶ if (i < len) { /* 1 or 2 bytes left, no padding */
Branch: 1 branch
BranchTrueFalseTotal
if (i < len) { /* 1 or 2 bytes left, no padding */ 129 18 147
442 129 unsigned v = (unsigned)s[i] << 16;
443 129 int two = (i + 1 < len);
444 129 ▶ if (two) v |= (unsigned)s[i+1] << 8;
Branch: 1 branch
BranchTrueFalseTotal
if (two) v |= (unsigned)s[i+1] << 8; 51 78 129
445 129 *d++ = PDBI_B64U[(v >> 18) & 0x3F];
446 129 *d++ = PDBI_B64U[(v >> 12) & 0x3F];
447 129 ▶ if (two) *d++ = PDBI_B64U[(v >> 6) & 0x3F];
Branch: 1 branch
BranchTrueFalseTotal
if (two) *d++ = PDBI_B64U[(v >> 6) & 0x3F]; 51 78 129
448 }
449 147 *d = '\0';
450 147 SvCUR_set(out, (STRLEN)(d - SvPVX(out)));
451 147 return out;
452 }
453  
454 /* Decode, accepting the padded and url-unsafe spellings too. NULL when the
455 * input is not base64 at all. */
456 152 static SV *pdbi_b64u_decode(pTHX_ const char *s, STRLEN len) {
457 152 SV *out = newSV(len + 1);
458 char *d;
459 152 unsigned v = 0;
460 152 int bits = 0;
461 STRLEN i;
462 152 SvPOK_on(out);
463 152 d = SvPVX(out);
464 7845 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 7702 143 7845
465 7702 char c = s[i];
466 int x;
467 /*
468 # uncoverable branch true
469 # uncoverable condition left count:1 class:ignore_covered_err */
470 7702 ▶ if (c == '=') break;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '=') break; 0 7702 7702
471 7702 ▶ if (c >= 'A' && c <= 'Z') x = c - 'A';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'Z') x = c - 'A'; 6944 758 7702
if (c >= 'A' && c <= 'Z') x = c - 'A'; 3102 3842 6944
472 4600 ▶ else if (c >= 'a' && c <= 'z') x = c - 'a' + 26; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 'a' && c <= 'z') x = c - 'a' + 26; /* # uncoverable condition right count:2 class:ignore_covered_err */ 3842 758 4600
else if (c >= 'a' && c <= 'z') x = c - 'a' + 26; /* # uncoverable condition right count:2 class:ignore_covered_err */ 3842 0 3842
473 758 ▶ else if (c >= '0' && c <= '9') x = c - '0' + 52; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= '0' && c <= '9') x = c - '0' + 52; /* # uncoverable condition right count:2 class:ignore_covered_err */ 739 19 758
else if (c >= '0' && c <= '9') x = c - '0' + 52; /* # uncoverable condition right count:2 class:ignore_covered_err */ 739 0 739
474 19 ▶ else if (c == '-' || c == '+') x = 62; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (c == '-' || c == '+') x = 62; /* # uncoverable condition left count:2 class:ignore_covered_err */ 9 10 19
else if (c == '-' || c == '+') x = 62; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 9 9
475 /*
476 # uncoverable branch true
477 # uncoverable condition left count:1 class:ignore_covered_err
478 # uncoverable condition left count:2 class:ignore_covered_err */
479 9 ▶ else if (c == '_' || c == '/') x = 63;
Branch: 2 branches
BranchTrueFalseTotal
else if (c == '_' || c == '/') x = 63; 9 0 9
else if (c == '_' || c == '/') x = 63; 0 9 9
480 9 else { SvREFCNT_dec(out); return NULL; }
481 7693 v = (v << 6) | (unsigned)x;
482 7693 bits += 6;
483 7693 ▶ if (bits >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (bits >= 8) { 5718 1975 7693
484 5718 bits -= 8;
485 5718 *d++ = (char)((v >> bits) & 0xFF);
486 }
487 }
488 143 *d = '\0';
489 143 SvCUR_set(out, (STRLEN)(d - SvPVX(out)));
490 143 return out;
491 }
492  
493 25 static SV *pdbi_encode_token(pTHX_ SV *val) {
494 25 HV *w = newHV();
495 SV *json, *tok;
496 STRLEN jl;
497 const char *jp;
498 25 (void)hv_stores(w, "k", newSVsv(val));
499 25 json = punk_frj(aTHX)->encode(aTHX_ sv_2mortal(newRV_noinc((SV *)w)), NULL);
500 25 jp = SvPV_const(json, jl);
501 25 tok = pdbi_b64u_encode(aTHX_ (const unsigned char *)jp, jl);
502 25 SvREFCNT_dec(json);
503 25 return tok;
504 }
505  
506 /* `cls` names the backend in the croak, so a bad token on the async backend
507 * does not blame the DBI one. A shared codec is a correctness requirement,
508 * not DRY: a `next` token minted by one backend must decode in the other, or
509 * switching backends breaks every paginated URL in flight. */
510 2 static SV *pdbi_decode_token(pTHX_ SV *tok, const char *cls) {
511 STRLEN tl;
512 4 ▶ const char *tp = SvOK(tok) ? SvPV_const(tok, tl) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
const char *tp = SvOK(tok) ? SvPV_const(tok, tl) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
513 2 SV *json, *doc, *out = NULL;
514 /*
515 # uncoverable branch true
516 # uncoverable condition left count:1 class:ignore_covered_err */
517 2 ▶ if (!SvOK(tok)) tl = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(tok)) tl = 0; 0 2 2
518 2 json = pdbi_b64u_decode(aTHX_ tp, tl);
519 /*
520 # uncoverable branch true
521 # uncoverable condition left count:1 class:ignore_covered_err */
522 2 ▶ if (!json) croak("%s: invalid pagination token", cls);
Branch: 1 branch
BranchTrueFalseTotal
if (!json) croak("%s: invalid pagination token", cls); 0 2 2
523 2 sv_2mortal(json);
524  
525 /* The decode dies on malformed JSON, and a bad token is a client error
526 * rather than a server one - so it has to be trapped and turned into the
527 * croak below. Trapping it goes through Perl's eval (call_pv + G_EVAL)
528 * rather than JMPENV directly: a longjmp past the C would leave the save
529 * stack unwound only as far as the jump, and this is an error path where
530 * correctness is worth more than the call. */
531 {
532 SV *argv[1];
533 2 dSP;
534 int count;
535 2 argv[0] = json;
536 2 ENTER; SAVETMPS;
537 2 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(json); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(json); PUTBACK; 0 2 2
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(json); PUTBACK; 0 2 2
538 2 count = call_pv("File::Raw::JSON::file_json_decode",
539 G_SCALAR | G_EVAL);
540 2 SPAGAIN;
541 2 ▶ doc = (count > 0) ? SvREFCNT_inc(POPs) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
doc = (count > 0) ? SvREFCNT_inc(POPs) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
542 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
543 /*
544 # uncoverable branch true
545 # uncoverable condition right count:1 class:ignore_covered_err
546 # uncoverable condition left count:2 class:ignore_covered_err */
547 2 ▶ if (SvTRUE(ERRSV)) { SvREFCNT_dec(doc); doc = NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { SvREFCNT_dec(doc); doc = NULL; } 2 0 2
if (SvTRUE(ERRSV)) { SvREFCNT_dec(doc); doc = NULL; } 0 2 2
548 PERL_UNUSED_VAR(argv);
549 }
550 /*
551 # uncoverable branch true
552 # uncoverable condition left count:1 class:ignore_covered_err */
553 2 ▶ if (!doc) croak("%s: invalid pagination token", cls);
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) croak("%s: invalid pagination token", cls); 0 2 2
554  
555 /*
556 # uncoverable branch false
557 # uncoverable condition right count:1 class:ignore_covered_err
558 # uncoverable condition right count:2 class:ignore_covered_err */
559 2 ▶ if (SvROK(doc) && SvTYPE(SvRV(doc)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(doc) && SvTYPE(SvRV(doc)) == SVt_PVHV) { 2 0 2
if (SvROK(doc) && SvTYPE(SvRV(doc)) == SVt_PVHV) { 2 0 2
560 2 SV *k = pdbi_get(aTHX_ (HV *)SvRV(doc), "k");
561 /*
562 # uncoverable branch false
563 # uncoverable condition right count:1 class:ignore_covered_err */
564 2 ▶ if (k) out = newSVsv(k);
Branch: 1 branch
BranchTrueFalseTotal
if (k) out = newSVsv(k); 2 0 2
565 }
566 2 SvREFCNT_dec(doc);
567 /*
568 # uncoverable branch true
569 # uncoverable condition left count:1 class:ignore_covered_err */
570 2 ▶ if (!out) croak("%s: invalid pagination token", cls);
Branch: 1 branch
BranchTrueFalseTotal
if (!out) croak("%s: invalid pagination token", cls); 0 2 2
571 2 return out;
572 }
573  
574 /* ---- shared SQL shapes ----------------------------------------------------- */
575  
576 /* '<qi(a)> = ? AND <qi(b)> = ?' for the sorted keys, pushing each key's value
577 * onto @bind in the same order, quoting through the slot's cache. Mortal. */
578 27 static SV *pdbi_where_eq_slot(pTHX_ HV *slot, SV *dbh, HV *src,
579 AV *keys, AV *bind) {
580 27 SV *where = sv_2mortal(newSVpvs(""));
581 27 SSize_t i, n = av_len(keys) + 1;
582 56 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 29 27 56
583 29 SV *k = *av_fetch(keys, i, 0);
584 HE *he;
585 29 ▶ if (i) sv_catpvs(where, " AND ");
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpvs(where, " AND "); 2 27 29
586 29 sv_catsv(where, pdbi_qi_slot(aTHX_ slot, dbh, k));
587 29 sv_catpvs(where, " = ?");
588 29 he = hv_fetch_ent(src, k, 0, 0);
589 29 ▶ av_push(bind, newSVsv(he ? HeVAL(he) : &PL_sv_undef)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
av_push(bind, newSVsv(he ? HeVAL(he) : &PL_sv_undef)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 29 0 29
590 }
591 27 return where;
592 }
593  
594 /* The bind half of pdbi_where_eq_slot on its own, in the same (sorted) order -
595 * what the cached-SQL path still has to do per call. */
596 15 static void pdbi_bind_keys(pTHX_ HV *src, AV *keys, AV *bind) {
597 15 SSize_t i, n = av_len(keys) + 1;
598 30 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 15 15 30
599 15 SV *k = *av_fetch(keys, i, 0);
600 15 HE *he = hv_fetch_ent(src, k, 0, 0);
601 15 ▶ av_push(bind, newSVsv(he ? HeVAL(he) : &PL_sv_undef)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
av_push(bind, newSVsv(he ? HeVAL(he) : &PL_sv_undef)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 15 0 15
602 }
603 15 }
604  
605 /*
606 # uncoverable statement
607 # uncoverable subroutine */
608 0 static SV *pdbi_where_eq(pTHX_ SV *self, HV *src, AV *keys, AV *bind) {
609 0 HV *slot = pdbi_slot_for(aTHX_ self); /* # uncoverable statement */
610 0 SV *dbh = pdbi_get(aTHX_ slot, "dbh"); /* # uncoverable statement */
611 /*
612 # uncoverable statement
613 # uncoverable condition left count:1
614 # uncoverable condition right count:1 */
615 0 ▶ return pdbi_where_eq_slot(aTHX_ slot, dbh ? dbh : &PL_sv_undef,
Branch: 1 branch
BranchTrueFalseTotal
return pdbi_where_eq_slot(aTHX_ slot, dbh ? dbh : &PL_sv_undef, 0 0 0
616 src, keys, bind);
617 }
618  
619 /* A memoised statement, for the fixed-shape operations.
620 *
621 * get and delete generate one SQL string per set of key columns, and a model
622 * is asked for the same set over and over - almost always the primary key. So
623 * the string is built once and looked up thereafter, which also skips the
624 * identifier quoting and the concatenation behind it. `sig` identifies the
625 * shape (the operation plus its sorted key names); the cache lives on the
626 * slot beside the quoting cache, and dies with the connection for the same
627 * reason. Returns the SQL, borrowed. */
628 35 static SV *pdbi_sql_cached(pTHX_ HV *slot, SV *sig, SV *sql) {
629 35 SV **e = hv_fetchs(slot, "sqlc", 0);
630 HV *cache;
631 HE *he;
632 /*
633 # uncoverable condition right count:2 class:ignore_covered_err
634 # uncoverable condition right count:3 class:ignore_covered_err
635 # uncoverable condition right count:4 class:ignore_covered_err */
636 35 ▶ if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 30 5 35
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 30 0 30
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 30 0 30
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 30 0 30
637 30 cache = (HV *)SvRV(*e);
638 else {
639 5 cache = newHV();
640 5 (void)hv_stores(slot, "sqlc", newRV_noinc((SV *)cache));
641 }
642 35 ▶ if (!sql) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sql) { 27 8 35
643 27 he = hv_fetch_ent(cache, sig, 0, 0);
644 /*
645 # uncoverable condition right count:2 class:ignore_covered_err
646 # uncoverable condition right count:3 class:ignore_covered_err */
647 27 ▶ return (he && HeVAL(he) && SvOK(HeVAL(he))) ? HeVAL(he) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (he && HeVAL(he) && SvOK(HeVAL(he))) ? HeVAL(he) : NULL; 15 12 27
return (he && HeVAL(he) && SvOK(HeVAL(he))) ? HeVAL(he) : NULL; 15 0 15
return (he && HeVAL(he) && SvOK(HeVAL(he))) ? HeVAL(he) : NULL; 15 0 15
648 }
649 8 he = hv_store_ent(cache, sig, newSVsv(sql), 0);
650 /*
651 # uncoverable condition right count:1 class:ignore_covered_err
652 # uncoverable condition right count:2 class:ignore_covered_err */
653 8 ▶ return (he && HeVAL(he)) ? HeVAL(he) : sql;
Branch: 2 branches
BranchTrueFalseTotal
return (he && HeVAL(he)) ? HeVAL(he) : sql; 8 0 8
return (he && HeVAL(he)) ? HeVAL(he) : sql; 8 0 8
654 }
655  
656 /* 'op\0table\0k1\0k2' - the shape of a fixed-form statement. The TABLE is in
657 * the signature because the cache hangs off the connection, which every model
658 * on that dsn shares: without it, two models with the same key columns would
659 * read each other's statement. Mortal. */
660 27 static SV *pdbi_sig(pTHX_ const char *op, SV *table, AV *keys) {
661 27 SV *sig = sv_2mortal(newSVpv(op, 0));
662 27 ▶ SSize_t i, n = keys ? av_len(keys) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = keys ? av_len(keys) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 27 0 27
663 27 sv_catpvn(sig, "\0", 1);
664 /*
665 # uncoverable branch false
666 # uncoverable condition right count:1 class:ignore_covered_err
667 # uncoverable condition right count:2 class:ignore_covered_err */
668 27 ▶ if (table && SvOK(table)) sv_catsv(sig, table);
Branch: 2 branches
BranchTrueFalseTotal
if (table && SvOK(table)) sv_catsv(sig, table); 27 0 27
if (table && SvOK(table)) sv_catsv(sig, table); 27 0 27
669 56 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 29 27 56
670 29 SV **k = av_fetch(keys, i, 0);
671 29 sv_catpvn(sig, "\0", 1);
672 /*
673 # uncoverable branch false
674 # uncoverable condition right count:1 class:ignore_covered_err
675 # uncoverable condition right count:2 class:ignore_covered_err */
676 29 ▶ if (k && *k) sv_catsv(sig, *k);
Branch: 2 branches
BranchTrueFalseTotal
if (k && *k) sv_catsv(sig, *k); 29 0 29
if (k && *k) sv_catsv(sig, *k); 29 0 29
677 }
678 27 return sig;
679 }
680  
681 /* The %key of get/delete, as sorted keys; croaks (naming the calling backend)
682 * when empty. */
683 102 static AV *pdbi_key_args(pTHX_ SV **st, I32 items, HV **out,
684 const char *what, const char *cls) {
685 102 HV *key = newHV();
686 I32 i;
687 /*
688 # uncoverable branch true
689 # uncoverable condition left count:1 class:ignore_covered_err
690 # uncoverable condition left count:2 class:ignore_covered_err */
691 102 ▶ if (items < 3 || !((items - 1) % 2 == 0))
Branch: 2 branches
BranchTrueFalseTotal
if (items < 3 || !((items - 1) % 2 == 0)) 102 0 102
if (items < 3 || !((items - 1) % 2 == 0)) 0 102 102
692 0 croak("%s: %s needs a key", cls, what); /* # uncoverable statement */
693 208 ▶ for (i = 1; i + 1 < items; i += 2)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) 106 102 208
694 106 (void)hv_store_ent(key, st[i], newSVsv(st[i + 1]), 0);
695 /*
696 # uncoverable branch true
697 # uncoverable condition left count:1 class:ignore_covered_err
698 # uncoverable condition left count:2 class:ignore_covered_err */
699 102 ▶ if (!HvUSEDKEYS(key)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!HvUSEDKEYS(key)) { 0 102 102
if (!HvUSEDKEYS(key)) { 0 102 102
700 0 SvREFCNT_dec((SV *)key); /* # uncoverable statement */
701 0 croak("%s: %s needs a key", cls, what); /* # uncoverable statement */
702 }
703 102 *out = (HV *)sv_2mortal((SV *)key);
704 102 return pdbi_sorted_keys(aTHX_ key);
705 }
706  
707 /* The backend object both shipped backends bless: the same slots the Perl
708 * used (opts/table/primary/columns/col/returning), parsed from the
709 * database/table/primary/columns pairs _instantiate passes. Shared so a
710 * second backend cannot drift from the shape t/0510 reaches through. */
711 75 static SV *pdbi_build_self(pTHX_ SV *class, SV **st, I32 items,
712 const char *cls) {
713 75 HV *h = newHV();
714 75 HV *col = newHV();
715 75 SV *table = NULL, *primary = NULL, *columns = NULL, *database = NULL;
716 I32 i;
717 394 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 319 75 394
718 319 STRLEN kl; const char *k = SvPV_const(st[i], kl);
719 319 ▶ if (kl == 5 && memEQ(k, "table", 5)) table = st[i + 1]; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 5 && memEQ(k, "table", 5)) table = st[i + 1]; /* # uncoverable condition right count:2 class:ignore_covered_err */ 75 244 319
if (kl == 5 && memEQ(k, "table", 5)) table = st[i + 1]; /* # uncoverable condition right count:2 class:ignore_covered_err */ 75 0 75
720 244 ▶ else if (kl == 7 && memEQ(k, "primary", 7)) primary = st[i + 1];
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 7 && memEQ(k, "primary", 7)) primary = st[i + 1]; 141 103 244
else if (kl == 7 && memEQ(k, "primary", 7)) primary = st[i + 1]; 71 70 141
721 173 ▶ else if (kl == 7 && memEQ(k, "columns", 7)) columns = st[i + 1]; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 7 && memEQ(k, "columns", 7)) columns = st[i + 1]; /* # uncoverable condition right count:2 class:ignore_covered_err */ 70 103 173
else if (kl == 7 && memEQ(k, "columns", 7)) columns = st[i + 1]; /* # uncoverable condition right count:2 class:ignore_covered_err */ 70 0 70
722 103 ▶ else if (kl == 8 && memEQ(k, "database", 8)) database = st[i + 1]; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 8 && memEQ(k, "database", 8)) database = st[i + 1]; /* # uncoverable condition right count:2 class:ignore_covered_err */ 73 30 103
else if (kl == 8 && memEQ(k, "database", 8)) database = st[i + 1]; /* # uncoverable condition right count:2 class:ignore_covered_err */ 73 0 73
723 }
724 /*
725 # uncoverable branch true
726 # uncoverable condition left count:1 class:ignore_covered_err
727 # uncoverable condition left count:2 class:ignore_covered_err
728 # uncoverable condition left count:3 class:ignore_covered_err */
729 75 ▶ if (!(table && SvOK(table) && SvTRUE(table))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(table && SvOK(table) && SvTRUE(table))) { 75 0 75
if (!(table && SvOK(table) && SvTRUE(table))) { 75 0 75
if (!(table && SvOK(table) && SvTRUE(table))) { 0 75 75
730 0 SvREFCNT_dec((SV *)h); SvREFCNT_dec((SV *)col); /* # uncoverable statement */
731 0 croak("%s: a model with no table", cls); /* # uncoverable statement */
732 }
733 /* the column set, for the create/update filters */
734 /*
735 # uncoverable condition right count:2 class:ignore_covered_err
736 # uncoverable condition right count:3 class:ignore_covered_err */
737 75 ▶ if (columns && SvROK(columns) && SvTYPE(SvRV(columns)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (columns && SvROK(columns) && SvTYPE(SvRV(columns)) == SVt_PVAV) { 70 5 75
if (columns && SvROK(columns) && SvTYPE(SvRV(columns)) == SVt_PVAV) { 70 0 70
if (columns && SvROK(columns) && SvTYPE(SvRV(columns)) == SVt_PVAV) { 70 0 70
738 70 AV *c = (AV *)SvRV(columns);
739 70 SSize_t n = av_len(c) + 1, j;
740 260 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 190 70 260
741 190 SV **e = av_fetch(c, j, 0);
742 /*
743 # uncoverable branch false
744 # uncoverable condition right count:1 class:ignore_covered_err
745 # uncoverable condition right count:2 class:ignore_covered_err */
746 190 ▶ if (e && *e) (void)hv_store_ent(col, *e, newSViv(1), 0);
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) (void)hv_store_ent(col, *e, newSViv(1), 0); 190 0 190
if (e && *e) (void)hv_store_ent(col, *e, newSViv(1), 0); 190 0 190
747 }
748 }
749 75 ▶ (void)hv_stores(h, "opts",
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(h, "opts", 73 2 75
(void)hv_stores(h, "opts", 73 0 73
(void)hv_stores(h, "opts", 73 0 73
750 (database && SvROK(database) && SvTYPE(SvRV(database)) == SVt_PVHV)
751 ? newSVsv(database) : newRV_noinc((SV *)newHV()));
752 75 (void)hv_stores(h, "table", newSVsv(table));
753 75 ▶ (void)hv_stores(h, "primary",
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(h, "primary", 71 4 75
(void)hv_stores(h, "primary", 69 2 71
754 (primary && SvOK(primary)) ? newSVsv(primary) : newSV(0));
755 75 ▶ (void)hv_stores(h, "columns",
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(h, "columns", 70 5 75
(void)hv_stores(h, "columns", 70 0 70
(void)hv_stores(h, "columns", 70 0 70
756 (columns && SvROK(columns) && SvTYPE(SvRV(columns)) == SVt_PVAV)
757 ? newSVsv(columns) : newRV_noinc((SV *)newAV()));
758 75 (void)hv_stores(h, "col", newRV_noinc((SV *)col));
759 75 (void)hv_stores(h, "returning", newSViv(0));
760 75 return sv_bless(newRV_noinc((SV *)h), gv_stashsv(class, GV_ADD));
761 }
762  
763 #endif /* PUNK_DBI_H */
« include/punk/punk_computed.h ↑ Summary include/punk/punk_obs_selftest.h »