include/punk/punk_dbq.h

filter: Statementstmt 98.0% 358 / 367 (2 uncoverable) Branchbran 78.8% 308 / 406 (18 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.9% 666 / 773 (20 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_sessionstore.h ↑ Summary include/punk/punk_mpstream.h »
1 /* punk_dbq.h - the query shapes both model backends share.
2 *
3 * The filter with its operators, the ordering, the keyset continuation and
4 * the token that carries it, and the count: built here, once, so an operator
5 * that works on Punk::Model::DBI works on Punk::Model::DBIx::Loop and a
6 * `next` token minted by either decodes on the other. The two backends
7 * differ in HOW a statement runs (a DBI call, or a future on the worker's
8 * loop), never in WHAT the statement says.
9 *
10 * Three rules hold everywhere below:
11 *
12 * - A column is validated against the model's declared fields and an
13 * operator against a fixed set, both before any SQL is built, so a
14 * filter hash that arrived in a request body is safe to hand straight
15 * to search: the only thing a client controls is bound values.
16 * - Identifiers are quoted through the connection's cache and values are
17 * bound, never interpolated. The ONE literal this file writes into SQL
18 * is `1=0` / `1=1`, for an `in` / `not_in` over an empty list.
19 * - Keys are sorted before they become SQL, so the same filter produces
20 * the same statement and prepare_cached sees one shape, not one per
21 * permutation.
22 *
23 * Include after punk_dbi.h (pdbi_qi_slot, pdbi_sorted_keys, the base64url
24 * codec, punk_frj) and before punk_model.h and punk_dbil.h. */
25  
26 #ifndef PUNK_DBQ_H
27 #define PUNK_DBQ_H
28  
29 /* ---- the operator set ------------------------------------------------------ */
30  
31 enum {
32 PDBQ_EQ = 0, PDBQ_NE, PDBQ_LT, PDBQ_LE, PDBQ_GT, PDBQ_GE,
33 PDBQ_IN, PDBQ_NOT_IN, PDBQ_LIKE, PDBQ_STARTS_WITH,
34 PDBQ_N_OPS
35 };
36  
37 static const struct { const char *name; STRLEN len; const char *sql; }
38 PDBQ_OPS[PDBQ_N_OPS] = {
39 { "=", 1, " = ?" },
40 { "!=", 2, " <> ?" },
41 { "<", 1, " < ?" },
42 { "<=", 2, " <= ?" },
43 { ">", 1, " > ?" },
44 { ">=", 2, " >= ?" },
45 { "in", 2, NULL },
46 { "not_in", 6, NULL },
47 { "like", 4, " LIKE ?" },
48 { "starts_with", 11, NULL },
49 };
50  
51 230 static int pdbq_op_index(const char *k, STRLEN kl) {
52 int i;
53 1491 ▶ for (i = 0; i < PDBQ_N_OPS; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PDBQ_N_OPS; i++) 1482 9 1491
54 1482 ▶ if (PDBQ_OPS[i].len == kl && memEQ(PDBQ_OPS[i].name, k, kl)) return i;
Branch: 2 branches
BranchTrueFalseTotal
if (PDBQ_OPS[i].len == kl && memEQ(PDBQ_OPS[i].name, k, kl)) return i; 546 936 1482
if (PDBQ_OPS[i].len == kl && memEQ(PDBQ_OPS[i].name, k, kl)) return i; 221 325 546
55 9 return -1;
56 }
57  
58 /* the operator list, for a croak */
59 16 static SV *pdbq_op_list(pTHX) {
60 16 SV *s = sv_2mortal(newSVpvs(""));
61 int i;
62 176 ▶ for (i = 0; i < PDBQ_N_OPS; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PDBQ_N_OPS; i++) { 160 16 176
63 160 ▶ if (i) sv_catpvs(s, ", ");
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpvs(s, ", "); 144 16 160
64 160 sv_catpv(s, PDBQ_OPS[i].name);
65 }
66 16 return s;
67 }
68  
69 /* ---- columns --------------------------------------------------------------- */
70  
71 /* the declared field names, comma-joined, for a croak */
72 25 static SV *pdbq_col_list(pTHX_ HV *col) {
73 25 AV *keys = pdbi_sorted_keys(aTHX_ col);
74 25 SV *s = sv_2mortal(newSVpvs(""));
75 25 SSize_t i, n = av_len(keys) + 1;
76 148 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 123 25 148
77 123 ▶ if (i) sv_catpvs(s, ", ");
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpvs(s, ", "); 98 25 123
78 123 sv_catsv(s, *av_fetch(keys, i, 0));
79 }
80 25 return s;
81 }
82  
83 /* A column named in a filter or an ordering must be one of the model's
84 * declared fields. A model that declares none has nothing to check against
85 * and anything goes - the same rule create and update apply when they keep
86 * only the known columns. */
87 802 static void pdbq_check_col(pTHX_ HV *col, SV *name, SV *table,
88 const char *cls, const char *what) {
89 802 ▶ if (!col || !HvUSEDKEYS(col)) return; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!col || !HvUSEDKEYS(col)) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ 751 51 802
if (!col || !HvUSEDKEYS(col)) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 751 751
if (!col || !HvUSEDKEYS(col)) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 751 751
90 751 ▶ if (hv_exists_ent(col, name, 0)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists_ent(col, name, 0)) return; 726 25 751
91 100 ▶ croak("%s: %s names '%s', which is not a field of %s (have: %s)",
Branch: 2 branches
BranchTrueFalseTotal
croak("%s: %s names '%s', which is not a field of %s (have: %s)", 25 0 25
croak("%s: %s names '%s', which is not a field of %s (have: %s)", 25 0 25
92 cls, what, SvPV_nolen(name),
93 table && SvOK(table) ? SvPV_nolen(table) : "the model",
94 SvPV_nolen(pdbq_col_list(aTHX_ col)));
95 }
96  
97 /* The key columns of a get or a delete, checked the way a filter's are. An
98 * unknown one is a caller's typo, and SQLite reads a quoted name it does not
99 * know as a string literal: the statement ran, matched nothing, and said
100 * nothing, where search would have named the mistake. */
101 102 static void pdbq_check_keys(pTHX_ SV *self, AV *keys, const char *cls,
102 const char *what) {
103 102 HV *h = pdbi_hv(aTHX_ self);
104 102 SV *col = pdbi_get(aTHX_ h, "col");
105 102 SSize_t i, n = av_len(keys) + 1;
106 /*
107 # uncoverable branch true
108 # uncoverable condition left count:1 class:ignore_covered_err
109 # uncoverable condition left count:2 class:ignore_covered_err
110 # uncoverable condition left count:3 class:ignore_covered_err */
111 102 ▶ if (!(col && SvROK(col) && SvTYPE(SvRV(col)) == SVt_PVHV)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(col && SvROK(col) && SvTYPE(SvRV(col)) == SVt_PVHV)) return; 102 0 102
if (!(col && SvROK(col) && SvTYPE(SvRV(col)) == SVt_PVHV)) return; 102 0 102
if (!(col && SvROK(col) && SvTYPE(SvRV(col)) == SVt_PVHV)) return; 0 102 102
112 195 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 106 89 195
113 106 pdbq_check_col(aTHX_ (HV *)SvRV(col), *av_fetch(keys, i, 0),
114 pdbi_get(aTHX_ h, "table"), cls, what);
115 }
116  
117 /* ---- one term of the filter ------------------------------------------------ */
118  
119 /* `\` `%` and `_` are LIKE's metacharacters; a prefix search escapes them so
120 * that "100%" means the four characters and not "starts with 100". Mortal. */
121 4 static SV *pdbq_like_prefix(pTHX_ SV *val) {
122 STRLEN vl, i;
123 4 const char *v = SvPV_const(val, vl);
124 4 SV *out = sv_2mortal(newSV(vl * 2 + 2));
125 4 SvPOK_on(out);
126 /*
127 # uncoverable branch true
128 # uncoverable condition left count:1 class:ignore_covered_err */
129 4 ▶ if (SvUTF8(val)) SvUTF8_on(out);
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(val)) SvUTF8_on(out); 0 4 4
130 16 ▶ for (i = 0; i < vl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < vl; i++) { 12 4 16
131 /*
132 # uncoverable condition left count:1 class:ignore_covered_err
133 # uncoverable condition left count:3 class:ignore_covered_err */
134 12 ▶ if (v[i] == '\\' || v[i] == '%' || v[i] == '_') sv_catpvn(out, "\\", 1);
Branch: 3 branches
BranchTrueFalseTotal
if (v[i] == '\\' || v[i] == '%' || v[i] == '_') sv_catpvn(out, "\\", 1); 12 0 12
if (v[i] == '\\' || v[i] == '%' || v[i] == '_') sv_catpvn(out, "\\", 1); 10 2 12
if (v[i] == '\\' || v[i] == '%' || v[i] == '_') sv_catpvn(out, "\\", 1); 0 10 10
135 12 sv_catpvn(out, v + i, 1);
136 }
137 4 sv_catpvs(out, "%");
138 4 return out;
139 }
140  
141 /* Append '<qcol> <op> ?' (or its IS NULL / IN (...) / 1=0 form) to `out`,
142 * pushing the bind values onto `bind`. */
143 131 static void pdbq_term(pTHX_ SV *out, SV *qcol, int op, SV *val, AV *bind,
144 const char *cls, SV *colname) {
145 131 ▶ int defined = val && SvOK(val); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
int defined = val && SvOK(val); /* # uncoverable condition right count:1 class:ignore_covered_err */ 131 0 131
int defined = val && SvOK(val); /* # uncoverable condition right count:1 class:ignore_covered_err */ 112 19 131
146 131 switch (op) { /* # uncoverable branch true count:5 */
147 37 case PDBQ_EQ:
148 case PDBQ_NE:
149 37 sv_catsv(out, qcol);
150 37 ▶ if (!defined) {
Branch: 1 branch
BranchTrueFalseTotal
if (!defined) { 14 23 37
151 14 ▶ sv_catpv(out, op == PDBQ_EQ ? " IS NULL" : " IS NOT NULL");
Branch: 1 branch
BranchTrueFalseTotal
sv_catpv(out, op == PDBQ_EQ ? " IS NULL" : " IS NOT NULL"); 2 12 14
152 14 return;
153 }
154 23 sv_catpv(out, PDBQ_OPS[op].sql);
155 23 av_push(bind, newSVsv(val));
156 23 return;
157 27 case PDBQ_LT: case PDBQ_LE: case PDBQ_GT: case PDBQ_GE:
158 case PDBQ_LIKE:
159 27 ▶ if (!defined)
Branch: 1 branch
BranchTrueFalseTotal
if (!defined) 4 23 27
160 4 croak("%s: '%s' on %s needs a value, not undef",
161 cls, PDBQ_OPS[op].name, SvPV_nolen(colname));
162 23 ▶ if (SvROK(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(val)) 2 21 23
163 4 croak("%s: '%s' on %s takes a plain value, not a %s",
164 cls, PDBQ_OPS[op].name, SvPV_nolen(colname),
165 sv_reftype(SvRV(val), 0));
166 21 sv_catsv(out, qcol);
167 21 sv_catpv(out, PDBQ_OPS[op].sql);
168 21 av_push(bind, newSVsv(val));
169 21 return;
170 6 case PDBQ_STARTS_WITH:
171 6 ▶ if (!defined)
Branch: 1 branch
BranchTrueFalseTotal
if (!defined) 1 5 6
172 1 croak("%s: 'starts_with' on %s needs a value, not undef",
173 cls, SvPV_nolen(colname));
174 5 ▶ if (SvROK(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(val)) 1 4 5
175 2 croak("%s: 'starts_with' on %s takes a plain value, not a %s",
176 cls, SvPV_nolen(colname), sv_reftype(SvRV(val), 0));
177 4 sv_catsv(out, qcol);
178 4 sv_catpvs(out, " LIKE ? ESCAPE '\\'");
179 4 av_push(bind, newSVsv(pdbq_like_prefix(aTHX_ val)));
180 4 return;
181 61 case PDBQ_IN:
182 case PDBQ_NOT_IN: {
183 AV *list;
184 SSize_t i, n;
185 /*
186 # uncoverable condition left count:1 class:ignore_covered_err
187 # uncoverable condition left count:3 class:ignore_covered_err */
188 61 ▶ if (!(defined && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(defined && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV)) 61 0 61
if (!(defined && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV)) 59 2 61
if (!(defined && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV)) 0 59 59
189 2 croak("%s: '%s' on %s takes an arrayref",
190 cls, PDBQ_OPS[op].name, SvPV_nolen(colname));
191 59 list = (AV *)SvRV(val);
192 59 n = av_len(list) + 1;
193 59 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 6 53 59
194 /* nothing is in an empty list, and everything is not in one:
195 * the answer the caller meant, rather than a syntax error */
196 6 ▶ sv_catpv(out, op == PDBQ_IN ? "1=0" : "1=1");
Branch: 1 branch
BranchTrueFalseTotal
sv_catpv(out, op == PDBQ_IN ? "1=0" : "1=1"); 4 2 6
197 6 return;
198 }
199 53 sv_catsv(out, qcol);
200 53 ▶ sv_catpv(out, op == PDBQ_IN ? " IN (" : " NOT IN (");
Branch: 1 branch
BranchTrueFalseTotal
sv_catpv(out, op == PDBQ_IN ? " IN (" : " NOT IN ("); 51 2 53
201 {
202 /* Past 16 the list is padded to the next power of two by
203 * repeating its last value, which changes neither IN nor NOT IN
204 * and keeps the number of distinct statement shapes - each a
205 * prepare_cached handle - logarithmic in the list length. */
206 53 SSize_t m = n;
207 85 ▶ if (m > 16) { m = 32; while (m < n) m *= 2; }
Branch: 2 branches
BranchTrueFalseTotal
if (m > 16) { m = 32; while (m < n) m *= 2; } 49 4 53
if (m > 16) { m = 32; while (m < n) m *= 2; } 32 49 81
208 2661 ▶ for (i = 0; i < m; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < m; i++) { 2608 53 2661
209 2608 ▶ SV **e = av_fetch(list, i < n ? i : n - 1, 0);
Branch: 1 branch
BranchTrueFalseTotal
SV **e = av_fetch(list, i < n ? i : n - 1, 0); 631 1977 2608
210 2608 ▶ if (i) sv_catpvs(out, ", ");
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpvs(out, ", "); 2555 53 2608
211 2608 sv_catpvs(out, "?");
212 /*
213 # uncoverable condition right count:1 class:ignore_covered_err
214 # uncoverable condition right count:2 class:ignore_covered_err */
215 2608 ▶ av_push(bind, (e && *e) ? newSVsv(*e) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(bind, (e && *e) ? newSVsv(*e) : newSV(0)); 2608 0 2608
av_push(bind, (e && *e) ? newSVsv(*e) : newSV(0)); 2608 0 2608
216 }
217 }
218 53 sv_catpvs(out, ")");
219 53 return;
220 }
221 0 default: /* # uncoverable statement */
222 0 croak("%s: unknown operator", cls); /* # uncoverable statement */
223 }
224 }
225  
226 /* ---- the filter ------------------------------------------------------------ */
227  
228 /* The WHERE body for a filter hash, or "" for an empty one. Mortal.
229 *
230 * { a => 1 } a = ?
231 * { a => undef } a IS NULL
232 * { a => { '>=' => 1, '<' => 9 } } a >= ? AND a < ? (operators sorted)
233 * { a => { in => [] } } 1=0
234 *
235 * Terms are AND-joined in sorted column order, operators sorted within a
236 * column, for one statement per shape. */
237 375 static SV *pdbq_where(pTHX_ HV *slot, SV *dbh, HV *col, SV *table,
238 HV *filter, AV *bind, const char *cls) {
239 375 SV *out = sv_2mortal(newSVpvs(""));
240 375 AV *keys = pdbi_sorted_keys(aTHX_ filter);
241 375 SSize_t i, n = av_len(keys) + 1;
242 375 int terms = 0;
243 494 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 141 353 494
244 141 SV *k = *av_fetch(keys, i, 0);
245 141 HE *he = hv_fetch_ent(filter, k, 0, 0);
246 141 ▶ SV *val = he ? HeVAL(he) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *val = he ? HeVAL(he) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 141 0 141
247 SV *qcol;
248 141 pdbq_check_col(aTHX_ col, k, table, cls, "the filter");
249 137 qcol = pdbi_qi_slot(aTHX_ slot, dbh, k);
250 137 ▶ if (val && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (val && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 137 0 137
if (val && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 114 23 137
if (val && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 111 3 114
251 111 HV *ops = (HV *)SvRV(val);
252 111 AV *onames = pdbi_sorted_keys(aTHX_ ops);
253 111 SSize_t j, m = av_len(onames) + 1;
254 111 ▶ if (m == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (m == 0) 2 109 111
255 6 ▶ croak("%s: the filter on %s is an empty hashref - it needs an "
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: the filter on %s is an empty hashref - it needs an " 2 0 2
256 "operator (%s)", cls, SvPV_nolen(k),
257 SvPV_nolen(pdbq_op_list(aTHX)));
258 207 ▶ for (j = 0; j < m; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) { 111 96 207
259 111 SV *on = *av_fetch(onames, j, 0);
260 111 STRLEN ol; const char *op = SvPV_const(on, ol);
261 111 int idx = pdbq_op_index(op, ol);
262 HE *oe;
263 111 ▶ if (idx < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) 3 108 111
264 9 ▶ croak("%s: unknown operator '%s' on %s (known: %s)",
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: unknown operator '%s' on %s (known: %s)", 3 0 3
265 cls, op, SvPV_nolen(k),
266 SvPV_nolen(pdbq_op_list(aTHX)));
267 108 oe = hv_fetch_ent(ops, on, 0, 0);
268 108 ▶ if (terms++) sv_catpvs(out, " AND ");
Branch: 1 branch
BranchTrueFalseTotal
if (terms++) sv_catpvs(out, " AND "); 6 102 108
269 108 ▶ pdbq_term(aTHX_ out, qcol, idx, oe ? HeVAL(oe) : NULL, /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
pdbq_term(aTHX_ out, qcol, idx, oe ? HeVAL(oe) : NULL, /* # uncoverable condition right count:1 class:ignore_covered_err */ 108 0 108
270 bind, cls, k);
271 }
272 96 continue;
273 }
274 26 ▶ if (val && SvROK(val)) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (val && SvROK(val)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 26 0 26
if (val && SvROK(val)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 23 26
275 6 ▶ croak("%s: the filter on %s is a %s - a list is spelled "
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: the filter on %s is a %s - a list is spelled " 0 3 3
276 "{ in => [...] }, and everything else is a plain value or "
277 "{ operator => value }", cls, SvPV_nolen(k),
278 SvTYPE(SvRV(val)) == SVt_PVAV ? "plain arrayref" /* # uncoverable condition left count:1 class:ignore_covered_err */
279 : sv_reftype(SvRV(val), 0)); /* # uncoverable statement */
280 /*
281 # uncoverable branch true
282 # uncoverable condition left count:1 class:ignore_covered_err */
283 23 ▶ if (terms++) sv_catpvs(out, " AND ");
Branch: 1 branch
BranchTrueFalseTotal
if (terms++) sv_catpvs(out, " AND "); 0 23 23
284 23 pdbq_term(aTHX_ out, qcol, PDBQ_EQ, val, bind, cls, k);
285 }
286 353 return out;
287 }
288  
289 /* ---- the ordering ---------------------------------------------------------- */
290  
291 /* Resolve `order_by` into parallel lists of column names and descending
292 * flags, with the primary key appended as the tie-breaker when it is not
293 * already named, in the direction of the last named column - so `created =>
294 * 'desc'` pages newest-first all the way down, ids included.
295 *
296 * undef -> (pk) asc
297 * 'created' -> (created, pk) asc, asc
298 * [ created => 'desc' ] -> (created, pk) desc, desc
299 * [ a => 'asc', b => 'desc' ] -> (a, b, pk) asc, desc, desc
300 *
301 * `pk_only` reports that nothing was asked for, which is what keeps the
302 * token in its original one-value shape for that case. */
303 539 static void pdbq_order(pTHX_ HV *col, SV *table, SV *pk, SV *opt,
304 const char *cls, AV **cols_out, AV **desc_out,
305 int *pk_only) {
306 539 AV *cols = (AV *)sv_2mortal((SV *)newAV());
307 539 AV *desc = (AV *)sv_2mortal((SV *)newAV());
308 539 int last_desc = 0, has_pk = 0;
309 SSize_t i, n;
310 539 *pk_only = 1;
311 539 ▶ if (opt && SvOK(opt)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (opt && SvOK(opt)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 229 310 539
if (opt && SvOK(opt)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 229 0 229
312 229 AV *pairs = NULL;
313 229 *pk_only = 0;
314 229 ▶ if (SvROK(opt) && SvTYPE(SvRV(opt)) == SVt_PVAV) pairs = (AV *)SvRV(opt);
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(opt) && SvTYPE(SvRV(opt)) == SVt_PVAV) pairs = (AV *)SvRV(opt); 212 17 229
if (SvROK(opt) && SvTYPE(SvRV(opt)) == SVt_PVAV) pairs = (AV *)SvRV(opt); 211 1 212
315 18 ▶ else if (SvROK(opt))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvROK(opt)) 1 17 18
316 1 croak("%s: order_by takes a column name or [ column => 'asc'|"
317 "'desc', ... ], not a %s", cls, sv_reftype(SvRV(opt), 0));
318 228 ▶ if (!pairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pairs) { 17 211 228
319 17 pdbq_check_col(aTHX_ col, opt, table, cls, "order_by");
320 15 av_push(cols, newSVsv(opt));
321 15 av_push(desc, newSViv(0));
322 }
323 else {
324 211 n = av_len(pairs) + 1;
325 211 ▶ if (n == 0 || (n & 1)) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (n == 0 || (n & 1)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 211 0 211
if (n == 0 || (n & 1)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 2 209 211
326 2 croak("%s: order_by takes [ column => 'asc'|'desc', ... ] "
327 "pairs", cls);
328 498 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 299 199 498
329 299 SV **cp = av_fetch(pairs, i, 0);
330 299 SV **dpp = av_fetch(pairs, i + 1, 0);
331 299 ▶ SV *c = (cp && *cp) ? *cp : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
SV *c = (cp && *cp) ? *cp : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 297 2 299
SV *c = (cp && *cp) ? *cp : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 297 0 297
332 299 ▶ SV *d = (dpp && *dpp) ? *dpp : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
SV *d = (dpp && *dpp) ? *dpp : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 297 2 299
SV *d = (dpp && *dpp) ? *dpp : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 297 0 297
333 STRLEN dl; const char *dp;
334 299 SSize_t j, have = av_len(cols) + 1;
335 int is_desc;
336 /* an array with a hole in it has no SV there at all */
337 299 ▶ if (!(c && SvOK(c)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(c && SvOK(c))) 297 2 299
if (!(c && SvOK(c))) 2 295 297
338 4 croak("%s: order_by takes [ column => 'asc'|'desc', ... ] "
339 "pairs, and one of the columns is undefined", cls);
340 295 pdbq_check_col(aTHX_ col, c, table, cls, "order_by");
341 588 ▶ dp = (d && SvOK(d)) ? SvPV_const(d, dl) : ""; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
dp = (d && SvOK(d)) ? SvPV_const(d, dl) : ""; /* # uncoverable condition right count:2 class:ignore_covered_err */ 293 2 295
dp = (d && SvOK(d)) ? SvPV_const(d, dl) : ""; /* # uncoverable condition right count:2 class:ignore_covered_err */ 293 0 293
342 295 ▶ if (!(d && SvOK(d))) dl = 0; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(d && SvOK(d))) dl = 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 293 2 295
if (!(d && SvOK(d))) dl = 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 293 293
343 /*
344 # uncoverable condition right count:2 class:ignore_covered_err
345 # uncoverable condition left count:3 class:ignore_covered_err
346 # uncoverable condition right count:3 class:ignore_covered_err */
347 295 ▶ if (dl == 3 && (memEQ(dp, "asc", 3) || memEQ(dp, "ASC", 3)))
Branch: 3 branches
BranchTrueFalseTotal
if (dl == 3 && (memEQ(dp, "asc", 3) || memEQ(dp, "ASC", 3))) 209 86 295
if (dl == 3 && (memEQ(dp, "asc", 3) || memEQ(dp, "ASC", 3))) 0 209 209
if (dl == 3 && (memEQ(dp, "asc", 3) || memEQ(dp, "ASC", 3))) 0 0 0
348 209 is_desc = 0;
349 86 ▶ else if (dl == 4 && (memEQ(dp, "desc", 4) || memEQ(dp, "DESC", 4)))
Branch: 3 branches
BranchTrueFalseTotal
else if (dl == 4 && (memEQ(dp, "desc", 4) || memEQ(dp, "DESC", 4))) 84 2 86
else if (dl == 4 && (memEQ(dp, "desc", 4) || memEQ(dp, "DESC", 4))) 4 80 84
else if (dl == 4 && (memEQ(dp, "desc", 4) || memEQ(dp, "DESC", 4))) 2 2 4
350 82 is_desc = 1;
351 else
352 4 croak("%s: order_by direction for %s must be 'asc' or "
353 "'desc', not '%s'", cls, SvPV_nolen(c), dp);
354 379 ▶ for (j = 0; j < have; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < have; j++) 90 289 379
355 90 ▶ if (sv_eq(*av_fetch(cols, j, 0), c))
Branch: 1 branch
BranchTrueFalseTotal
if (sv_eq(*av_fetch(cols, j, 0), c)) 2 88 90
356 2 croak("%s: order_by names %s twice", cls,
357 SvPV_nolen(c));
358 289 av_push(cols, newSVsv(c));
359 289 av_push(desc, newSViv(is_desc));
360 289 last_desc = is_desc;
361 }
362 }
363 }
364 524 ▶ if (pk && SvOK(pk)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (pk && SvOK(pk)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 524 0 524
if (pk && SvOK(pk)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 521 3 524
365 521 n = av_len(cols) + 1;
366 807 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 301 506 807
367 301 ▶ if (sv_eq(*av_fetch(cols, i, 0), pk)) { has_pk = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (sv_eq(*av_fetch(cols, i, 0), pk)) { has_pk = 1; break; } 15 286 301
368 521 ▶ if (!has_pk) {
Branch: 1 branch
BranchTrueFalseTotal
if (!has_pk) { 506 15 521
369 506 av_push(cols, newSVsv(pk));
370 506 av_push(desc, newSViv(last_desc));
371 }
372 }
373 524 *cols_out = cols;
374 524 *desc_out = desc;
375 524 }
376  
377 /* 'ORDER BY a DESC, id DESC' - "" when there is nothing to order by. Mortal. */
378 313 static SV *pdbq_order_sql(pTHX_ HV *slot, SV *dbh, AV *cols, AV *desc) {
379 313 SV *out = sv_2mortal(newSVpvs(""));
380 313 SSize_t i, n = av_len(cols) + 1;
381 867 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 554 313 867
382 554 SV *d = *av_fetch(desc, i, 0);
383 554 ▶ sv_catpv(out, i ? ", " : " ORDER BY ");
Branch: 1 branch
BranchTrueFalseTotal
sv_catpv(out, i ? ", " : " ORDER BY "); 241 313 554
384 554 sv_catsv(out, pdbi_qi_slot(aTHX_ slot, dbh, *av_fetch(cols, i, 0)));
385 554 ▶ sv_catpv(out, SvIV(d) ? " DESC" : " ASC");
Branch: 1 branch
BranchTrueFalseTotal
sv_catpv(out, SvIV(d) ? " DESC" : " ASC"); 119 435 554
386 }
387 313 return out;
388 }
389  
390 /* ---- the keyset continuation ----------------------------------------------- */
391  
392 /* The rows after a given (a, b, id) under an ordering, as the expanded
393 * comparison every driver takes:
394 *
395 * (a < ?) OR (a = ? AND b > ?) OR (a = ? AND b = ? AND id > ?)
396 *
397 * each column compared in its own direction, which is what lets a mixed
398 * ordering page correctly - the row-value form (a, b) < (?, ?) cannot.
399 *
400 * A NULL in a sort column needs its own branches, because `a > NULL` and
401 * `a = NULL` are never true: a page that ended on a row with a NULL there
402 * used to mint a token nothing came after, and every later row was
403 * unreachable. Where the NULLs sort is the database's choice (pdbq_nulls),
404 * so the branches depend on it:
405 *
406 * the token holds NULL, NULLs come first: after = a IS NOT NULL
407 * the token holds NULL, NULLs come last: after = nothing
408 * a value, NULLs come last: after = a > ? OR a IS NULL
409 * a value, NULLs come first: after = a > ?
410 *
411 * and "equal" is `a IS NULL` for a NULL. Mortal; pushes the binds in order. */
412  
413 enum { PDBQ_NULLS_UNKNOWN = 0, PDBQ_NULLS_LOW = 1, PDBQ_NULLS_HIGH = 2 };
414  
415 /* Does this database sort NULL below every value or above? SQLite, MySQL and
416 * MariaDB put it below; PostgreSQL and Oracle above. Asked once per
417 * connection. Anything else is unknown, and a continuation that would have
418 * to guess croaks instead. */
419 24 static int pdbq_nulls(pTHX_ HV *slot, SV *dbh) {
420 24 ▶ SV *have = slot ? pdbi_get(aTHX_ slot, "nulls") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *have = slot ? pdbi_get(aTHX_ slot, "nulls") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 24 0 24
421 SV *drv, *name;
422 24 int r = PDBQ_NULLS_UNKNOWN;
423 24 ▶ if (have && SvOK(have)) return (int)SvIV(have); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (have && SvOK(have)) return (int)SvIV(have); /* # uncoverable condition right count:2 class:ignore_covered_err */ 22 2 24
if (have && SvOK(have)) return (int)SvIV(have); /* # uncoverable condition right count:2 class:ignore_covered_err */ 22 0 22
424 2 drv = pdbi_attr(aTHX_ dbh, "Driver");
425 2 ▶ name = drv ? pdbi_attr(aTHX_ drv, "Name") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
name = drv ? pdbi_attr(aTHX_ drv, "Name") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
426 /*
427 # uncoverable branch false
428 # uncoverable condition right count:1 class:ignore_covered_err
429 # uncoverable condition right count:2 class:ignore_covered_err */
430 2 ▶ if (name && SvOK(name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (name && SvOK(name)) { 2 0 2
if (name && SvOK(name)) { 2 0 2
431 STRLEN nl;
432 2 const char *n = SvPV_const(name, nl);
433 /*
434 # uncoverable branch false
435 # uncoverable condition right count:1 class:ignore_covered_err
436 # uncoverable condition right count:2 class:ignore_covered_err
437 # uncoverable condition left count:3 class:ignore_covered_err
438 # uncoverable condition right count:3 class:ignore_covered_err
439 # uncoverable condition left count:4 class:ignore_covered_err
440 # uncoverable condition right count:4 class:ignore_covered_err
441 # uncoverable condition left count:5 class:ignore_covered_err
442 # uncoverable condition right count:5 class:ignore_covered_err
443 # uncoverable condition left count:6 class:ignore_covered_err
444 # uncoverable condition right count:6 class:ignore_covered_err */
445 2 ▶ if ((nl == 6 && memEQ(n, "SQLite", 6)) || (nl == 5 && memEQ(n, "mysql", 5))
Branch: 4 branches
BranchTrueFalseTotal
if ((nl == 6 && memEQ(n, "SQLite", 6)) || (nl == 5 && memEQ(n, "mysql", 5)) 2 0 2
if ((nl == 6 && memEQ(n, "SQLite", 6)) || (nl == 5 && memEQ(n, "mysql", 5)) 0 2 2
if ((nl == 6 && memEQ(n, "SQLite", 6)) || (nl == 5 && memEQ(n, "mysql", 5)) 0 0 0
if ((nl == 6 && memEQ(n, "SQLite", 6)) || (nl == 5 && memEQ(n, "mysql", 5)) 0 0 0
446 0 ▶ || (nl == 7 && memEQ(n, "MariaDB", 7))) /* # uncoverable statement */
Branch: 2 branches
BranchTrueFalseTotal
|| (nl == 7 && memEQ(n, "MariaDB", 7))) /* # uncoverable statement */ 0 0 0
|| (nl == 7 && memEQ(n, "MariaDB", 7))) /* # uncoverable statement */ 0 0 0
447 2 r = PDBQ_NULLS_LOW;
448 /*
449 # uncoverable statement
450 # uncoverable branch true
451 # uncoverable branch false
452 # uncoverable condition left count:1
453 # uncoverable condition right count:1
454 # uncoverable condition left count:2
455 # uncoverable condition right count:2
456 # uncoverable condition left count:3
457 # uncoverable condition right count:3
458 # uncoverable condition left count:4
459 # uncoverable condition right count:4 */
460 0 ▶ else if ((nl == 2 && memEQ(n, "Pg", 2)) || (nl == 6 && memEQ(n, "Oracle", 6)))
Branch: 4 branches
BranchTrueFalseTotal
else if ((nl == 2 && memEQ(n, "Pg", 2)) || (nl == 6 && memEQ(n, "Oracle", 6))) 0 0 0
else if ((nl == 2 && memEQ(n, "Pg", 2)) || (nl == 6 && memEQ(n, "Oracle", 6))) 0 0 0
else if ((nl == 2 && memEQ(n, "Pg", 2)) || (nl == 6 && memEQ(n, "Oracle", 6))) 0 0 0
else if ((nl == 2 && memEQ(n, "Pg", 2)) || (nl == 6 && memEQ(n, "Oracle", 6))) 0 0 0
461 0 r = PDBQ_NULLS_HIGH; /* # uncoverable statement */
462 }
463 /*
464 # uncoverable branch false
465 # uncoverable condition right count:1 class:ignore_covered_err */
466 2 ▶ if (slot) (void)hv_stores(slot, "nulls", newSViv(r));
Branch: 1 branch
BranchTrueFalseTotal
if (slot) (void)hv_stores(slot, "nulls", newSViv(r)); 2 0 2
467 2 return r;
468 }
469  
470 116 static SV *pdbq_keyset(pTHX_ HV *slot, SV *dbh, AV *cols, AV *desc,
471 AV *vals, AV *bind, const char *cls) {
472 116 SV *out = sv_2mortal(newSVpvs("("));
473 116 SSize_t i, j, n = av_len(cols) + 1;
474 116 int nulls = PDBQ_NULLS_UNKNOWN, any_null = 0, terms = 0;
475 390 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 274 116 390
476 274 ▶ if (!SvOK(*av_fetch(vals, i, 0))) any_null = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(*av_fetch(vals, i, 0))) any_null = 1; 30 244 274
477 116 ▶ if (any_null) {
Branch: 1 branch
BranchTrueFalseTotal
if (any_null) { 24 92 116
478 24 nulls = pdbq_nulls(aTHX_ slot, dbh);
479 /*
480 # uncoverable branch true
481 # uncoverable condition left count:1 class:ignore_covered_err */
482 24 ▶ if (nulls == PDBQ_NULLS_UNKNOWN)
Branch: 1 branch
BranchTrueFalseTotal
if (nulls == PDBQ_NULLS_UNKNOWN) 0 24 24
483 0 croak("%s: the page ended on a row with a NULL in an order_by " /* # uncoverable statement */
484 "column, and this driver's NULL ordering is not known, so "
485 "the next page cannot be found - order by a column that "
486 "is never NULL", cls); /* # uncoverable statement */
487 }
488 390 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 274 116 390
489 274 SV *v = *av_fetch(vals, i, 0);
490 274 int is_desc = SvIV(*av_fetch(desc, i, 0)) ? 1 : 0;
491 /* NULLs first under this column's direction? Only asked when some
492 * token value is NULL; without one the old shape is the right one
493 * on every driver, and rows with a NULL keep falling out as the POD
494 * says they do. */
495 274 ▶ int null_first = (nulls == PDBQ_NULLS_LOW) ? !is_desc : is_desc;
Branch: 1 branch
BranchTrueFalseTotal
int null_first = (nulls == PDBQ_NULLS_LOW) ? !is_desc : is_desc; 62 212 274
496 274 ▶ if (!SvOK(v) && !null_first) continue; /* nothing sorts after */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(v) && !null_first) continue; /* nothing sorts after */ 30 244 274
if (!SvOK(v) && !null_first) continue; /* nothing sorts after */ 6 24 30
497 268 ▶ if (terms++) sv_catpvs(out, " OR ");
Branch: 1 branch
BranchTrueFalseTotal
if (terms++) sv_catpvs(out, " OR "); 152 116 268
498 268 sv_catpvs(out, "(");
499 470 ▶ for (j = 0; j < i; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < i; j++) { 202 268 470
500 202 SV *pv = *av_fetch(vals, j, 0);
501 202 sv_catsv(out, pdbi_qi_slot(aTHX_ slot, dbh, *av_fetch(cols, j, 0)));
502 202 ▶ if (SvOK(pv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(pv)) { 168 34 202
503 168 sv_catpvs(out, " = ? AND ");
504 168 av_push(bind, newSVsv(pv));
505 }
506 34 else sv_catpvs(out, " IS NULL AND ");
507 }
508 268 ▶ if (!SvOK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(v)) { 24 244 268
509 24 sv_catsv(out, pdbi_qi_slot(aTHX_ slot, dbh, *av_fetch(cols, i, 0)));
510 24 sv_catpvs(out, " IS NOT NULL");
511 }
512 254 ▶ else if (any_null && !null_first) {
Branch: 2 branches
BranchTrueFalseTotal
else if (any_null && !null_first) { 32 212 244
else if (any_null && !null_first) { 10 22 32
513 10 SV *q = pdbi_qi_slot(aTHX_ slot, dbh, *av_fetch(cols, i, 0));
514 10 sv_catpvs(out, "(");
515 10 sv_catsv(out, q);
516 10 ▶ sv_catpv(out, is_desc ? " < ? OR " : " > ? OR "); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
sv_catpv(out, is_desc ? " < ? OR " : " > ? OR "); /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
517 10 sv_catsv(out, q);
518 10 sv_catpvs(out, " IS NULL)");
519 10 av_push(bind, newSVsv(v));
520 }
521 else {
522 234 sv_catsv(out, pdbi_qi_slot(aTHX_ slot, dbh, *av_fetch(cols, i, 0)));
523 234 ▶ sv_catpv(out, is_desc ? " < ?" : " > ?");
Branch: 1 branch
BranchTrueFalseTotal
sv_catpv(out, is_desc ? " < ?" : " > ?"); 52 182 234
524 234 av_push(bind, newSVsv(v));
525 }
526 268 sv_catpvs(out, ")");
527 }
528 /*
529 # uncoverable branch true
530 # uncoverable condition left count:1 class:ignore_covered_err */
531 116 ▶ if (!terms) sv_catpvs(out, "1=0");
Branch: 1 branch
BranchTrueFalseTotal
if (!terms) sv_catpvs(out, "1=0"); 0 116 116
532 116 sv_catpvs(out, ")");
533 116 return out;
534 }
535  
536 /* ---- the token --------------------------------------------------------------
537 *
538 * Without an ordering the token is what it always was - base64url of
539 * {"k":<pk>} - so a URL minted before this existed still pages. With one it
540 * is {"s":"created:d,id:d","v":[...]}: the ordering's signature and the last
541 * row's values under it. A token presented against a different ordering is
542 * refused, because the values would be compared against the wrong columns
543 * and the page would be wrong rather than an error. */
544  
545 246 static SV *pdbq_order_sig(pTHX_ AV *cols, AV *desc) {
546 246 SV *s = sv_2mortal(newSVpvs(""));
547 246 SSize_t i, n = av_len(cols) + 1;
548 832 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 586 246 832
549 586 ▶ if (i) sv_catpvs(s, ",");
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpvs(s, ","); 340 246 586
550 586 sv_catsv(s, *av_fetch(cols, i, 0));
551 586 ▶ sv_catpv(s, SvIV(*av_fetch(desc, i, 0)) ? ":d" : ":a");
Branch: 1 branch
BranchTrueFalseTotal
sv_catpv(s, SvIV(*av_fetch(desc, i, 0)) ? ":d" : ":a"); 140 446 586
552 }
553 246 return s;
554 }
555  
556 /* The `next` token for the last row of a page, or undef when the row lacks
557 * a value the ordering needs. Owned by the caller. */
558 145 static SV *pdbq_next_token(pTHX_ AV *cols, AV *desc, int pk_only, HV *last) {
559 145 SSize_t i, n = av_len(cols) + 1;
560 145 ▶ if (pk_only) {
Branch: 1 branch
BranchTrueFalseTotal
if (pk_only) { 23 122 145
561 23 ▶ HE *he = n ? hv_fetch_ent(last, *av_fetch(cols, 0, 0), 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
HE *he = n ? hv_fetch_ent(last, *av_fetch(cols, 0, 0), 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 23 0 23
562 23 ▶ return he ? pdbi_encode_token(aTHX_ HeVAL(he)) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return he ? pdbi_encode_token(aTHX_ HeVAL(he)) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */ 23 0 23
563 }
564 {
565 122 HV *w = newHV();
566 122 AV *v = newAV();
567 SV *json, *tok;
568 STRLEN jl; const char *jp;
569 414 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 292 122 414
570 292 HE *he = hv_fetch_ent(last, *av_fetch(cols, i, 0), 0, 0);
571 /*
572 # uncoverable branch true
573 # uncoverable condition left count:1 class:ignore_covered_err */
574 292 ▶ if (!he) { SvREFCNT_dec((SV *)w); SvREFCNT_dec((SV *)v); return newSV(0); }
Branch: 1 branch
BranchTrueFalseTotal
if (!he) { SvREFCNT_dec((SV *)w); SvREFCNT_dec((SV *)v); return newSV(0); } 0 292 292
575 292 av_push(v, newSVsv(HeVAL(he)));
576 }
577 122 (void)hv_stores(w, "s", newSVsv(pdbq_order_sig(aTHX_ cols, desc)));
578 122 (void)hv_stores(w, "v", newRV_noinc((SV *)v));
579 122 json = punk_frj(aTHX)->encode(aTHX_ sv_2mortal(newRV_noinc((SV *)w)), NULL);
580 122 jp = SvPV_const(json, jl);
581 122 tok = pdbi_b64u_encode(aTHX_ (const unsigned char *)jp, jl);
582 122 SvREFCNT_dec(json);
583 122 return tok;
584 }
585 }
586  
587 /* The decoded token as the values to continue from, one per ordering
588 * column. Mortal AV. Croaks (naming the backend) on a token that is not one
589 * or was issued under another ordering. */
590 150 static AV *pdbq_decode_token(pTHX_ SV *tok, AV *cols, AV *desc, int pk_only,
591 const char *cls) {
592 STRLEN tl;
593 300 ▶ const char *tp = SvOK(tok) ? SvPV_const(tok, tl) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
const char *tp = SvOK(tok) ? SvPV_const(tok, tl) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ 150 0 150
594 SV *json, *doc;
595 150 AV *out = (AV *)sv_2mortal((SV *)newAV());
596 HV *d;
597 /*
598 # uncoverable branch true
599 # uncoverable condition left count:1 class:ignore_covered_err */
600 150 ▶ if (!SvOK(tok)) tl = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(tok)) tl = 0; 0 150 150
601 150 json = pdbi_b64u_decode(aTHX_ tp, tl);
602 150 ▶ if (!json) croak("%s: invalid pagination token", cls);
Branch: 1 branch
BranchTrueFalseTotal
if (!json) croak("%s: invalid pagination token", cls); 9 141 150
603 141 sv_2mortal(json);
604 {
605 141 dSP;
606 int count;
607 141 ENTER; SAVETMPS;
608 141 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(json); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(json); PUTBACK; 0 141 141
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(json); PUTBACK; 0 141 141
609 141 count = call_pv("File::Raw::JSON::file_json_decode",
610 G_SCALAR | G_EVAL);
611 141 SPAGAIN;
612 141 ▶ doc = (count > 0) ? SvREFCNT_inc(POPs) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
doc = (count > 0) ? SvREFCNT_inc(POPs) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 141 0 141
613 141 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 141 0 141
614 /*
615 # uncoverable branch true
616 # uncoverable condition right count:1 class:ignore_covered_err
617 # uncoverable condition left count:2 class:ignore_covered_err */
618 141 ▶ if (SvTRUE(ERRSV)) { SvREFCNT_dec(doc); doc = NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { SvREFCNT_dec(doc); doc = NULL; } 141 0 141
if (SvTRUE(ERRSV)) { SvREFCNT_dec(doc); doc = NULL; } 0 141 141
619 }
620 141 ▶ if (!(doc && SvROK(doc) && SvTYPE(SvRV(doc)) == SVt_PVHV)) { /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!(doc && SvROK(doc) && SvTYPE(SvRV(doc)) == SVt_PVHV)) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 141 0 141
if (!(doc && SvROK(doc) && SvTYPE(SvRV(doc)) == SVt_PVHV)) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 140 1 141
if (!(doc && SvROK(doc) && SvTYPE(SvRV(doc)) == SVt_PVHV)) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 139 140
621 /*
622 # uncoverable branch false
623 # uncoverable condition right count:1 class:ignore_covered_err */
624 2 ▶ if (doc) SvREFCNT_dec(doc);
Branch: 1 branch
BranchTrueFalseTotal
if (doc) SvREFCNT_dec(doc); 2 0 2
625 2 croak("%s: invalid pagination token", cls);
626 }
627 139 sv_2mortal(doc);
628 139 d = (HV *)SvRV(doc);
629 {
630 139 SV *k = pdbi_get(aTHX_ d, "k");
631 139 SV *s = pdbi_get(aTHX_ d, "s");
632 139 SV *v = pdbi_get(aTHX_ d, "v");
633 139 ▶ if (k && !s) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (k && !s) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 14 125 139
if (k && !s) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 14 0 14
634 /* the one-value token: valid only for the plain ordering */
635 14 ▶ if (!pk_only)
Branch: 1 branch
BranchTrueFalseTotal
if (!pk_only) 7 7 14
636 7 croak("%s: this pagination token was issued without an "
637 "order_by and cannot continue one", cls);
638 7 av_push(out, newSVsv(k));
639 7 return out;
640 }
641 /*
642 # uncoverable condition right count:2 class:ignore_covered_err
643 # uncoverable condition right count:3 class:ignore_covered_err
644 # uncoverable condition right count:4 class:ignore_covered_err */
645 125 ▶ if (s && v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (s && v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) { 124 1 125
if (s && v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) { 124 0 124
if (s && v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) { 124 0 124
if (s && v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) { 124 0 124
646 124 AV *vals = (AV *)SvRV(v);
647 124 SSize_t i, n = av_len(cols) + 1;
648 124 ▶ if (!sv_eq(s, pdbq_order_sig(aTHX_ cols, desc)))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_eq(s, pdbq_order_sig(aTHX_ cols, desc))) 4 120 124
649 4 croak("%s: this pagination token was issued for a different "
650 "ordering", cls);
651 /*
652 # uncoverable branch true
653 # uncoverable condition left count:1 class:ignore_covered_err */
654 120 ▶ if (av_len(vals) + 1 != n)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(vals) + 1 != n) 0 120 120
655 0 croak("%s: invalid pagination token", cls); /* # uncoverable statement */
656 408 ▶ for (i = 0; i < n; i++) av_push(out, newSVsv(*av_fetch(vals, i, 0)));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) av_push(out, newSVsv(*av_fetch(vals, i, 0))); 288 120 408
657 120 return out;
658 }
659 }
660 1 croak("%s: invalid pagination token", cls);
661 return out; /* not reached */
662 }
663  
664 /* ---- the statements --------------------------------------------------------- */
665  
666 /* The options search takes. Anything else is a typo, and a typo in `limit`
667 * that silently returned twenty rows would be the worse outcome. */
668 580 static void pdbq_check_opts(pTHX_ HV *o, const char *cls) {
669 HE *he;
670 580 ▶ if (!o) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!o) return; 256 324 580
671 324 hv_iterinit(o);
672 938 ▶ while ((he = hv_iternext(o))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(o))) { 621 317 938
673 621 ▶ STRLEN kl; const char *k = HePV(he, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(he, kl); 0 621 621
674 /*
675 # uncoverable condition right count:2 class:ignore_covered_err
676 # uncoverable condition right count:4 class:ignore_covered_err
677 # uncoverable condition right count:6 class:ignore_covered_err */
678 621 ▶ if ((kl == 5 && memEQ(k, "limit", 5))
Branch: 2 branches
BranchTrueFalseTotal
if ((kl == 5 && memEQ(k, "limit", 5)) 385 236 621
if ((kl == 5 && memEQ(k, "limit", 5)) 156 229 385
679 392 ▶ || (kl == 5 && memEQ(k, "after", 5))
Branch: 2 branches
BranchTrueFalseTotal
|| (kl == 5 && memEQ(k, "after", 5)) 156 236 392
|| (kl == 5 && memEQ(k, "after", 5)) 0 156 156
680 621 ▶ || (kl == 8 && memEQ(k, "order_by", 8))) continue;
Branch: 2 branches
BranchTrueFalseTotal
|| (kl == 8 && memEQ(k, "order_by", 8))) continue; 229 7 236
|| (kl == 8 && memEQ(k, "order_by", 8))) continue; 229 0 229
681 7 croak("%s: unknown search option '%.*s' (known: after, limit, "
682 "order_by)", cls, (int)kl, k);
683 }
684 }
685  
686 /* SELECT * FROM t [WHERE ...] [ORDER BY ...] LIMIT n+1, with the binds, the
687 * resolved ordering (for the next token) and the limit. Mortal SV. */
688 364 static SV *pdbq_search_sql(pTHX_ HV *slot, SV *dbh, HV *col, SV *table,
689 SV *pk, HV *f, HV *o, const char *cls, AV *bind,
690 IV *limit_out, AV **cols_out, AV **desc_out,
691 int *pk_only_out) {
692 364 ▶ SV *lim_sv = o ? pdbi_get(aTHX_ o, "limit") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *lim_sv = o ? pdbi_get(aTHX_ o, "limit") : NULL; 255 109 364
693 364 ▶ SV *after = o ? pdbi_get(aTHX_ o, "after") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *after = o ? pdbi_get(aTHX_ o, "after") : NULL; 255 109 364
694 364 ▶ SV *order = o ? pdbi_get(aTHX_ o, "order_by") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *order = o ? pdbi_get(aTHX_ o, "order_by") : NULL; 255 109 364
695 364 ▶ IV limit = (lim_sv && SvOK(lim_sv)) ? SvIV(lim_sv) : 20; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
IV limit = (lim_sv && SvOK(lim_sv)) ? SvIV(lim_sv) : 20; /* # uncoverable condition right count:2 class:ignore_covered_err */ 202 162 364
IV limit = (lim_sv && SvOK(lim_sv)) ? SvIV(lim_sv) : 20; /* # uncoverable condition right count:2 class:ignore_covered_err */ 202 0 202
696 /* a value past what an IV holds does not come back from SvIV as a large
697 * one, so it is asked for as a float first */
698 364 ▶ int huge = (lim_sv && SvOK(lim_sv) && SvNV(lim_sv) >= (NV)IV_MAX) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
int huge = (lim_sv && SvOK(lim_sv) && SvNV(lim_sv) >= (NV)IV_MAX) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 202 162 364
int huge = (lim_sv && SvOK(lim_sv) && SvNV(lim_sv) >= (NV)IV_MAX) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 202 0 202
int huge = (lim_sv && SvOK(lim_sv) && SvNV(lim_sv) >= (NV)IV_MAX) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 6 196 202
699 364 SV *sql = sv_2mortal(newSVpvs("SELECT * FROM "));
700 SV *where;
701 AV *cols, *desc;
702 int pk_only, has_after;
703  
704 364 pdbq_check_opts(aTHX_ o, cls);
705 362 ▶ if (limit < 1) limit = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (limit < 1) limit = 1; 6 356 362
706 /* the statement asks for one row more, and IV_MAX + 1 is a negative
707 * LIMIT: unbounded on SQLite, an error on PostgreSQL */
708 362 ▶ if (huge || limit > IV_MAX - 1) limit = IV_MAX - 1; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (huge || limit > IV_MAX - 1) limit = IV_MAX - 1; /* # uncoverable condition left count:2 class:ignore_covered_err */ 356 6 362
if (huge || limit > IV_MAX - 1) limit = IV_MAX - 1; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 356 356
709 362 ▶ has_after = (after && SvOK(after) && SvCUR(after)) ? 1 : 0; /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
has_after = (after && SvOK(after) && SvCUR(after)) ? 1 : 0; /* # uncoverable condition right count:3 class:ignore_covered_err */ 134 228 362
has_after = (after && SvOK(after) && SvCUR(after)) ? 1 : 0; /* # uncoverable condition right count:3 class:ignore_covered_err */ 130 4 134
has_after = (after && SvOK(after) && SvCUR(after)) ? 1 : 0; /* # uncoverable condition right count:3 class:ignore_covered_err */ 130 0 130
710  
711 362 pdbq_order(aTHX_ col, table, pk, order, cls, &cols, &desc, &pk_only);
712 347 ▶ if (has_after && av_len(cols) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (has_after && av_len(cols) < 0) 130 217 347
if (has_after && av_len(cols) < 0) 1 129 130
713 1 croak("%s: pagination needs a primary key or an order_by", cls);
714  
715 346 sv_catsv(sql, pdbi_qi_slot(aTHX_ slot, dbh, table));
716 346 where = pdbq_where(aTHX_ slot, dbh, col, table, f, bind, cls);
717 326 ▶ if (SvCUR(where) || has_after) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvCUR(where) || has_after) { 225 101 326
if (SvCUR(where) || has_after) { 125 100 225
718 226 sv_catpvs(sql, " WHERE ");
719 226 sv_catsv(sql, where);
720 226 ▶ if (has_after) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_after) { 129 97 226
721 129 AV *vals = pdbq_decode_token(aTHX_ after, cols, desc, pk_only, cls);
722 116 ▶ if (SvCUR(where)) sv_catpvs(sql, " AND ");
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(where)) sv_catpvs(sql, " AND "); 4 112 116
723 116 sv_catsv(sql, pdbq_keyset(aTHX_ slot, dbh, cols, desc, vals, bind,
724 cls));
725 }
726 }
727 313 sv_catsv(sql, pdbq_order_sql(aTHX_ slot, dbh, cols, desc));
728 /* bound, not written into the SQL: the statement is prepare_cached, and
729 * a limit in the text made every distinct limit another cached handle */
730 313 sv_catpvs(sql, " LIMIT ?");
731 313 av_push(bind, newSViv(limit + 1));
732  
733 313 *limit_out = limit;
734 313 *cols_out = cols;
735 313 *desc_out = desc;
736 313 *pk_only_out = pk_only;
737 313 return sql;
738 }
739  
740 /* SELECT COUNT(*) FROM t [WHERE ...]. Mortal SV. */
741 29 static SV *pdbq_count_sql(pTHX_ HV *slot, SV *dbh, HV *col, SV *table,
742 HV *f, const char *cls, AV *bind) {
743 29 SV *sql = sv_2mortal(newSVpvs("SELECT COUNT(*) FROM "));
744 SV *where;
745 29 sv_catsv(sql, pdbi_qi_slot(aTHX_ slot, dbh, table));
746 29 where = pdbq_where(aTHX_ slot, dbh, col, table, f, bind, cls);
747 27 ▶ if (SvCUR(where)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(where)) { 14 13 27
748 14 sv_catpvs(sql, " WHERE ");
749 14 sv_catsv(sql, where);
750 }
751 27 return sql;
752 }
753  
754 /* the search result hash { rows, has_more_data, next } from the fetched
755 * rows (one past the limit); takes ownership of rows_rv. Returns an RV. */
756 479 static SV *pdbq_page(pTHX_ SV *rows_rv, IV limit, AV *cols, AV *desc,
757 int pk_only) {
758 AV *rows;
759 SSize_t got;
760 int has_more;
761 479 HV *out = newHV();
762 /*
763 # uncoverable branch true
764 # uncoverable condition left count:1 class:ignore_covered_err
765 # uncoverable condition left count:2 class:ignore_covered_err
766 # uncoverable condition left count:3 class:ignore_covered_err */
767 479 ▶ if (!(rows_rv && SvROK(rows_rv) && SvTYPE(SvRV(rows_rv)) == SVt_PVAV)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(rows_rv && SvROK(rows_rv) && SvTYPE(SvRV(rows_rv)) == SVt_PVAV)) { 479 0 479
if (!(rows_rv && SvROK(rows_rv) && SvTYPE(SvRV(rows_rv)) == SVt_PVAV)) { 479 0 479
if (!(rows_rv && SvROK(rows_rv) && SvTYPE(SvRV(rows_rv)) == SVt_PVAV)) { 0 479 479
768 /*
769 # uncoverable statement
770 # uncoverable branch true
771 # uncoverable branch false
772 # uncoverable condition left count:1
773 # uncoverable condition right count:1 */
774 0 ▶ if (rows_rv) SvREFCNT_dec(rows_rv);
Branch: 1 branch
BranchTrueFalseTotal
if (rows_rv) SvREFCNT_dec(rows_rv); 0 0 0
775 0 rows_rv = newRV_noinc((SV *)newAV()); /* # uncoverable statement */
776 }
777 479 rows = (AV *)SvRV(rows_rv);
778 479 got = av_len(rows) + 1;
779 479 has_more = (got > limit) ? 1 : 0;
780 479 ▶ if (has_more) { SV *x = av_pop(rows); if (x) SvREFCNT_dec(x); } /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (has_more) { SV *x = av_pop(rows); if (x) SvREFCNT_dec(x); } /* # uncoverable condition right count:2 class:ignore_covered_err */ 145 334 479
if (has_more) { SV *x = av_pop(rows); if (x) SvREFCNT_dec(x); } /* # uncoverable condition right count:2 class:ignore_covered_err */ 145 0 145
781 479 (void)hv_stores(out, "rows", rows_rv);
782 479 (void)hv_stores(out, "has_more_data", newSViv(has_more));
783 /*
784 # uncoverable condition right count:2 class:ignore_covered_err
785 # uncoverable condition right count:3 class:ignore_covered_err */
786 624 ▶ if (has_more && av_len(cols) >= 0 && av_len(rows) >= 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (has_more && av_len(cols) >= 0 && av_len(rows) >= 0) { 145 334 479
if (has_more && av_len(cols) >= 0 && av_len(rows) >= 0) { 145 0 145
if (has_more && av_len(cols) >= 0 && av_len(rows) >= 0) { 145 0 145
787 145 SV **last = av_fetch(rows, av_len(rows), 0);
788 145 ▶ HV *lh = (last && *last && SvROK(*last)
Branch: 2 branches
BranchTrueFalseTotal
HV *lh = (last && *last && SvROK(*last) 145 0 145
HV *lh = (last && *last && SvROK(*last) 145 0 145
789 290 ▶ && SvTYPE(SvRV(*last)) == SVt_PVHV) ? (HV *)SvRV(*last) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
&& SvTYPE(SvRV(*last)) == SVt_PVHV) ? (HV *)SvRV(*last) : NULL; 145 0 145
&& SvTYPE(SvRV(*last)) == SVt_PVHV) ? (HV *)SvRV(*last) : NULL; 145 0 145
790 145 ▶ (void)hv_stores(out, "next",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(out, "next", 145 0 145
791 lh ? pdbq_next_token(aTHX_ cols, desc, pk_only, lh) : newSV(0));
792 }
793 334 else (void)hv_stores(out, "next", newSV(0));
794 479 return newRV_noinc((SV *)out);
795 }
796  
797 #endif /* PUNK_DBQ_H */
« include/punk/punk_sessionstore.h ↑ Summary include/punk/punk_mpstream.h »