Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/isolate-sql-compiler-cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Isolate compiled SQL fragment caches by compiler instance.
21 changes: 13 additions & 8 deletions packages/effect/src/unstable/sql/Statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,22 +799,25 @@ export const makeCompiler = <C extends Custom<any, any, any, any> = any>(
self.options = options
self.dialect = options.dialect
self.disableTransforms = false
self.statementCache = new WeakMap<Fragment, CompiledStatement>()
self.statementCacheNoTransform = new WeakMap<Fragment, CompiledStatement>()
return self
}

type CompiledStatement = readonly [sql: string, binds: ReadonlyArray<unknown>]

interface CompilerImpl extends Compiler {
readonly options: CompilerOptions
readonly disableTransforms: boolean
readonly statementCache: WeakMap<Fragment, CompiledStatement>
readonly statementCacheNoTransform: WeakMap<Fragment, CompiledStatement>
compile(
statement: Fragment,
withoutTransform?: boolean,
placeholderOverride?: (u: unknown) => string
): readonly [sql: string, binds: ReadonlyArray<unknown>]
): CompiledStatement
}

const statementCacheSymbol = Symbol.for("effect/unstable/sql/Statement/statementCache")
const statementCacheNoTransformSymbol = Symbol.for("effect/unstable/sql/Statement/statementCacheNoTransform")

const CompilerProto = {
compile(
this: CompilerImpl,
Expand All @@ -824,9 +827,10 @@ const CompilerProto = {
): readonly [sql: string, binds: ReadonlyArray<unknown>] {
const opts = this.options
withoutTransform = withoutTransform || this.disableTransforms
const cacheSymbol = withoutTransform ? statementCacheNoTransformSymbol : statementCacheSymbol
if (cacheSymbol in statement) {
return (statement as any)[cacheSymbol]
const cache = withoutTransform ? this.statementCacheNoTransform : this.statementCache
const cached = cache.get(statement)
if (cached !== undefined) {
return cached
}

const segments = statement.segments
Expand Down Expand Up @@ -1033,7 +1037,8 @@ const CompilerProto = {
if (placeholderOverride !== undefined) {
return result
}
return (statement as any)[cacheSymbol] = result
cache.set(statement, result)
return result
},

get withoutTransform() {
Expand Down
18 changes: 18 additions & 0 deletions packages/effect/test/unstable/sql/Statement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,22 @@ describe("Statement", () => {
assert.deepStrictEqual(nested.array([[row]]), [[{ OWN: 2 }]])
assert.deepStrictEqual(flat.array([row]), [{ OWN: 2 }])
})

it("compiles one fragment independently for each compiler", () => {
const postgres = Statement.makeCompiler({
dialect: "pg",
placeholder: (index) => `$${index}`,
onIdentifier: Statement.defaultEscape("\""),
onRecordUpdate: () => ["", []],
onCustom: () => ["", []]
})
const sqlite = Statement.makeCompilerSqlite()
const fragment = Statement.fragment([
Statement.identifier("value"),
Statement.parameter(1)
])

assert.deepStrictEqual(postgres.compile(fragment, false), ["\"value\"$1", [1]])
assert.deepStrictEqual(sqlite.compile(fragment, false), ["\"value\"?", [1]])
})
})
Loading