include/pq_dbi.h

filter: Statementstmt 91.6% 219 / 239 Branchbran 51.7% 120 / 232 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 71.9% 339 / 471 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/worker.xs ↑ Summary include/pq_frj.h »
1 #ifndef PQ_DBI_H
2 #define PQ_DBI_H
3  
4 /* pq_dbi.h - the DBI seam, adapted from Punk's punk_dbi.h.
5 *
6 * What this buys and what it does not is worth stating plainly, because the
7 * shape of the win is unusual. Every database operation is still a DBI
8 * method call - connect, prepare_cached, execute, fetchrow_arrayref - and
9 * those are Perl-level calls into DBI's own XS that no amount of C on this
10 * side removes. The round trip to the database dominates either way.
11 *
12 * What moves into C is the glue around them: assembling the SQL, building
13 * the bind vectors, decoding rows positionally, the JSON codec (through
14 * frj_abi), and the connection pool bookkeeping. The queue's contract
15 * methods are XSUBs, so an enqueue is one C frame calling DBI rather than
16 * two or three Perl frames doing it.
17 *
18 * (This shape - every op ending in a DBI method call - is also why the
19 * once-planned C ABI was dropped: it could never have been a speed
20 * feature, and its only consumer lived in this dist. See
21 * plan_punk_queue/phase-7-abi.md.)
22 *
23 * Include after pq_compat.h, pq_frj.h and pq_time.h. */
24  
25 /* ---- small helpers -------------------------------------------------------- */
26  
27 28068 static HV *pq_hv(pTHX_ SV *self, const char *what) {
28 28068 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 28068 0 28068
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 0 28068 28068
29 0 croak("%s: not an object", what);
30 28068 return (HV *)SvRV(self);
31 }
32  
33 178154 static SV *pq_get(pTHX_ HV *h, const char *k) {
34 178154 ▶ SV **e = h ? hv_fetch(h, k, (I32)strlen(k), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = h ? hv_fetch(h, k, (I32)strlen(k), 0) : NULL; 178154 0 178154
35 178154 ▶ return (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? *e : NULL; 114214 63940 178154
return (e && *e) ? *e : NULL; 114214 0 114214
36 }
37  
38 19918 static HV *pq_get_hv(pTHX_ HV *h, const char *k) {
39 19918 SV *v = pq_get(aTHX_ h, k);
40 19730 ▶ return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) ? (HV *)SvRV(v)
Branch: 2 branches
BranchTrueFalseTotal
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) ? (HV *)SvRV(v) 19730 0 19730
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) ? (HV *)SvRV(v) 19730 0 19730
41 39648 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 19730 188 19918
42 }
43  
44 /* Call $obj->$meth(@argv) in its own stack scope, returning the scalar
45 * result (+1) when want is true, else NULL. argv holds stable SV* captured
46 * before any stack manipulation - EXTEND may move the caller's stack, so
47 * ST() is stale afterwards.
48 *
49 * Note the SvREFCNT_inc before FREETMPS: a value that is only mortal when
50 * FREETMPS runs is freed by it, and the caller gets a corpse. This is the
51 * bug that cost Punk's logging tier a day. */
52 25606 static SV *pq_call_meth(pTHX_ SV *obj, const char *meth,
53 SV **argv, int nargs, int want) {
54 25606 dSP; SV *r = NULL; int count, i;
55 25606 ENTER; SAVETMPS;
56 25606 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 25606 25606
57 25606 ▶ EXTEND(SP, nargs + 1);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, nargs + 1); 25606 0 25606
EXTEND(SP, nargs + 1); 0 25606 25606
58 25606 PUSHs(obj);
59 59219 ▶ for (i = 0; i < nargs; i++) PUSHs(argv[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nargs; i++) PUSHs(argv[i]); 33613 25606 59219
60 25606 PUTBACK;
61 25606 ▶ count = call_method(meth, want ? G_SCALAR : G_VOID);
Branch: 1 branch
BranchTrueFalseTotal
count = call_method(meth, want ? G_SCALAR : G_VOID); 25606 0 25606
62 25605 SPAGAIN;
63 25605 ▶ if (want && count > 0) r = SvREFCNT_inc(POPs);
Branch: 2 branches
BranchTrueFalseTotal
if (want && count > 0) r = SvREFCNT_inc(POPs); 25605 0 25605
if (want && count > 0) r = SvREFCNT_inc(POPs); 25605 0 25605
64 25605 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 25605 0 25605
65 25605 return r;
66 }
67  
68 /* As above but trapping a die; *died is set and ERRSV holds the message.
69 * Used where a failure is expected and recoverable - a BEGIN IMMEDIATE that
70 * loses a race, a unique-constraint violation on a dedupe key. */
71 2724 static SV *pq_call_meth_ev(pTHX_ SV *obj, const char *meth,
72 SV **argv, int nargs, int want, int *died) {
73 2724 dSP; SV *r = NULL; int count, i;
74 2724 ENTER; SAVETMPS;
75 2724 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2724 2724
76 2724 ▶ EXTEND(SP, nargs + 1);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, nargs + 1); 2724 0 2724
EXTEND(SP, nargs + 1); 0 2724 2724
77 2724 PUSHs(obj);
78 17324 ▶ for (i = 0; i < nargs; i++) PUSHs(argv[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nargs; i++) PUSHs(argv[i]); 14600 2724 17324
79 2724 PUTBACK;
80 2724 ▶ count = call_method(meth, (want ? G_SCALAR : G_VOID) | G_EVAL);
Branch: 1 branch
BranchTrueFalseTotal
count = call_method(meth, (want ? G_SCALAR : G_VOID) | G_EVAL); 2724 0 2724
81 2710 SPAGAIN;
82 2710 ▶ if (want && count > 0) r = SvREFCNT_inc(POPs);
Branch: 2 branches
BranchTrueFalseTotal
if (want && count > 0) r = SvREFCNT_inc(POPs); 2710 0 2710
if (want && count > 0) r = SvREFCNT_inc(POPs); 2710 0 2710
83 2710 ▶ *died = SvTRUE(ERRSV) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
*died = SvTRUE(ERRSV) ? 1 : 0; 2710 0 2710
84 2710 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2710 0 2710
85 2710 ▶ if (*died && r) { SvREFCNT_dec(r); r = NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (*died && r) { SvREFCNT_dec(r); r = NULL; } 704 2006 2710
if (*died && r) { SvREFCNT_dec(r); r = NULL; } 704 0 704
86 2710 return r;
87 }
88  
89 /* An attribute off a DBI handle's hash ($dbh->{Driver}). DBI handles are
90 * magical hashes, so this goes through hv_fetch, which runs the magic. */
91 7933 static SV *pq_attr(pTHX_ SV *h, const char *name) {
92 SV **e;
93 7933 ▶ 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; 7933 0 7933
if (!(h && SvROK(h) && SvTYPE(SvRV(h)) == SVt_PVHV)) return NULL; 7933 0 7933
if (!(h && SvROK(h) && SvTYPE(SvRV(h)) == SVt_PVHV)) return NULL; 0 7933 7933
94 7933 e = hv_fetch((HV *)SvRV(h), name, (I32)strlen(name), 0);
95 7933 ▶ if (!(e && *e)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && *e)) return NULL; 7933 0 7933
if (!(e && *e)) return NULL; 0 7933 7933
96 7933 SvGETMAGIC(*e);
97 7933 return *e;
98 }
99  
100 /* $dbh->{Driver}{Name}: "SQLite", "Pg", ... Borrowed, or NULL. */
101 3700 static SV *pq_driver_name(pTHX_ SV *dbh) {
102 3700 SV *drv = pq_attr(aTHX_ dbh, "Driver");
103 3700 ▶ return drv ? pq_attr(aTHX_ drv, "Name") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return drv ? pq_attr(aTHX_ drv, "Name") : NULL; 3700 0 3700
104 }
105  
106 3700 static int pq_driver_is(pTHX_ SV *dbh, const char *want) {
107 3700 SV *n = pq_driver_name(aTHX_ dbh);
108 3700 STRLEN l, wl = strlen(want);
109 const char *s;
110 3700 ▶ if (!(n && SvOK(n))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(n && SvOK(n))) return 0; 3700 0 3700
if (!(n && SvOK(n))) return 0; 0 3700 3700
111 3700 s = SvPV_const(n, l);
112 3700 ▶ return (l == wl && memEQ(s, want, wl)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (l == wl && memEQ(s, want, wl)) ? 1 : 0; 603 3097 3700
return (l == wl && memEQ(s, want, wl)) ? 1 : 0; 603 0 603
113 }
114  
115 /* ---- the connection pool ---------------------------------------------------
116 *
117 * One handle per distinct connection (dsn + credentials), shared by every
118 * backend object using it, so a queue and a worker on the same database open
119 * one connection per process rather than two. The pid lives in the reconnect
120 * check, so a fork gets a fresh handle instead of a corrupted shared one -
121 * which matters more here than in a web app, because forking is the worker
122 * pool's whole design. */
123  
124 static HV *PQ_POOL = NULL;
125  
126 16814 static HV *pq_pool(pTHX) {
127 16814 ▶ if (!PQ_POOL) PQ_POOL = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PQ_POOL) PQ_POOL = newHV(); 85 16729 16814
128 16814 return PQ_POOL;
129 }
130  
131 16814 static SV *pq_slot(pTHX_ HV *pool, SV *key) {
132 16814 HE *he = hv_fetch_ent(pool, key, 0, 0);
133 16814 ▶ if (!he) {
Branch: 1 branch
BranchTrueFalseTotal
if (!he) { 190 16624 16814
134 190 HV *slot = newHV();
135 190 (void)hv_stores(slot, "pid", newSViv(-1));
136 190 he = hv_store_ent(pool, key, newRV_noinc((SV *)slot), 0);
137 }
138 16814 return HeVAL(he);
139 }
140  
141 /* RETURNING support, feature-detected once per connection: PostgreSQL
142 * always, SQLite from 3.35. Anything else re-reads the row after a write.
143 * Detection failing is not fatal - a 0 here only costs an extra SELECT. */
144 201 static int pq_detect_returning(pTHX_ SV *dbh) {
145 201 ▶ if (pq_driver_is(aTHX_ dbh, "Pg")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (pq_driver_is(aTHX_ dbh, "Pg")) return 1; 0 201 201
146 201 ▶ if (pq_driver_is(aTHX_ dbh, "SQLite")) {
Branch: 1 branch
BranchTrueFalseTotal
if (pq_driver_is(aTHX_ dbh, "SQLite")) { 201 0 201
147 201 SV *v = pq_attr(aTHX_ dbh, "sqlite_version");
148 201 ▶ if (v && SvOK(v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (v && SvOK(v)) { 201 0 201
if (v && SvOK(v)) { 201 0 201
149 STRLEN vl;
150 201 const char *s = SvPV_const(v, vl);
151 201 int maj = 0, min = 0;
152 201 STRLEN i = 0;
153 402 ▶ 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'); 402 0 402
while (i < vl && isDIGIT((U8)s[i])) maj = maj * 10 + (s[i++] - '0'); 201 201 402
154 201 ▶ if (i < vl && s[i] == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (i < vl && s[i] == '.') { 201 0 201
if (i < vl && s[i] == '.') { 201 0 201
155 201 i++;
156 603 ▶ 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'); 603 0 603
while (i < vl && isDIGIT((U8)s[i])) min = min * 10 + (s[i++] - '0'); 402 201 603
157 }
158 201 ▶ if (maj > 3 || (maj == 3 && min >= 35)) return 1;
Branch: 3 branches
BranchTrueFalseTotal
if (maj > 3 || (maj == 3 && min >= 35)) return 1; 201 0 201
if (maj > 3 || (maj == 3 && min >= 35)) return 1; 201 0 201
if (maj > 3 || (maj == 3 && min >= 35)) return 1; 201 0 201
159 }
160 }
161 0 return 0;
162 }
163  
164 /* $dbh->do($sql) with no binds, for pragmas and transaction control. */
165 804 static void pq_dbh_do_pv(pTHX_ SV *dbh, const char *sql) {
166 SV *argv[1], *r;
167 804 argv[0] = sv_2mortal(newSVpv(sql, 0));
168 804 r = pq_call_meth(aTHX_ dbh, "do", argv, 1, 1);
169 804 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 804 0 804
170 804 }
171  
172 /* Per-driver connect-time setup.
173 *
174 * This lives here rather than behind a vtable because there are exactly two
175 * drivers, both of them ours, and a function-pointer registry for two cases
176 * is indirection that costs more to read than it saves. When a third
177 * backend arrives, this is the function that grows a branch.
178 *
179 * SQLite: WAL is mandatory (readers must not block the claim transaction),
180 * busy_timeout turns SQLITE_BUSY into a wait rather than an error under
181 * contending workers, and foreign_keys must be enabled per connection or
182 * the ON DELETE CASCADE on pq_job_deps silently does nothing. */
183 201 static void pq_after_connect(pTHX_ SV *dbh) {
184 201 ▶ if (pq_driver_is(aTHX_ dbh, "SQLite")) {
Branch: 1 branch
BranchTrueFalseTotal
if (pq_driver_is(aTHX_ dbh, "SQLite")) { 201 0 201
185 201 pq_dbh_do_pv(aTHX_ dbh, "PRAGMA journal_mode = WAL");
186 201 pq_dbh_do_pv(aTHX_ dbh, "PRAGMA synchronous = NORMAL");
187 201 pq_dbh_do_pv(aTHX_ dbh, "PRAGMA busy_timeout = 5000");
188 201 pq_dbh_do_pv(aTHX_ dbh, "PRAGMA foreign_keys = ON");
189 }
190 201 }
191  
192 /* Forward declaration: the clock probe is per-backend SQL, defined in
193 * pq_sqlite.h / pq_pg.h, but the pool has to run it at connect time. */
194 static double pq_probe_clock(pTHX_ SV *dbh);
195  
196 /* The live handle for this backend's dsn, connected on first use in this
197 * process and shared with every other object on the same one. Borrowed. */
198 16814 static SV *pq_dbh(pTHX_ SV *self) {
199 16814 HV *h = pq_hv(aTHX_ self, "Punk::Queue::Backend");
200 16814 HV *o = pq_get_hv(aTHX_ h, "opts");
201 16814 ▶ SV *dsn = o ? pq_get(aTHX_ o, "dsn") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *dsn = o ? pq_get(aTHX_ o, "dsn") : NULL; 16814 0 16814
202 16814 ▶ SV *user = o ? pq_get(aTHX_ o, "user") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *user = o ? pq_get(aTHX_ o, "user") : NULL; 16814 0 16814
203 16814 ▶ SV *pass = o ? pq_get(aTHX_ o, "password") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *pass = o ? pq_get(aTHX_ o, "password") : NULL; 16814 0 16814
204 SV *key, *slot_sv, *dbh, *pid;
205 16814 HV *pool = pq_pool(aTHX), *slot;
206  
207 /* An externally supplied handle ($q = Punk::Queue->new(dbh => $dbh))
208 * bypasses the pool entirely: we did not open it and must not pool,
209 * reconnect or close it. */
210 {
211 16814 SV *ext = pq_get(aTHX_ h, "dbh");
212 16814 ▶ if (ext && SvROK(ext)) return ext;
Branch: 2 branches
BranchTrueFalseTotal
if (ext && SvROK(ext)) return ext; 0 16814 16814
if (ext && SvROK(ext)) return ext; 0 0 0
213 }
214  
215 16814 ▶ if (!(dsn && SvOK(dsn) && SvTRUE(dsn)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(dsn && SvOK(dsn) && SvTRUE(dsn))) 16814 0 16814
if (!(dsn && SvOK(dsn) && SvTRUE(dsn))) 16814 0 16814
if (!(dsn && SvOK(dsn) && SvTRUE(dsn))) 0 16814 16814
216 0 croak("Punk::Queue: no dsn - pass dsn => 'dbi:SQLite:dbname=...' "
217 "or dbh => $dbh");
218  
219 /* NUL-joined so a dsn containing the separator cannot collide */
220 16814 key = sv_2mortal(newSVsv(dsn));
221 16814 sv_catpvn(key, "\0", 1);
222 16814 ▶ if (user && SvOK(user)) sv_catsv(key, user);
Branch: 2 branches
BranchTrueFalseTotal
if (user && SvOK(user)) sv_catsv(key, user); 0 16814 16814
if (user && SvOK(user)) sv_catsv(key, user); 0 0 0
223 16814 sv_catpvn(key, "\0", 1);
224 16814 ▶ if (pass && SvOK(pass)) sv_catsv(key, pass);
Branch: 2 branches
BranchTrueFalseTotal
if (pass && SvOK(pass)) sv_catsv(key, pass); 0 16814 16814
if (pass && SvOK(pass)) sv_catsv(key, pass); 0 0 0
225  
226 16814 slot_sv = pq_slot(aTHX_ pool, key);
227 16814 slot = (HV *)SvRV(slot_sv);
228 16814 pid = pq_get(aTHX_ slot, "pid");
229 16814 dbh = pq_get(aTHX_ slot, "dbh");
230  
231 16814 ▶ if (!(pid && SvOK(pid) && SvIV(pid) == (IV)PerlProc_getpid())
Branch: 3 branches
BranchTrueFalseTotal
if (!(pid && SvOK(pid) && SvIV(pid) == (IV)PerlProc_getpid()) 16814 0 16814
if (!(pid && SvOK(pid) && SvIV(pid) == (IV)PerlProc_getpid()) 16814 0 16814
if (!(pid && SvOK(pid) && SvIV(pid) == (IV)PerlProc_getpid()) 16613 201 16814
232 16613 ▶ || !(dbh && SvROK(dbh))) {
Branch: 2 branches
BranchTrueFalseTotal
|| !(dbh && SvROK(dbh))) { 16613 0 16613
|| !(dbh && SvROK(dbh))) { 0 16613 16613
233 SV *argv[4], *attr_rv, *conn;
234 201 HV *attr = newHV();
235 201 ▶ SV *user_attr = o ? pq_get(aTHX_ o, "attr") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *user_attr = o ? pq_get(aTHX_ o, "attr") : NULL; 201 0 201
236 201 (void)hv_stores(attr, "RaiseError", newSViv(1));
237 201 (void)hv_stores(attr, "AutoCommit", newSViv(1));
238 201 (void)hv_stores(attr, "PrintError", newSViv(0));
239 /* A forked child inherits the parent's handle, and a normal DESTROY
240 * in the child sends a termination on the SHARED socket - killing
241 * the parent's connection from another process. AutoInactiveDestroy
242 * makes DESTROY a no-op in any process that did not create the
243 * handle. Forking is the worker pool's whole design, so this is
244 * core behaviour, not a test workaround. (DBI 1.614+.) */
245 201 (void)hv_stores(attr, "AutoInactiveDestroy", newSViv(1));
246 201 ▶ if (user_attr && SvROK(user_attr)
Branch: 2 branches
BranchTrueFalseTotal
if (user_attr && SvROK(user_attr) 0 201 201
if (user_attr && SvROK(user_attr) 0 0 0
247 0 ▶ && SvTYPE(SvRV(user_attr)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(user_attr)) == SVt_PVHV) { 0 0 0
248 0 HV *ua = (HV *)SvRV(user_attr);
249 HE *he;
250 0 hv_iterinit(ua);
251 0 ▶ while ((he = hv_iternext(ua)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(ua))) 0 0 0
252 0 ▶ (void)hv_store_ent(attr, HeSVKEY_force(he),
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_store_ent(attr, HeSVKEY_force(he), 0 0 0
(void)hv_store_ent(attr, HeSVKEY_force(he), 0 0 0
253 newSVsv(HeVAL(he)), 0);
254 }
255 201 attr_rv = sv_2mortal(newRV_noinc((SV *)attr));
256  
257 201 eval_pv("require DBI;", TRUE);
258 201 argv[0] = dsn;
259 201 ▶ argv[1] = (user && SvOK(user)) ? user : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
argv[1] = (user && SvOK(user)) ? user : &PL_sv_undef; 0 201 201
argv[1] = (user && SvOK(user)) ? user : &PL_sv_undef; 0 0 0
260 201 ▶ argv[2] = (pass && SvOK(pass)) ? pass : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
argv[2] = (pass && SvOK(pass)) ? pass : &PL_sv_undef; 0 201 201
argv[2] = (pass && SvOK(pass)) ? pass : &PL_sv_undef; 0 0 0
261 201 argv[3] = attr_rv;
262 201 conn = pq_call_meth(aTHX_ sv_2mortal(newSVpvs("DBI")), "connect",
263 argv, 4, 1);
264 201 ▶ if (!conn || !SvROK(conn)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!conn || !SvROK(conn)) { 201 0 201
if (!conn || !SvROK(conn)) { 0 201 201
265 0 ▶ if (conn) SvREFCNT_dec(conn);
Branch: 1 branch
BranchTrueFalseTotal
if (conn) SvREFCNT_dec(conn); 0 0 0
266 0 croak("Punk::Queue: DBI->connect failed for %s",
267 SvPV_nolen(dsn));
268 }
269 201 pq_after_connect(aTHX_ conn);
270 201 (void)hv_stores(slot, "dbh", conn);
271 201 (void)hv_stores(slot, "pid", newSViv((IV)PerlProc_getpid()));
272 201 (void)hv_stores(slot, "returning",
273 newSViv(pq_detect_returning(aTHX_ conn)));
274 /* Probed once per connection, and therefore re-probed after a fork
275 * and after a reconnect, which is exactly when it can have moved. */
276 201 (void)hv_stores(slot, PQ_DELTA_KEY,
277 newSVnv(pq_probe_clock(aTHX_ conn)));
278 201 dbh = conn;
279 }
280  
281 /* Mirror the pooled facts onto the instance, which is where the rest of
282 * the code reads them. */
283 {
284 16814 SV *r = pq_get(aTHX_ slot, "returning");
285 16814 SV *d = pq_get(aTHX_ slot, PQ_DELTA_KEY);
286 16814 ▶ (void)hv_stores(h, "returning", newSViv(r ? SvIV(r) : 0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, "returning", newSViv(r ? SvIV(r) : 0)); 16814 0 16814
287 16814 ▶ (void)hv_stores(h, PQ_DELTA_KEY, newSVnv(d ? SvNV(d) : 0.0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, PQ_DELTA_KEY, newSVnv(d ? SvNV(d) : 0.0)); 16814 0 16814
288 }
289 16814 return dbh;
290 }
291  
292 /* now(), in the database's frame. Every timestamp this dist binds comes
293 * from here, never from pq_now_local directly. */
294 1904 static double pq_now(pTHX_ SV *self) {
295 1904 HV *h = pq_hv(aTHX_ self, "Punk::Queue::Backend");
296 SV *d;
297 1904 (void)pq_dbh(aTHX_ self); /* ensures the delta is populated */
298 1904 d = pq_get(aTHX_ h, PQ_DELTA_KEY);
299 1904 ▶ return pq_now_delta(aTHX_ d ? SvNV(d) : 0.0);
Branch: 1 branch
BranchTrueFalseTotal
return pq_now_delta(aTHX_ d ? SvNV(d) : 0.0); 1904 0 1904
300 }
301  
302 2025 static int pq_has_returning(pTHX_ SV *self) {
303 2025 HV *h = pq_hv(aTHX_ self, "Punk::Queue::Backend");
304 SV *r;
305 2025 (void)pq_dbh(aTHX_ self);
306 2025 r = pq_get(aTHX_ h, "returning");
307 2025 ▶ return r ? (int)SvIV(r) : 0;
Branch: 1 branch
BranchTrueFalseTotal
return r ? (int)SvIV(r) : 0; 2025 0 2025
308 }
309  
310 /* ---- statements ----------------------------------------------------------- */
311  
312 /* prepare_cached: one statement handle per distinct SQL string on the
313 * connection, which is what keeps repeated queries off the parser. This is
314 * also why the SQL text must be stable - an IN list expanded to a different
315 * arity, or a filter built in hash order, defeats it silently. Mortal. */
316 8592 static SV *pq_sth(pTHX_ SV *self, SV *sql) {
317 8592 SV *dbh = pq_dbh(aTHX_ self);
318 SV *argv[3], *sth;
319 8592 argv[0] = sql;
320 8592 argv[1] = &PL_sv_undef;
321 8592 argv[2] = sv_2mortal(newSViv(3)); /* 3 = reuse even if still Active */
322 8592 sth = pq_call_meth(aTHX_ dbh, "prepare_cached", argv, 3, 1);
323 8591 ▶ if (!sth) croak("Punk::Queue: prepare_cached returned nothing");
Branch: 1 branch
BranchTrueFalseTotal
if (!sth) croak("Punk::Queue: prepare_cached returned nothing"); 0 8591 8591
324 8591 return sv_2mortal(sth);
325 }
326  
327 /* $sth->execute(@bind) -> rows affected. DBI returns "0E0" for zero, which
328 * is true but numifies to 0, so SvIV is the right read. */
329 7589 static IV pq_execute(pTHX_ SV *sth, AV *bind) {
330 7589 ▶ SSize_t n = bind ? av_len(bind) + 1 : 0, i;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t n = bind ? av_len(bind) + 1 : 0, i; 2871 4718 7589
331 7589 ▶ SV **argv = n ? (SV **)pq_xmalloc(aTHX_ sizeof(SV *) * (size_t)n) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **argv = n ? (SV **)pq_xmalloc(aTHX_ sizeof(SV *) * (size_t)n) : NULL; 2743 4846 7589
332 SV *r;
333 IV rows;
334 13818 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 6229 7589 13818
335 6229 SV **e = av_fetch(bind, i, 0);
336 6229 ▶ argv[i] = (e && *e) ? *e : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
argv[i] = (e && *e) ? *e : &PL_sv_undef; 6229 0 6229
argv[i] = (e && *e) ? *e : &PL_sv_undef; 6229 0 6229
337 }
338 7589 r = pq_call_meth(aTHX_ sth, "execute", argv, (int)n, 1);
339 7589 ▶ if (argv) safefree(argv);
Branch: 1 branch
BranchTrueFalseTotal
if (argv) safefree(argv); 2743 4846 7589
340 7589 ▶ rows = r ? SvIV(r) : 0;
Branch: 1 branch
BranchTrueFalseTotal
rows = r ? SvIV(r) : 0; 7589 0 7589
341 7589 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 7589 0 7589
342 7589 return rows;
343 }
344  
345 /* One row, positionally, copied out of DBI's reused buffer. +1 or NULL. */
346 4629 static AV *pq_fetchrow(pTHX_ SV *sth) {
347 4629 SV *row = pq_call_meth(aTHX_ sth, "fetchrow_arrayref", NULL, 0, 1);
348 AV *out, *in;
349 SSize_t n, i;
350 4629 ▶ if (!row) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!row) return NULL; 0 4629 4629
351 4629 ▶ if (!SvROK(row) || SvTYPE(SvRV(row)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(row) || SvTYPE(SvRV(row)) != SVt_PVAV) { 3656 973 4629
if (!SvROK(row) || SvTYPE(SvRV(row)) != SVt_PVAV) { 0 3656 3656
352 973 SvREFCNT_dec(row);
353 973 return NULL;
354 }
355 3656 in = (AV *)SvRV(row);
356 3656 n = av_len(in) + 1;
357 3656 out = newAV();
358 3656 av_extend(out, n - 1);
359 20840 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 17184 3656 20840
360 17184 SV **e = av_fetch(in, i, 0);
361 17184 ▶ av_push(out, newSVsv((e && *e) ? *e : &PL_sv_undef));
Branch: 2 branches
BranchTrueFalseTotal
av_push(out, newSVsv((e && *e) ? *e : &PL_sv_undef)); 17184 0 17184
av_push(out, newSVsv((e && *e) ? *e : &PL_sv_undef)); 17184 0 17184
362 }
363 3656 SvREFCNT_dec(row);
364 3656 return out;
365 }
366  
367 /* prepare_cached + execute, discarding any result set. Rows affected. */
368 5076 static IV pq_do(pTHX_ SV *self, SV *sql, AV *bind) {
369 5076 SV *sth = pq_sth(aTHX_ self, sql);
370 5076 return pq_execute(aTHX_ sth, bind);
371 }
372  
373 1262 static IV pq_do_pv(pTHX_ SV *self, const char *sql) {
374 1262 return pq_do(aTHX_ self, sv_2mortal(newSVpv(sql, 0)), NULL);
375 }
376  
377 /* prepare_cached + execute + first row. +1 or NULL. Finishes the handle so
378 * a cached sth is not left Active with rows pending. */
379 1755 static AV *pq_selectrow(pTHX_ SV *self, SV *sql, AV *bind) {
380 1755 SV *sth = pq_sth(aTHX_ self, sql);
381 AV *row;
382 1755 (void)pq_execute(aTHX_ sth, bind);
383 1755 row = pq_fetchrow(aTHX_ sth);
384 1755 { SV *r = pq_call_meth(aTHX_ sth, "finish", NULL, 0, 1);
385 1755 ▶ if (r) SvREFCNT_dec(r); }
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); } 1755 0 1755
386 1755 return row;
387 }
388  
389 /* Element i of a fetched row, borrowed; &PL_sv_undef past the end. */
390 17180 static SV *pq_col(pTHX_ AV *row, SSize_t i) {
391 17180 ▶ SV **e = row ? av_fetch(row, i, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = row ? av_fetch(row, i, 0) : NULL; 17180 0 17180
392 17180 ▶ return (e && *e) ? *e : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? *e : &PL_sv_undef; 17180 0 17180
return (e && *e) ? *e : &PL_sv_undef; 17180 0 17180
393 }
394  
395 6258 static IV pq_col_iv(pTHX_ AV *row, SSize_t i) {
396 6258 SV *v = pq_col(aTHX_ row, i);
397 6258 ▶ return SvOK(v) ? SvIV(v) : 0;
Branch: 1 branch
BranchTrueFalseTotal
return SvOK(v) ? SvIV(v) : 0; 6258 0 6258
398 }
399  
400 /* The id of the row just inserted, when RETURNING was not available. */
401 0 static IV pq_last_insert_id(pTHX_ SV *self) {
402 0 SV *dbh = pq_dbh(aTHX_ self);
403 SV *argv[4], *r;
404 IV id;
405 0 argv[0] = &PL_sv_undef;
406 0 argv[1] = &PL_sv_undef;
407 0 argv[2] = &PL_sv_undef;
408 0 argv[3] = &PL_sv_undef;
409 0 r = pq_call_meth(aTHX_ dbh, "last_insert_id", argv, 4, 1);
410 0 ▶ id = (r && SvOK(r)) ? SvIV(r) : 0;
Branch: 2 branches
BranchTrueFalseTotal
id = (r && SvOK(r)) ? SvIV(r) : 0; 0 0 0
id = (r && SvOK(r)) ? SvIV(r) : 0; 0 0 0
411 0 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 0 0
412 0 return id;
413 }
414  
415 /* ---- bind vectors --------------------------------------------------------- */
416  
417 /* A mortal AV to accumulate binds into. The pq_bind_* helpers below all
418 * push a fresh SV, so nothing aliases a caller's value. */
419 4298 static AV *pq_binds(pTHX) {
420 4298 return (AV *)sv_2mortal((SV *)newAV());
421 }
422  
423 5516 static void pq_bind_sv(pTHX_ AV *b, SV *v) {
424 5516 ▶ av_push(b, newSVsv(v ? v : &PL_sv_undef));
Branch: 1 branch
BranchTrueFalseTotal
av_push(b, newSVsv(v ? v : &PL_sv_undef)); 5516 0 5516
425 5516 }
426  
427 951 static void pq_bind_pv(pTHX_ AV *b, const char *s) {
428 951 av_push(b, newSVpv(s, 0));
429 951 }
430  
431 5382 static void pq_bind_iv(pTHX_ AV *b, IV v) {
432 5382 av_push(b, newSViv(v));
433 5382 }
434  
435 3611 static void pq_bind_nv(pTHX_ AV *b, double v) {
436 3611 av_push(b, newSVnv(v));
437 3611 }
438  
439 2997 static void pq_bind_undef(pTHX_ AV *b) {
440 2997 av_push(b, newSV(0));
441 2997 }
442  
443 /* a numeric option off the backend's opts hash, with a default */
444 418 static double pq_opt_num(pTHX_ SV *self, const char *key, double dflt) {
445 418 HV *h = pq_hv(aTHX_ self, "Punk::Queue::Backend");
446 418 HV *o = pq_get_hv(aTHX_ h, "opts");
447 418 ▶ SV *v = o ? pq_get(aTHX_ o, key) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *v = o ? pq_get(aTHX_ o, key) : NULL; 418 0 418
448 418 ▶ return (v && SvOK(v)) ? SvNV(v) : dflt;
Branch: 2 branches
BranchTrueFalseTotal
return (v && SvOK(v)) ? SvNV(v) : dflt; 11 407 418
return (v && SvOK(v)) ? SvNV(v) : dflt; 11 0 11
449 }
450  
451 #endif /* PQ_DBI_H */
« xs/worker.xs ↑ Summary include/pq_frj.h »