import { Col, colUnwrap, colWrap, textCol } from "./Column"; import { BinOp } from "./Exp"; import { Q, restrict } from "./Imperative"; import { SqlType } from "./SqlType"; /** * Is the given column null? * * This is like SQL's `IS NULL` check */ export function isNull(col: Col): Col { return colWrap({ type: "EUnOp", op: { type: "UIsNull" }, exp: colUnwrap(col), parser: SqlType.booleanParser }); } /** * Is the given column not null? * * This is like SQL's `IS NOT NULL` check * * `isNotNull(c)` is equivalent to `not(isNull(c))` */ export function isNotNull(col: Col): Col { return not(isNull(col)); } /** * Boolean negation. * * This is like SQL's `NOT` operator */ export function not(col: Col): Col { return colWrap({ type: "EUnOp", op: { type: "UNot" }, exp: colUnwrap(col), parser: SqlType.booleanParser }); } /** * A shortcut that calls `restrict` comparing two columns for equality. * * `restrictEq(q, x, y)` is equivalent to `restrict(q, e(x, "=", y))` */ export function restrictEq(q: Q, lhs: Col, rhs: Col) { const expr = colWrap({ type: "EBinOp", op: BinOp.Eq, lhs: colUnwrap(lhs), rhs: colUnwrap(rhs), parser: SqlType.booleanParser // TODO I don't think we actually need this }); restrict(q, expr); } /** * Returns true if the string matches the supplied pattern * * SQL equivalent: `LIKE` * * @param str The string to be matched against * @param pattern The pattern to use. May use special characters '%' and '_' */ export function like(str: Col, pattern: string): Col; /** * Returns true if the string matches the supplied pattern * * SQL equivalent: `LIKE` * * @param str The string to be matched against * @param pattern The pattern to use. May use special characters '%' and '_' */ export function like(str: Col, pattern: Col): Col; export function like(str: Col, pattern: string | Col): Col { if (typeof pattern === "string") { return colWrap({ type: "EBinOp", op: BinOp.Like, lhs: colUnwrap(str), rhs: colUnwrap(textCol(pattern)), parser: SqlType.booleanParser }); } else { return colWrap({ type: "EBinOp", op: BinOp.Like, lhs: colUnwrap(str), rhs: colUnwrap(pattern), parser: SqlType.booleanParser }); } } /** * Returns true if the string matches the supplied pattern, using case-insensitive matching * * SQL equivalent: `ILIKE` * * @param str The string to be matched against * @param pattern The pattern to use. May use special characters '%' and '_' */ export function ilike(str: Col, pattern: string): Col; /** * Returns true if the string matches the supplied pattern, using case-insensitive matching * * SQL equivalent: `ILIKE` * * @param str The string to be matched against * @param pattern The pattern to use. May use special characters '%' and '_' */ export function ilike(str: Col, pattern: Col): Col; export function ilike(str: Col, pattern: string | Col): Col { if (typeof pattern === "string") { return colWrap({ type: "EBinOp", op: BinOp.ILike, lhs: colUnwrap(str), rhs: colUnwrap(textCol(pattern)), parser: SqlType.booleanParser }); } else { return colWrap({ type: "EBinOp", op: BinOp.ILike, lhs: colUnwrap(str), rhs: colUnwrap(pattern), parser: SqlType.booleanParser }); } }