Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@balena/abstract-sql-compiler

Package Overview
Dependencies
Maintainers
0
Versions
461
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@balena/abstract-sql-compiler - npm Package Compare versions

Comparing version 10.0.0 to 10.0.1-build-comparison-operator-precedence-simple-b39c90a5a816b647bd1d4e4b1294bb377a798726-1

40

out/AbstractSQLRules2SQL.js

@@ -256,4 +256,4 @@ "use strict";

(0, exports.checkArgs)(comparison, args, 2);
const a = AnyValue((0, exports.getAbstractSqlQuery)(args, 0), indent);
const b = AnyValue((0, exports.getAbstractSqlQuery)(args, 1), indent);
const a = precedenceSafeOpValue(comparison, AnyValue, args, 0, indent);
const b = precedenceSafeOpValue(comparison, AnyValue, args, 1, indent);
return a + exports.comparisons[comparison] + b;

@@ -329,10 +329,12 @@ };

]);
const mathOpValue = (valueMatchFn, args, index, indent) => {
const precedenceSafeOpValue = (parentNodeType, valueMatchFn, args, index, indent) => {
const operandAbstractSql = (0, exports.getAbstractSqlQuery)(args, index);
const numericValue = valueMatchFn(operandAbstractSql, indent);
const valueExpr = valueMatchFn(operandAbstractSql, indent);
const [childNodeType] = operandAbstractSql;
if (mathOperatorNodeTypes.has(childNodeType)) {
return `(${numericValue})`;
if ((mathOperatorNodeTypes.has(parentNodeType) &&
mathOperatorNodeTypes.has(childNodeType)) ||
(parentNodeType in exports.comparisons && childNodeType in exports.comparisons)) {
return `(${valueExpr})`;
}
return numericValue;
return valueExpr;
};

@@ -342,4 +344,4 @@ const MathOp = (type) => {

(0, exports.checkArgs)(type, args, 2);
const a = mathOpValue(NumericValue, args, 0, indent);
const b = mathOpValue(NumericValue, args, 1, indent);
const a = precedenceSafeOpValue(type, NumericValue, args, 0, indent);
const b = precedenceSafeOpValue(type, NumericValue, args, 1, indent);
return `${a} ${mathOps[type]} ${b}`;

@@ -412,4 +414,4 @@ };

(0, exports.checkArgs)('AddDateNumber', args, 2);
const a = mathOpValue(DateValue, args, 0, indent);
const b = mathOpValue(NumericValue, args, 1, indent);
const a = precedenceSafeOpValue('AddDateNumber', DateValue, args, 0, indent);
const b = precedenceSafeOpValue('AddDateNumber', NumericValue, args, 1, indent);
if (engine === "postgres") {

@@ -427,4 +429,4 @@ return `${a} + ${b}`;

(0, exports.checkArgs)('AddDateDuration', args, 2);
const a = mathOpValue(DateValue, args, 0, indent);
const b = mathOpValue(DurationValue, args, 1, indent);
const a = precedenceSafeOpValue('AddDateDuration', DateValue, args, 0, indent);
const b = precedenceSafeOpValue('AddDateDuration', DurationValue, args, 1, indent);
if (engine === "postgres") {

@@ -442,4 +444,4 @@ return `${a} + ${b}`;

(0, exports.checkArgs)('SubtractDateDuration', args, 2);
const a = mathOpValue(DateValue, args, 0, indent);
const b = mathOpValue(DurationValue, args, 1, indent);
const a = precedenceSafeOpValue('SubtractDateDuration', DateValue, args, 0, indent);
const b = precedenceSafeOpValue('SubtractDateDuration', DurationValue, args, 1, indent);
if (engine === "postgres") {

@@ -457,4 +459,4 @@ return `${a} - ${b}`;

(0, exports.checkArgs)('SubtractDateNumber', args, 2);
const a = mathOpValue(DateValue, args, 0, indent);
const b = mathOpValue(NumericValue, args, 1, indent);
const a = precedenceSafeOpValue('SubtractDateNumber', DateValue, args, 0, indent);
const b = precedenceSafeOpValue('SubtractDateNumber', NumericValue, args, 1, indent);
if (engine === "postgres") {

@@ -472,4 +474,4 @@ return `${a} - ${b}`;

(0, exports.checkArgs)('SubtractDateDate', args, 2);
const a = mathOpValue(DateValue, args, 0, indent);
const b = mathOpValue(DateValue, args, 1, indent);
const a = precedenceSafeOpValue('SubtractDateDate', DateValue, args, 0, indent);
const b = precedenceSafeOpValue('SubtractDateDate', DateValue, args, 1, indent);
if (engine === "postgres") {

@@ -476,0 +478,0 @@ return `${a} - ${b}`;

{
"name": "@balena/abstract-sql-compiler",
"version": "10.0.0",
"version": "10.0.1-build-comparison-operator-precedence-simple-b39c90a5a816b647bd1d4e4b1294bb377a798726-1",
"description": "A translator for abstract sql into sql.",

@@ -67,4 +67,4 @@ "type": "commonjs",

"versionist": {
"publishedAt": "2024-10-04T20:22:55.874Z"
"publishedAt": "2024-10-07T12:45:46.072Z"
}
}

@@ -319,4 +319,4 @@ import _ from 'lodash';

checkArgs(comparison, args, 2);
const a = AnyValue(getAbstractSqlQuery(args, 0), indent);
const b = AnyValue(getAbstractSqlQuery(args, 1), indent);
const a = precedenceSafeOpValue(comparison, AnyValue, args, 0, indent);
const b = precedenceSafeOpValue(comparison, AnyValue, args, 1, indent);
return a + comparisons[comparison] + b;

@@ -400,3 +400,4 @@ };

const mathOpValue = (
const precedenceSafeOpValue = (
parentNodeType: string,
valueMatchFn: MetaMatchFn,

@@ -408,8 +409,13 @@ args: AbstractSqlType[],

const operandAbstractSql = getAbstractSqlQuery(args, index);
const numericValue = valueMatchFn(operandAbstractSql, indent);
const valueExpr = valueMatchFn(operandAbstractSql, indent);
const [childNodeType] = operandAbstractSql;
if (mathOperatorNodeTypes.has(childNodeType)) {
return `(${numericValue})`;
if (
(mathOperatorNodeTypes.has(parentNodeType) &&
mathOperatorNodeTypes.has(childNodeType)) ||
// We need parenthesis for chained boolean comparisons, otherwise PostgreSQL complains.
(parentNodeType in comparisons && childNodeType in comparisons)
) {
return `(${valueExpr})`;
}
return numericValue;
return valueExpr;
};

@@ -420,4 +426,4 @@

checkArgs(type, args, 2);
const a = mathOpValue(NumericValue, args, 0, indent);
const b = mathOpValue(NumericValue, args, 1, indent);
const a = precedenceSafeOpValue(type, NumericValue, args, 0, indent);
const b = precedenceSafeOpValue(type, NumericValue, args, 1, indent);
return `${a} ${mathOps[type]} ${b}`;

@@ -491,4 +497,10 @@ };

checkArgs('AddDateNumber', args, 2);
const a = mathOpValue(DateValue, args, 0, indent);
const b = mathOpValue(NumericValue, args, 1, indent);
const a = precedenceSafeOpValue('AddDateNumber', DateValue, args, 0, indent);
const b = precedenceSafeOpValue(
'AddDateNumber',
NumericValue,
args,
1,
indent,
);

@@ -506,4 +518,16 @@ if (engine === Engines.postgres) {

checkArgs('AddDateDuration', args, 2);
const a = mathOpValue(DateValue, args, 0, indent);
const b = mathOpValue(DurationValue, args, 1, indent);
const a = precedenceSafeOpValue(
'AddDateDuration',
DateValue,
args,
0,
indent,
);
const b = precedenceSafeOpValue(
'AddDateDuration',
DurationValue,
args,
1,
indent,
);

@@ -521,4 +545,16 @@ if (engine === Engines.postgres) {

checkArgs('SubtractDateDuration', args, 2);
const a = mathOpValue(DateValue, args, 0, indent);
const b = mathOpValue(DurationValue, args, 1, indent);
const a = precedenceSafeOpValue(
'SubtractDateDuration',
DateValue,
args,
0,
indent,
);
const b = precedenceSafeOpValue(
'SubtractDateDuration',
DurationValue,
args,
1,
indent,
);

@@ -536,4 +572,16 @@ if (engine === Engines.postgres) {

checkArgs('SubtractDateNumber', args, 2);
const a = mathOpValue(DateValue, args, 0, indent);
const b = mathOpValue(NumericValue, args, 1, indent);
const a = precedenceSafeOpValue(
'SubtractDateNumber',
DateValue,
args,
0,
indent,
);
const b = precedenceSafeOpValue(
'SubtractDateNumber',
NumericValue,
args,
1,
indent,
);

@@ -551,4 +599,16 @@ if (engine === Engines.postgres) {

checkArgs('SubtractDateDate', args, 2);
const a = mathOpValue(DateValue, args, 0, indent);
const b = mathOpValue(DateValue, args, 1, indent);
const a = precedenceSafeOpValue(
'SubtractDateDate',
DateValue,
args,
0,
indent,
);
const b = precedenceSafeOpValue(
'SubtractDateDate',
DateValue,
args,
1,
indent,
);
if (engine === Engines.postgres) {

@@ -555,0 +615,0 @@ return `${a} - ${b}`;

@@ -0,1 +1,2 @@

import { stripIndent } from 'common-tags';
import type { AnyTypeNodes } from '../../src/AbstractSQLCompiler';

@@ -27,1 +28,333 @@

});
describe('Comparison Operator Precedence', () => {
// Different precedence
test(
[
'SelectQuery',
[
'Select',
[
[
'Equals',
['Boolean', true],
['Equals', ['Boolean', true], ['Boolean', true]],
],
],
],
],
(result, sqlEquals) => {
it('should produce a valid Equals statement when the second operand is also an Equals', () => {
sqlEquals(result.query, 'SELECT TRUE = (TRUE = TRUE)');
});
},
);
test(
[
'SelectQuery',
[
'Select',
[
[
'NotEquals',
['Equals', ['Boolean', false], ['Boolean', false]],
['Equals', ['Boolean', true], ['Boolean', true]],
],
],
],
],
(result, sqlEquals) => {
it('should produce a valid NotEquals statement when both operands are Equals comparisons', () => {
sqlEquals(result.query, 'SELECT (FALSE = FALSE) != (TRUE = TRUE)');
});
},
);
test(
[
'SelectQuery',
[
'Select',
[
[
'NotEquals',
[
'Add',
['Integer', 1],
[
'Add',
['Integer', 2],
['Multiply', ['Integer', 3], ['Integer', 4]],
],
],
['Add', ['Integer', 1], ['Integer', 0]],
],
],
],
],
(result, sqlEquals) => {
it('should produce a valid NotEquals statement when the operands are math expressions with nested parenthesis', () => {
sqlEquals(
result.query,
stripIndent`
SELECT 1 + (2 + (3 * 4)) != 1 + 0
`,
);
});
},
);
test(
[
'SelectQuery',
[
'Select',
[
[
'GreaterThanOrEqual',
[
'Multiply',
['Add', ['Integer', 1], ['Integer', 2]],
['Integer', 5],
],
['Add', ['Integer', 12], ['Integer', 2]],
],
],
],
],
(result, sqlEquals) => {
// Evaluating this would match the expression w/o parenthesis
it('should produce a valid NotEquals statement when the operands are math expressions with left sided parenthesis', () => {
sqlEquals(
result.query,
stripIndent`
SELECT (1 + 2) * 5 >= 12 + 2
`,
);
});
},
);
test(
[
'SelectQuery',
[
'Select',
[
[
'GreaterThanOrEqual',
[
'Add',
['Integer', 1],
['Multiply', ['Integer', 2], ['Integer', 5]],
],
['Add', ['Integer', 12], ['Integer', 2]],
],
],
],
],
(result, sqlEquals) => {
// Evaluating this would give a different result than if it was w/o parenthesis
it('should produce a valid NotEquals statement when the operands are math expressions with right sided parenthesis', () => {
sqlEquals(
result.query,
stripIndent`
SELECT 1 + (2 * 5) >= 12 + 2
`,
);
});
},
);
test(
[
'SelectQuery',
[
'Select',
[
[
'And',
[
'Or',
['GreaterThan', ['Integer', 1], ['Integer', 0]],
['LessThan', ['Integer', 1], ['Integer', 0]],
],
['GreaterThan', ['Integer', 1], ['Integer', 0]],
],
],
],
],
(result, sqlEquals) => {
it('should produce a valid And statement when the operands are composite boolean expressions', () => {
sqlEquals(
result.query,
stripIndent`
SELECT (1 > 0
OR 1 < 0)
AND 1 > 0
`,
);
});
},
);
test(
[
'SelectQuery',
[
'Select',
[
[
'Or',
[
'And',
['GreaterThan', ['Integer', 0], ['Integer', 1]],
['Equals', ['Integer', 0], ['Integer', 1]],
],
['GreaterThan', ['Integer', 1], ['Integer', 0]],
],
],
],
],
(result, sqlEquals) => {
// Even though the parenthesis are not added around the AND, the expression is correct b/c of precedence.
it('should produce a valid Or statement when the first operand is a composite boolean expression', () => {
sqlEquals(
result.query,
stripIndent`
SELECT (0 > 1
AND 0 = 1
OR 1 > 0)
`,
);
});
},
);
test(
[
'SelectQuery',
[
'Select',
[
[
'Or',
['GreaterThan', ['Integer', 1], ['Integer', 0]],
[
'And',
['Equals', ['Integer', 0], ['Integer', 1]],
['GreaterThan', ['Integer', 0], ['Integer', 1]],
],
],
],
],
],
(result, sqlEquals) => {
// Even though the parenthesis are not added around the AND, the expression is correct b/c of precedence.
it('should produce a valid Or statement when the second operand is a composite boolean expression', () => {
sqlEquals(
result.query,
stripIndent`
SELECT (1 > 0
OR 0 = 1
AND 0 > 1)
`,
);
});
},
);
test(
[
'SelectQuery',
[
'Select',
[
[
'And',
[
'Or',
['GreaterThan', ['Integer', 1], ['Integer', 0]],
['Equals', ['Integer', 0], ['Integer', 1]],
],
['GreaterThan', ['Integer', 0], ['Integer', 1]],
],
],
],
],
(result, sqlEquals) => {
// If it was w/o parenthesis, evaluating this would give a different result
it('should produce a valid And statement when the first operand is an Or boolean expressions', () => {
sqlEquals(
result.query,
stripIndent`
SELECT (1 > 0
OR 0 = 1)
AND 0 > 1
`,
);
});
},
);
test(
[
'SelectQuery',
[
'Select',
[
[
'And',
['GreaterThan', ['Integer', 0], ['Integer', 1]],
[
'Or',
['Equals', ['Integer', 0], ['Integer', 1]],
['GreaterThan', ['Integer', 1], ['Integer', 0]],
],
],
],
],
],
(result, sqlEquals) => {
// If it was w/o parenthesis, evaluating this would give a different result
it('should produce a valid And statement when the second operand is an Or boolean expressions', () => {
sqlEquals(
result.query,
stripIndent`
SELECT 0 > 1
AND (0 = 1
OR 1 > 0)
`,
);
});
},
);
test(
[
'SelectQuery',
[
'Select',
[
[
'Between',
['Add', ['Integer', 1], ['Integer', 0]],
['Add', ['Integer', 1], ['Integer', 0]],
['Add', ['Integer', 1], ['Integer', 0]],
],
],
],
],
(result, sqlEquals) => {
it('should produce a valid Between statement when the operands are math expressions', () => {
sqlEquals(
result.query,
stripIndent`
SELECT 1 + 0 BETWEEN 1 + 0 AND (1 + 0)
`,
);
});
},
);
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc