xs/dbi.xs

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