Evaluate a WHERE condition against a binding */
| 2622 | |
| 2623 | /* Evaluate a WHERE condition against a binding */ |
| 2624 | static bool eval_condition(const cbm_condition_t *c, binding_t *b) { |
| 2625 | /* Label test: WHERE n:Label (#241) — compare the bound node's label |
| 2626 | * directly rather than a property value. */ |
| 2627 | if (strcmp(c->op, "HAS_LABEL") == 0) { |
| 2628 | cbm_node_t *n = binding_get(b, c->variable); |
| 2629 | bool result = n && n->label && c->value && strcmp(n->label, c->value) == 0; |
| 2630 | return c->negated ? !result : result; |
| 2631 | } |
| 2632 | |
| 2633 | /* EXISTS { (var)-[:TYPE]->() }: does the bound node have any edge of the |
| 2634 | * given type in the requested direction? (dir 0=out, 1=in, 2=any) */ |
| 2635 | if (strcmp(c->op, "EXISTS") == 0) { |
| 2636 | cbm_node_t *n = binding_get(b, c->variable); |
| 2637 | bool result = false; |
| 2638 | if (n && b->store) { |
| 2639 | cbm_edge_t *edges = NULL; |
| 2640 | int cnt = 0; |
| 2641 | if (c->exists_dir != 1) { /* outbound or any */ |
| 2642 | if (c->value) { |
| 2643 | cbm_store_find_edges_by_source_type(b->store, n->id, c->value, &edges, &cnt); |
| 2644 | } else { |
| 2645 | cbm_store_find_edges_by_source(b->store, n->id, &edges, &cnt); |
| 2646 | } |
| 2647 | result = cnt > 0; |
| 2648 | cbm_store_free_edges(edges, cnt); |
| 2649 | } |
| 2650 | if (!result && c->exists_dir != 0) { /* inbound or any */ |
| 2651 | edges = NULL; |
| 2652 | cnt = 0; |
| 2653 | if (c->value) { |
| 2654 | cbm_store_find_edges_by_target_type(b->store, n->id, c->value, &edges, &cnt); |
| 2655 | } else { |
| 2656 | cbm_store_find_edges_by_target(b->store, n->id, &edges, &cnt); |
| 2657 | } |
| 2658 | result = cnt > 0; |
| 2659 | cbm_store_free_edges(edges, cnt); |
| 2660 | } |
| 2661 | } |
| 2662 | return c->negated ? !result : result; |
| 2663 | } |
| 2664 | |
| 2665 | const char *actual = resolve_condition_value(c, b); |
| 2666 | /* coalesce(var.prop, literal) (#874): a missing/empty property value |
| 2667 | * falls back to the literal default before the operator runs. */ |
| 2668 | if (c->coalesce_default && (!actual || actual[0] == '\0')) { |
| 2669 | actual = c->coalesce_default; |
| 2670 | } |
| 2671 | if (!actual) { |
| 2672 | return true; |
| 2673 | } |
| 2674 | |
| 2675 | bool result; |
| 2676 | |
| 2677 | /* IS NULL / IS NOT NULL */ |
| 2678 | if (strcmp(c->op, "IS NULL") == 0) { |
| 2679 | result = (actual[0] == '\0'); |
| 2680 | return c->negated ? !result : result; |
| 2681 | } |
no test coverage detected