UNPKG

@neo4j/cypher-builder

Version:

A programmatic API for building Cypher queries for Neo4j

74 lines (73 loc) 2.33 kB
"use strict"; /* * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] */ Object.defineProperty(exports, "__esModule", { value: true }); exports.When = exports.Case = void 0; const CypherASTNode_1 = require("../CypherASTNode"); const compile_cypher_if_exists_1 = require("../utils/compile-cypher-if-exists"); const pad_block_1 = require("../utils/pad-block"); /** Case statement * @group Expressions * @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/expressions/#query-syntax-case | Cypher Documentation} */ class Case extends CypherASTNode_1.CypherASTNode { comparator; whenClauses = []; default; constructor(comparator) { super(); this.comparator = comparator; } // public when(expr: C extends Expr ? Expr : Predicate): When<C> { when(...exprs) { const whenClause = new When(this, exprs); this.whenClauses.push(whenClause); return whenClause; } else(defaultExpr) { this.default = defaultExpr; return this; } /** * @internal */ getCypher(env) { const comparatorStr = (0, compile_cypher_if_exists_1.compileCypherIfExists)(this.comparator, env, { prefix: " " }); const whenStr = this.whenClauses.map((c) => c.getCypher(env)).join("\n"); const defaultStr = (0, compile_cypher_if_exists_1.compileCypherIfExists)(this.default, env, { prefix: "\nELSE " }); const innerStr = (0, pad_block_1.padBlock)(`${whenStr}${defaultStr}`); return `CASE${comparatorStr}\n${innerStr}\nEND`; } } exports.Case = Case; /** * @group Expressions */ class When extends CypherASTNode_1.CypherASTNode { parent; predicates; result; /** @internal */ constructor(parent, predicate) { super(parent); this.parent = parent; this.predicates = predicate; } then(expr) { this.result = expr; return this.parent; } /** * @internal */ getCypher(env) { const predicateStr = this.predicates.map((p) => p.getCypher(env)).join(", "); if (!this.result) throw new Error("Cannot generate CASE ... WHEN statement without THEN"); const resultStr = this.result.getCypher(env); return `WHEN ${predicateStr} THEN ${resultStr}`; } } exports.When = When;