import { TQueryValue } from "../../types"; import { Type } from "@simplysm/sd-core-common"; import { SdOrmUtils } from "../../utils/SdOrmUtils"; import { QueryUnit } from "../queryable/QueryUnit"; import { QueryHelper } from "../query-builder/QueryHelper"; import { TEntityValue } from "../queryable/types"; export class CaseWhenQueryHelper { private readonly _cases: any[] = []; private _type: Type | undefined = undefined; constructor( private readonly _qh: QueryHelper, private readonly _arg: TEntityValue, ) {} when(arg: TEntityValue, then: TEntityValue): CaseWhenQueryHelper { this._type = SdOrmUtils.getQueryValueType(then) ?? this._type; this._cases.push( ...[ " WHEN ", this._qh.getQueryValue(this._qh.equal(this._arg, arg)), " THEN ", this._qh.getQueryValue(then), ], ); return this as any; } else(then: TEntityValue): QueryUnit { this._type = SdOrmUtils.getQueryValueType(then) ?? this._type; return new QueryUnit(this._type, [ "CASE ", ...this._cases, " ELSE ", this._qh.getQueryValue(then), " END", ]); } }