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
467
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-184-operator-precedence-light-fea0d1a2c1bc3dd2f2047991e354e00a3698d9a1-1

88

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;

@@ -321,18 +321,46 @@ };

};
const mathOperatorNodeTypes = new Set([
...Object.keys(mathOps),
'AddDateDuration',
'AddDateNumber',
'SubtractDateDate',
'SubtractDateDuration',
'SubtractDateNumber',
]);
const mathOpValue = (valueMatchFn, args, index, indent) => {
const opsPrecedence = (() => {
const operatorsByPrecedence = [
['Multiply', 'Divide'],
[
'Add',
'AddDateDuration',
'AddDateNumber',
'Subtract',
'SubtractDateDate',
'SubtractDateDuration',
'SubtractDateNumber',
],
['Between', 'Like'],
[
'Equals',
'NotEquals',
'GreaterThan',
'GreaterThanOrEqual',
'LessThan',
'LessThanOrEqual',
],
];
const operatorPrecedence = {};
let precedence = 0;
for (const samePrecedenceOps of operatorsByPrecedence) {
for (const op of samePrecedenceOps) {
operatorPrecedence[op] = precedence;
}
precedence++;
}
return operatorPrecedence;
})();
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})`;
const parentOperatorPrecedence = opsPrecedence[parentNodeType];
const childOperatorPrecedence = opsPrecedence[childNodeType];
if (childOperatorPrecedence != null &&
parentOperatorPrecedence != null &&
parentOperatorPrecedence <= childOperatorPrecedence) {
return `(${valueExpr})`;
}
return numericValue;
return valueExpr;
};

@@ -342,4 +370,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 +440,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 +455,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 +470,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 +485,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 +500,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") {

@@ -853,5 +881,5 @@ return `${a} - ${b}`;

(0, exports.checkArgs)('Between', args, 3);
const v = AnyValue((0, exports.getAbstractSqlQuery)(args, 0), indent);
const a = AnyValue((0, exports.getAbstractSqlQuery)(args, 1), indent);
const b = AnyValue((0, exports.getAbstractSqlQuery)(args, 2), indent);
const v = precedenceSafeOpValue('Between', AnyValue, args, 0, indent);
const a = precedenceSafeOpValue('Between', AnyValue, args, 1, indent);
const b = precedenceSafeOpValue('Between', AnyValue, args, 2, indent);
return `${v} BETWEEN ${a} AND (${b})`;

@@ -858,0 +886,0 @@ },

{
"name": "@balena/abstract-sql-compiler",
"version": "10.0.0",
"version": "10.0.1-build-184-operator-precedence-light-fea0d1a2c1bc3dd2f2047991e354e00a3698d9a1-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:58:56.223Z"
}
}

@@ -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;

@@ -391,12 +391,40 @@ };

const mathOperatorNodeTypes = new Set([
...Object.keys(mathOps),
'AddDateDuration',
'AddDateNumber',
'SubtractDateDate',
'SubtractDateDuration',
'SubtractDateNumber',
]);
const opsPrecedence = (() => {
const operatorsByPrecedence = [
['Multiply', 'Divide'],
[
'Add',
'AddDateDuration',
'AddDateNumber',
'Subtract',
'SubtractDateDate',
'SubtractDateDuration',
'SubtractDateNumber',
],
['Between', 'Like'],
[
'Equals',
'NotEquals',
'GreaterThan',
'GreaterThanOrEqual',
'LessThan',
'LessThanOrEqual',
],
// In, Exists, NotExists, 'IsDistinctFrom', 'IsNotDistinctFrom', Not,
// And, Or are already adding parenthesis.
] as const;
const mathOpValue = (
const operatorPrecedence = {} as Record<string, number>;
let precedence = 0;
for (const samePrecedenceOps of operatorsByPrecedence) {
for (const op of samePrecedenceOps) {
operatorPrecedence[op] = precedence;
}
precedence++;
}
return operatorPrecedence;
})();
const precedenceSafeOpValue = (
parentNodeType: string,
valueMatchFn: MetaMatchFn,

@@ -408,8 +436,14 @@ 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})`;
const parentOperatorPrecedence = opsPrecedence[parentNodeType];
const childOperatorPrecedence = opsPrecedence[childNodeType];
if (
childOperatorPrecedence != null &&
parentOperatorPrecedence != null &&
parentOperatorPrecedence <= childOperatorPrecedence
) {
return `(${valueExpr})`;
}
return numericValue;
return valueExpr;
};

@@ -420,4 +454,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 +525,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 +546,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 +573,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 +600,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 +627,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) {

@@ -968,5 +1056,5 @@ return `${a} - ${b}`;

checkArgs('Between', args, 3);
const v = AnyValue(getAbstractSqlQuery(args, 0), indent);
const a = AnyValue(getAbstractSqlQuery(args, 1), indent);
const b = AnyValue(getAbstractSqlQuery(args, 2), indent);
const v = precedenceSafeOpValue('Between', AnyValue, args, 0, indent);
const a = precedenceSafeOpValue('Between', AnyValue, args, 1, indent);
const b = precedenceSafeOpValue('Between', AnyValue, args, 2, indent);
return `${v} BETWEEN ${a} AND (${b})`;

@@ -973,0 +1061,0 @@ },

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

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

@@ -27,1 +28,388 @@

});
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,
// ideally we shouldn't have any parenthesis here
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',
['Equals', ['Integer', 1], ['Integer', 0]],
['LessThan', ['Integer', 1], ['Integer', 0]],
['GreaterThan', ['Integer', 1], ['Integer', 0]],
],
],
],
],
(result, sqlEquals) => {
it('should produce a valid Between statement when the operands are comparison expressions', () => {
sqlEquals(
result.query,
stripIndent`
SELECT (1 = 0) BETWEEN (1 < 0) AND ((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)
`,
);
});
},
);
test(
[
'SelectQuery',
[
'Select',
[
[
'Between',
['Equals', ['Integer', 1], ['Integer', 0]],
['LessThan', ['Integer', 1], ['Integer', 0]],
['GreaterThan', ['Integer', 1], ['Integer', 0]],
],
],
],
],
(result, sqlEquals) => {
it('should produce a valid Between statement when the operands are comparison expressions', () => {
sqlEquals(
result.query,
stripIndent`
SELECT (1 = 0) BETWEEN (1 < 0) AND ((1 > 0))
`,
);
});
},
);
});

@@ -131,3 +131,3 @@ import type { AnyTypeNodes } from '../../src/AbstractSQLCompiler';

it('should produce a valid Add statement when the first operand is a Multiply', () => {
sqlEquals(result.query, 'SELECT (2 * 3) + 4');
sqlEquals(result.query, 'SELECT 2 * 3 + 4');
});

@@ -147,3 +147,3 @@ },

it('should produce a valid Add statement when the second operand is a Multiply', () => {
sqlEquals(result.query, 'SELECT 2 + (3 * 4)');
sqlEquals(result.query, 'SELECT 2 + 3 * 4');
});

@@ -169,3 +169,3 @@ },

it('should produce a valid Add statement of two Multiplications', () => {
sqlEquals(result.query, 'SELECT (2 * 3) + (4 * 5)');
sqlEquals(result.query, 'SELECT 2 * 3 + 4 * 5');
});

@@ -242,3 +242,3 @@ },

it('should produce a valid Subtract statement of two Multiplications', () => {
sqlEquals(result.query, 'SELECT (2 * 3) - (4 * 5)');
sqlEquals(result.query, 'SELECT 2 * 3 - 4 * 5');
});

@@ -264,3 +264,3 @@ },

it('should produce a valid Subtract statement of two Divisions', () => {
sqlEquals(result.query, 'SELECT (2 / 3) - (4 / 5)');
sqlEquals(result.query, 'SELECT 2 / 3 - 4 / 5');
});

@@ -267,0 +267,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