Skip to content

Commit

Permalink
dry up joins.
Browse files Browse the repository at this point in the history
  • Loading branch information
igalklebanov committed Feb 18, 2025
1 parent 92c7e44 commit 1623b48
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 110 deletions.
31 changes: 9 additions & 22 deletions src/query-builder/delete-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import {
SelectExpressionFromOutputCallback,
SelectExpressionFromOutputExpression,
} from './output-interface.js'
import { JoinType } from '../operation-node/join-node.js'

export class DeleteQueryBuilder<DB, TB extends keyof DB, O>
implements
Expand Down Expand Up @@ -407,13 +408,7 @@ export class DeleteQueryBuilder<DB, TB extends keyof DB, O>
>(table: TE, callback: FN): DeleteQueryBuilderWithInnerJoin<DB, TB, O, TE>

innerJoin(...args: any): any {
return new DeleteQueryBuilder({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('InnerJoin', args),
),
})
return this.#join('InnerJoin', args)
}

/**
Expand All @@ -431,13 +426,7 @@ export class DeleteQueryBuilder<DB, TB extends keyof DB, O>
>(table: TE, callback: FN): DeleteQueryBuilderWithLeftJoin<DB, TB, O, TE>

leftJoin(...args: any): any {
return new DeleteQueryBuilder({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('LeftJoin', args),
),
})
return this.#join('LeftJoin', args)
}

/**
Expand All @@ -455,13 +444,7 @@ export class DeleteQueryBuilder<DB, TB extends keyof DB, O>
>(table: TE, callback: FN): DeleteQueryBuilderWithRightJoin<DB, TB, O, TE>

rightJoin(...args: any): any {
return new DeleteQueryBuilder({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('RightJoin', args),
),
})
return this.#join('RightJoin', args)
}

/**
Expand All @@ -479,11 +462,15 @@ export class DeleteQueryBuilder<DB, TB extends keyof DB, O>
>(table: TE, callback: FN): DeleteQueryBuilderWithFullJoin<DB, TB, O, TE>

fullJoin(...args: any): any {
return this.#join('FullJoin', args)
}

#join(joinType: JoinType, args: any[]): any {
return new DeleteQueryBuilder({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('FullJoin', args),
parseJoin(joinType, args),
),
})
}
Expand Down
83 changes: 17 additions & 66 deletions src/query-builder/select-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import { FetchModifier } from '../operation-node/fetch-node.js'
import { parseFetch } from '../parser/fetch-parser.js'
import { TopModifier } from '../operation-node/top-node.js'
import { parseTop } from '../parser/top-parser.js'
import { JoinType } from '../operation-node/join-node.js'

export interface SelectQueryBuilder<DB, TB extends keyof DB, O>
extends WhereInterface<DB, TB>,
Expand Down Expand Up @@ -2435,101 +2436,51 @@ class SelectQueryBuilderImpl<DB, TB extends keyof DB, O>
}

innerJoin(...args: any): any {
return new SelectQueryBuilderImpl({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('InnerJoin', args),
),
})
return this.#join('InnerJoin', args)
}

leftJoin(...args: any): any {
return new SelectQueryBuilderImpl({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('LeftJoin', args),
),
})
return this.#join('LeftJoin', args)
}

rightJoin(...args: any): any {
return new SelectQueryBuilderImpl({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('RightJoin', args),
),
})
return this.#join('RightJoin', args)
}

fullJoin(...args: any): any {
return new SelectQueryBuilderImpl({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('FullJoin', args),
),
})
return this.#join('FullJoin', args)
}

crossJoin(...args: any): any {
return new SelectQueryBuilderImpl({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('CrossJoin', args),
),
})
return this.#join('CrossJoin', args)
}

innerJoinLateral(...args: any): any {
return new SelectQueryBuilderImpl({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('LateralInnerJoin', args),
),
})
return this.#join('LateralInnerJoin', args)
}

leftJoinLateral(...args: any): any {
return new SelectQueryBuilderImpl({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('LateralLeftJoin', args),
),
})
return this.#join('LateralLeftJoin', args)
}

crossJoinLateral(...args: any): any {
return new SelectQueryBuilderImpl({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('LateralCrossJoin', args),
),
})
return this.#join('LateralCrossJoin', args)
}

crossApply(table: any): any {
return new SelectQueryBuilderImpl({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('CrossApply', [table]),
),
})
crossApply(...args: any): any {
return this.#join('CrossApply', args)
}

outerApply(...args: any[]): any {
return this.#join('OuterApply', args)
}

outerApply(table: any): any {
#join(joinType: JoinType, args: any[]): any {
return new SelectQueryBuilderImpl({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('OuterApply', [table]),
parseJoin(joinType, args),
),
})
}
Expand Down
31 changes: 9 additions & 22 deletions src/query-builder/update-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import {
SelectExpressionFromOutputCallback,
SelectExpressionFromOutputExpression,
} from './output-interface.js'
import { JoinType } from '../operation-node/join-node.js'

export class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyof DB, O>
implements
Expand Down Expand Up @@ -365,13 +366,7 @@ export class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyof DB, O>
>(table: TE, callback: FN): UpdateQueryBuilderWithInnerJoin<DB, UT, TB, O, TE>

innerJoin(...args: any): any {
return new UpdateQueryBuilder({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('InnerJoin', args),
),
})
return this.#join('InnerJoin', args)
}

/**
Expand All @@ -393,13 +388,7 @@ export class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyof DB, O>
>(table: TE, callback: FN): UpdateQueryBuilderWithLeftJoin<DB, UT, TB, O, TE>

leftJoin(...args: any): any {
return new UpdateQueryBuilder({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('LeftJoin', args),
),
})
return this.#join('LeftJoin', args)
}

/**
Expand All @@ -421,13 +410,7 @@ export class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyof DB, O>
>(table: TE, callback: FN): UpdateQueryBuilderWithRightJoin<DB, UT, TB, O, TE>

rightJoin(...args: any): any {
return new UpdateQueryBuilder({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('RightJoin', args),
),
})
return this.#join('RightJoin', args)
}

/**
Expand All @@ -449,11 +432,15 @@ export class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyof DB, O>
>(table: TE, callback: FN): UpdateQueryBuilderWithFullJoin<DB, UT, TB, O, TE>

fullJoin(...args: any): any {
return this.#join('FullJoin', args)
}

#join(joinType: JoinType, args: any[]): any {
return new UpdateQueryBuilder({
...this.#props,
queryNode: QueryNode.cloneWithJoin(
this.#props.queryNode,
parseJoin('FullJoin', args),
parseJoin(joinType, args),
),
})
}
Expand Down

0 comments on commit 1623b48

Please sign in to comment.