Skip to content

Commit

Permalink
handler or query not working in case of where query array with join
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Aug 2, 2023
1 parent 625c4ff commit e60e6d2
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 12 deletions.
28 changes: 18 additions & 10 deletions src/worker/executors/select/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,18 @@ class Join {
switch (column) {
case "or":
const filteredOr = {};
const whereQryOr = whereQuery[column];
const whereQryOr = whereQryParam[column];
for (const orColumn in whereQryOr) {
const columnInTable = table.columns.find(q => q.name === orColumn);
if (!columnInTable) {
filteredOr[orColumn] = whereQryOr[orColumn];
}
}
whereQryAfterJoin['or'] = filteredOr;
for (const orColumn in filteredOr) {
delete whereQryOr[orColumn];
if (getLength(filteredOr) > 0) {
whereQryAfterJoin['or'] = filteredOr;
for (const orColumn in filteredOr) {
delete whereQryOr[orColumn];
}
}
break;
default:
Expand All @@ -110,21 +112,27 @@ class Join {
whereQryAfterJoin
}
}
let whereQryAfterJoin;
let whereQryAfterJoin, shouldDeleteWhere;
if (Array.isArray(whereQuery)) {
whereQryAfterJoin = [];
query.where = whereQuery.filter((qry, index) => {
query.where = whereQuery.filter((qry) => {
const result = removeJoinColumn(qry);
whereQryAfterJoin.push(result.whereQryAfterJoin);
if (Object.keys(result.whereQryAfterJoin).length > 0) {
whereQryAfterJoin.push(result.whereQryAfterJoin);
}
return !result.isWhereEmpty
});
console.log("query.where", query.where);
debugger;
shouldDeleteWhere = query.where.length === 0;
}
else {
const result = removeJoinColumn(whereQuery);
whereQryAfterJoin = result.whereQryAfterJoin;
if (result.isWhereEmpty) {
delete query.where;
}
shouldDeleteWhere = result.isWhereEmpty;
}
if (shouldDeleteWhere) {
delete query.where;
}
const joinQuery = this.joinQueryStack_[0];
Object.assign(joinQuery['whereJoin'], whereQryAfterJoin);
Expand Down
83 changes: 81 additions & 2 deletions test/cases/select/join.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,12 @@ describe('Test join', function () {
done(err);
})
});

it('left join when data match from second table using where', function (done) {

it(`SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID where shipperId=2
`, function (done) {
con.select({
from: "Orders",
join: {
Expand All @@ -203,6 +207,81 @@ describe('Test join', function () {
}).catch(done)
});

it(`SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID where shipperId=2 or employeeId=4
`, function (done) {
con.select({
from: "Orders",
join: {
with: "Customers",
type: "left",
on: "Orders.customerId=Customers.customerId",
as: {
customerId: 'cId'
},
},
where: [
{
shipperId: 2,
},
{
or: {
employeeId: 4,
}
}
]
}).then(function (results) {
expect(results).to.be.an('array').length(97);
done();
}).catch(done)
});

it(`SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID where shipperId=2 and (employeeId=4 and Orders.customerId=34) or (employeeId=4 and Orders.customerId=76) order by orderid asc
`, function (done) {
con.select({
from: "Orders",
join: {
with: "Customers",
type: "left",
on: "Orders.customerId=Customers.customerId",
as: {
customerId: 'cId'
},
},
where: [
{
shipperId: 2,
},
{
employeeId: 4,
customerId: 34,
},
{
or: {
employeeId: 4,
customerId: 76,
}
}
],
order: {
by: 'Orders.orderId',
type: 'asc'
}
}).then(function (results) {
expect(results).to.be.an('array').length(3);
const expectedIds = results.map(result => result.orderId);
expect(expectedIds).eql([10250, 10252, 10302])
done();
}).catch(done)
});



it('left join when data does not match from second table using where', function (done) {
con.select({
from: 'Customers',
Expand Down

0 comments on commit e60e6d2

Please sign in to comment.