@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
173 lines (172 loc) • 8.18 kB
JavaScript
"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.
*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OptionalCall = exports.Call = void 0;
const compile_cypher_if_exists_1 = require("../utils/compile-cypher-if-exists");
const is_number_1 = require("../utils/is-number");
const pad_block_1 = require("../utils/pad-block");
const Clause_1 = require("./Clause");
const Union_1 = require("./Union");
const WithCreate_1 = require("./mixins/clauses/WithCreate");
const WithMatch_1 = require("./mixins/clauses/WithMatch");
const WithMerge_1 = require("./mixins/clauses/WithMerge");
const WithReturn_1 = require("./mixins/clauses/WithReturn");
const WithUnwind_1 = require("./mixins/clauses/WithUnwind");
const WithWith_1 = require("./mixins/clauses/WithWith");
const WithDelete_1 = require("./mixins/sub-clauses/WithDelete");
const WithOrder_1 = require("./mixins/sub-clauses/WithOrder");
const WithSetRemove_1 = require("./mixins/sub-clauses/WithSetRemove");
const ImportWith_1 = require("./sub-clauses/ImportWith");
const concat_1 = require("./utils/concat");
const mixin_1 = require("./utils/mixin");
/** Adds a `CALL` subquery
* @param subquery - A clause to be wrapped in a `CALL` clause
* @param variableScope - A list of variables to pass to the scope of the clause: `CALL (var0) {`
* @see {@link https://neo4j.com/docs/cypher-manual/current/clauses/call-subquery/ | Cypher Documentation}
* @group Subqueries
*/
let Call = class Call extends Clause_1.Clause {
constructor(subquery, variableScope) {
super();
this._optional = false;
const rootQuery = subquery.getRoot();
this.addChildren(rootQuery);
this.subquery = rootQuery;
this.variableScope = variableScope;
}
/** Adds a `WITH` statement inside `CALL {`, this `WITH` can is used to import variables outside of the subquery
* @see {@link https://neo4j.com/docs/cypher-manual/current/subqueries/call-subquery/#call-importing-variables | Cypher Documentation}
* @deprecated Use constructor parameter `variableScope` instead
*/
importWith(...params) {
if (this._importWith) {
throw new Error(`Call import "WITH" already set`);
}
if (this.variableScope) {
throw new Error(`Call import cannot be used along with scope clauses "Call (<variable>)"`);
}
if (params.length > 0) {
this._importWith = new ImportWith_1.ImportWith(this, [...params]);
this.addChildren(this._importWith);
}
return this;
}
inTransactions(config = {}) {
this.inTransactionsConfig = config;
return this;
}
/** Makes the subquery an OPTIONAL CALL
* @see {@link https://neo4j.com/docs/cypher-manual/current/subqueries/call-subquery/#optional-call | Cypher Documentation}
* @since Neo4j 5.24
*/
optional() {
this._optional = true;
return this;
}
/** @internal */
getCypher(env) {
const importWithCypher = (0, compile_cypher_if_exists_1.compileCypherIfExists)(this._importWith, env, { suffix: "\n" });
const subQueryStr = this.getSubqueryCypher(env, importWithCypher);
const setCypher = this.compileSetCypher(env);
const deleteCypher = (0, compile_cypher_if_exists_1.compileCypherIfExists)(this.deleteClause, env, { prefix: "\n" });
const orderByCypher = (0, compile_cypher_if_exists_1.compileCypherIfExists)(this.orderByStatement, env, { prefix: "\n" });
const inTransactionCypher = this.generateInTransactionsStr();
const inCallBlock = `${importWithCypher}${subQueryStr}`;
const variableScopeStr = this.generateVariableScopeStr(env);
const nextClause = this.compileNextClause(env);
const optionalStr = this._optional ? "OPTIONAL " : "";
return `${optionalStr}CALL${variableScopeStr} {\n${(0, pad_block_1.padBlock)(inCallBlock)}\n}${inTransactionCypher}${setCypher}${deleteCypher}${orderByCypher}${nextClause}`;
}
getSubqueryCypher(env, importWithCypher) {
// This ensures the import with is added to all the union subqueries
if (this.subquery instanceof Union_1.Union || this.subquery instanceof concat_1.CompositeClause) {
return this.subquery.getCypher(env, importWithCypher);
}
return this.subquery.getCypher(env);
}
generateVariableScopeStr(env) {
if (!this.variableScope) {
return "";
}
if (this.variableScope === "*") {
return ` (*)`;
}
const variablesString = this.variableScope.map((variable) => variable.getCypher(env));
return ` (${variablesString.join(", ")})`;
}
generateInTransactionsStr() {
if (!this.inTransactionsConfig) {
return "";
}
const rows = this.inTransactionsConfig.ofRows;
const error = this.inTransactionsConfig.onError;
const ofRowsStr = rows ? ` OF ${rows} ROWS` : "";
let onErrorStr = "";
if (this.inTransactionsConfig.retry !== undefined && this.inTransactionsConfig.retry !== false) {
onErrorStr = this.generateRetryErrorStr(this.inTransactionsConfig);
}
else if (error) {
onErrorStr = ` ON ERROR ${this.getOnErrorStr(error)}`;
}
const concurrentTransactions = this.inTransactionsConfig.concurrentTransactions;
const concurrentTransactionsStr = typeof concurrentTransactions === "number" ? ` ${concurrentTransactions} CONCURRENT` : "";
return ` IN${concurrentTransactionsStr} TRANSACTIONS${ofRowsStr}${onErrorStr}`;
}
generateRetryErrorStr(transactionConfig) {
const error = transactionConfig.onError;
const hasTime = (0, is_number_1.isNumber)(transactionConfig.retry);
const thenStr = error ? ` THEN ${this.getOnErrorStr(error)}` : "";
const timeStr = hasTime ? ` FOR ${transactionConfig.retry} SECONDS` : "";
return ` ON ERROR RETRY${timeStr}${thenStr}`;
}
getOnErrorStr(err) {
const errorMap = {
continue: "CONTINUE",
break: "BREAK",
fail: "FAIL",
};
if (!errorMap[err]) {
throw new Error(`Incorrect ON ERROR option ${err}`);
}
return errorMap[err];
}
};
exports.Call = Call;
exports.Call = Call = __decorate([
(0, mixin_1.mixin)(WithReturn_1.WithReturn, WithWith_1.WithWith, WithUnwind_1.WithUnwind, WithDelete_1.WithDelete, WithSetRemove_1.WithSetRemove, WithMatch_1.WithMatch, WithCreate_1.WithCreate, WithMerge_1.WithMerge, WithOrder_1.WithOrder)
], Call);
/**
* @see {@link https://neo4j.com/docs/cypher-manual/current/subqueries/call-subquery/#optional-call | Cypher Documentation}
* @group Subqueries
* @since Neo4j 5.24
*/
class OptionalCall extends Call {
constructor(subquery, variableScope) {
super(subquery, variableScope);
this.optional();
}
}
exports.OptionalCall = OptionalCall;