Skip to content

Commit 4d63a59

Browse files
committed
Fix dynamic in helper after insert porsager#305
1 parent 576ac28 commit 4d63a59

File tree

6 files changed

+48
-3
lines changed

6 files changed

+48
-3
lines changed

cjs/src/types.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ const builders = Object.entries({
145145
).join(',') + ')values' +
146146
valuesBuilder(Array.isArray(first) ? first : [first], parameters, types, transform, columns)
147147
}
148-
}).map(([x, fn]) => ([new RegExp('(^|[\\s(])' + x, 'i'), fn]))
148+
}).map(([x, fn]) => ([new RegExp('(^|[\\s(])' + x + '($|[\\s(])', 'i'), fn]))
149149

150150
function notTagged() {
151151
throw Errors.generic('NOT_TAGGED_CALL', 'Query not called as a tagged template literal')

cjs/tests/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,21 @@ t('dynamic insert pluck', async() => {
893893
return [null, (await sql`insert into test ${ sql(x, 'a') } returning *`)[0].b, await sql`drop table test`]
894894
})
895895

896+
t('dynamic in after insert', async() => {
897+
await sql`create table test (a int, b text)`
898+
const [{ x }] = await sql`
899+
with x as (
900+
insert into test values (1, 'hej')
901+
returning *
902+
)
903+
select 1 in ${ sql([1, 2, 3]) } as x from x
904+
`
905+
return [
906+
true, x,
907+
await sql`drop table test`
908+
]
909+
})
910+
896911
t('array insert', async() => {
897912
await sql`create table test (a int, b int)`
898913
return [2, (await sql`insert into test (a, b) values ${ sql([1, 2]) } returning *`)[0].b, await sql`drop table test`]

deno/src/types.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ const builders = Object.entries({
146146
).join(',') + ')values' +
147147
valuesBuilder(Array.isArray(first) ? first : [first], parameters, types, transform, columns)
148148
}
149-
}).map(([x, fn]) => ([new RegExp('(^|[\\s(])' + x, 'i'), fn]))
149+
}).map(([x, fn]) => ([new RegExp('(^|[\\s(])' + x + '($|[\\s(])', 'i'), fn]))
150150

151151
function notTagged() {
152152
throw Errors.generic('NOT_TAGGED_CALL', 'Query not called as a tagged template literal')

deno/tests/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,21 @@ t('dynamic insert pluck', async() => {
895895
return [null, (await sql`insert into test ${ sql(x, 'a') } returning *`)[0].b, await sql`drop table test`]
896896
})
897897

898+
t('dynamic in after insert', async() => {
899+
await sql`create table test (a int, b text)`
900+
const [{ x }] = await sql`
901+
with x as (
902+
insert into test values (1, 'hej')
903+
returning *
904+
)
905+
select 1 in ${ sql([1, 2, 3]) } as x from x
906+
`
907+
return [
908+
true, x,
909+
await sql`drop table test`
910+
]
911+
})
912+
898913
t('array insert', async() => {
899914
await sql`create table test (a int, b int)`
900915
return [2, (await sql`insert into test (a, b) values ${ sql([1, 2]) } returning *`)[0].b, await sql`drop table test`]

src/types.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ const builders = Object.entries({
145145
).join(',') + ')values' +
146146
valuesBuilder(Array.isArray(first) ? first : [first], parameters, types, transform, columns)
147147
}
148-
}).map(([x, fn]) => ([new RegExp('(^|[\\s(])' + x, 'i'), fn]))
148+
}).map(([x, fn]) => ([new RegExp('(^|[\\s(])' + x + '($|[\\s(])', 'i'), fn]))
149149

150150
function notTagged() {
151151
throw Errors.generic('NOT_TAGGED_CALL', 'Query not called as a tagged template literal')

tests/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,21 @@ t('dynamic insert pluck', async() => {
893893
return [null, (await sql`insert into test ${ sql(x, 'a') } returning *`)[0].b, await sql`drop table test`]
894894
})
895895

896+
t('dynamic in after insert', async() => {
897+
await sql`create table test (a int, b text)`
898+
const [{ x }] = await sql`
899+
with x as (
900+
insert into test values (1, 'hej')
901+
returning *
902+
)
903+
select 1 in ${ sql([1, 2, 3]) } as x from x
904+
`
905+
return [
906+
true, x,
907+
await sql`drop table test`
908+
]
909+
})
910+
896911
t('array insert', async() => {
897912
await sql`create table test (a int, b int)`
898913
return [2, (await sql`insert into test (a, b) values ${ sql([1, 2]) } returning *`)[0].b, await sql`drop table test`]

0 commit comments

Comments
 (0)