Skip to content

Commit

Permalink
handle when condition parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
igalklebanov committed Sep 22, 2023
1 parent 0ee03a7 commit 00bdcdc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
17 changes: 17 additions & 0 deletions src/parser/binary-operation-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import { SelectType } from '../util/column-type.js'
import { AndNode } from '../operation-node/and-node.js'
import { ParensNode } from '../operation-node/parens-node.js'
import { OrNode } from '../operation-node/or-node.js'
import { WhenNode } from '../operation-node/when-node.js'
import { RawNode } from '../operation-node/raw-node.js'

export type OperandValueExpression<
DB,
Expand Down Expand Up @@ -153,6 +155,21 @@ export function parseFilterList(
return node
}

export function parseMergeWhenCondition(
isMatched: boolean,
and: Expression<any> | undefined
) {
return WhenNode.create(
parseFilterList(
[
RawNode.create([isMatched ? 'matched' : 'not matched'], []),
...(and ? [and] : []),
],
'and'
)
)
}

function isIsOperator(
operator: BinaryOperatorExpression
): operator is 'is' | 'is not' {
Expand Down
21 changes: 10 additions & 11 deletions src/query-builder/merge-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import {
} from '../expression/expression-builder.js'
import { AliasedExpression, Expression } from '../expression/expression.js'
import { MergeQueryNode } from '../operation-node/merge-query-node'
import { UsingNode } from '../operation-node/using-node.js'
import { parseMergeWhenCondition } from '../parser/binary-operation-parser.js'
import {
JoinCallbackExpression,
JoinReferenceExpression,
parseJoin,
} from '../parser/join-parser.js'
import { TableExpression } from '../parser/table-parser.js'
import { UpdateObjectExpression } from '../parser/update-set-parser.js'
import { CompiledQuery } from '../query-compiler/compiled-query.js'
import { QueryExecutor } from '../query-executor/query-executor.js'
import { Compilable } from '../util/compilable.js'
Expand Down Expand Up @@ -52,7 +51,7 @@ export class MergeQueryBuilder<DB, IT extends keyof DB> {

preventAwait(
MergeQueryBuilder,
"don't await MergeQueryBuilder instances directly. To execute the query you need to call `execute` or `executeTakeFirst`."
"don't await MergeQueryBuilder instances directly. To execute the query you need to call `execute` when available."
)

export interface MergeQueryBuilderProps {
Expand All @@ -78,10 +77,10 @@ export class WheneableMergeQueryBuilder<
): MatchedMergeQueryBuilder<DB, IT, UT> {
return new MatchedMergeQueryBuilder({
...this.#props,
// queryNode: MergeQueryNode.cloneWithWhen(
// this.#props.queryNode,
// and?.(createExpressionBuilder()) // ??!
// ),
queryNode: MergeQueryNode.cloneWithWhen(
this.#props.queryNode,
parseMergeWhenCondition(true, and?.(createExpressionBuilder()))
),
})
}

Expand All @@ -90,10 +89,10 @@ export class WheneableMergeQueryBuilder<
): NotMatchedMergeQueryBuilder<DB, IT, UT> {
return new NotMatchedMergeQueryBuilder({
...this.#props,
// queryNode: MergeQueryNode.cloneWithWhen(
// this.#props.queryNode,
// and?.(createExpressionBuilder()) // ??!
// ),
queryNode: MergeQueryNode.cloneWithWhen(
this.#props.queryNode,
parseMergeWhenCondition(false, and?.(createExpressionBuilder()))
),
})
}

Expand Down

0 comments on commit 00bdcdc

Please sign in to comment.