* Compiles a BasicExpression to a SQL string, mutating the params array.
( exp: IR.BasicExpression<unknown>, params: Array<unknown>, compileOptions?: CompileSQLiteOptions, )
| 80 | * Compiles a BasicExpression to a SQL string, mutating the params array. |
| 81 | */ |
| 82 | function compileExpression( |
| 83 | exp: IR.BasicExpression<unknown>, |
| 84 | params: Array<unknown>, |
| 85 | compileOptions?: CompileSQLiteOptions, |
| 86 | ): string { |
| 87 | switch (exp.type) { |
| 88 | case `val`: |
| 89 | params.push(exp.value) |
| 90 | return `?` |
| 91 | case `ref`: { |
| 92 | if (exp.path.length !== 1) { |
| 93 | throw new Error( |
| 94 | `SQLite compiler doesn't support nested properties: ${exp.path.join(`.`)}`, |
| 95 | ) |
| 96 | } |
| 97 | const columnName = exp.path[0]! |
| 98 | |
| 99 | // PowerSync stores `id` as a top-level row column rather than inside the |
| 100 | // JSON `data` object, so we skip json_extract. However, when compiling for |
| 101 | // trigger WHEN clauses we still need the OLD./NEW. prefix. Extract it from |
| 102 | // the jsonColumn option. |
| 103 | if (compileOptions?.jsonColumn && columnName === `id`) { |
| 104 | const prefix = compileOptions.jsonColumn.split('.')[0]! |
| 105 | return `${prefix}.${quoteIdentifier(columnName)}` |
| 106 | } else if (compileOptions?.jsonColumn) { |
| 107 | return `json_extract(${compileOptions.jsonColumn}, '$.${columnName}')` |
| 108 | } |
| 109 | return quoteIdentifier(columnName) |
| 110 | } |
| 111 | case `func`: |
| 112 | return compileFunction(exp, params, compileOptions) |
| 113 | default: |
| 114 | throw new Error(`Unknown expression type: ${(exp as any).type}`) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Compiles an OrderBy array to a SQL ORDER BY clause. |
no test coverage detected