xs/dbi.xs

filter: Statementstmt 91.5% 248 / 271 (2 uncoverable) Branchbran 56.3% 139 / 252 (5 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 74.5% 387 / 523 (7 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/pk_abi_impl.h ↑ Summary include/punk/punk_ua.h »
1 MODULE = Punk PACKAGE = Punk::Model::DBI
2  
3 PROTOTYPES: DISABLE
4  
5 # The shipped model backend, in C (punk_dbi.h). Plain DBI, no ORM: the six
6 # contract methods build their SQL here and hand it to DBI, which is still
7 # where every round trip happens. Punk::Model::DBI.pm is the documentation.
8  
9 SV *
10 new(class, ...)
11 SV *class
12 CODE:
13 18 RETVAL = pdbi_build_self(aTHX_ class, &ST(0), items,
14 "Punk::Model::DBI");
15 OUTPUT:
16 RETVAL
17  
18 # The live per-worker handle for this backend's dsn, connected on first use
19 # and shared with every other backend on the same database.
20 SV *
21 dbh(self)
22 SV *self
23 CODE:
24 13 RETVAL = newSVsv(pdbi_dbh(aTHX_ self));
25 OUTPUT:
26 RETVAL
27  
28 # create/update read this; connecting is what detects it, so make sure a
29 # connection exists before the answer is believed.
30 IV
31 _returning(self)
32 SV *self
33 CODE:
34 {
35 SV *r;
36 2 (void)pdbi_dbh(aTHX_ self);
37 2 r = pdbi_get(aTHX_ pdbi_hv(aTHX_ self), "returning");
38 2 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 2 0 2
# uncoverable condition right count:1 class:ignore_covered_err 0 2 2
39 # uncoverable condition left count:2 class:ignore_covered_err
40 RETVAL = r ? SvIV(r) : 0;
41 }
42 OUTPUT:
43 RETVAL
44  
45 IV
46 _detect_returning(self, dbh)
47 SV *self
48 SV *dbh
49 CODE:
50 PERL_UNUSED_VAR(self);
51 1 ▶ RETVAL = pdbi_detect_returning(aTHX_ dbh);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = pdbi_detect_returning(aTHX_ dbh); 0 1 1
52 OUTPUT:
53 RETVAL
54  
55 SV *
56 _sth(self, sql)
57 SV *self
58 SV *sql
59 CODE:
60 2 RETVAL = newSVsv(pdbi_sth(aTHX_ self, sql));
61 OUTPUT:
62 RETVAL
63  
64 SV *
65 _qi(self, name)
66 SV *self
67 SV *name
68 CODE:
69 2 RETVAL = newSVsv(pdbi_qi(aTHX_ self, name));
70 OUTPUT:
71 RETVAL
72  
73 # ---- reads ------------------------------------------------------------------
74  
75 # get(%key) -> the row hashref, or undef.
76 SV *
77 get(self, ...)
78 SV *self
79 CODE:
80 {
81 HV *key;
82 22 AV *keys = pdbi_key_args(aTHX_ &ST(0), items, &key, "get", "Punk::Model::DBI");
83 22 AV *bind = (AV *)sv_2mortal((SV *)newAV());
84 22 HV *slot = pdbi_slot_for(aTHX_ self);
85 22 SV *sig = pdbi_sig(aTHX_ "get",
86 pdbi_get(aTHX_ pdbi_hv(aTHX_ self), "table"), keys);
87 22 SV *sql = pdbi_sql_cached(aTHX_ slot, sig, NULL);
88 SV *sth, *row;
89 22 pdbq_check_keys(aTHX_ self, keys, "Punk::Model::DBI", "get");
90  
91 /* the statement is the same for every get on these key columns, so it
92 * is built once and only the bind values change */
93 20 ▶ if (sql) pdbi_bind_keys(aTHX_ key, keys, bind);
Branch: 1 branch
BranchTrueFalseTotal
if (sql) pdbi_bind_keys(aTHX_ key, keys, bind); 14 6 20
94 else {
95 6 SV *dbh = pdbi_get(aTHX_ slot, "dbh");
96 6 SV *where = pdbi_where_eq_slot(aTHX_ slot, dbh, key, keys, bind);
97 6 SV *table = pdbi_get(aTHX_ pdbi_hv(aTHX_ self), "table");
98 6 SV *built = sv_2mortal(newSVpvs("SELECT * FROM "));
99 6 sv_catsv(built, pdbi_qi_slot(aTHX_ slot, dbh, table));
100 6 sv_catpvs(built, " WHERE ");
101 6 sv_catsv(built, where);
102 6 sv_catpvs(built, " LIMIT 1");
103 6 sql = pdbi_sql_cached(aTHX_ slot, sig, built);
104 }
105  
106 20 sth = pdbi_sth_dbh(aTHX_ pdbi_get(aTHX_ slot, "dbh"), sql);
107 20 pdbi_execute_sql(aTHX_ sth, bind, sql);
108 20 row = pdbi_meth0(aTHX_ sth, "fetchrow_hashref");
109 20 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 20 0 20
110 25 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 20 0 20
# uncoverable condition right count:1 class:ignore_covered_err 5 15 20
111 5 ▶ { SV *f = pdbi_meth0(aTHX_ sth, "finish"); if (f) SvREFCNT_dec(f); }
Branch: 1 branch
BranchTrueFalseTotal
{ SV *f = pdbi_meth0(aTHX_ sth, "finish"); if (f) SvREFCNT_dec(f); } 5 0 5
112 RETVAL = (row && SvOK(row)) ? row : (row ? (SvREFCNT_dec(row), newSV(0)) /* # uncoverable condition left count:1 class:ignore_covered_err */
113 : newSV(0)); /* # uncoverable statement class:ignore_covered_err */
114 }
115 OUTPUT:
116 RETVAL
117  
118 SV *
119 # uncoverable statement
120 # uncoverable subroutine
121 all(self)
122 SV *self
123 CODE:
124 {
125 SV *argv[2];
126 0 argv[0] = sv_2mortal(newRV_noinc((SV *)newHV())); /* # uncoverable statement */
127 0 argv[1] = sv_2mortal(newRV_noinc((SV *)newHV())); /* # uncoverable statement */
128 0 RETVAL = pcx_call_meth(aTHX_ self, "search", argv, 2, 1); /* # uncoverable statement */
129 0 ▶ # uncoverable statement
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable statement 0 0 0
130 # uncoverable branch true
131 # uncoverable branch false
132 # uncoverable condition left count:1
133 # uncoverable condition right count:1
134 if (!RETVAL) RETVAL = newSV(0);
135 }
136 OUTPUT:
137 RETVAL
138  
139 # search(\%filter, \%opts) -> { rows, has_more_data, next }
140 #
141 # The filter and its operators, the ordering and the keyset continuation are
142 # built in punk_dbq.h, shared with the async backend. LIMIT n+1 so one row
143 # past the limit answers "is there another page" without a second COUNT.
144 SV *
145 search(self, filter = &PL_sv_undef, opts = &PL_sv_undef)
146 SV *self
147 SV *filter
148 SV *opts
149 CODE:
150 {
151 228 HV *h = pdbi_hv(aTHX_ self);
152 228 ▶ HV *f = (SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *f = (SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV) 228 0 228
153 456 ▶ ? (HV *)SvRV(filter) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(filter) : NULL; 228 0 228
154 146 ▶ HV *o = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *o = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 146 0 146
155 374 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 146 82 228
156 228 SV *pk = pdbi_get(aTHX_ h, "primary");
157 228 SV *table = pdbi_get(aTHX_ h, "table");
158 228 SV *colsv = pdbi_get(aTHX_ h, "col");
159 228 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 228 0 228
# uncoverable condition right count:1 class:ignore_covered_err 228 0 228
160 228 # uncoverable condition right count:2 class:ignore_covered_err
161 228 HV *col = (colsv && SvROK(colsv)) ? (HV *)SvRV(colsv) : NULL;
162 228 HV *slot = pdbi_slot_for(aTHX_ self);
163 SV *dbh = pdbi_get(aTHX_ slot, "dbh");
164 AV *bind = (AV *)sv_2mortal((SV *)newAV());
165 AV *cols, *desc;
166 IV limit;
167 int pk_only;
168 228 SV *sql, *sth, *rows_sv;
169  
170 sql = pdbq_search_sql(aTHX_ slot, dbh, col, table, pk, f, o,
171 "Punk::Model::DBI", bind,
172 196 &limit, &cols, &desc, &pk_only);
173 196  
174 sth = pdbi_sth_dbh(aTHX_ dbh, sql);
175 pdbi_execute_sql(aTHX_ sth, bind, sql);
176 196 {
177 196 SV *argv[1];
178 argv[0] = sv_2mortal(newRV_noinc((SV *)newHV()));
179 196 ▶ rows_sv = pcx_call_meth(aTHX_ sth, "fetchall_arrayref", argv, 1, 1);
Branch: 1 branch
BranchTrueFalseTotal
rows_sv = pcx_call_meth(aTHX_ sth, "fetchall_arrayref", argv, 1, 1); 196 0 196
180 }
181 196 # uncoverable branch false class:ignore_covered_err
182 # uncoverable condition right count:1 class:ignore_covered_err
183 { SV *f2 = pdbi_meth0(aTHX_ sth, "finish"); if (f2) SvREFCNT_dec(f2); }
184  
185 RETVAL = pdbq_page(aTHX_ rows_sv, limit, cols, desc, pk_only);
186 }
187 OUTPUT:
188 RETVAL
189  
190 # count(\%filter) -> how many rows match. The same filter search takes, and
191 # the same validation; no page, no order, no token.
192 IV
193 count(self, filter = &PL_sv_undef)
194 SV *self
195 SV *filter
196 CODE:
197 {
198 11 HV *h = pdbi_hv(aTHX_ self);
199 9 ▶ HV *f = (SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *f = (SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV) 9 0 9
200 20 ▶ ? (HV *)SvRV(filter) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(filter) : NULL; 9 2 11
201 11 SV *table = pdbi_get(aTHX_ h, "table");
202 11 SV *colsv = pdbi_get(aTHX_ h, "col");
203 11 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 11 0 11
# uncoverable condition right count:1 class:ignore_covered_err 11 0 11
204 11 # uncoverable condition right count:2 class:ignore_covered_err
205 11 HV *col = (colsv && SvROK(colsv)) ? (HV *)SvRV(colsv) : NULL;
206 11 HV *slot = pdbi_slot_for(aTHX_ self);
207 11 SV *dbh = pdbi_get(aTHX_ slot, "dbh");
208 AV *bind = (AV *)sv_2mortal((SV *)newAV());
209 SV *sql = pdbq_count_sql(aTHX_ slot, dbh, col, table, f,
210 "Punk::Model::DBI", bind);
211 10 SV *sth, *row;
212 10  
213 10 sth = pdbi_sth_dbh(aTHX_ dbh, sql);
214 10 ▶ pdbi_execute_sql(aTHX_ sth, bind, sql);
Branch: 1 branch
BranchTrueFalseTotal
pdbi_execute_sql(aTHX_ sth, bind, sql); 10 0 10
215 10 row = pdbi_meth0(aTHX_ sth, "fetchrow_arrayref");
216 10 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 10 0 10
# uncoverable branch false class:ignore_covered_err 10 0 10
# uncoverable branch false class:ignore_covered_err 10 0 10
217 10 # uncoverable condition right count:1 class:ignore_covered_err
218 10 ▶ { SV *f2 = pdbi_meth0(aTHX_ sth, "finish"); if (f2) SvREFCNT_dec(f2); }
Branch: 3 branches
BranchTrueFalseTotal
{ SV *f2 = pdbi_meth0(aTHX_ sth, "finish"); if (f2) SvREFCNT_dec(f2); } 10 0 10
{ SV *f2 = pdbi_meth0(aTHX_ sth, "finish"); if (f2) SvREFCNT_dec(f2); } 10 0 10
{ SV *f2 = pdbi_meth0(aTHX_ sth, "finish"); if (f2) SvREFCNT_dec(f2); } 10 0 10
219 RETVAL = 0;
220 10 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 10 0 10
221 # uncoverable condition right count:1 class:ignore_covered_err
222 # uncoverable condition right count:2 class:ignore_covered_err
223 # uncoverable condition right count:3 class:ignore_covered_err
224 if (row && SvROK(row) && SvTYPE(SvRV(row)) == SVt_PVAV) {
225 SV **n = av_fetch((AV *)SvRV(row), 0, 0);
226 # uncoverable branch false
227 # uncoverable condition right count:1 class:ignore_covered_err
228 # uncoverable condition right count:2 class:ignore_covered_err
229 # uncoverable condition right count:3 class:ignore_covered_err
230 if (n && *n && SvOK(*n)) RETVAL = SvIV(*n);
231 }
232 # uncoverable branch false class:ignore_covered_err
233 # uncoverable condition right count:1 class:ignore_covered_err
234 if (row) SvREFCNT_dec(row);
235 }
236 OUTPUT:
237 RETVAL
238  
239 # ---- writes -----------------------------------------------------------------
240  
241 # create(\%data): insert the known columns and hand back the stored row -
242 # through RETURNING where the driver has it, otherwise re-fetched by key so
243 # server-side defaults still come back.
244 SV *
245 create(self, data)
246 SV *self
247 SV *data
248 CODE:
249 {
250 19 HV *h = pdbi_hv(aTHX_ self);
251 19 ▶ HV *d = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *d = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) 19 0 19
252 38 ▶ ? (HV *)SvRV(data) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(data) : NULL; 19 0 19
253 19 SV *colset = pdbi_get(aTHX_ h, "col");
254 19 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 19 0 19
# uncoverable condition right count:1 class:ignore_covered_err 19 0 19
255 19 # uncoverable condition right count:2 class:ignore_covered_err
256 19 HV *known = (colset && SvROK(colset)) ? (HV *)SvRV(colset) : NULL;
257 19 SV *table = pdbi_get(aTHX_ h, "table");
258 19 SV *pk = pdbi_get(aTHX_ h, "primary");
259 19 AV *all = pdbi_sorted_keys(aTHX_ d);
260 AV *cols = (AV *)sv_2mortal((SV *)newAV());
261 19 AV *bind = (AV *)sv_2mortal((SV *)newAV());
262 SV *sql, *cl, *ph, *sth;
263 52 ▶ SSize_t i, n = av_len(all) + 1;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = av_len(all) + 1; 33 19 52
264 33  
265 33 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 33 0 33
266 33 ▶ SV *k = *av_fetch(all, i, 0);
Branch: 2 branches
BranchTrueFalseTotal
SV *k = *av_fetch(all, i, 0); 33 0 33
SV *k = *av_fetch(all, i, 0); 1 32 33
267 32 ▶ HE *he = d ? hv_fetch_ent(d, k, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
HE *he = d ? hv_fetch_ent(d, k, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 32 0 32
HE *he = d ? hv_fetch_ent(d, k, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 0 32 32
268 32 if (!(known && hv_exists_ent(known, k, 0))) continue; /* # uncoverable condition left count:1 class:ignore_covered_err */
269 32 # uncoverable branch true
270 # uncoverable condition left count:1 class:ignore_covered_err
271 19 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 1 18 19
272 1 if (!(he && SvOK(HeVAL(he)))) continue; /* defined values only */
273 av_push(cols, newSVsv(k));
274 18 av_push(bind, newSVsv(HeVAL(he)));
275 18 }
276 18 if (av_len(cols) < 0)
277 50 ▶ croak("Punk::Model::DBI: create with no known columns");
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::Model::DBI: create with no known columns"); 32 18 50
278 32 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
14 18 32
279 32 cl = sv_2mortal(newSVpvs(""));
280 32 ph = sv_2mortal(newSVpvs(""));
281 n = av_len(cols) + 1;
282 18 for (i = 0; i < n; i++) {
283 18 if (i) { sv_catpvs(cl, ", "); sv_catpvs(ph, ", "); }
284 18 sv_catsv(cl, pdbi_qi(aTHX_ self, *av_fetch(cols, i, 0)));
285 18 sv_catpvs(ph, "?");
286 18 }
287 sql = sv_2mortal(newSVpvs("INSERT INTO "));
288 18 sv_catsv(sql, pdbi_qi(aTHX_ self, table));
289 sv_catpvs(sql, " ("); sv_catsv(sql, cl);
290 18 sv_catpvs(sql, ") VALUES ("); sv_catsv(sql, ph);
291 18 ▶ sv_catpvs(sql, ")");
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvs(sql, ")"); 18 0 18
sv_catpvs(sql, ")"); 18 0 18
292  
293 18 (void)pdbi_dbh(aTHX_ self); /* connect, so returning is set */
294 18 {
295 18 SV *r = pdbi_get(aTHX_ h, "returning");
296 18 # uncoverable branch false
297 18 # uncoverable condition right count:1 class:ignore_covered_err
298 18 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 18 0 18
299 18 ▶ if (r && SvIV(r)) {
Branch: 1 branch
BranchTrueFalseTotal
if (r && SvIV(r)) { 0 18 18
300 36 ▶ SV *row;
Branch: 2 branches
BranchTrueFalseTotal
SV *row; 18 0 18
SV *row; 0 0 0
301 18 sv_catpvs(sql, " RETURNING *");
302 sth = pdbi_sth(aTHX_ self, sql);
303 pdbi_execute_sql(aTHX_ sth, bind, sql);
304 row = pdbi_meth0(aTHX_ sth, "fetchrow_hashref");
305 0 { SV *f = pdbi_meth0(aTHX_ sth, "finish");
306 0 # uncoverable branch false
307 0 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 0 0 0
# uncoverable condition right count:1 class:ignore_covered_err 0 0 0
308 0 ▶ if (f) SvREFCNT_dec(f); }
Branch: 1 branch
BranchTrueFalseTotal
if (f) SvREFCNT_dec(f); } 0 0 0
309 RETVAL = (row && SvOK(row)) ? row
310 0 ▶ # uncoverable statement class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable statement class:ignore_covered_err 0 0 0
# uncoverable statement class:ignore_covered_err 0 0 0
311 # uncoverable condition left count:1
312 # uncoverable condition right count:1
313 0 : (row ? (SvREFCNT_dec(row), newSV(0)) : newSV(0));
314 0 goto done_create;
315 0 }
316 }
317 0 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
0 0 0
318 sth = pdbi_sth(aTHX_ self, sql); /* # uncoverable statement */
319 pdbi_execute_sql(aTHX_ sth, bind, sql); /* # uncoverable statement */
320 # uncoverable statement
321 0 # uncoverable branch true
322 0 # uncoverable branch false
323 0 ▶ # uncoverable condition left count:1
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 0 0 0
324 # uncoverable condition right count:1
325 # uncoverable condition left count:2
326 0 ▶ # uncoverable condition right count:2
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:2 0 0 0
327 18 if (pk && SvOK(pk)) {
328 # uncoverable statement
329 # uncoverable condition left count:1
330 # uncoverable condition right count:1
331 HE *he = d ? hv_fetch_ent(d, pk, 0, 0) : NULL;
332 SV *id;
333 # uncoverable statement
334 # uncoverable branch true
335 # uncoverable branch false
336 # uncoverable condition left count:1
337 # uncoverable condition right count:1
338 # uncoverable condition left count:2
339 # uncoverable condition right count:2
340 if (he && SvOK(HeVAL(he))) id = sv_2mortal(newSVsv(HeVAL(he)));
341 else {
342 SV *argv[4];
343 argv[0] = &PL_sv_undef; argv[1] = &PL_sv_undef; /* # uncoverable statement */
344 argv[2] = table; argv[3] = &PL_sv_undef; /* # uncoverable statement */
345 id = pcx_call_meth(aTHX_ pdbi_dbh(aTHX_ self), /* # uncoverable statement */
346 "last_insert_id", argv, 4, 1); /* # uncoverable statement */
347 # uncoverable statement
348 # uncoverable condition left count:1
349 # uncoverable condition right count:1
350 id = id ? sv_2mortal(id) : &PL_sv_undef;
351 }
352 {
353 SV *argv[2];
354 argv[0] = pk; argv[1] = id; /* # uncoverable statement */
355 RETVAL = pcx_call_meth(aTHX_ self, "get", argv, 2, 1); /* # uncoverable statement */
356 # uncoverable statement
357 # uncoverable branch true
358 # uncoverable branch false
359 # uncoverable condition left count:1
360 # uncoverable condition right count:1
361 if (!RETVAL) RETVAL = newSV(0);
362 }
363 }
364 # uncoverable statement
365 # uncoverable condition left count:1
366 # uncoverable condition right count:1
367 else RETVAL = d ? newRV_noinc((SV *)newHVhv(d)) : newSV(0);
368 done_create: ;
369 }
370 OUTPUT:
371 RETVAL
372  
373 # update(\%key_and_changes): the primary key names the row, the rest is what
374 # changes.
375 SV *
376 update(self, data)
377 SV *self
378 SV *data
379 CODE:
380 {
381 8 HV *h = pdbi_hv(aTHX_ self);
382 8 ▶ HV *d = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *d = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) 8 0 8
383 16 ▶ ? (HV *)SvRV(data) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(data) : NULL; 8 0 8
384 8 SV *colset = pdbi_get(aTHX_ h, "col");
385 8 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 8 0 8
# uncoverable condition right count:1 class:ignore_covered_err 8 0 8
386 8 # uncoverable condition right count:2 class:ignore_covered_err
387 8 HV *known = (colset && SvROK(colset)) ? (HV *)SvRV(colset) : NULL;
388 SV *table = pdbi_get(aTHX_ h, "table");
389 SV *pk = pdbi_get(aTHX_ h, "primary");
390 AV *all, *cols, *bind;
391 SV *sql, *set, *id, *sth;
392 HE *pke;
393 8 ▶ SSize_t i, n;
Branch: 2 branches
BranchTrueFalseTotal
SSize_t i, n; 8 0 8
SSize_t i, n; 0 8 8
394 0  
395 8 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 8 0 8
396 8 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 7 1 8
# uncoverable condition left count:1 class:ignore_covered_err 0 7 7
397 1 # uncoverable condition left count:2 class:ignore_covered_err
398 7 if (!(pk && SvOK(pk)))
399 croak("Punk::Model::DBI: update needs a primary key"); /* # uncoverable statement */
400 7 pke = d ? hv_fetch_ent(d, pk, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
401 7 if (!(pke && SvOK(HeVAL(pke)))) /* # uncoverable condition left count:2 class:ignore_covered_err */
402 7 croak("Punk::Model::DBI: update needs the primary key in the data");
403 7 id = sv_2mortal(newSVsv(HeVAL(pke)));
404 20 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
13 7 20
405 13 all = pdbi_sorted_keys(aTHX_ d);
406 cols = (AV *)sv_2mortal((SV *)newAV());
407 13 ▶ bind = (AV *)sv_2mortal((SV *)newAV());
Branch: 1 branch
BranchTrueFalseTotal
bind = (AV *)sv_2mortal((SV *)newAV()); 7 6 13
408 6 ▶ n = av_len(all) + 1;
Branch: 2 branches
BranchTrueFalseTotal
n = av_len(all) + 1; 6 0 6
n = av_len(all) + 1; 0 6 6
409 6 for (i = 0; i < n; i++) {
410 6 SV *k = *av_fetch(all, i, 0);
411 6 ▶ HE *he;
Branch: 1 branch
BranchTrueFalseTotal
HE *he; 6 0 6
412 if (sv_eq(k, pk)) continue;
413 7 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 1 6 7
414 1 # uncoverable condition left count:1 class:ignore_covered_err
415 # uncoverable condition left count:2 class:ignore_covered_err
416 6 if (!(known && hv_exists_ent(known, k, 0))) continue;
417 6 he = hv_fetch_ent(d, k, 0, 0);
418 12 ▶ av_push(cols, newSVsv(k));
Branch: 1 branch
BranchTrueFalseTotal
av_push(cols, newSVsv(k)); 6 6 12
419 6 ▶ 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 */ 0 6 6
420 6 }
421 6 if (av_len(cols) < 0)
422 croak("Punk::Model::DBI: update with no columns to change");
423 6  
424 6 set = sv_2mortal(newSVpvs(""));
425 6 n = av_len(cols) + 1;
426 6 for (i = 0; i < n; i++) {
427 6 # uncoverable branch true
428 6 # uncoverable condition left count:1 class:ignore_covered_err
429 if (i) sv_catpvs(set, ", ");
430 6 sv_catsv(set, pdbi_qi(aTHX_ self, *av_fetch(cols, i, 0)));
431 sv_catpvs(set, " = ?");
432 6 }
433 6 ▶ sql = sv_2mortal(newSVpvs("UPDATE "));
Branch: 2 branches
BranchTrueFalseTotal
sql = sv_2mortal(newSVpvs("UPDATE ")); 6 0 6
sql = sv_2mortal(newSVpvs("UPDATE ")); 6 0 6
434 sv_catsv(sql, pdbi_qi(aTHX_ self, table));
435 6 sv_catpvs(sql, " SET "); sv_catsv(sql, set);
436 6 sv_catpvs(sql, " WHERE "); sv_catsv(sql, pdbi_qi(aTHX_ self, pk));
437 6 sv_catpvs(sql, " = ?");
438 6 av_push(bind, newSVsv(id)); /* the key binds last */
439 6  
440 6 ▶ (void)pdbi_dbh(aTHX_ self);
Branch: 1 branch
BranchTrueFalseTotal
(void)pdbi_dbh(aTHX_ self); 6 0 6
441 6 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
{ 0 6 6
442 12 ▶ SV *r = pdbi_get(aTHX_ h, "returning");
Branch: 2 branches
BranchTrueFalseTotal
SV *r = pdbi_get(aTHX_ h, "returning"); 6 0 6
SV *r = pdbi_get(aTHX_ h, "returning"); 0 0 0
443 6 # uncoverable branch false
444 # uncoverable condition right count:1 class:ignore_covered_err
445 # uncoverable condition right count:2 class:ignore_covered_err
446 0 if (r && SvIV(r)) {
447 0 SV *row;
448 sv_catpvs(sql, " RETURNING *");
449 sth = pdbi_sth(aTHX_ self, sql);
450 0 pdbi_execute_sql(aTHX_ sth, bind, sql);
451 0 row = pdbi_meth0(aTHX_ sth, "fetchrow_hashref");
452 0 ▶ { SV *f = pdbi_meth0(aTHX_ sth, "finish");
Branch: 1 branch
BranchTrueFalseTotal
{ SV *f = pdbi_meth0(aTHX_ sth, "finish"); 0 0 0
453 # uncoverable branch false
454 6 # uncoverable condition right count:1 class:ignore_covered_err
455 if (f) SvREFCNT_dec(f); }
456 RETVAL = (row && SvOK(row)) ? row
457 # uncoverable statement class:ignore_covered_err
458 # uncoverable condition left count:1
459 # uncoverable condition right count:1
460 : (row ? (SvREFCNT_dec(row), newSV(0)) : newSV(0));
461 goto done_update;
462 }
463 }
464 sth = pdbi_sth(aTHX_ self, sql); /* # uncoverable statement */
465 pdbi_execute_sql(aTHX_ sth, bind, sql); /* # uncoverable statement */
466 {
467 SV *argv[2];
468 argv[0] = pk; argv[1] = id; /* # uncoverable statement */
469 RETVAL = pcx_call_meth(aTHX_ self, "get", argv, 2, 1); /* # uncoverable statement */
470 # uncoverable statement
471 # uncoverable branch true
472 # uncoverable branch false
473 # uncoverable condition left count:1
474 # uncoverable condition right count:1
475 if (!RETVAL) RETVAL = newSV(0);
476 }
477 done_update: ;
478 }
479 OUTPUT:
480 RETVAL
481  
482 # delete(%key) -> the affected row count.
483 IV
484 delete(self, ...)
485 SV *self
486 CODE:
487 {
488 HV *key;
489 5 AV *keys = pdbi_key_args(aTHX_ &ST(0), items, &key, "delete", "Punk::Model::DBI");
490 5 AV *bind = (AV *)sv_2mortal((SV *)newAV());
491 5 HV *slot = pdbi_slot_for(aTHX_ self);
492 5 SV *sig = pdbi_sig(aTHX_ "delete",
493 pdbi_get(aTHX_ pdbi_hv(aTHX_ self), "table"), keys);
494 5 SV *sql = pdbi_sql_cached(aTHX_ slot, sig, NULL);
495 SV *sth, *n;
496 5 pdbq_check_keys(aTHX_ self, keys, "Punk::Model::DBI", "delete");
497  
498 3 ▶ if (sql) pdbi_bind_keys(aTHX_ key, keys, bind);
Branch: 1 branch
BranchTrueFalseTotal
if (sql) pdbi_bind_keys(aTHX_ key, keys, bind); 1 2 3
499 else {
500 2 SV *dbh = pdbi_get(aTHX_ slot, "dbh");
501 2 SV *where = pdbi_where_eq_slot(aTHX_ slot, dbh, key, keys, bind);
502 2 SV *table = pdbi_get(aTHX_ pdbi_hv(aTHX_ self), "table");
503 2 SV *built = sv_2mortal(newSVpvs("DELETE FROM "));
504 2 sv_catsv(built, pdbi_qi_slot(aTHX_ slot, dbh, table));
505 2 sv_catpvs(built, " WHERE ");
506 2 sv_catsv(built, where);
507 2 sql = pdbi_sql_cached(aTHX_ slot, sig, built);
508 }
509  
510 3 sth = pdbi_sth_dbh(aTHX_ pdbi_get(aTHX_ slot, "dbh"), sql);
511 3 pdbi_execute_sql(aTHX_ sth, bind, sql);
512 3 n = pdbi_meth0(aTHX_ sth, "rows");
513 3 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 3 0 3
514 3 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 3 0 3
# uncoverable condition right count:1 class:ignore_covered_err 3 0 3
515 3 ▶ { SV *f = pdbi_meth0(aTHX_ sth, "finish"); if (f) SvREFCNT_dec(f); }
Branch: 1 branch
BranchTrueFalseTotal
{ SV *f = pdbi_meth0(aTHX_ sth, "finish"); if (f) SvREFCNT_dec(f); } 3 0 3
516 # uncoverable condition right count:1 class:ignore_covered_err
517 # uncoverable condition right count:2 class:ignore_covered_err
518 RETVAL = (n && SvOK(n)) ? SvIV(n) : 0;
519 # uncoverable branch false class:ignore_covered_err
520 # uncoverable condition right count:1 class:ignore_covered_err
521 if (n) SvREFCNT_dec(n);
522 }
523 OUTPUT:
524 RETVAL
525  
526 # ---- the opaque keyset token ------------------------------------------------
527  
528 SV *
529 _encode_token(self, val)
530 SV *self
531 SV *val
532 CODE:
533 PERL_UNUSED_VAR(self);
534 1 RETVAL = pdbi_encode_token(aTHX_ val);
535 OUTPUT:
536 RETVAL
537  
538 SV *
539 _decode_token(self, tok)
540 SV *self
541 SV *tok
542 CODE:
543 PERL_UNUSED_VAR(self);
544 1 RETVAL = pdbi_decode_token(aTHX_ tok, "Punk::Model::DBI");
545 OUTPUT:
546 RETVAL
547  
548 MODULE = Punk PACKAGE = Punk::DBI::db
549  
550 # The database-handle methods DBI implements in its own dispatch rather than
551 # through the public prepare/execute pair. A Callbacks hash cannot see these:
552 # they reach the inner execute without passing anything a consumer can
553 # register on, and a callback runs before the method, so there is no done side
554 # to report from. Wrapping is what gives both.
555 void
556 do(...)
557 ALIAS:
558 selectall_arrayref = 1
559 selectall_hashref = 2
560 selectcol_arrayref = 3
561 selectrow_array = 4
562 selectrow_arrayref = 5
563 selectrow_hashref = 6
564 PPCODE:
565 {
566 9 const punk_dbiobs_m *m = &PUNK_DBIOBS[ix];
567 9 void *tok = NULL;
568 9 ▶ I32 flags = ((GIMME_V == G_ARRAY) ? G_ARRAY : G_SCALAR) | G_EVAL; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
I32 flags = ((GIMME_V == G_ARRAY) ? G_ARRAY : G_SCALAR) | G_EVAL; /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 9 9
569 I32 count, i;
570 9 SV **out = NULL;
571 9 SV *err = NULL;
572 9 ▶ int nbind = (items > m->bind_from) ? (int)(items - m->bind_from) : 0;
Branch: 1 branch
BranchTrueFalseTotal
int nbind = (items > m->bind_from) ? (int)(items - m->bind_from) : 0; 5 4 9
573  
574 9 ▶ # uncoverable branch false
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false 9 0 9
# uncoverable branch false 9 0 9
575 18 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 9 0 9
576 9 # uncoverable condition right count:2 class:ignore_covered_err
577 if (!PUNK_DBIOBS_IN && PK_OBS_WANT_QUERY)
578 9 tok = pk_obs_query_start(aTHX_
579 9 punk_dbiobs_sql(aTHX_ items > 1 ? ST(1) : NULL), nbind);
580 9  
581 ENTER;
582 9 ▶ SAVEDESTRUCTOR_X(punk_dbiobs_leave, INT2PTR(void *, (IV)PUNK_DBIOBS_IN));
Branch: 1 branch
BranchTrueFalseTotal
SAVEDESTRUCTOR_X(punk_dbiobs_leave, INT2PTR(void *, (IV)PUNK_DBIOBS_IN)); 0 9 9
583 9 ▶ PUNK_DBIOBS_IN = 1;
Branch: 2 branches
BranchTrueFalseTotal
PUNK_DBIOBS_IN = 1; 9 0 9
PUNK_DBIOBS_IN = 1; 0 9 9
584 39 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
30 9 39
585 9 PUSHMARK(SP);
586 9 EXTEND(SP, items);
587 9 for (i = 0; i < items; i++) PUSHs(ST(i));
588 PUTBACK;
589 9 ▶ count = call_sv((SV *)punk_dbiobs_super(aTHX_ (int)ix, m->super), flags);
Branch: 1 branch
BranchTrueFalseTotal
count = call_sv((SV *)punk_dbiobs_super(aTHX_ (int)ix, m->super), flags); 9 0 9
590 9 SPAGAIN;
591 18 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
9 9 18
592 # uncoverable branch false
593 9 # uncoverable condition right count:1 class:ignore_covered_err
594 9 if (count > 0) {
595 Newx(out, count, SV *);
596 9 ▶ for (i = count - 1; i >= 0; i--) out[i] = sv_2mortal(SvREFCNT_inc(POPs));
Branch: 3 branches
BranchTrueFalseTotal
for (i = count - 1; i >= 0; i--) out[i] = sv_2mortal(SvREFCNT_inc(POPs)); 9 0 9
for (i = count - 1; i >= 0; i--) out[i] = sv_2mortal(SvREFCNT_inc(POPs)); 1 8 9
for (i = count - 1; i >= 0; i--) out[i] = sv_2mortal(SvREFCNT_inc(POPs)); 1 0 1
597 }
598 PUTBACK;
599 LEAVE; /* puts PUNK_DBIOBS_IN back */
600  
601 # uncoverable condition right count:1 class:ignore_covered_err
602 9 ▶ # uncoverable condition right count:3 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:3 class:ignore_covered_err 9 0 9
# uncoverable condition right count:3 class:ignore_covered_err 1 8 9
603 if (SvTRUE(ERRSV)) err = sv_2mortal(newSVsv(ERRSV));
604 9 ▶ 
Branch: 2 branches
BranchTrueFalseTotal
1 8 9
1 0 1
605 /* Success here is "it ran", not what it returned: these hand back
606 8 * data, and a query that legitimately matched no rows has not failed.
607 8 ▶ * One that went wrong raised, because RaiseError is this framework's
Branch: 2 branches
BranchTrueFalseTotal
* One that went wrong raised, because RaiseError is this framework's 8 0 8
* One that went wrong raised, because RaiseError is this framework's 0 8 8
608 16 ▶ * default, and that is the failure worth reporting. */
Branch: 1 branch
BranchTrueFalseTotal
* default, and that is the failure worth reporting. */ 8 8 16
609 8 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 8 0 8
610 8 # uncoverable condition right count:1 class:ignore_covered_err
611 8 if (tok) pk_obs_query_done(aTHX_ tok, err ? &PL_sv_no : &PL_sv_yes);
612  
613 if (err) { if (out) Safefree(out); croak_sv(err); } /* # uncoverable condition right count:2 class:ignore_covered_err */
614  
615 SP = PL_stack_base + ax - 1;
616 EXTEND(SP, count);
617 for (i = 0; i < count; i++) PUSHs(out[i]);
618 # uncoverable branch false
619 # uncoverable condition right count:1 class:ignore_covered_err
620 if (out) Safefree(out);
621 PUTBACK;
622 return;
623 }
624  
625 MODULE = Punk PACKAGE = Punk::DBI::st
626  
627 # The path Punk::Model::DBI's own generated methods take, through
628 # prepare_cached. `ok` is what DBI's execute returned, which is the contract
629 # pk_abi.h states for on_query: a false or absent value is a failure.
630 void
631 execute(...)
632 PPCODE:
633 {
634 13 void *tok = NULL;
635 I32 count, i;
636 13 SV **out = NULL;
637 13 SV *err = NULL, *r = NULL;
638  
639 /* Inside a Punk::DBI::db wrapper this is DBI's own inner execute and
640 * the statement was already reported at the altitude the caller asked
641 * for. */
642 13 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 8 5 13
# uncoverable condition right count:2 class:ignore_covered_err 8 0 8
# uncoverable condition right count:2 class:ignore_covered_err 8 0 8
643 8 # uncoverable condition right count:3 class:ignore_covered_err
644 8 if (!PUNK_DBIOBS_IN && PK_OBS_WANT_QUERY && items > 0)
645 tok = pk_obs_query_start(aTHX_
646 13 punk_dbiobs_sql(aTHX_ ST(0)), (int)(items - 1));
647 13  
648 13 ENTER;
649 SAVEDESTRUCTOR_X(punk_dbiobs_leave, INT2PTR(void *, (IV)PUNK_DBIOBS_IN));
650 13 ▶ PUNK_DBIOBS_IN = 1;
Branch: 1 branch
BranchTrueFalseTotal
PUNK_DBIOBS_IN = 1; 0 13 13
651 13 ▶ 
Branch: 2 branches
BranchTrueFalseTotal
13 0 13
0 13 13
652 43 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 30 13 43
653 13 EXTEND(SP, items);
654 13 for (i = 0; i < items; i++) PUSHs(ST(i));
655 PUTBACK;
656 13 count = call_sv((SV *)punk_dbiobs_super(aTHX_ 7, "DBI::st::execute"),
657 G_SCALAR | G_EVAL);
658 13 ▶ SPAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
SPAGAIN; 13 0 13
659 13  
660 26 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 13 13 26
661 13 # uncoverable condition right count:1 class:ignore_covered_err
662 if (count > 0) {
663 13 Newx(out, count, SV *);
664 13 for (i = count - 1; i >= 0; i--) out[i] = sv_2mortal(SvREFCNT_inc(POPs));
665 r = out[0];
666 13 ▶ }
Branch: 3 branches
BranchTrueFalseTotal
} 13 0 13
} 0 13 13
} 0 0 0
667 13 ▶ PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUTBACK; 8 5 13
PUTBACK; 8 0 8
668 LEAVE;
669 13 ▶ 
Branch: 2 branches
BranchTrueFalseTotal
0 13 13
0 0 0
670 # uncoverable branch true
671 13 # uncoverable condition right count:1 class:ignore_covered_err
672 13 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 13 0 13
# uncoverable condition left count:2 class:ignore_covered_err 0 13 13
673 26 ▶ # uncoverable condition left count:3 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:3 class:ignore_covered_err 13 13 26
674 13 ▶ # uncoverable condition right count:3 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:3 class:ignore_covered_err 13 0 13
675 13 if (SvTRUE(ERRSV)) err = sv_2mortal(newSVsv(ERRSV));
676 13 if (tok) pk_obs_query_done(aTHX_ tok, err ? &PL_sv_no : r); /* # uncoverable condition left count:2 class:ignore_covered_err */
677  
678 # uncoverable branch true
679 # uncoverable condition left count:1 class:ignore_covered_err
680 # uncoverable condition left count:2 class:ignore_covered_err
681 # uncoverable condition right count:2 class:ignore_covered_err
682 if (err) { if (out) Safefree(out); croak_sv(err); }
683  
684 SP = PL_stack_base + ax - 1;
685 EXTEND(SP, count);
686 for (i = 0; i < count; i++) PUSHs(out[i]);
687 # uncoverable branch false
688 # uncoverable condition right count:1 class:ignore_covered_err
689 if (out) Safefree(out);
690 PUTBACK;
691 return;
692 }
« include/punk/pk_abi_impl.h ↑ Summary include/punk/punk_ua.h »