UNPKG

@neo4j/cypher-builder

Version:

A programmatic API for building Cypher queries for Neo4j

87 lines (86 loc) 2.96 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.OrderBy = void 0; const CypherASTNode_1 = require("../../CypherASTNode"); const compile_cypher_if_exists_1 = require("../../utils/compile-cypher-if-exists"); const normalize_variable_1 = require("../../utils/normalize-variable"); class OrderBy extends CypherASTNode_1.CypherASTNode { constructor() { super(...arguments); this.exprs = []; } addOrderElements(exprs) { this.exprs.push(...exprs); } skip(offset) { const offsetVar = (0, normalize_variable_1.normalizeExpr)(offset); this.skipClause = new Skip(offsetVar); } offset(offset) { const offsetVar = (0, normalize_variable_1.normalizeExpr)(offset); this.skipClause = new Skip(offsetVar, true); } limit(limit) { const limitVar = (0, normalize_variable_1.normalizeExpr)(limit); this.limitClause = new Limit(limitVar); } hasOrder() { return this.exprs.length > 0; } /** @internal */ getCypher(env) { let orderStr = ""; const skipStr = (0, compile_cypher_if_exists_1.compileCypherIfExists)(this.skipClause, env, { prefix: "\n" }); const limitStr = (0, compile_cypher_if_exists_1.compileCypherIfExists)(this.limitClause, env, { prefix: "\n" }); if (this.hasOrder()) { const exprStr = this.exprs .map(([expr, order]) => { return `${expr.getCypher(env)} ${order}`; }) .join(", "); orderStr = `ORDER BY ${exprStr}`; } return `${orderStr}${skipStr}${limitStr}`; } } exports.OrderBy = OrderBy; class Skip extends CypherASTNode_1.CypherASTNode { constructor(value, useOffset = false) { super(); this.value = value; this.useOffset = useOffset; } getCypher(env) { const valueStr = this.value.getCypher(env); const skipStr = this.useOffset ? "OFFSET" : "SKIP"; return `${skipStr} ${valueStr}`; } } class Limit extends CypherASTNode_1.CypherASTNode { constructor(value) { super(); this.value = value; } getCypher(env) { const valueStr = this.value.getCypher(env); return `LIMIT ${valueStr}`; } }