MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeCompiler

Function makeCompiler

packages/sql/pglite/src/PgliteClient.ts:416–456  ·  view source on GitHub ↗
(
  transform?: (_: string) => string,
  transformJson = true
)

Source from the content-addressed store, hash-verified

414 * @since 4.0.0
415 */
416export const makeCompiler = (
417 transform?: (_: string) => string,
418 transformJson = true
419): Statement.Compiler => {
420 const transformValue = transformJson && transform
421 ? Statement.defaultTransforms(transform).value
422 : undefined
423
424 return Statement.makeCompiler<PgCustom>({
425 dialect: "pg",
426 placeholder(_) {
427 return `$${_}`
428 },
429 onIdentifier: transform ?
430 function(value, withoutTransform) {
431 return withoutTransform ? escape(value) : escape(transform(value))
432 } :
433 escape,
434 onRecordUpdate(placeholders, valueAlias, valueColumns, values, returning) {
435 return [
436 `(values ${placeholders}) AS ${valueAlias}${valueColumns}${returning ? ` RETURNING ${returning[0]}` : ""}`,
437 returning ?
438 values.flat().concat(returning[1]) :
439 values.flat()
440 ]
441 },
442 onCustom(type, placeholder, withoutTransform) {
443 switch (type.kind) {
444 case "PgJson": {
445 const value = withoutTransform || transformValue === undefined
446 ? type.paramA
447 : transformValue(type.paramA)
448 return [
449 placeholder(undefined),
450 [typeof value === "string" ? JSON.stringify(value) : value]
451 ]
452 }
453 }
454 }
455 })
456}
457
458const escape = Statement.defaultEscape("\"")
459const escapeLiteral = (value: string) => `'${value.replace(/'/g, "''")}'`

Callers 1

fromClientFunction · 0.70

Calls 2

escapeFunction · 0.50
transformFunction · 0.50

Tested by

no test coverage detected