| 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 |
|
| /* |
| 57 |
|
| # uncoverable branch true |
| 58 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 59 |
17 |
▶ | if (!first) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!first) return NULL; |
0 |
17 |
17 |
|
| 60 |
17 |
| SvGETMAGIC(first); |
| 61 |
17 |
▶ | if (!SvROK(first)) return first; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(first)) return first; |
9 |
8 |
17 |
|
| 62 |
|
| /* a statement handle in the statement's place: ask it, through magic, |
| 63 |
|
| * for what it prepared */ |
| 64 |
8 |
| argv[0] = sv_2mortal(newSVpvs("Statement")); |
| 65 |
8 |
| st = pcx_call_meth(aTHX_ first, "FETCH", argv, 1, 1); |
| 66 |
8 |
▶ | return st ? sv_2mortal(st) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| return st ? sv_2mortal(st) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
8 |
0 |
8 |
|
| 67 |
|
| } |
| 68 |
|
| |
| 69 |
|
| /* The CV of a DBI superclass method, resolved once per slot. Croaks rather |
| 70 |
|
| * than silently doing nothing: a missing DBI method means this subclass is |
| 71 |
|
| * wrong about what it wraps, and a statement that quietly did not run is far |
| 72 |
|
| * worse than one that said so. */ |
| 73 |
|
| static CV *PUNK_DBIOBS_CV[8]; |
| 74 |
|
| |
| 75 |
22 |
| static CV *punk_dbiobs_super(pTHX_ int slot, const char *name) { |
| 76 |
22 |
▶ | if (!PUNK_DBIOBS_CV[slot]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PUNK_DBIOBS_CV[slot]) { |
9 |
13 |
22 |
|
| 77 |
9 |
| CV *cv = get_cv(name, 0); |
| 78 |
|
| /* |
| 79 |
|
| # uncoverable branch true |
| 80 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 81 |
9 |
▶ | if (!cv) croak("Punk::DBI: %s is not there to wrap", name); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cv) croak("Punk::DBI: %s is not there to wrap", name); |
0 |
9 |
9 |
|
| 82 |
9 |
| PUNK_DBIOBS_CV[slot] = cv; |
| 83 |
|
| } |
| 84 |
22 |
| return PUNK_DBIOBS_CV[slot]; |
| 85 |
|
| } |
| 86 |
|
| |
| 87 |
|
| #endif /* PUNK_DBIOBS_H */ |