include/punk/punk_dbiobs.h

filter: Statementstmt 100% 16 / 16 Branchbran 70.0% 7 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.4% 23 / 26 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/log.xs ↑ Summary include/punk/punk_entropy.h »
1 /* punk_dbiobs.h - Punk::DBI, the observed DBI handle.
2 *
3 * pk_abi's query observer used to fire from Punk::Model::DBI's six generated
4 * methods, which are a LAYER ABOVE the handle: an application reaching through
5 * to $model->backend->dbh for an OR, a UNION, a FOR UPDATE or an upsert ran
6 * statements no observer could see, and nothing said so. These wrappers sit on
7 * the handle instead, so every statement is seen however it was asked for.
8 *
9 * Included by Punk.xs after punk_obs.h (the registry) and punk_context.h.
10 */
11  
12 #ifndef PUNK_DBIOBS_H
13 #define PUNK_DBIOBS_H
14  
15 /* The database-handle methods DBI implements in its own dispatch rather than
16 * through the public prepare/execute pair, with where the bind values start
17 * counting from ST(0).
18 *
19 * The offsets are not all the same, and guessing one is how a bind count ends
20 * up off by one: selectall_hashref takes a key field between the statement and
21 * the attributes, and nothing else here does.
22 *
23 * ST(0) is the handle, ST(1) the statement, so `do($sql, \%attr, @bind)` has
24 * its binds from ST(3) on. */
25 typedef struct { const char *super; int bind_from; } punk_dbiobs_m;
26  
27 static const punk_dbiobs_m PUNK_DBIOBS[] = {
28 { "DBI::db::do", 3 },
29 { "DBI::db::selectall_arrayref", 3 },
30 { "DBI::db::selectall_hashref", 4 },
31 { "DBI::db::selectcol_arrayref", 3 },
32 { "DBI::db::selectrow_array", 3 },
33 { "DBI::db::selectrow_arrayref", 3 },
34 { "DBI::db::selectrow_hashref", 3 }
35 };
36  
37 /* ONE REPORT PER STATEMENT, AT THE OUTERMOST LEVEL.
38 *
39 * Whether DBI answers a select* or a do natively or falls back to prepare and
40 * execute is a per-driver, per-call detail: DBD::SQLite runs a bind-free `do`
41 * itself and takes the prepare/execute path the moment there is a placeholder.
42 * Without this the same statement is reported once or twice depending on
43 * whether it had binds, and every duration is double counted on the branch
44 * that did. The caller asked for selectall_arrayref; that is the statement. */
45 static int PUNK_DBIOBS_IN = 0;
46  
47 22 static void punk_dbiobs_leave(pTHX_ void *p) {
48 PERL_UNUSED_CONTEXT;
49 22 PUNK_DBIOBS_IN = PTR2IV(p);
50 22 }
51  
52 /* The statement text out of the first argument, which every one of these also
53 * accepts as an already-prepared handle. Borrowed, and mortal when fetched. */
54 17 static SV *punk_dbiobs_sql(pTHX_ SV *first) {
55 SV *argv[1], *st;
56 17 ▶ if (!first) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!first) return NULL; 0 17 17
57 17 SvGETMAGIC(first);
58 17 ▶ if (!SvROK(first)) return first;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(first)) return first; 9 8 17
59 /* a statement handle in the statement's place: ask it, through magic,
60 * for what it prepared */
61 8 argv[0] = sv_2mortal(newSVpvs("Statement"));
62 8 st = pcx_call_meth(aTHX_ first, "FETCH", argv, 1, 1);
63 8 ▶ return st ? sv_2mortal(st) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return st ? sv_2mortal(st) : NULL; 8 0 8
64 }
65  
66 /* The CV of a DBI superclass method, resolved once per slot. Croaks rather
67 * than silently doing nothing: a missing DBI method means this subclass is
68 * wrong about what it wraps, and a statement that quietly did not run is far
69 * worse than one that said so. */
70 static CV *PUNK_DBIOBS_CV[8];
71  
72 22 static CV *punk_dbiobs_super(pTHX_ int slot, const char *name) {
73 22 ▶ if (!PUNK_DBIOBS_CV[slot]) {
Branch: 1 branch
BranchTrueFalseTotal
if (!PUNK_DBIOBS_CV[slot]) { 9 13 22
74 9 CV *cv = get_cv(name, 0);
75 9 ▶ if (!cv) croak("Punk::DBI: %s is not there to wrap", name);
Branch: 1 branch
BranchTrueFalseTotal
if (!cv) croak("Punk::DBI: %s is not there to wrap", name); 0 9 9
76 9 PUNK_DBIOBS_CV[slot] = cv;
77 }
78 22 return PUNK_DBIOBS_CV[slot];
79 }
80  
81 #endif /* PUNK_DBIOBS_H */
« xs/log.xs ↑ Summary include/punk/punk_entropy.h »