| 1 |
|
| #ifndef PQ_SQL_H |
| 2 |
|
| #define PQ_SQL_H |
| 3 |
|
| |
| 4 |
|
| /* pq_sql.h - SQL text assembly and the name rules. |
| 5 |
|
| * |
| 6 |
|
| * Two jobs. The first is building statement text whose *shape* is stable, |
| 7 |
|
| * because prepare_cached keys on the string: an IN list expanded to a |
| 8 |
|
| * different arity, or a filter built in hash-iteration order, silently |
| 9 |
|
| * produces a new statement handle every call and quietly undoes the cache. |
| 10 |
|
| * |
| 11 |
|
| * The second is the name rule. Queue and task names are validated once, at |
| 12 |
|
| * enqueue, against a deliberately narrow character class. That is not |
| 13 |
|
| * decoration: phase 5 assembles `LISTEN "pq.<queue>"` as an identifier in C, |
| 14 |
|
| * and a name that cannot contain a quote or a backslash makes that safe by |
| 15 |
|
| * construction rather than by remembering to escape at the call site. |
| 16 |
|
| * |
| 17 |
|
| * Include after pq_compat.h. */ |
| 18 |
|
| |
| 19 |
|
| /* ---- names ---------------------------------------------------------------- */ |
| 20 |
|
| |
| 21 |
|
| #define PQ_NAME_MAX 64 |
| 22 |
|
| |
| 23 |
|
| /* [A-Za-z0-9_.:-]{1,64}. Returns 1 if acceptable. */ |
| 24 |
1363 |
| static int pq_name_ok(pTHX_ SV *sv) { |
| 25 |
|
| STRLEN len, i; |
| 26 |
|
| const char *s; |
| 27 |
1363 |
▶ | if (!sv || !SvOK(sv)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sv || !SvOK(sv)) return 0; |
1363 |
0 |
1363 |
| if (!sv || !SvOK(sv)) return 0; |
0 |
1363 |
1363 |
|
| 28 |
1363 |
| s = SvPV_const(sv, len); |
| 29 |
1363 |
▶ | if (len < 1 || len > PQ_NAME_MAX) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len < 1 || len > PQ_NAME_MAX) return 0; |
1361 |
2 |
1363 |
| if (len < 1 || len > PQ_NAME_MAX) return 0; |
2 |
1359 |
1361 |
|
| 30 |
8198 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
6851 |
1347 |
8198 |
|
| 31 |
6851 |
| const char c = s[i]; |
| 32 |
6851 |
▶ | if (isWORDCHAR((U8)c) || c == '.' || c == ':' || c == '-') continue; |
Branch: 4 branches
| Branch | True | False | Total |
| if (isWORDCHAR((U8)c) || c == '.' || c == ':' || c == '-') continue; |
187 |
6664 |
6851 |
| if (isWORDCHAR((U8)c) || c == '.' || c == ':' || c == '-') continue; |
43 |
144 |
187 |
| if (isWORDCHAR((U8)c) || c == '.' || c == ':' || c == '-') continue; |
20 |
23 |
43 |
| if (isWORDCHAR((U8)c) || c == '.' || c == ':' || c == '-') continue; |
8 |
12 |
20 |
|
| 33 |
12 |
| return 0; |
| 34 |
|
| } |
| 35 |
1347 |
| return 1; |
| 36 |
|
| } |
| 37 |
|
| |
| 38 |
|
| /* Validate or croak, naming the offender and the rule. A bad name is a |
| 39 |
|
| * programming error, caught at enqueue, not something to sanitise silently. */ |
| 40 |
1363 |
| static void pq_name_check(pTHX_ SV *sv, const char *what) { |
| 41 |
1363 |
▶ | if (!pq_name_ok(aTHX_ sv)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pq_name_ok(aTHX_ sv)) |
16 |
1347 |
1363 |
|
| 42 |
32 |
▶ | croak("Punk::Queue: invalid %s name '%s' - 1 to %d characters of " |
Branch: 2 branches
| Branch | True | False | Total |
| croak("Punk::Queue: invalid %s name '%s' - 1 to %d characters of " |
16 |
0 |
16 |
| croak("Punk::Queue: invalid %s name '%s' - 1 to %d characters of " |
16 |
0 |
16 |
|
| 43 |
|
| "[A-Za-z0-9_.:-]", |
| 44 |
|
| what, |
| 45 |
|
| (sv && SvOK(sv)) ? SvPV_nolen(sv) : "(undef)", |
| 46 |
|
| PQ_NAME_MAX); |
| 47 |
1347 |
| } |
| 48 |
|
| |
| 49 |
|
| /* ---- statement text ------------------------------------------------------- */ |
| 50 |
|
| |
| 51 |
|
| /* A growable SQL buffer. Mortal SV underneath, so an exception between here |
| 52 |
|
| * and the execute leaks nothing. */ |
| 53 |
2038 |
| static SV *pq_sql_new(pTHX_ const char *initial) { |
| 54 |
2038 |
▶ | SV *s = sv_2mortal(newSVpv(initial ? initial : "", 0)); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *s = sv_2mortal(newSVpv(initial ? initial : "", 0)); |
2038 |
0 |
2038 |
|
| 55 |
2038 |
| return s; |
| 56 |
|
| } |
| 57 |
|
| |
| 58 |
2313 |
| static void pq_sql_cat(pTHX_ SV *sql, const char *frag) { |
| 59 |
2313 |
| sv_catpv(sql, frag); |
| 60 |
2313 |
| } |
| 61 |
|
| |
| 62 |
|
| /* Append one buffer to another - the assembled-WHERE case, where the same |
| 63 |
|
| * fragment ends up in both the count and the page statements. */ |
| 64 |
378 |
| static void pq_sql_cat_all(pTHX_ SV *sql, SV *frag) { |
| 65 |
378 |
| sv_catsv(sql, frag); |
| 66 |
378 |
| } |
| 67 |
|
| |
| 68 |
|
| /* Append "?, ?, ?" for n placeholders. n must be >= 1. */ |
| 69 |
282 |
| static void pq_sql_placeholders(pTHX_ SV *sql, SSize_t n) { |
| 70 |
|
| SSize_t i; |
| 71 |
565 |
▶ | for (i = 0; i < n; i++) sv_catpvn(sql, i ? ", ?" : "?", i ? 3 : 1); |
Branch: 3 branches
| Branch | True | False | Total |
| for (i = 0; i < n; i++) sv_catpvn(sql, i ? ", ?" : "?", i ? 3 : 1); |
1 |
282 |
283 |
| for (i = 0; i < n; i++) sv_catpvn(sql, i ? ", ?" : "?", i ? 3 : 1); |
1 |
282 |
283 |
| for (i = 0; i < n; i++) sv_catpvn(sql, i ? ", ?" : "?", i ? 3 : 1); |
283 |
282 |
565 |
|
| 72 |
282 |
| } |
| 73 |
|
| |
| 74 |
|
| /* Append "<col> IN (?, ?, ?)" for the n values the caller will bind. |
| 75 |
|
| * |
| 76 |
|
| * Built in C rather than passed as an array bind (`= ANY(?)` on Pg) for two |
| 77 |
|
| * reasons: the array literal needs Pg-specific quoting that has to be right |
| 78 |
|
| * for names containing commas or braces, and it does not port to SQLite. The |
| 79 |
|
| * worker's queue and task sets are fixed at boot, so the arity - and |
| 80 |
|
| * therefore the statement text - is stable for the life of the process. */ |
| 81 |
274 |
| static void pq_sql_in(pTHX_ SV *sql, const char *col, SSize_t n) { |
| 82 |
274 |
| sv_catpv(sql, col); |
| 83 |
274 |
| sv_catpvs(sql, " IN ("); |
| 84 |
274 |
| pq_sql_placeholders(aTHX_ sql, n); |
| 85 |
274 |
| sv_catpvs(sql, ")"); |
| 86 |
274 |
| } |
| 87 |
|
| |
| 88 |
|
| /* The elements of an arrayref, or a single scalar treated as a one-element |
| 89 |
|
| * list, or NULL for absent. Mortal AV; elements are copies. This is the |
| 90 |
|
| * coercion behind `queues => 'mail'` and `queues => ['mail','sms']` both |
| 91 |
|
| * working. */ |
| 92 |
533 |
| static AV *pq_sql_list(pTHX_ SV *v) { |
| 93 |
533 |
| AV *out = (AV *)sv_2mortal((SV *)newAV()); |
| 94 |
533 |
▶ | if (!v || !SvOK(v)) return out; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!v || !SvOK(v)) return out; |
115 |
418 |
533 |
| if (!v || !SvOK(v)) return out; |
0 |
115 |
115 |
|
| 95 |
214 |
▶ | if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) { |
99 |
16 |
115 |
| if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) { |
99 |
0 |
99 |
|
| 96 |
99 |
| AV *in = (AV *)SvRV(v); |
| 97 |
99 |
| SSize_t n = av_len(in) + 1, i; |
| 98 |
173 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
74 |
99 |
173 |
|
| 99 |
74 |
| SV **e = av_fetch(in, i, 0); |
| 100 |
74 |
▶ | if (e && *e && SvOK(*e)) av_push(out, newSVsv(*e)); |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && *e && SvOK(*e)) av_push(out, newSVsv(*e)); |
74 |
0 |
74 |
| if (e && *e && SvOK(*e)) av_push(out, newSVsv(*e)); |
74 |
0 |
74 |
| if (e && *e && SvOK(*e)) av_push(out, newSVsv(*e)); |
74 |
0 |
74 |
|
| 101 |
|
| } |
| 102 |
|
| } |
| 103 |
|
| else { |
| 104 |
16 |
| av_push(out, newSVsv(v)); |
| 105 |
|
| } |
| 106 |
115 |
| return out; |
| 107 |
|
| } |
| 108 |
|
| |
| 109 |
|
| #endif /* PQ_SQL_H */ |