MCPcopy Create free account
hub / github.com/TanStack/db / compileFunction

Function compileFunction

packages/powersync-db-collection/src/sqlite-compiler.ts:168–341  ·  view source on GitHub ↗

* Compiles a function expression (operator) to SQL.

(
  exp: IR.Func<unknown>,
  params: Array<unknown>,
  compileOptions?: CompileSQLiteOptions,
)

Source from the content-addressed store, hash-verified

166 * Compiles a function expression (operator) to SQL.
167 */
168function compileFunction(
169 exp: IR.Func<unknown>,
170 params: Array<unknown>,
171 compileOptions?: CompileSQLiteOptions,
172): string {
173 const { name, args } = exp
174
175 // Check for null values in comparison operators
176 if (isComparisonOp(name)) {
177 const hasNullArg = args.some((arg: IR.BasicExpression) => isNullValue(arg))
178 if (hasNullArg) {
179 throw new Error(
180 `Cannot use null/undefined with '${name}' operator. ` +
181 `Use isNull() to check for null values.`,
182 )
183 }
184 }
185
186 // Compile arguments
187 const compiledArgs = args.map((arg: IR.BasicExpression) =>
188 compileExpression(arg, params, compileOptions),
189 )
190
191 // Handle different operator types
192 switch (name) {
193 // Binary comparison operators
194 case `eq`:
195 case `gt`:
196 case `gte`:
197 case `lt`:
198 case `lte`: {
199 if (compiledArgs.length !== 2) {
200 throw new Error(`${name} expects 2 arguments`)
201 }
202 const opSymbol = getComparisonOp(name)
203 return `${compiledArgs[0]} ${opSymbol} ${compiledArgs[1]}`
204 }
205
206 // Logical operators
207 case `and`:
208 case `or`: {
209 if (compiledArgs.length < 2) {
210 throw new Error(`${name} expects at least 2 arguments`)
211 }
212 const opKeyword = name === `and` ? `AND` : `OR`
213 return compiledArgs
214 .map((arg: string) => `(${arg})`)
215 .join(` ${opKeyword} `)
216 }
217
218 case `not`: {
219 if (compiledArgs.length !== 1) {
220 throw new Error(`not expects 1 argument`)
221 }
222 // Check if argument is isNull/isUndefined for IS NOT NULL
223 const arg = args[0]
224 if (arg && arg.type === `func`) {
225 if (arg.name === `isNull` || arg.name === `isUndefined`) {

Callers 1

compileExpressionFunction · 0.70

Calls 6

getComparisonOpFunction · 0.85
isComparisonOpFunction · 0.70
isNullValueFunction · 0.70
compileExpressionFunction · 0.70
mapMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected