Parse UNWIND [...] AS var clause into query */
| 1938 | |
| 1939 | /* Parse UNWIND [...] AS var clause into query */ |
| 1940 | static void parse_unwind_clause(parser_t *p, cbm_query_t *q) { |
| 1941 | advance(p); |
| 1942 | if (check(p, TOK_LBRACKET)) { |
| 1943 | /* Literal list: [1, 2, 3] — collect as JSON array string */ |
| 1944 | advance(p); |
| 1945 | char buf[CBM_SZ_2K] = "["; |
| 1946 | int blen = SKIP_ONE; |
| 1947 | /* snprintf returns the length it WOULD have written, so a single |
| 1948 | * oversized token (string literals lex up to CBM_SZ_4K-1) can push |
| 1949 | * blen past sizeof(buf). Clamp after every write and guard the raw |
| 1950 | * buf[blen++] stores, mirroring format_collect_list(). */ |
| 1951 | const int cap = (int)sizeof(buf); |
| 1952 | while (!check(p, TOK_RBRACKET) && !check(p, TOK_EOF)) { |
| 1953 | if (blen > SKIP_ONE && blen < cap - SKIP_ONE) { |
| 1954 | buf[blen++] = ','; |
| 1955 | } |
| 1956 | if (check(p, TOK_STRING)) { |
| 1957 | blen += snprintf(buf + blen, (size_t)(cap - blen), "\"%s\"", peek(p)->text); |
| 1958 | advance(p); |
| 1959 | } else if (check(p, TOK_NUMBER)) { |
| 1960 | blen += snprintf(buf + blen, (size_t)(cap - blen), "%s", peek(p)->text); |
| 1961 | advance(p); |
| 1962 | } else { |
| 1963 | advance(p); |
| 1964 | } |
| 1965 | if (blen >= cap) { |
| 1966 | blen = cap - SKIP_ONE; |
| 1967 | } |
| 1968 | match(p, TOK_COMMA); |
| 1969 | } |
| 1970 | expect(p, TOK_RBRACKET); |
| 1971 | if (blen < cap - SKIP_ONE) { |
| 1972 | buf[blen++] = ']'; |
| 1973 | } |
| 1974 | buf[blen] = '\0'; |
| 1975 | q->unwind_expr = heap_strdup(buf); |
| 1976 | } else if (check(p, TOK_IDENT)) { |
| 1977 | q->unwind_expr = heap_strdup(advance(p)->text); |
| 1978 | } |
| 1979 | expect(p, TOK_AS); |
| 1980 | const cbm_token_t *alias = expect(p, TOK_IDENT); |
| 1981 | if (alias) { |
| 1982 | q->unwind_alias = heap_strdup(alias->text); |
| 1983 | } |
| 1984 | } |
| 1985 | |
| 1986 | /* Parse a chain of MATCH / OPTIONAL MATCH patterns into query. |
| 1987 | * Returns -1 on error (fills p->error). */ |
no test coverage detected