UNPKG

@neo4j/cypher-builder

Version:

A programmatic API for building Cypher queries for Neo4j

83 lines (82 loc) 2.86 kB
"use strict"; /* * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] * * This file is part of Neo4j. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 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 { constructor(comparator) { super(); this.whenClauses = []; 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 { /** @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;