Evaluate a WHERE condition against a binding */
| 2734 | |
| 2735 | /* Evaluate a WHERE condition against a binding */ |
| 2736 | static bool eval_condition(const cbm_condition_t *c, binding_t *b) { |
| 2737 | /* Label test: WHERE n:Label (#241) — compare the bound node's label |
| 2738 | * directly rather than a property value. */ |
| 2739 | if (strcmp(c->op, "HAS_LABEL") == 0) { |
| 2740 | cbm_node_t *n = binding_get(b, c->variable); |
| 2741 | bool result = n && n->label && c->value && strcmp(n->label, c->value) == 0; |
| 2742 | return c->negated ? !result : result; |
| 2743 | } |
| 2744 | |
| 2745 | /* EXISTS { (var)-[:TYPE]->() }: does the bound node have any edge of the |
| 2746 | * given type in the requested direction? (dir 0=out, 1=in, 2=any) */ |
| 2747 | if (strcmp(c->op, "EXISTS") == 0) { |
| 2748 | cbm_node_t *n = binding_get(b, c->variable); |
| 2749 | bool result = false; |
| 2750 | if (n && b->store) { |
| 2751 | cbm_edge_t *edges = NULL; |
| 2752 | int cnt = 0; |
| 2753 | if (c->exists_dir != 1) { /* outbound or any */ |
| 2754 | if (c->value) { |
| 2755 | cbm_store_find_edges_by_source_type(b->store, n->id, c->value, &edges, &cnt); |
| 2756 | } else { |
| 2757 | cbm_store_find_edges_by_source(b->store, n->id, &edges, &cnt); |
| 2758 | } |
| 2759 | result = cnt > 0; |
| 2760 | cbm_store_free_edges(edges, cnt); |
| 2761 | } |
| 2762 | if (!result && c->exists_dir != 0) { /* inbound or any */ |
| 2763 | edges = NULL; |
| 2764 | cnt = 0; |
| 2765 | if (c->value) { |
| 2766 | cbm_store_find_edges_by_target_type(b->store, n->id, c->value, &edges, &cnt); |
| 2767 | } else { |
| 2768 | cbm_store_find_edges_by_target(b->store, n->id, &edges, &cnt); |
| 2769 | } |
| 2770 | result = cnt > 0; |
| 2771 | cbm_store_free_edges(edges, cnt); |
| 2772 | } |
| 2773 | } |
| 2774 | return c->negated ? !result : result; |
| 2775 | } |
| 2776 | |
| 2777 | const char *actual = resolve_condition_value(c, b); |
| 2778 | /* coalesce(var.prop, literal) (#874): a missing/empty property value |
| 2779 | * falls back to the literal default before the operator runs. */ |
| 2780 | if (c->coalesce_default && (!actual || actual[0] == '\0')) { |
| 2781 | actual = c->coalesce_default; |
| 2782 | } |
| 2783 | if (!actual) { |
| 2784 | return true; |
| 2785 | } |
| 2786 | |
| 2787 | bool result; |
| 2788 | |
| 2789 | /* IS NULL / IS NOT NULL */ |
| 2790 | if (strcmp(c->op, "IS NULL") == 0) { |
| 2791 | result = (actual[0] == '\0'); |
| 2792 | return c->negated ? !result : result; |
| 2793 | } |
no test coverage detected