Skip to content

Commit

Permalink
fix: filtering of sub-queries (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
fhur authored Oct 2, 2024
1 parent cb8934d commit a851857
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('compareJoins', () => {
otherColumn: store.column('manager_id'),
},
],
where: {},
},
{
table: pet,
Expand All @@ -29,6 +30,7 @@ describe('compareJoins', () => {
otherColumn: person.column('id'),
},
],
where: {},
},
{
table: pet,
Expand All @@ -39,6 +41,7 @@ describe('compareJoins', () => {
otherColumn: person.column('id'),
},
],
where: {},
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ function collectJoins(query: AnyQuery, defaultSchema: string): Array<Join> {
},
);

const whereExceptRefOps: Where<AnyDB, string> = Object.fromEntries(
Object.entries(query.where).filter(([_, refOp]) => !isRefOp(refOp)),
);

if (conditions.length === 0) {
return [];
}
Expand All @@ -71,6 +75,7 @@ function collectJoins(query: AnyQuery, defaultSchema: string): Array<Join> {
{
table: TableRef.fromQuery(defaultSchema, query),
conditions,
where: whereExceptRefOps,
},
];
});
Expand All @@ -80,12 +85,7 @@ function collectWhere(
query: AnyQuery,
defaultSchema: string,
): Array<{ table: TableRef; where: Where<AnyDB, string> }> {
return collectFromQuery(query, (query) => {
return [
{
table: TableRef.fromQuery(defaultSchema, query),
where: query.where,
},
];
});
return [
{ table: TableRef.fromQuery(defaultSchema, query), where: query.where },
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -358,24 +358,32 @@ export class SqlBuilder {
leftJoin(join: Join) {
const table = join.table;

const where = new SqlBuilder().expressionFromWhere({
table,
where: join.where,
});

return this.add('left join ')
.add(table.canonical())
.space()
.add(table.aliasQuoted())
.add(' on ')
.addInterleaved(
join.conditions.map((cond) => {
const own = cond.ownColumn.aliasQuoted();
const other = cond.otherColumn.aliasQuoted();
const op = cond.op;
return new SqlBuilder()
.add(own)
.space()
.add(op)
.space()
.add(other)
.space();
}),
[
...join.conditions.map((cond) => {
const own = cond.ownColumn.aliasQuoted();
const other = cond.otherColumn.aliasQuoted();
const op = cond.op;
return new SqlBuilder()
.add(own)
.space()
.add(op)
.space()
.add(other)
.space();
}),
where,
].filter((b) => !b.isEmpty()),
SqlBuilder.and(),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SqlBuilder } from './exp';
import { TableRef } from '../../../../refs/TableRef';
import { ColumnRef } from '../../../../refs/ColumnRef';
import { JoinOp } from '@synthql/queries';
import { AnyQuery, JoinOp, Where } from '@synthql/queries';

export interface Selection {
extractFromRow(row: any, target: any): void;
Expand All @@ -15,4 +15,5 @@ export type Join = {
otherColumn: ColumnRef;
op: JoinOp;
}>;
where: AnyQuery['where'];
};
16 changes: 16 additions & 0 deletions packages/backend/src/tests/e2e/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,20 @@ describe('select', () => {
),
).toEqual(resultNonDeferred);
});

test('join with where', async () => {
const language = from('language')
.columns('name')
.where({
language_id: col('film.language_id'),
name: 'Fake language',
})
.all();

const query = from('film').columns('title').include({ language }).all();

const result = await run(query);

expect(result.flatMap((r) => r.language)).toEqual([]);
});
});
2 changes: 1 addition & 1 deletion packages/docs/static/reference/assets/navigation.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a851857

Please sign in to comment.