| 6528 | } |
| 6529 | |
| 6530 | int cbm_cypher_execute(cbm_store_t *store, const char *query, const char *project, int max_rows, |
| 6531 | cbm_cypher_result_t *out) { |
| 6532 | memset(out, 0, sizeof(*out)); |
| 6533 | g_cypher_depth_clamped = 0; |
| 6534 | g_cypher_trail_truncated = 0; |
| 6535 | g_cypher_truncated = false; |
| 6536 | g_cypher_regex_refused = false; |
| 6537 | g_cypher_scan_error[0] = '\0'; |
| 6538 | cypher_deadline_arm(); /* #601: start the wall-clock budget for this query */ |
| 6539 | /* max_rows sizes the initial binding array: non-positive means the |
| 6540 | * ceiling, and nothing above the ceiling is ever materialized anyway. */ |
| 6541 | if (max_rows <= 0 || max_rows > CYPHER_RESULT_CEILING) { |
| 6542 | max_rows = CYPHER_RESULT_CEILING; |
| 6543 | } |
| 6544 | |
| 6545 | cbm_query_t *q = NULL; |
| 6546 | char *err = NULL; |
| 6547 | if (cbm_cypher_parse(query, &q, &err) < 0) { |
| 6548 | out->error = err; |
| 6549 | return CBM_NOT_FOUND; |
| 6550 | } |
| 6551 | |
| 6552 | for (const cbm_query_t *sq = q; sq; sq = sq->union_next) { |
| 6553 | char *scope_err = check_projection_scope(sq); |
| 6554 | if (scope_err) { |
| 6555 | cbm_query_free(q); |
| 6556 | out->error = scope_err; |
| 6557 | return CBM_NOT_FOUND; |
| 6558 | } |
| 6559 | } |
| 6560 | |
| 6561 | result_builder_t rb = {0}; |
| 6562 | if (execute_single(store, q, project, max_rows, &rb) < 0) { |
| 6563 | rb_free(&rb); |
| 6564 | cbm_query_free(q); |
| 6565 | out->truncated = g_cypher_truncated; |
| 6566 | out->error = cypher_abort_error(); |
| 6567 | return CBM_NOT_FOUND; |
| 6568 | } |
| 6569 | |
| 6570 | /* UNION chain */ |
| 6571 | cbm_query_t *uq = q->union_next; |
| 6572 | while (uq) { |
| 6573 | result_builder_t rb2 = {0}; |
| 6574 | if (execute_single(store, uq, project, max_rows, &rb2) < 0) { |
| 6575 | rb_free(&rb); |
| 6576 | rb_free(&rb2); |
| 6577 | cbm_query_free(q); |
| 6578 | out->truncated = g_cypher_truncated; |
| 6579 | out->error = cypher_abort_error(); |
| 6580 | return CBM_NOT_FOUND; |
| 6581 | } |
| 6582 | /* Concatenate rows from rb2 into rb */ |
| 6583 | for (int i = 0; i < rb2.row_count; i++) { |
| 6584 | rb_add_row(&rb, rb2.rows[i]); |
| 6585 | } |
| 6586 | rb_free(&rb2); |
| 6587 | |