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"; import { TQueryBuilderValue } from "../query-builder/types"; export class CaseQueryHelper { private readonly _cases: any[] = []; constructor( private readonly _qh: QueryHelper, private _type: Type | undefined, ) {} case( predicate: TEntityValue | TQueryBuilderValue, then: TEntityValue, ): this { this._type = SdOrmUtils.getQueryValueType(then) ?? this._type; this._cases.push( ...[" WHEN ", this._qh.getQueryValue(predicate), " THEN ", this._qh.getQueryValue(then)], ); return this; } 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", ]); } }