include/punk/punk_dbq.h

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