* Compiles the expression to a SQL string and mutates the params array with the values. * @param exp - The expression to compile * @param params - The params array * @param encodeColumnName - Optional function to encode column names (e.g., camelCase to snake_case) * @returns The compiled SQL str
( exp: IR.BasicExpression<unknown>, params: Array<unknown>, encodeColumnName?: ColumnEncoder, )
| 99 | * @returns The compiled SQL string |
| 100 | */ |
| 101 | function compileBasicExpression( |
| 102 | exp: IR.BasicExpression<unknown>, |
| 103 | params: Array<unknown>, |
| 104 | encodeColumnName?: ColumnEncoder, |
| 105 | ): string { |
| 106 | switch (exp.type) { |
| 107 | case `val`: |
| 108 | params.push(exp.value) |
| 109 | return `$${params.length}` |
| 110 | case `ref`: |
| 111 | // TODO: doesn't yet support JSON(B) values which could be accessed with nested props |
| 112 | if (exp.path.length !== 1) { |
| 113 | throw new Error( |
| 114 | `Compiler can't handle nested properties: ${exp.path.join(`.`)}`, |
| 115 | ) |
| 116 | } |
| 117 | return quoteIdentifier(exp.path[0]!, encodeColumnName) |
| 118 | case `func`: |
| 119 | return compileFunction(exp, params, encodeColumnName) |
| 120 | default: |
| 121 | throw new Error(`Unknown expression type`) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | function compileOrderBy( |
| 126 | orderBy: IR.OrderBy, |
no test coverage detected