UNPKG

@neo4j/cypher-builder

Version:

A programmatic API for building Cypher queries for Neo4j

75 lines (74 loc) 2.36 kB
"use strict"; /* * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] */ 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_expr_1 = require("../../utils/normalize-expr"); class OrderBy extends CypherASTNode_1.CypherASTNode { exprs = []; skipClause; limitClause; addOrderElements(exprs) { this.exprs.push(...exprs); } skip(offset) { const offsetVar = (0, normalize_expr_1.normalizeExpr)(offset); this.skipClause = new Skip(offsetVar); } offset(offset) { const offsetVar = (0, normalize_expr_1.normalizeExpr)(offset); this.skipClause = new Skip(offsetVar, true); } limit(limit) { const limitVar = (0, normalize_expr_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 { value; useOffset; 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 { value; constructor(value) { super(); this.value = value; } getCypher(env) { const valueStr = this.value.getCypher(env); return `LIMIT ${valueStr}`; } }