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

Function compile

packages/effect/src/sql/Statement.ts:966–1199  ·  view source on GitHub ↗
(
    this: CompilerImpl,
    statement: Fragment,
    withoutTransform = false,
    placeholderOverride?: (u: unknown) => string
  )

Source from the content-addressed store, hash-verified

964
965const CompilerProto = {
966 compile(
967 this: CompilerImpl,
968 statement: Fragment,
969 withoutTransform = false,
970 placeholderOverride?: (u: unknown) => string
971 ): readonly [sql: string, binds: ReadonlyArray<unknown>] {
972 const opts = this.options
973 withoutTransform = withoutTransform || this.disableTransforms
974 const cache = withoutTransform ? this.statementCacheNoTransform : this.statementCache
975 const cached = cache.get(statement)
976 if (cached !== undefined && placeholderOverride === undefined) {
977 return cached
978 }
979
980 const segments = statement.segments
981 const len = segments.length
982
983 let sql = ""
984 const binds: Array<unknown> = []
985 let placeholderCount = 0
986 const placeholder = placeholderOverride ?? ((u: unknown) => opts.placeholder(++placeholderCount, u))
987 const placeholderNoIncrement = (u: unknown) => opts.placeholder(placeholderCount, u)
988 const placeholders = makePlaceholdersArray(placeholder)
989
990 for (let i = 0; i < len; i++) {
991 const segment = segments[i]
992
993 switch (segment._tag) {
994 case "Literal": {
995 sql += segment.value
996 if (segment.params) {
997 binds.push.apply(binds, segment.params as any)
998 }
999 break
1000 }
1001
1002 case "Identifier": {
1003 sql += opts.onIdentifier(segment.value, withoutTransform)
1004 break
1005 }
1006
1007 case "Parameter": {
1008 sql += placeholder(segment.value)
1009 binds.push(segment.value)
1010 break
1011 }
1012
1013 case "ArrayHelper": {
1014 if (segment.value.length === 0) {
1015 const inKeyword = emptyInKeyword.exec(sql)
1016 if (inKeyword === null || isInLineComment(sql, inKeyword.index)) {
1017 sql += "()"
1018 } else if (this.dialect === "pg") {
1019 // an empty subquery is typed as text in postgres, so compare with an
1020 // empty array, which takes the type of the left operand instead
1021 sql = sql.slice(0, inKeyword.index) + (inKeyword[1] ? "<> ALL('{}')" : "= ANY('{}')")
1022 } else {
1023 sql += "(SELECT NULL WHERE 1=0)"

Callers

nothing calls this directly

Calls 15

makePlaceholdersArrayFunction · 0.85
isInLineCommentFunction · 0.85
extractPrimitiveFunction · 0.85
generateColumnsFunction · 0.85
sliceMethod · 0.80
keysMethod · 0.80
compileMethod · 0.80
placeholderFunction · 0.70
getMethod · 0.65
pushMethod · 0.65
setMethod · 0.65
placeholderMethod · 0.45

Tested by

no test coverage detected