include/punk/punk_dbi.h

filter: Statementstmt 95.4% 293 / 307 Branchbran 62.2% 213 / 342 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.9% 506 / 649 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/auth.xs ↑ Summary include/punk/punk_sitemap.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 1302 static HV *pdbi_hv(pTHX_ SV *self) {
30 1302 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 1302 0 1302
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 0 1302 1302
31 0 croak("Punk::Model::DBI: not a backend object");
32 1302 return (HV *)SvRV(self);
33 }
34  
35 9870 static SV *pdbi_get(pTHX_ HV *h, const char *k) {
36 9870 SV **e = hv_fetch(h, k, (I32)strlen(k), 0);
37 9870 ▶ return (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? *e : NULL; 7813 2057 9870
return (e && *e) ? *e : NULL; 7813 0 7813
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 379 static HV *pdbi_pool(pTHX) {
50 379 ▶ if (!PDBI_POOL) PDBI_POOL = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PDBI_POOL) PDBI_POOL = newHV(); 7 372 379
51 379 return PDBI_POOL;
52 }
53  
54 /* The pool slot for a connection key, created empty on first use. */
55 601 static SV *pdbi_slot(pTHX_ HV *pool, SV *key) {
56 601 HE *he = hv_fetch_ent(pool, key, 0, 0);
57 601 ▶ if (!he) {
Branch: 1 branch
BranchTrueFalseTotal
if (!he) { 19 582 601
58 19 HV *slot = newHV();
59 19 (void)hv_stores(slot, "pid", newSViv(-1));
60 19 he = hv_store_ent(pool, key, newRV_noinc((SV *)slot), 0);
61 }
62 601 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 70 static SV *pdbi_attr(pTHX_ SV *h, const char *name) {
68 SV **e;
69 70 ▶ 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; 70 0 70
if (!(h && SvROK(h) && SvTYPE(SvRV(h)) == SVt_PVHV)) return NULL; 70 0 70
if (!(h && SvROK(h) && SvTYPE(SvRV(h)) == SVt_PVHV)) return NULL; 0 70 70
70 70 e = hv_fetch((HV *)SvRV(h), name, (I32)strlen(name), 0);
71 70 ▶ if (!(e && *e)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && *e)) return NULL; 70 0 70
if (!(e && *e)) return NULL; 0 70 70
72 70 SvGETMAGIC(*e);
73 70 return *e;
74 }
75  
76 /* RETURNING support, feature-detected once per connection: PostgreSQL always,
77 * SQLite from 3.35. Anything else re-fetches the row after a write. Detection
78 * failing is not fatal - a 0 here only costs an extra SELECT. */
79 22 static int pdbi_detect_returning(pTHX_ SV *dbh) {
80 22 SV *drv = pdbi_attr(aTHX_ dbh, "Driver");
81 22 ▶ SV *name = drv ? pdbi_attr(aTHX_ drv, "Name") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *name = drv ? pdbi_attr(aTHX_ drv, "Name") : NULL; 22 0 22
82 const char *n;
83 STRLEN nl;
84 22 ▶ if (!(name && SvOK(name))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(name && SvOK(name))) return 0; 22 0 22
if (!(name && SvOK(name))) return 0; 0 22 22
85 22 n = SvPV_const(name, nl);
86 22 ▶ if (nl == 2 && memEQ(n, "Pg", 2)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (nl == 2 && memEQ(n, "Pg", 2)) return 1; 0 22 22
if (nl == 2 && memEQ(n, "Pg", 2)) return 1; 0 0 0
87 22 ▶ if (nl == 6 && memEQ(n, "SQLite", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
if (nl == 6 && memEQ(n, "SQLite", 6)) { 22 0 22
if (nl == 6 && memEQ(n, "SQLite", 6)) { 22 0 22
88 22 SV *v = pdbi_attr(aTHX_ dbh, "sqlite_version");
89 22 ▶ if (v && SvOK(v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (v && SvOK(v)) { 22 0 22
if (v && SvOK(v)) { 22 0 22
90 STRLEN vl;
91 22 const char *s = SvPV_const(v, vl);
92 22 int maj = 0, min = 0;
93 22 STRLEN i = 0;
94 44 ▶ while (i < vl && isDIGIT((U8)s[i])) maj = maj * 10 + (s[i++] - '0');
Branch: 2 branches
BranchTrueFalseTotal
while (i < vl && isDIGIT((U8)s[i])) maj = maj * 10 + (s[i++] - '0'); 44 0 44
while (i < vl && isDIGIT((U8)s[i])) maj = maj * 10 + (s[i++] - '0'); 22 22 44
95 22 ▶ if (i < vl && s[i] == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (i < vl && s[i] == '.') { 22 0 22
if (i < vl && s[i] == '.') { 22 0 22
96 22 i++;
97 66 ▶ while (i < vl && isDIGIT((U8)s[i])) min = min * 10 + (s[i++] - '0');
Branch: 2 branches
BranchTrueFalseTotal
while (i < vl && isDIGIT((U8)s[i])) min = min * 10 + (s[i++] - '0'); 66 0 66
while (i < vl && isDIGIT((U8)s[i])) min = min * 10 + (s[i++] - '0'); 44 22 66
98 }
99 22 ▶ if (maj > 3 || (maj == 3 && min >= 35)) return 1;
Branch: 3 branches
BranchTrueFalseTotal
if (maj > 3 || (maj == 3 && min >= 35)) return 1; 22 0 22
if (maj > 3 || (maj == 3 && min >= 35)) return 1; 22 0 22
if (maj > 3 || (maj == 3 && min >= 35)) return 1; 22 0 22
100 }
101 }
102 0 return 0;
103 }
104  
105 /* The pool slot for a set of connection options, connected on first use in
106 * this process and shared with every backend on the same ones. Borrowed.
107 * Split from pdbi_slot_for so a transaction, which belongs to a connection
108 * and not to any one model, can reach the slot from the database options
109 * the `database` keyword recorded. */
110 379 static HV *pdbi_slot_for_opts(pTHX_ HV *o) {
111 379 ▶ SV *dsn = o ? pdbi_get(aTHX_ o, "dsn") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *dsn = o ? pdbi_get(aTHX_ o, "dsn") : NULL; 379 0 379
112 379 ▶ SV *user = o ? pdbi_get(aTHX_ o, "user") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *user = o ? pdbi_get(aTHX_ o, "user") : NULL; 379 0 379
113 379 ▶ SV *pass = o ? pdbi_get(aTHX_ o, "password") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *pass = o ? pdbi_get(aTHX_ o, "password") : NULL; 379 0 379
114 SV *key, *slot_sv, *dbh;
115 379 HV *pool = pdbi_pool(aTHX), *slot;
116 SV *pid;
117  
118 379 ▶ if (!(dsn && SvOK(dsn) && SvTRUE(dsn)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(dsn && SvOK(dsn) && SvTRUE(dsn))) 379 0 379
if (!(dsn && SvOK(dsn) && SvTRUE(dsn))) 379 0 379
if (!(dsn && SvOK(dsn) && SvTRUE(dsn))) 0 379 379
119 0 croak("Punk::Model::DBI: no dsn - add a database keyword");
120  
121 /* NUL-joined so a dsn containing the separator cannot collide */
122 379 key = sv_2mortal(newSVsv(dsn));
123 379 sv_catpvn(key, "\0", 1);
124 379 ▶ if (user && SvOK(user)) sv_catsv(key, user);
Branch: 2 branches
BranchTrueFalseTotal
if (user && SvOK(user)) sv_catsv(key, user); 0 379 379
if (user && SvOK(user)) sv_catsv(key, user); 0 0 0
125 379 sv_catpvn(key, "\0", 1);
126 379 ▶ if (pass && SvOK(pass)) sv_catsv(key, pass);
Branch: 2 branches
BranchTrueFalseTotal
if (pass && SvOK(pass)) sv_catsv(key, pass); 0 379 379
if (pass && SvOK(pass)) sv_catsv(key, pass); 0 0 0
127  
128 379 slot_sv = pdbi_slot(aTHX_ pool, key);
129 379 slot = (HV *)SvRV(slot_sv);
130 379 pid = pdbi_get(aTHX_ slot, "pid");
131 379 dbh = pdbi_get(aTHX_ slot, "dbh");
132  
133 379 ▶ if (!(pid && SvOK(pid) && SvIV(pid) == (IV)PerlProc_getpid())
Branch: 3 branches
BranchTrueFalseTotal
if (!(pid && SvOK(pid) && SvIV(pid) == (IV)PerlProc_getpid()) 379 0 379
if (!(pid && SvOK(pid) && SvIV(pid) == (IV)PerlProc_getpid()) 379 0 379
if (!(pid && SvOK(pid) && SvIV(pid) == (IV)PerlProc_getpid()) 368 11 379
134 368 ▶ || !(dbh && SvROK(dbh))) {
Branch: 2 branches
BranchTrueFalseTotal
|| !(dbh && SvROK(dbh))) { 368 0 368
|| !(dbh && SvROK(dbh))) { 0 368 368
135 SV *argv[4], *attr_rv, *conn;
136 11 HV *attr = newHV();
137 11 ▶ SV *user_attr = o ? pdbi_get(aTHX_ o, "attr") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *user_attr = o ? pdbi_get(aTHX_ o, "attr") : NULL; 11 0 11
138 11 (void)hv_stores(attr, "RaiseError", newSViv(1));
139 11 (void)hv_stores(attr, "AutoCommit", newSViv(1));
140 11 (void)hv_stores(attr, "PrintError", newSViv(0));
141 /* A handle inherited across a fork is freed when the child connects
142 * for itself, and freeing it must not say goodbye on a socket the
143 * parent is still using. */
144 11 (void)hv_stores(attr, "AutoInactiveDestroy", newSViv(1));
145 11 ▶ if (user_attr && SvROK(user_attr)
Branch: 2 branches
BranchTrueFalseTotal
if (user_attr && SvROK(user_attr) 2 9 11
if (user_attr && SvROK(user_attr) 2 0 2
146 2 ▶ && SvTYPE(SvRV(user_attr)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(user_attr)) == SVt_PVHV) { 2 0 2
147 2 HV *ua = (HV *)SvRV(user_attr);
148 HE *he;
149 2 hv_iterinit(ua);
150 4 ▶ while ((he = hv_iternext(ua)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(ua))) 2 2 4
151 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
152 newSVsv(HeVAL(he)), 0);
153 }
154 /* Observed statements. The six methods below are not the only way an
155 * application runs one - it reaches through to this handle for an OR,
156 * a UNION, a FOR UPDATE, an upsert - and those never touch this file.
157 * Punk::DBI wraps the handle instead, so they are seen too.
158 *
159 * Only when somebody is listening, so an uninstrumented application
160 * pays no Perl frame per statement; and never over an application's
161 * own RootClass, which it would be losing to get this. */
162 11 ▶ 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 8 11
if (PK_OBS_WANT_QUERY && !hv_exists(attr, "RootClass", 9)) { 2 1 3
163 2 (void)pk_require_once(aTHX_ "Punk::DBI", TRUE);
164 2 (void)hv_stores(attr, "RootClass", newSVpvs("Punk::DBI"));
165 }
166 11 attr_rv = sv_2mortal(newRV_noinc((SV *)attr));
167  
168 11 (void)pk_require_once(aTHX_ "DBI", TRUE);
169 11 argv[0] = dsn;
170 11 ▶ argv[1] = (user && SvOK(user)) ? user : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
argv[1] = (user && SvOK(user)) ? user : &PL_sv_undef; 0 11 11
argv[1] = (user && SvOK(user)) ? user : &PL_sv_undef; 0 0 0
171 11 ▶ argv[2] = (pass && SvOK(pass)) ? pass : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
argv[2] = (pass && SvOK(pass)) ? pass : &PL_sv_undef; 0 11 11
argv[2] = (pass && SvOK(pass)) ? pass : &PL_sv_undef; 0 0 0
172 11 argv[3] = attr_rv;
173 11 conn = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("DBI")), "connect",
174 argv, 4, 1);
175 11 ▶ if (!conn) conn = newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!conn) conn = newSV(0); 0 11 11
176 11 (void)hv_stores(slot, "dbh", conn);
177 11 (void)hv_stores(slot, "pid", newSViv((IV)PerlProc_getpid()));
178 11 (void)hv_stores(slot, "returning",
179 newSViv(pdbi_detect_returning(aTHX_ conn)));
180 /* Both caches belong to the connection: the quoted identifiers
181 * because quoting is the driver's, and the assembled statements
182 * because they have those identifiers baked into them. A reconnect
183 * (or a fork) starts fresh ones. */
184 11 (void)hv_delete(slot, "qi", 2, G_DISCARD);
185 11 (void)hv_delete(slot, "sqlc", 4, G_DISCARD);
186 /* a transaction open on the old handle died with it */
187 11 (void)hv_delete(slot, "txn", 3, G_DISCARD);
188 }
189 379 return slot;
190 }
191  
192 /* The pool slot for this backend's dsn. Borrowed. */
193 368 static HV *pdbi_slot_for(pTHX_ SV *self) {
194 368 HV *h = pdbi_hv(aTHX_ self);
195 368 SV *opts = pdbi_get(aTHX_ h, "opts");
196 368 ▶ HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 368 0 368
HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 368 0 368
197 736 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 368 0 368
198 368 HV *slot = pdbi_slot_for_opts(aTHX_ o);
199 /* create/update read this off the instance; keep it in step with the
200 * pooled slot, which is where the detection actually lives */
201 {
202 368 SV *r = pdbi_get(aTHX_ slot, "returning");
203 368 ▶ (void)hv_stores(h, "returning", newSViv(r ? SvIV(r) : 0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, "returning", newSViv(r ? SvIV(r) : 0)); 368 0 368
204 }
205 368 return slot;
206 }
207  
208 /* The live handle for this backend's dsn. Borrowed. */
209 56 static SV *pdbi_dbh(pTHX_ SV *self) {
210 56 SV *dbh = pdbi_get(aTHX_ pdbi_slot_for(aTHX_ self), "dbh");
211 56 ▶ return dbh ? dbh : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return dbh ? dbh : &PL_sv_undef; 56 0 56
212 }
213  
214 /* $dbh->quote_identifier($name), on a handle the caller already holds - the
215 * DBIx::Loop backend quotes against its parent handle, which is not in this
216 * pool. Mortal. */
217 64 static SV *pdbi_qi_dbh(pTHX_ SV *dbh, SV *name) {
218 SV *argv[1], *q;
219 64 argv[0] = name;
220 64 q = pcx_call_meth(aTHX_ dbh, "quote_identifier", argv, 1, 1);
221 64 ▶ return q ? sv_2mortal(q) : sv_2mortal(newSVsv(name));
Branch: 1 branch
BranchTrueFalseTotal
return q ? sv_2mortal(q) : sv_2mortal(newSVsv(name)); 64 0 64
222 }
223  
224 /* quote_identifier, memoised on the pool slot.
225 *
226 * It is a Perl method call into DBI, and on a small statement it dominates:
227 * a get quotes the table and one key column, and those two calls were 1.5us
228 * of a 3.6us round trip - more than the query itself. The answer depends only
229 * on the driver and the name, and a model's identifiers are a fixed, tiny set
230 * (its table and its columns), so the first call per name pays for the rest.
231 *
232 * The cache lives on the SLOT rather than the instance because quoting is a
233 * property of the connection: every model on one dsn shares it, and a
234 * reconnect drops it (see pdbi_slot_for). The returned SV is borrowed from
235 * the cache - callers concatenate it into the SQL immediately. */
236 1709 static SV *pdbi_qi_slot(pTHX_ HV *slot, SV *dbh, SV *name) {
237 1709 SV **e = hv_fetchs(slot, "qi", 0);
238 HV *cache;
239 HE *he;
240 1709 ▶ if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 1694 15 1709
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 1694 0 1694
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 1694 0 1694
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 1694 0 1694
241 1694 cache = (HV *)SvRV(*e);
242 else {
243 15 cache = newHV();
244 15 (void)hv_stores(slot, "qi", newRV_noinc((SV *)cache));
245 }
246 1709 he = hv_fetch_ent(cache, name, 0, 0);
247 1709 ▶ if (he && HeVAL(he) && SvOK(HeVAL(he))) return HeVAL(he);
Branch: 3 branches
BranchTrueFalseTotal
if (he && HeVAL(he) && SvOK(HeVAL(he))) return HeVAL(he); 1645 64 1709
if (he && HeVAL(he) && SvOK(HeVAL(he))) return HeVAL(he); 1645 0 1645
if (he && HeVAL(he) && SvOK(HeVAL(he))) return HeVAL(he); 1645 0 1645
248 64 he = hv_store_ent(cache, name,
249 newSVsv(pdbi_qi_dbh(aTHX_ dbh, name)), 0);
250 64 ▶ return (he && HeVAL(he)) ? HeVAL(he) : name;
Branch: 2 branches
BranchTrueFalseTotal
return (he && HeVAL(he)) ? HeVAL(he) : name; 64 0 64
return (he && HeVAL(he)) ? HeVAL(he) : name; 64 0 64
251 }
252  
253 /* $self->dbh->quote_identifier($name), memoised. Borrowed. */
254 63 static SV *pdbi_qi(pTHX_ SV *self, SV *name) {
255 63 HV *slot = pdbi_slot_for(aTHX_ self);
256 63 SV *dbh = pdbi_get(aTHX_ slot, "dbh");
257 63 ▶ return pdbi_qi_slot(aTHX_ slot, dbh ? dbh : &PL_sv_undef, name);
Branch: 1 branch
BranchTrueFalseTotal
return pdbi_qi_slot(aTHX_ slot, dbh ? dbh : &PL_sv_undef, name); 63 0 63
258 }
259  
260 0 static SV *pdbi_qi_pv(pTHX_ SV *self, const char *name) {
261 0 return pdbi_qi(aTHX_ self, sv_2mortal(newSVpv(name, 0)));
262 }
263  
264 /* prepare_cached: one statement handle per distinct SQL string on the
265 * connection, which is what keeps repeated queries off the parser (and what
266 * t/0510 checks through $dbh->{CachedKids}). Mortal. */
267 247 static SV *pdbi_sth_dbh(pTHX_ SV *dbh, SV *sql) {
268 SV *argv[3], *sth;
269 247 argv[0] = sql;
270 247 argv[1] = &PL_sv_undef;
271 247 argv[2] = sv_2mortal(newSViv(3));
272 247 sth = pcx_call_meth(aTHX_ dbh, "prepare_cached", argv, 3, 1);
273 247 ▶ 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 247 247
274 247 return sv_2mortal(sth);
275 }
276  
277 22 static SV *pdbi_sth(pTHX_ SV *self, SV *sql) {
278 22 return pdbi_sth_dbh(aTHX_ pdbi_dbh(aTHX_ self), sql);
279 }
280  
281 /* $sth->execute(@bind)
282 *
283 * The query observers used to fire HERE, which was the wrong altitude: these
284 * six methods are a layer above the handle, and an application reaching
285 * through to $model->backend->dbh for an OR, a UNION, a FOR UPDATE or an
286 * upsert ran statements no observer could see. Punk::DBI wraps the handle
287 * instead, so this path is covered by Punk::DBI::st::execute along with every
288 * other, and firing again here would report each of these twice.
289 *
290 * `sql` stays in the signature: the callers have it, a future observer at
291 * this altitude would want it, and threading it back through eight call sites
292 * later is worse than an unused argument now. */
293 247 static void pdbi_execute_sql(pTHX_ SV *sth, AV *bind, SV *sql) {
294 247 ▶ SSize_t n = bind ? av_len(bind) + 1 : 0, i;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t n = bind ? av_len(bind) + 1 : 0, i; 247 0 247
295 247 ▶ 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; 244 3 247
296 SV *r;
297 3336 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3089 247 3336
298 3089 SV **e = av_fetch(bind, i, 0);
299 3089 ▶ argv[i] = (e && *e) ? *e : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
argv[i] = (e && *e) ? *e : &PL_sv_undef; 3089 0 3089
argv[i] = (e && *e) ? *e : &PL_sv_undef; 3089 0 3089
300 }
301 PERL_UNUSED_ARG(sql);
302 247 r = pcx_call_meth(aTHX_ sth, "execute", argv, (int)n, 1);
303 247 ▶ if (argv) safefree(argv);
Branch: 1 branch
BranchTrueFalseTotal
if (argv) safefree(argv); 244 3 247
304 247 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 247 0 247
305 247 }
306  
307 302 static SV *pdbi_meth0(pTHX_ SV *obj, const char *meth) {
308 302 return pcx_call_meth(aTHX_ obj, meth, NULL, 0, 1);
309 }
310  
311 /* The hash keys of an unblessed hashref, sorted the way `sort keys %$h` would
312 * sort them - the generated SQL has to be stable, or prepare_cached sees a
313 * different statement for every permutation of the same filter. Mortal AV. */
314 764 static AV *pdbi_sorted_keys(pTHX_ HV *h) {
315 764 AV *out = (AV *)sv_2mortal((SV *)newAV());
316 HE *he;
317 SSize_t n;
318 764 ▶ if (!h) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return out; 4 760 764
319 760 hv_iterinit(h);
320 1403 ▶ while ((he = hv_iternext(h)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(h))) 643 760 1403
321 643 ▶ av_push(out, newSVsv(HeSVKEY_force(he)));
Branch: 2 branches
BranchTrueFalseTotal
av_push(out, newSVsv(HeSVKEY_force(he))); 643 0 643
av_push(out, newSVsv(HeSVKEY_force(he))); 0 643 643
322 760 n = av_len(out) + 1;
323 760 ▶ 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); 89 671 760
324 760 return out;
325 }
326  
327 /* ---- the opaque keyset token ----------------------------------------------
328 *
329 * base64url of {"k":<primary key>}: url-safe, unpadded, and opaque enough
330 * that nobody builds one by hand and depends on the shape. */
331  
332 static const char PDBI_B64U[] =
333 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
334  
335 130 static SV *pdbi_b64u_encode(pTHX_ const unsigned char *s, STRLEN len) {
336 130 SV *out = newSV(((len + 2) / 3) * 4 + 1);
337 char *d;
338 130 STRLEN i = 0;
339 130 SvPOK_on(out);
340 130 d = SvPVX(out);
341 1848 ▶ while (i + 2 < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i + 2 < len) { 1718 130 1848
342 1718 unsigned v = ((unsigned)s[i] << 16) | ((unsigned)s[i+1] << 8) | s[i+2];
343 1718 *d++ = PDBI_B64U[(v >> 18) & 0x3F];
344 1718 *d++ = PDBI_B64U[(v >> 12) & 0x3F];
345 1718 *d++ = PDBI_B64U[(v >> 6) & 0x3F];
346 1718 *d++ = PDBI_B64U[ v & 0x3F];
347 1718 i += 3;
348 }
349 130 ▶ if (i < len) { /* 1 or 2 bytes left, no padding */
Branch: 1 branch
BranchTrueFalseTotal
if (i < len) { /* 1 or 2 bytes left, no padding */ 112 18 130
350 112 unsigned v = (unsigned)s[i] << 16;
351 112 int two = (i + 1 < len);
352 112 ▶ if (two) v |= (unsigned)s[i+1] << 8;
Branch: 1 branch
BranchTrueFalseTotal
if (two) v |= (unsigned)s[i+1] << 8; 43 69 112
353 112 *d++ = PDBI_B64U[(v >> 18) & 0x3F];
354 112 *d++ = PDBI_B64U[(v >> 12) & 0x3F];
355 112 ▶ if (two) *d++ = PDBI_B64U[(v >> 6) & 0x3F];
Branch: 1 branch
BranchTrueFalseTotal
if (two) *d++ = PDBI_B64U[(v >> 6) & 0x3F]; 43 69 112
356 }
357 130 *d = '\0';
358 130 SvCUR_set(out, (STRLEN)(d - SvPVX(out)));
359 130 return out;
360 }
361  
362 /* Decode, accepting the padded and url-unsafe spellings too. NULL when the
363 * input is not base64 at all. */
364 132 static SV *pdbi_b64u_decode(pTHX_ const char *s, STRLEN len) {
365 132 SV *out = newSV(len + 1);
366 char *d;
367 132 unsigned v = 0;
368 132 int bits = 0;
369 STRLEN i;
370 132 SvPOK_on(out);
371 132 d = SvPVX(out);
372 7328 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 7201 127 7328
373 7201 char c = s[i];
374 int x;
375 7201 ▶ if (c == '=') break;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '=') break; 0 7201 7201
376 7201 ▶ if (c >= 'A' && c <= 'Z') x = c - 'A';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'Z') x = c - 'A'; 6514 687 7201
if (c >= 'A' && c <= 'Z') x = c - 'A'; 2980 3534 6514
377 4221 ▶ else if (c >= 'a' && c <= 'z') x = c - 'a' + 26;
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 'a' && c <= 'z') x = c - 'a' + 26; 3534 687 4221
else if (c >= 'a' && c <= 'z') x = c - 'a' + 26; 3534 0 3534
378 687 ▶ else if (c >= '0' && c <= '9') x = c - '0' + 52;
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= '0' && c <= '9') x = c - '0' + 52; 680 7 687
else if (c >= '0' && c <= '9') x = c - '0' + 52; 680 0 680
379 7 ▶ else if (c == '-' || c == '+') x = 62;
Branch: 2 branches
BranchTrueFalseTotal
else if (c == '-' || c == '+') x = 62; 5 2 7
else if (c == '-' || c == '+') x = 62; 0 5 5
380 5 ▶ else if (c == '_' || c == '/') x = 63;
Branch: 2 branches
BranchTrueFalseTotal
else if (c == '_' || c == '/') x = 63; 5 0 5
else if (c == '_' || c == '/') x = 63; 0 5 5
381 5 else { SvREFCNT_dec(out); return NULL; }
382 7196 v = (v << 6) | (unsigned)x;
383 7196 bits += 6;
384 7196 ▶ if (bits >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (bits >= 8) { 5352 1844 7196
385 5352 bits -= 8;
386 5352 *d++ = (char)((v >> bits) & 0xFF);
387 }
388 }
389 127 *d = '\0';
390 127 SvCUR_set(out, (STRLEN)(d - SvPVX(out)));
391 127 return out;
392 }
393  
394 16 static SV *pdbi_encode_token(pTHX_ SV *val) {
395 16 HV *w = newHV();
396 SV *json, *tok;
397 STRLEN jl;
398 const char *jp;
399 16 (void)hv_stores(w, "k", newSVsv(val));
400 16 json = punk_frj(aTHX)->encode(aTHX_ sv_2mortal(newRV_noinc((SV *)w)), NULL);
401 16 jp = SvPV_const(json, jl);
402 16 tok = pdbi_b64u_encode(aTHX_ (const unsigned char *)jp, jl);
403 16 SvREFCNT_dec(json);
404 16 return tok;
405 }
406  
407 /* `cls` names the backend in the croak, so a bad token on the async backend
408 * does not blame the DBI one. A shared codec is a correctness requirement,
409 * not DRY: a `next` token minted by one backend must decode in the other, or
410 * switching backends breaks every paginated URL in flight. */
411 2 static SV *pdbi_decode_token(pTHX_ SV *tok, const char *cls) {
412 STRLEN tl;
413 4 ▶ const char *tp = SvOK(tok) ? SvPV_const(tok, tl) : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *tp = SvOK(tok) ? SvPV_const(tok, tl) : ""; 2 0 2
414 2 SV *json, *doc, *out = NULL;
415 2 ▶ if (!SvOK(tok)) tl = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(tok)) tl = 0; 0 2 2
416 2 json = pdbi_b64u_decode(aTHX_ tp, tl);
417 2 ▶ if (!json) croak("%s: invalid pagination token", cls);
Branch: 1 branch
BranchTrueFalseTotal
if (!json) croak("%s: invalid pagination token", cls); 0 2 2
418 2 sv_2mortal(json);
419  
420 /* The decode dies on malformed JSON, and a bad token is a client error
421 * rather than a server one - so it has to be trapped and turned into the
422 * croak below. Trapping it goes through Perl's eval (call_pv + G_EVAL)
423 * rather than JMPENV directly: a longjmp past the C would leave the save
424 * stack unwound only as far as the jump, and this is an error path where
425 * correctness is worth more than the call. */
426 {
427 SV *argv[1];
428 2 dSP;
429 int count;
430 2 argv[0] = json;
431 2 ENTER; SAVETMPS;
432 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
433 2 count = call_pv("File::Raw::JSON::file_json_decode",
434 G_SCALAR | G_EVAL);
435 2 SPAGAIN;
436 2 ▶ doc = (count > 0) ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
doc = (count > 0) ? SvREFCNT_inc(POPs) : NULL; 2 0 2
437 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
438 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
439 PERL_UNUSED_VAR(argv);
440 }
441 2 ▶ if (!doc) croak("%s: invalid pagination token", cls);
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) croak("%s: invalid pagination token", cls); 0 2 2
442  
443 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
444 2 SV *k = pdbi_get(aTHX_ (HV *)SvRV(doc), "k");
445 2 ▶ if (k) out = newSVsv(k);
Branch: 1 branch
BranchTrueFalseTotal
if (k) out = newSVsv(k); 2 0 2
446 }
447 2 SvREFCNT_dec(doc);
448 2 ▶ if (!out) croak("%s: invalid pagination token", cls);
Branch: 1 branch
BranchTrueFalseTotal
if (!out) croak("%s: invalid pagination token", cls); 0 2 2
449 2 return out;
450 }
451  
452 /* ---- shared SQL shapes ----------------------------------------------------- */
453  
454 /* '<qi(a)> = ? AND <qi(b)> = ?' for the sorted keys, pushing each key's value
455 * onto @bind in the same order, quoting through the slot's cache. Mortal. */
456 27 static SV *pdbi_where_eq_slot(pTHX_ HV *slot, SV *dbh, HV *src,
457 AV *keys, AV *bind) {
458 27 SV *where = sv_2mortal(newSVpvs(""));
459 27 SSize_t i, n = av_len(keys) + 1;
460 56 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 29 27 56
461 29 SV *k = *av_fetch(keys, i, 0);
462 HE *he;
463 29 ▶ if (i) sv_catpvs(where, " AND ");
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpvs(where, " AND "); 2 27 29
464 29 sv_catsv(where, pdbi_qi_slot(aTHX_ slot, dbh, k));
465 29 sv_catpvs(where, " = ?");
466 29 he = hv_fetch_ent(src, k, 0, 0);
467 29 ▶ av_push(bind, newSVsv(he ? HeVAL(he) : &PL_sv_undef));
Branch: 1 branch
BranchTrueFalseTotal
av_push(bind, newSVsv(he ? HeVAL(he) : &PL_sv_undef)); 29 0 29
468 }
469 27 return where;
470 }
471  
472 /* The bind half of pdbi_where_eq_slot on its own, in the same (sorted) order -
473 * what the cached-SQL path still has to do per call. */
474 15 static void pdbi_bind_keys(pTHX_ HV *src, AV *keys, AV *bind) {
475 15 SSize_t i, n = av_len(keys) + 1;
476 30 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 15 15 30
477 15 SV *k = *av_fetch(keys, i, 0);
478 15 HE *he = hv_fetch_ent(src, k, 0, 0);
479 15 ▶ av_push(bind, newSVsv(he ? HeVAL(he) : &PL_sv_undef));
Branch: 1 branch
BranchTrueFalseTotal
av_push(bind, newSVsv(he ? HeVAL(he) : &PL_sv_undef)); 15 0 15
480 }
481 15 }
482  
483 0 static SV *pdbi_where_eq(pTHX_ SV *self, HV *src, AV *keys, AV *bind) {
484 0 HV *slot = pdbi_slot_for(aTHX_ self);
485 0 SV *dbh = pdbi_get(aTHX_ slot, "dbh");
486 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
487 src, keys, bind);
488 }
489  
490 /* A memoised statement, for the fixed-shape operations.
491 *
492 * get and delete generate one SQL string per set of key columns, and a model
493 * is asked for the same set over and over - almost always the primary key. So
494 * the string is built once and looked up thereafter, which also skips the
495 * identifier quoting and the concatenation behind it. `sig` identifies the
496 * shape (the operation plus its sorted key names); the cache lives on the
497 * slot beside the quoting cache, and dies with the connection for the same
498 * reason. Returns the SQL, borrowed. */
499 35 static SV *pdbi_sql_cached(pTHX_ HV *slot, SV *sig, SV *sql) {
500 35 SV **e = hv_fetchs(slot, "sqlc", 0);
501 HV *cache;
502 HE *he;
503 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
504 30 cache = (HV *)SvRV(*e);
505 else {
506 5 cache = newHV();
507 5 (void)hv_stores(slot, "sqlc", newRV_noinc((SV *)cache));
508 }
509 35 ▶ if (!sql) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sql) { 27 8 35
510 27 he = hv_fetch_ent(cache, sig, 0, 0);
511 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
512 }
513 8 he = hv_store_ent(cache, sig, newSVsv(sql), 0);
514 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
515 }
516  
517 /* 'op\0table\0k1\0k2' - the shape of a fixed-form statement. The TABLE is in
518 * the signature because the cache hangs off the connection, which every model
519 * on that dsn shares: without it, two models with the same key columns would
520 * read each other's statement. Mortal. */
521 27 static SV *pdbi_sig(pTHX_ const char *op, SV *table, AV *keys) {
522 27 SV *sig = sv_2mortal(newSVpv(op, 0));
523 27 ▶ SSize_t i, n = keys ? av_len(keys) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = keys ? av_len(keys) + 1 : 0; 27 0 27
524 27 sv_catpvn(sig, "\0", 1);
525 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
526 56 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 29 27 56
527 29 SV **k = av_fetch(keys, i, 0);
528 29 sv_catpvn(sig, "\0", 1);
529 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
530 }
531 27 return sig;
532 }
533  
534 /* The %key of get/delete, as sorted keys; croaks (naming the calling backend)
535 * when empty. */
536 62 static AV *pdbi_key_args(pTHX_ SV **st, I32 items, HV **out,
537 const char *what, const char *cls) {
538 62 HV *key = newHV();
539 I32 i;
540 62 ▶ if (items < 3 || !((items - 1) % 2 == 0))
Branch: 2 branches
BranchTrueFalseTotal
if (items < 3 || !((items - 1) % 2 == 0)) 62 0 62
if (items < 3 || !((items - 1) % 2 == 0)) 0 62 62
541 0 croak("%s: %s needs a key", cls, what);
542 128 ▶ for (i = 1; i + 1 < items; i += 2)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) 66 62 128
543 66 (void)hv_store_ent(key, st[i], newSVsv(st[i + 1]), 0);
544 62 ▶ if (!HvUSEDKEYS(key)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!HvUSEDKEYS(key)) { 0 62 62
if (!HvUSEDKEYS(key)) { 0 62 62
545 0 SvREFCNT_dec((SV *)key);
546 0 croak("%s: %s needs a key", cls, what);
547 }
548 62 *out = (HV *)sv_2mortal((SV *)key);
549 62 return pdbi_sorted_keys(aTHX_ key);
550 }
551  
552 /* The backend object both shipped backends bless: the same slots the Perl
553 * used (opts/table/primary/columns/col/returning), parsed from the
554 * database/table/primary/columns pairs _instantiate passes. Shared so a
555 * second backend cannot drift from the shape t/0510 reaches through. */
556 42 static SV *pdbi_build_self(pTHX_ SV *class, SV **st, I32 items,
557 const char *cls) {
558 42 HV *h = newHV();
559 42 HV *col = newHV();
560 42 SV *table = NULL, *primary = NULL, *columns = NULL, *database = NULL;
561 I32 i;
562 215 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 173 42 215
563 173 STRLEN kl; const char *k = SvPV_const(st[i], kl);
564 173 ▶ if (kl == 5 && memEQ(k, "table", 5)) table = st[i + 1];
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 5 && memEQ(k, "table", 5)) table = st[i + 1]; 42 131 173
if (kl == 5 && memEQ(k, "table", 5)) table = st[i + 1]; 42 0 42
565 131 ▶ 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]; 75 56 131
else if (kl == 7 && memEQ(k, "primary", 7)) primary = st[i + 1]; 38 37 75
566 93 ▶ else if (kl == 7 && memEQ(k, "columns", 7)) columns = st[i + 1];
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 7 && memEQ(k, "columns", 7)) columns = st[i + 1]; 37 56 93
else if (kl == 7 && memEQ(k, "columns", 7)) columns = st[i + 1]; 37 0 37
567 56 ▶ else if (kl == 8 && memEQ(k, "database", 8)) database = st[i + 1];
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 8 && memEQ(k, "database", 8)) database = st[i + 1]; 40 16 56
else if (kl == 8 && memEQ(k, "database", 8)) database = st[i + 1]; 40 0 40
568 }
569 42 ▶ if (!(table && SvOK(table) && SvTRUE(table))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(table && SvOK(table) && SvTRUE(table))) { 42 0 42
if (!(table && SvOK(table) && SvTRUE(table))) { 42 0 42
if (!(table && SvOK(table) && SvTRUE(table))) { 0 42 42
570 0 SvREFCNT_dec((SV *)h); SvREFCNT_dec((SV *)col);
571 0 croak("%s: a model with no table", cls);
572 }
573 /* the column set, for the create/update filters */
574 42 ▶ if (columns && SvROK(columns) && SvTYPE(SvRV(columns)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (columns && SvROK(columns) && SvTYPE(SvRV(columns)) == SVt_PVAV) { 37 5 42
if (columns && SvROK(columns) && SvTYPE(SvRV(columns)) == SVt_PVAV) { 37 0 37
if (columns && SvROK(columns) && SvTYPE(SvRV(columns)) == SVt_PVAV) { 37 0 37
575 37 AV *c = (AV *)SvRV(columns);
576 37 SSize_t n = av_len(c) + 1, j;
577 141 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 104 37 141
578 104 SV **e = av_fetch(c, j, 0);
579 104 ▶ 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); 104 0 104
if (e && *e) (void)hv_store_ent(col, *e, newSViv(1), 0); 104 0 104
580 }
581 }
582 42 ▶ (void)hv_stores(h, "opts",
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(h, "opts", 40 2 42
(void)hv_stores(h, "opts", 40 0 40
(void)hv_stores(h, "opts", 40 0 40
583 (database && SvROK(database) && SvTYPE(SvRV(database)) == SVt_PVHV)
584 ? newSVsv(database) : newRV_noinc((SV *)newHV()));
585 42 (void)hv_stores(h, "table", newSVsv(table));
586 42 ▶ (void)hv_stores(h, "primary",
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(h, "primary", 38 4 42
(void)hv_stores(h, "primary", 38 0 38
587 (primary && SvOK(primary)) ? newSVsv(primary) : newSV(0));
588 42 ▶ (void)hv_stores(h, "columns",
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(h, "columns", 37 5 42
(void)hv_stores(h, "columns", 37 0 37
(void)hv_stores(h, "columns", 37 0 37
589 (columns && SvROK(columns) && SvTYPE(SvRV(columns)) == SVt_PVAV)
590 ? newSVsv(columns) : newRV_noinc((SV *)newAV()));
591 42 (void)hv_stores(h, "col", newRV_noinc((SV *)col));
592 42 (void)hv_stores(h, "returning", newSViv(0));
593 42 return sv_bless(newRV_noinc((SV *)h), gv_stashsv(class, GV_ADD));
594 }
595  
596 #endif /* PUNK_DBI_H */
« xs/auth.xs ↑ Summary include/punk/punk_sitemap.h »