import { Col, colUnwrap, colWrap } from "./Column"; import { Aggr } from "./Query"; export namespace Unsafe { /** * Perform a runtime cast of a column to a specified SQL type * * @param sqlType The SQL type, such as `BIGINT` */ export function unsafeCast(col: Col, sqlType: string, parser: (val: string) => b): Col { return colWrap({ type: "ECast", exp: colUnwrap(col), sqlType: sqlType, parser: parser }); } /** * A unary operation. Note that the provided function name is spliced directly * into the resulting SQL query. Thus, this function should ONLY be used to * implement well-defined functions that are missing from Zol's standard library, * and NOT in an ad hoc manner during queries. * * @param funName Name of the SQL function * @param col Argument to the function * @param parser Function that parses the raw SQL value into the return type of the function */ export function unsafeFun(funName: string, col: Col, parser: (val: string) => b): Col { return colWrap({ type: "EUnOp", op: { type: "UFun", name: funName }, exp: colUnwrap(col), parser: parser }); } /** * Like [[unsafeFun]], but with two arguments. * * @param funName Name of the SQL function * @param col1 First argument to the function * @param col2 Second argument to the function * @param parser Function that parses the raw SQL value into the return type of the function */ export function unsafeFun2(funName: string, col1: Col, col2: Col, parser: (val: string) => c): Col { return colWrap({ type: "EFun2", name: funName, lhs: colUnwrap(col1), rhs: colUnwrap(col2), parser: parser }); } /** * Like [[unsafeFun]], but with three arguments. * * @param funName Name of the SQL function * @param col1 First argument to the function * @param col2 Second argument to the function * @param col3 Third argument to the function * @param parser Function that parses the raw SQL value into the return type of the function */ export function unsafeFun3(funName: string, col1: Col, col2: Col, col3: Col, parser: (val: string) => d): Col { return colWrap({ type: "EFun3", name: funName, col1: colUnwrap(col1), col2: colUnwrap(col2), col3: colUnwrap(col3), parser: parser }); } /** * Like [[unsafeFun]], but with an arbitrary number of arguments. * * @param funName Name of the SQL function * @param cols Arguments to the function * @param parser Function that parses the raw SQL value into the return type of the function */ export function unsafeFunN(funName: string, cols: Col[], parser: (val: string) => a): Col { return colWrap({ type: "EFunN", name: funName, cols: cols.map(colUnwrap), parser: parser }); } /** * Create a named aggregate function. Like [[unsafeFun]], this function is generally * unsafe and should ONLY be used to implement missing backend-specific * functionality. * * @param funName Name of the SQL function * @param col Argument to the function * @param parser Function that parses the raw SQL value into the return type of the function */ export function unsafeAggr(funName: string, col: Col, parser: (val: string) => b): Aggr { return colWrap({ type: "EAggrEx", name: funName, exp: colUnwrap(col), parser: parser }); } export function unsafeBinOp(opName: string, lhs: Col, rhs: Col, parser: (val: string) => c): Col { return colWrap({ type: "ECustomBinOp", op: opName, lhs: colUnwrap(lhs), rhs: colUnwrap(rhs), parser: parser }); } /** * This is very unsafe. Can easily lead to SQL injections if used without care * * @param fragments * @param parser */ export function unsafeRaw(fragments: (string | Col)[], parser: (val: string) => a): Col { return colWrap({ type: "ERaw", fragments: fragments, parser: parser }); } }