Skip to content

Commit b002007

Browse files
authored
Merge pull request #69 from synatic/develop
Fix for doing a join onto a sub select clause
2 parents f3c7463 + 28cbec0 commit b002007

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

lib/make/makeJoinForPipeline.js

+5
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ function makeJoinPart(join, previousJoin, pipeline) {
140140
lookupPipeline = makeAggregatePipelineModule.makeAggregatePipeline(
141141
join.expr.ast
142142
);
143+
if (join.expr.ast.from[0] && join.expr.ast.from[0].table) {
144+
toTable = join.expr.ast.from[0].table;
145+
} else {
146+
throw new Error('Missing table for join sub query');
147+
}
143148
}
144149
lookupPipeline.push({$match: {$expr: joinQuery}});
145150

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@synatic/sql-to-mongo",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "Convert SQL to mongo queries or aggregates",
55
"main": "index.js",
66
"files": [

test/Client.js

+89
Original file line numberDiff line numberDiff line change
@@ -881,5 +881,94 @@ describe('Client Queries', function () {
881881
});
882882
});
883883
});
884+
describe('Case statement with functions', () => {
885+
it.skip('Should work when the case statement does not have backticks', async () => {
886+
const queryText = `
887+
SELECT
888+
o.item,
889+
o.notes,
890+
o.specialChars,
891+
(case
892+
WHEN o.item = "pecans"
893+
THEN 'Y'
894+
ELSE 'N'
895+
END)
896+
as OriginalExecutive
897+
FROM orders o
898+
where id=2`;
899+
const parsedQuery = SQLParser.makeMongoAggregate(queryText);
900+
const pipeline = [
901+
{$match: {id: {$eq: 2}}},
902+
{$project: {o: '$$ROOT'}},
903+
{
904+
$project: {
905+
item: '$o.item',
906+
notes: '$o.notes',
907+
specialChars: '$o.specialChars',
908+
OriginalExecutive: {
909+
$switch: {
910+
branches: [
911+
{
912+
case: {$eq: ['$o.item', 'pecans']},
913+
then: 'Y',
914+
},
915+
],
916+
default: {$literal: 'N'},
917+
},
918+
},
919+
},
920+
},
921+
];
922+
assert(
923+
parsedQuery.pipeline[2].$project.OriginalExecutive.$switch
924+
.branches[0].case.$eq[0] === '$o.item'
925+
);
926+
const results = await mongoClient
927+
.db(_dbName)
928+
.collection(parsedQuery.collections[0])
929+
.aggregate(parsedQuery.pipeline)
930+
.toArray();
931+
assert(results);
932+
assert(results[0].OriginalExecutive === 'Y');
933+
});
934+
});
935+
describe('select in select', () => {
936+
it('Should work without an order by on a local field', async () => {
937+
const queryText = `
938+
SELECT Address,
939+
SELECT \`Film Title\` from Inventory where inventoryId=1 as latestFilm
940+
FROM stores
941+
where _id=1`;
942+
const parsedQuery = SQLParser.makeMongoAggregate(queryText);
943+
const results = await mongoClient
944+
.db(_dbName)
945+
.collection(parsedQuery.collections[0])
946+
.aggregate(parsedQuery.pipeline)
947+
.toArray();
948+
assert(results);
949+
console.log(results);
950+
});
951+
it('Should work without an order by on another table', async () => {
952+
const queryText = `SELECT c.*,cn.* FROM customers c inner join (select * from \`customer-notes\` where id > 2) cn on cn.id=c.id`;
953+
const parsedQuery = SQLParser.makeMongoAggregate(queryText);
954+
const results = await mongoClient
955+
.db(_dbName)
956+
.collection(parsedQuery.collections[0])
957+
.aggregate(parsedQuery.pipeline)
958+
.toArray();
959+
assert(results);
960+
});
961+
it('Should work with an order by on another table', async () => {
962+
const queryText = `SELECT c.*,cn.* FROM customers c inner join (select * from \`customer-notes\` ORDER BY id DESC limit 1) cn on cn.id=c.id`;
963+
const parsedQuery = SQLParser.makeMongoAggregate(queryText);
964+
const results = await mongoClient
965+
.db(_dbName)
966+
.collection(parsedQuery.collections[0])
967+
.aggregate(parsedQuery.pipeline)
968+
.toArray();
969+
assert(results);
970+
assert(results[0].cn[0].id === 5);
971+
});
972+
});
884973
});
885974
});

test/exampleData/orders.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
{
1111
"id": 2,
1212
"item": "pecans",
13+
"notes": "testing",
1314
"price": 20,
1415
"quantity": 1,
1516
"customerId": 2,

0 commit comments

Comments
 (0)