import { ConditionParam, Constructable, TableType } from "../util.ts"; import { SqlSelectable, SqlStatementDataset } from "../SqlStatement.ts"; /** @public */ export interface SelectSqlGenerator { (columns?: undefined | ""): ChainSelect<{}>; (columns: "*"): ChainSelect; (columns: Constructable<{ [key in keyof T]: string | boolean; }>): ChainSelect; (columns: Constructable): ChainSelect; (columns: Constructable): ChainSelect; } /** @public */ export type SelectAsNameOption = { as?: string; }; /** @public */ export interface ChainSelect { /** * @example * ```ts * from("table1", {as:"t1"}).from("table2 AS t2") // SELECT ... FROM table1 AS t1 FROM table2 AS t2 * ``` */ from(selectable: Constructable, option?: SelectAsNameOption): ChainSelectAfterFirstFrom; } /** @public */ export interface ChainSelectAfterFirstFrom extends ChainSelectAfterFrom { from(selectable: Constructable, option?: SelectAsNameOption): ChainSelectAfterFirstFrom; } /** @public */ export type SelectJoinOption = SelectAsNameOption & { on?: Constructable; }; /** @public */ export interface ChainSelectAfterFrom extends ChainSelectAfterJoin { innerJoin(selectable: Constructable, options?: SelectJoinOption): ChainSelectAfterFrom; leftJoin(selectable: Constructable, options?: SelectJoinOption): ChainSelectAfterFrom; rightJoin(selectable: Constructable, options?: SelectJoinOption): ChainSelectAfterFrom; fullJoin(selectable: Constructable, options?: SelectJoinOption): ChainSelectAfterFrom; naturalJoin(selectable: Constructable, options?: SelectAsNameOption): ChainSelectAfterFrom; crossJoin(selectable: Constructable, options?: SelectAsNameOption): ChainSelectAfterFrom; } /** @public */ export interface ChainSelectAfterJoin extends ChainSelectAfterWhere { /** * @example * ```ts * where(undefined) // SELECT ... FROM table1 * where([]) // SELECT ... FROM table1 * where("age > 18") // SELECT ... FROM table1 WHERE age > 18 * where(["age > 18", "name = 'Alice'"]) // SELECT ... FROM table1 WHERE age > 18 AND name = 'Alice' * ``` */ where(param: Constructable): ChainSelectAfterWhere; } /** @public */ export interface ChainSelectAfterWhere extends ChainSelectAfterHaving { /** * @example * ```ts * groupBy("age") // SELECT ... FROM table1 GROUP BY age * groupBy(["age", "name"]) // SELECT ... FROM table1 GROUP BY age, name * ``` */ groupBy(columns?: string | string[]): ChainSelectAfterGroupBy; } /** @public */ export interface ChainSelectAfterGroupBy extends ChainSelectAfterHaving { having(param: Constructable): ChainSelectAfterHaving; } /** @public */ export interface ChainSelectAfterHaving extends ChainSelectAfterOrderBy { /** * @example * ```ts * orderBy([]) // "" * orderBy({}) // "" * orderBy() // "" * orderBy("age") // ORDER BY age * orderBy(["age DESC", "name ASC NULLS LAST"]) // ORDER BY age DESC, name ASC NULLS LAST * orderBy({key:"age", target:"DESC NULLS LAST"}) // ORDER BY age DESC NULLS LAST * orderBy([{ key: "age", asc: false, nullLast: true }, { key: "name", asc: true }]) * // ORDER BY age DESC NULLS LAST, name ASC * ``` */ orderBy(param: Constructable): ChainSelectAfterOrderBy; } /** @public */ export interface ChainSelectAfterOrderBy extends SqlStatementDataset { limit(limit?: number | bigint, offset?: number | bigint): ChainSelectAfterLimit; } /** @public */ export interface ChainSelectAfterLimit extends SqlStatementDataset { } /** @public */ export type OrderValue = "ASC" | "DESC"; type OrderByValue = `${OrderValue} NULLS ${"FIRST" | "LAST"}` | OrderValue; /** @public */ export type OrderBehavior = { key: string; asc: boolean; nullLast?: boolean; target?: undefined; } | { key: string; target: OrderByValue; }; /** @public */ export type OrderByParam = string | OrderBehavior | (string | OrderBehavior)[]; export {};