Skip to content

Commit

Permalink
Accept not sql values in interpolation and safely wrap them
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Oct 7, 2024
1 parent ddcf04a commit ec659ad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/core/Sql.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {DriverSpecs} from './Driver.ts'
import type {Emitter} from './Emitter.ts'
import {type HasSql, getSql, internalSql} from './Internal.ts'
import {type HasSql, getSql, hasSql, internalSql} from './Internal.ts'
import type {Runtime} from './MetaData.ts'
import type {FieldData} from './expr/Field.ts'
import type {JsonPath} from './expr/Json.ts'
Expand Down Expand Up @@ -150,15 +150,17 @@ export class Sql<Value = unknown> implements HasSql<Value> {

export function sql<T>(
strings: TemplateStringsArray,
...inner: Array<HasSql>
...inner: Array<HasSql | unknown>
): Sql<T> {
const sql = new Sql<T>()

for (let i = 0; i < strings.length; i++) {
sql.unsafe(strings[i]!)
if (i < inner.length) {
const insert = inner[i]!
sql.add(insert)
const insert = inner[i]
if (insert !== null && typeof insert === 'object' && hasSql(insert))
sql.add(insert)
else sql.value(insert)
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/core/Sql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ suite(import.meta, test => {
test('inline value', () => {
test.equal(emit(sql.empty().inline(1)), '1')
})

test('unknown values', () => {
test.equal(emit(sql`${1}`), '1')
test.equal(emit(sql`${null}`), 'null')
test.equal(emit(sql`${'"'}`), JSON.stringify('"'))
})
})

0 comments on commit ec659ad

Please sign in to comment.