Skip to content

Commit e74f16d

Browse files
authored
Feat/current date in on clause (#211)
2 parents 758b8b7 + 5bffd06 commit e74f16d

File tree

4 files changed

+78
-41
lines changed

4 files changed

+78
-41
lines changed

lib/make/makeJoinForPipeline.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ function makeJoinPart(join, previousJoin, aliases, pipeline, context, ast) {
118118
const replacePaths = [];
119119
$json.walk(joinQuery, (val, path) => {
120120
if ($check.string(val) && val.startsWith('$$')) {
121+
if (val === '$$NOW') {
122+
return;
123+
}
121124
const varName = val.substring(2).replace(/[.-]/g, '_');
122125
if (join.as) {
123126
inputVars[varName] = `$${val.substring(2)}`;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@synatic/noql",
3-
"version": "4.1.19",
3+
"version": "4.1.20",
44
"description": "Convert SQL statements to mongo queries or aggregates",
55
"main": "index.js",
66
"files": [

test/bug-fix-tests/bug-fix.json

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@
1818
]
1919
}
2020
},
21-
"current-date": {
22-
"case1": {
23-
"expectedResults": [
24-
{
25-
"Today": "$date-placeholder",
26-
"Today2": "$date-placeholder"
27-
}
28-
]
29-
}
30-
},
3121
"join-fn": {
3222
"case1": {
3323
"expectedResults": [
@@ -629,20 +619,6 @@
629619
]
630620
}
631621
},
632-
"current_date": {
633-
"case1": {
634-
"expectedResults": [
635-
{
636-
"id": 2,
637-
"orderDate": "$date-placeholder"
638-
},
639-
{
640-
"id": 5,
641-
"orderDate": "$date-placeholder"
642-
}
643-
]
644-
}
645-
},
646622
"unique": {
647623
"case1": {
648624
"expectedResults": [
@@ -845,6 +821,48 @@
845821
}
846822
]
847823
}
824+
},
825+
"current-date": {
826+
"case1": {
827+
"expectedResults": [
828+
{
829+
"Today": "$date-placeholder",
830+
"Today2": "$date-placeholder"
831+
}
832+
]
833+
},
834+
"case2": {
835+
"expectedResults": [
836+
{
837+
"id": 2,
838+
"orderDate": "$date-placeholder"
839+
},
840+
{
841+
"id": 5,
842+
"orderDate": "$date-placeholder"
843+
}
844+
]
845+
},
846+
"case3": {
847+
"expectedResults": [
848+
{
849+
"id": 1,
850+
"orderDate": null
851+
},
852+
{
853+
"id": 2,
854+
"orderDate": "$date-placeholder"
855+
},
856+
{
857+
"id": 3,
858+
"orderDate": null
859+
},
860+
{
861+
"id": 4,
862+
"orderDate": null
863+
}
864+
]
865+
}
848866
}
849867
},
850868
"scratchpad": {

test/bug-fix-tests/bug-fix.test.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,6 @@ describe('bug-fixes', function () {
5858
});
5959
});
6060
});
61-
describe('currentDate', async () => {
62-
it('should work with or without parentheses', async () => {
63-
const queryString = `
64-
SELECT CURRENT_DATE() as Today,
65-
current_date as Today2,
66-
unset(_id)
67-
FROM function-test-data
68-
LIMIT 1`;
69-
await queryResultTester({
70-
queryString: queryString,
71-
casePath: 'bugfix.current-date.case1',
72-
ignoreDateValues: true,
73-
});
74-
});
75-
});
7661
describe('join function', () => {
7762
it('should be able to join strings together with a symbol in a standard select', async () => {
7863
const queryString = `
@@ -751,6 +736,20 @@ describe('bug-fixes', function () {
751736
});
752737
});
753738
describe('Current_Date', () => {
739+
it('should work with or without parentheses', async () => {
740+
const queryString = `
741+
SELECT CURRENT_DATE() as Today,
742+
current_date as Today2,
743+
unset(_id)
744+
FROM function-test-data
745+
LIMIT 1`;
746+
await queryResultTester({
747+
queryString: queryString,
748+
casePath: 'bugfix.current-date.case1',
749+
ignoreDateValues: true,
750+
mode,
751+
});
752+
});
754753
it('should allow you to compare dates', async () => {
755754
const queryString = `
756755
SELECT id,
@@ -761,11 +760,28 @@ describe('bug-fixes', function () {
761760
`;
762761
await queryResultTester({
763762
queryString: queryString,
764-
casePath: 'bugfix.current_date.case1',
763+
casePath: 'bugfix.current-date.case2',
765764
mode,
766765
ignoreDateValues: true,
767766
});
768767
});
768+
it('should allow you to use current_date in the on clause', async () => {
769+
const queryString = `
770+
SELECT o.id,
771+
o.orderDate
772+
FROM orders o
773+
INNER JOIN inventory i on o.item = i.sku
774+
AND o.orderDate <= CURRENT_DATE()
775+
`;
776+
await queryResultTester({
777+
queryString: queryString,
778+
casePath: 'bugfix.current-date.case3',
779+
mode,
780+
ignoreDateValues: true,
781+
unsetId: true,
782+
outputPipeline: false,
783+
});
784+
});
769785
});
770786
describe('unique', () => {
771787
it.skip('should get unique values', async () => {

0 commit comments

Comments
 (0)