@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
213 lines (212 loc) • 7.32 kB
JavaScript
;
/*
* 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.NormalizationOperator = exports.ComparisonOp = void 0;
exports.eq = eq;
exports.neq = neq;
exports.gt = gt;
exports.gte = gte;
exports.lt = lt;
exports.lte = lte;
exports.isNull = isNull;
exports.isNotNull = isNotNull;
exports.inOp = inOp;
exports.contains = contains;
exports.startsWith = startsWith;
exports.endsWith = endsWith;
exports.matches = matches;
exports.isNormalized = isNormalized;
exports.isNotNormalized = isNotNormalized;
const CypherASTNode_1 = require("../../CypherASTNode");
/**
* Comparison operator expression. Created using comparison operator functions.
* @group Operators
* @category Comparison
* @example
* ```
* const op = Cypher.gt(var1, var2);
* ```
*/
class ComparisonOp extends CypherASTNode_1.CypherASTNode {
/** @internal */
constructor(operator, left, right) {
super();
this.operator = operator;
this.leftExpr = left;
this.rightExpr = right;
}
/**
* @internal
*/
getCypher(env) {
const leftStr = `${this.leftExpr.getCypher(env)} `;
const rightStr = this.rightExpr ? ` ${this.rightExpr.getCypher(env)}` : "";
return `${leftStr}${this.operator}${rightStr}`;
}
}
exports.ComparisonOp = ComparisonOp;
class NormalizationOperator extends ComparisonOp {
constructor(operator, left, normalizationType) {
super(operator, left, undefined);
this.normalizationType = normalizationType;
}
/**
* @internal
*/
getCypher(env) {
const leftStr = `${this.leftExpr.getCypher(env)} `;
const notStr = this.operator === "IS NOT NORMALIZED" ? "NOT " : "";
const typeStr = this.normalizationType ? `${this.normalizationType} ` : "";
return `${leftStr}IS ${notStr}${typeStr}NORMALIZED`;
}
}
exports.NormalizationOperator = NormalizationOperator;
function createOp(op, leftExpr, rightExpr) {
return new ComparisonOp(op, leftExpr, rightExpr);
}
/** Equality (`=`) operator
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison | Cypher Documentation}
* @group Operators
* @category Comparison
*/
function eq(leftExpr, rightExpr) {
return createOp("=", leftExpr, rightExpr);
}
/** Inequality (`<>`) operator
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison | Cypher Documentation}
* @group Operators
* @category Comparison
*/
function neq(leftExpr, rightExpr) {
return createOp("<>", leftExpr, rightExpr);
}
/** Greater Than (`>`) operator
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison | Cypher Documentation}
* @group Operators
* @category Comparison
*/
function gt(leftExpr, rightExpr) {
return createOp(">", leftExpr, rightExpr);
}
/** Greater Than Equal (`>=`) operator
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison | Cypher Documentation}
* @group Operators
* @category Comparison
*/
function gte(leftExpr, rightExpr) {
return createOp(">=", leftExpr, rightExpr);
}
/** Less Than (`<`) operator
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison | Cypher Documentation}
* @group Operators
* @category Comparison
*/
function lt(leftExpr, rightExpr) {
return createOp("<", leftExpr, rightExpr);
}
/** Less Than Equal (`<=`) operator
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison | Cypher Documentation}
* @group Operators
* @category Comparison
*/
function lte(leftExpr, rightExpr) {
return createOp("<=", leftExpr, rightExpr);
}
/** `IS NULL` operator
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison | Cypher Documentation}
* @group Operators
* @category Comparison
* @example
* ```cypher
* this0.title IS NULL
* ```
*/
function isNull(childExpr) {
return createOp("IS NULL", childExpr);
}
/** `IS NOT NULL` operator
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison | Cypher Documentation}
* @group Operators
* @category Comparison
* @example
* ```cypher
* this0.title IS NULL
* ```
*/
function isNotNull(childExpr) {
return createOp("IS NOT NULL", childExpr);
}
/** `IN` operator
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison | Cypher Documentation}
* @group Operators
* @category Comparison
*/
function inOp(leftExpr, rightExpr) {
return createOp("IN", leftExpr, rightExpr);
}
/** `CONTAINS` operator
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison | Cypher Documentation}
* @group Operators
* @category Comparison
*/
function contains(leftExpr, rightExpr) {
return createOp("CONTAINS", leftExpr, rightExpr);
}
/** `STARTS WITH` operator
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison | Cypher Documentation}
* @group Operators
* @category Comparison
*/
function startsWith(leftExpr, rightExpr) {
return createOp("STARTS WITH", leftExpr, rightExpr);
}
/** `ENDS WITH` operator
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison | Cypher Documentation}
* @group Operators
* @category Comparison
*/
function endsWith(leftExpr, rightExpr) {
return createOp("ENDS WITH", leftExpr, rightExpr);
}
/** Matching (=~) operator.
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison | Cypher Documentation}
* @group Operators
* @category Comparison
*/
function matches(leftExpr, rightExpr) {
return createOp("=~", leftExpr, rightExpr);
}
/** `IS NORMALIZED` operator
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison | Cypher Documentation}
* @group Operators
* @category Comparison
* @since Neo4j 5.17
*/
function isNormalized(leftExpr, normalizationType) {
return new NormalizationOperator("IS NORMALIZED", leftExpr, normalizationType);
}
/** `IS NOT NORMALIZED` operator
* @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison | Cypher Documentation}
* @group Operators
* @category Comparison
*/
function isNotNormalized(leftExpr, normalizationType) {
return new NormalizationOperator("IS NOT NORMALIZED", leftExpr, normalizationType);
}