@balena/abstract-sql-compiler
Advanced tools
Comparing version 7.6.0 to 7.7.0-empty-query-optimization-39431de07eeb98d731a114c1717575d364ddc06f
@@ -7,2 +7,6 @@ # Change Log | ||
## 7.7.0 - 2021-01-01 | ||
* Optimize EXISTS/NOT EXISTS for empty queries [Pagan Gazzard] | ||
## 7.6.0 - 2021-01-01 | ||
@@ -9,0 +13,0 @@ |
@@ -29,2 +29,19 @@ "use strict"; | ||
}; | ||
const isEmptySelectQuery = (query) => { | ||
const [type, ...rest] = query; | ||
switch (type) { | ||
case 'SelectQuery': | ||
for (const arg of rest) { | ||
if (arg[0] === 'Where') { | ||
const maybeBool = arg[1]; | ||
if (maybeBool[0] === 'Boolean') { | ||
if (maybeBool[1] === false) { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
}; | ||
const rewriteMatch = (name, matchers, rewriteFn) => (args) => { | ||
@@ -793,5 +810,12 @@ checkArgs(name, args, matchers.length); | ||
}, | ||
Exists: (args) => { | ||
Exists: tryMatches(Helper((args) => { | ||
checkArgs('Exists', args, 1); | ||
const arg = getAbstractSqlQuery(args, 0); | ||
if (isEmptySelectQuery(arg)) { | ||
return ['Boolean', false]; | ||
} | ||
return false; | ||
}), (args) => { | ||
checkArgs('Exists', args, 1); | ||
const arg = getAbstractSqlQuery(args, 0); | ||
const [type, ...rest] = arg; | ||
@@ -805,4 +829,11 @@ switch (type) { | ||
} | ||
}, | ||
NotExists: (args) => { | ||
}), | ||
NotExists: tryMatches(Helper((args) => { | ||
checkArgs('Exists', args, 1); | ||
const arg = getAbstractSqlQuery(args, 0); | ||
if (isEmptySelectQuery(arg)) { | ||
return ['Boolean', true]; | ||
} | ||
return false; | ||
}), (args) => { | ||
checkArgs('NotExists', args, 1); | ||
@@ -818,3 +849,3 @@ const arg = getAbstractSqlQuery(args, 0); | ||
} | ||
}, | ||
}), | ||
Not: tryMatches(Helper((args) => { | ||
@@ -821,0 +852,0 @@ checkArgs('Not', args, 1); |
{ | ||
"name": "@balena/abstract-sql-compiler", | ||
"version": "7.6.0", | ||
"version": "7.7.0-empty-query-optimization-39431de07eeb98d731a114c1717575d364ddc06f", | ||
"description": "A translator for abstract sql into sql.", | ||
@@ -5,0 +5,0 @@ "main": "out/AbstractSQLCompiler.js", |
@@ -49,2 +49,20 @@ import * as _ from 'lodash'; | ||
const isEmptySelectQuery = (query: AbstractSqlQuery): boolean => { | ||
const [type, ...rest] = query; | ||
switch (type) { | ||
case 'SelectQuery': | ||
for (const arg of rest) { | ||
if (arg[0] === 'Where') { | ||
const maybeBool = arg[1]; | ||
if (maybeBool[0] === 'Boolean') { | ||
if (maybeBool[1] === false) { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
}; | ||
const rewriteMatch = ( | ||
@@ -918,26 +936,46 @@ name: string, | ||
}, | ||
Exists: (args) => { | ||
checkArgs('Exists', args, 1); | ||
const arg = getAbstractSqlQuery(args, 0); | ||
const [type, ...rest] = arg; | ||
switch (type) { | ||
case 'SelectQuery': | ||
case 'UnionQuery': | ||
return ['Exists', typeRules[type](rest)]; | ||
default: | ||
return ['Exists', AnyValue(arg)]; | ||
} | ||
}, | ||
NotExists: (args) => { | ||
checkArgs('NotExists', args, 1); | ||
const arg = getAbstractSqlQuery(args, 0); | ||
const [type, ...rest] = arg; | ||
switch (type) { | ||
case 'SelectQuery': | ||
case 'UnionQuery': | ||
return ['NotExists', typeRules[type](rest)]; | ||
default: | ||
return ['NotExists', AnyValue(arg)]; | ||
} | ||
}, | ||
Exists: tryMatches( | ||
Helper<OptimisationMatchFn>((args) => { | ||
checkArgs('Exists', args, 1); | ||
const arg = getAbstractSqlQuery(args, 0); | ||
if (isEmptySelectQuery(arg)) { | ||
return ['Boolean', false] as AbstractSqlQuery; | ||
} | ||
return false; | ||
}), | ||
(args) => { | ||
checkArgs('Exists', args, 1); | ||
const arg = getAbstractSqlQuery(args, 0); | ||
const [type, ...rest] = arg; | ||
switch (type) { | ||
case 'SelectQuery': | ||
case 'UnionQuery': | ||
return ['Exists', typeRules[type](rest)]; | ||
default: | ||
return ['Exists', AnyValue(arg)]; | ||
} | ||
}, | ||
), | ||
NotExists: tryMatches( | ||
Helper<OptimisationMatchFn>((args) => { | ||
checkArgs('Exists', args, 1); | ||
const arg = getAbstractSqlQuery(args, 0); | ||
if (isEmptySelectQuery(arg)) { | ||
return ['Boolean', true] as AbstractSqlQuery; | ||
} | ||
return false; | ||
}), | ||
(args) => { | ||
checkArgs('NotExists', args, 1); | ||
const arg = getAbstractSqlQuery(args, 0); | ||
const [type, ...rest] = arg; | ||
switch (type) { | ||
case 'SelectQuery': | ||
case 'UnionQuery': | ||
return ['NotExists', typeRules[type](rest)]; | ||
default: | ||
return ['NotExists', AnyValue(arg)]; | ||
} | ||
}, | ||
), | ||
Not: tryMatches( | ||
@@ -944,0 +982,0 @@ Helper<OptimisationMatchFn>((args) => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
573865
52
12640
2