Skip to content

Commit

Permalink
fixes #537
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed Jun 16, 2023
1 parent 8f1e4a9 commit 79b62bf
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/plugin/with-schema/with-schema-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const ROOT_OPERATION_NODES: Record<RootOperationNode['kind'], true> = freeze({
export class WithSchemaTransformer extends OperationNodeTransformer {
readonly #schema: string
readonly #schemableIds = new Set<string>()
readonly #ctes = new Set<string>()

constructor(schema: string) {
super()
Expand All @@ -47,6 +48,12 @@ export class WithSchemaTransformer extends OperationNodeTransformer {
return super.transformNodeImpl(node)
}

const ctes = this.#collectCTEs(node)

for (const cte of ctes) {
this.#ctes.add(cte)
}

const tables = this.#collectSchemableIds(node)

for (const table of tables) {
Expand All @@ -59,6 +66,10 @@ export class WithSchemaTransformer extends OperationNodeTransformer {
this.#schemableIds.delete(table)
}

for (const cte of ctes) {
this.#ctes.delete(cte)
}

return transformed
}

Expand Down Expand Up @@ -124,11 +135,17 @@ export class WithSchemaTransformer extends OperationNodeTransformer {
}
}

return schemableIds
}

#collectCTEs(node: RootOperationNode): Set<string> {
const ctes = new Set<string>()

if ('with' in node && node.with) {
this.#removeCommonTableExpressionTables(node.with, schemableIds)
this.#collectCTEIds(node.with, ctes)
}

return schemableIds
return ctes
}

#collectSchemableIdsFromTableExpr(
Expand All @@ -150,17 +167,20 @@ export class WithSchemaTransformer extends OperationNodeTransformer {
node: SchemableIdentifierNode,
schemableIds: Set<string>
): void {
if (!this.#schemableIds.has(node.identifier.name)) {
schemableIds.add(node.identifier.name)
const id = node.identifier.name

if (!this.#schemableIds.has(id) && !this.#ctes.has(id)) {
schemableIds.add(id)
}
}

#removeCommonTableExpressionTables(
node: WithNode,
schemableIds: Set<string>
) {
#collectCTEIds(node: WithNode, ctes: Set<string>): void {
for (const expr of node.expressions) {
schemableIds.delete(expr.name.table.table.identifier.name)
const cteId = expr.name.table.table.identifier.name

if (!this.#ctes.has(cteId)) {
ctes.add(cteId)
}
}
}
}
25 changes: 25 additions & 0 deletions test/node/src/with-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,31 @@ if (DIALECTS.includes('postgres')) {

await query.execute()
})

it('should not add schema for common table expression names in subqueries', async () => {
const query = ctx.db
.withSchema('mammals')
.with('doggo', (qb) =>
qb.selectFrom('pet').where('name', '=', 'Doggo').select('pet.id')
)
.selectFrom('pet')
.select((eb) => [
'pet.id',
eb.selectFrom('doggo').select('id').as('doggo_id'),
])
.selectAll()

testSql(query, dialect, {
postgres: {
sql: 'with "doggo" as (select "mammals"."pet"."id" from "mammals"."pet" where "name" = $1) select "mammals"."pet"."id", (select "id" from "doggo") as "doggo_id", * from "mammals"."pet"',
parameters: ['Doggo'],
},
mysql: NOT_SUPPORTED,
sqlite: NOT_SUPPORTED,
})

await query.execute()
})
})

describe('create table', () => {
Expand Down

1 comment on commit 79b62bf

@vercel
Copy link

@vercel vercel bot commented on 79b62bf Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kysely – ./

kysely-git-master-kysely-team.vercel.app
kysely-kysely-team.vercel.app
www.kysely.dev
kysely.dev

Please sign in to comment.