UNPKG

@neo4j/graphql

Version:

A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations

68 lines 3.53 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. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MathInputField = void 0; const cypher_builder_1 = __importDefault(require("@neo4j/cypher-builder")); const ParamInputField_1 = require("../ParamInputField"); class MathInputField extends ParamInputField_1.ParamInputField { constructor({ attribute, attachedTo, inputValue, operation, }) { super({ attribute, attachedTo, inputValue }); this.operation = operation; if (operation == "divide" && inputValue === 0) { throw new Error("Division by zero is not supported"); } } getChildren() { return []; } getSubqueries(queryASTContext) { const prop = this.getLeftExpression(queryASTContext); const bitSize = this.attribute.typeHelper.isInt() ? 32 : 64; const rightExpr = this.getRightExpression(queryASTContext); // Avoid overflows, for 64 bit overflows, a long overflow is raised anyway by Neo4j const maxBit = cypher_builder_1.default.minus(cypher_builder_1.default.pow(new cypher_builder_1.default.Literal(2), new cypher_builder_1.default.Literal(bitSize - 1)), new cypher_builder_1.default.Literal(1)); return [ cypher_builder_1.default.utils.concat(cypher_builder_1.default.apoc.util.validate(cypher_builder_1.default.isNull(prop), "Cannot %s %s to Nan", new cypher_builder_1.default.List([new cypher_builder_1.default.Literal(this.operation), this.getParam()])), cypher_builder_1.default.apoc.util.validate(cypher_builder_1.default.gt(rightExpr, maxBit), "Overflow: Value returned from operator %s is larger than %s bit", new cypher_builder_1.default.List([new cypher_builder_1.default.Literal(this.operation), new cypher_builder_1.default.Literal(bitSize)]))), ]; } getRightExpression(queryASTContext) { const rightVariable = super.getRightExpression(queryASTContext); const targetProperty = this.getLeftExpression(queryASTContext); switch (this.operation) { case "add": case "increment": return cypher_builder_1.default.plus(targetProperty, rightVariable); case "decrement": case "subtract": return cypher_builder_1.default.minus(targetProperty, rightVariable); case "divide": return cypher_builder_1.default.divide(targetProperty, rightVariable); case "multiply": return cypher_builder_1.default.multiply(targetProperty, rightVariable); default: throw new Error(`Unknown operation ${this.operation}`); } } } exports.MathInputField = MathInputField; //# sourceMappingURL=MathInputField.js.map