Skip to content

fix: join with inner sql statement #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2025

Conversation

caoer
Copy link
Contributor

@caoer caoer commented May 31, 2025

No description provided.

Copy link

vercel bot commented May 31, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
pgkit ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 6, 2025 7:14am

@caoer
Copy link
Contributor Author

caoer commented May 31, 2025

@mmkal sorry for the reformatting of the file. haven't found out yet where is wrong for my setup.

for this issue is just fixing the issue with join + inner:

when processing SQL fragments that have the token: "sql", the code is directly pushing value.sql to segments without processing the values from those inner SQL fragments. This means the parameter values from the inner SQL fragments are not being added to the outer query's values array, and the parameter placeholders in the inner SQL are not being adjusted.

I've added a test to demo this issue.

@caoer
Copy link
Contributor Author

caoer commented May 31, 2025

btw, recommend https://biomejs.dev/ which is super for lint and format. (I'm not in anything associated with them, just. a fan)

@mmkal
Copy link
Owner

mmkal commented Jun 4, 2025

I have a custom eslint plugin I use on all my projects, it has a prettier config preset, and a few typescript-eslint rules and others. You should be able to run pnpm eslint . --fix in the relevant project - until then I can't really review this. Cursor/vscode should auto format too but only if you have the eslint extension which maybe you don't if you use biome.

I might set up autofix.ci which will push a commit automatically though.

@caoer
Copy link
Contributor Author

caoer commented Jun 4, 2025

@mmkal I ran pnpm eslint . --fix under client, it also changed readme.md, but I did not submit it yet.
there are some code can not revert to previous ones due to how biome and eslint different, but it should be much more. readable now

@@ -1,19 +1,19 @@
import pgPromise from 'pg-promise'
import {QueryError} from './errors'
import {nameQuery} from './naming'
import {StandardSchemaV1} from './standard-schema/contract'
import type {StandardSchemaV1} from './standard-schema/contract'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

being able to identify import 'type' is probably the biggest reason I switched to biome

const innerArgs = value.templateArgs() as Parameters<SQLTagFunction>
const innerResult = sqlFnInner({priorValues: values.length + priorValues}, ...innerArgs)
segments.push(...innerResult.segments())
values.push(...innerResult.values)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mmkal here is the fix

@caoer
Copy link
Contributor Author

caoer commented Jun 4, 2025

ah. I think the formatting break the tests. let me work on it

@caoer
Copy link
Contributor Author

caoer commented Jun 4, 2025

@mmkal it is ready now. I think the CI are broken? looks it is failing on the pgkit branch

@mmkal
Copy link
Owner

mmkal commented Jun 4, 2025

Thanks! And yes I need to fix - can merge this once CI is green, will try to do today.

Comment on lines 142 to 172
test('join param', async () => {
const parts = []
parts.push(sql`name = ${'user name'}`, sql`location = ${'london'}`)

const query = sql`
update users
set ${sql.join(parts, sql`, `)}
where id = ${1}
`

expect(query).toMatchInlineSnapshot(`
{
"name": "update_d57bde2",
"parse": [Function],
"segments": [Function],
"sql": "
update users
set name = $1, location = $2
where id = $3
",
"templateArgs": [Function],
"then": [Function],
"token": "sql",
"values": [
"user name",
"london",
1,
],
}
`)
})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks good - but i'd prefer a runnable query to make sure it really works. something like:

Suggested change
test('join param', async () => {
const parts = []
parts.push(sql`name = ${'user name'}`, sql`location = ${'london'}`)
const query = sql`
update users
set ${sql.join(parts, sql`, `)}
where id = ${1}
`
expect(query).toMatchInlineSnapshot(`
{
"name": "update_d57bde2",
"parse": [Function],
"segments": [Function],
"sql": "
update users
set name = $1, location = $2
where id = $3
",
"templateArgs": [Function],
"then": [Function],
"token": "sql",
"values": [
"user name",
"london",
1,
],
}
`)
})
test('join param', async () => {
const parts = [
sql`id = ${11}`,
sql`name = ${'eleven'}`,
]
const query = sql`
update edge_cases_test
set ${sql.join(parts, sql`, `)}
where id = ${1}
`
expect(query).toMatchInlineSnapshot()
await client.query(query)
const result = await client.one(sql`select * from edge_cases_test where id = ${11}`)
expect(result).toEqual({id: 11, name: 'eleven'})
})

@caoer
Copy link
Contributor Author

caoer commented Jun 6, 2025

@mmkal updated

@mmkal mmkal merged commit 748bd9a into mmkal:pgkit Jun 6, 2025
4 checks passed
@mmkal
Copy link
Owner

mmkal commented Jun 6, 2025

@caoer thank you - good find! i might add just one more test and will publish asap

Copy link

github-actions bot commented Jun 6, 2025

Release v0.5.0 addresses this.

Copy link

github-actions bot commented Jun 6, 2025

Release v0.5.0 addresses this.

2 similar comments
Copy link

github-actions bot commented Jun 6, 2025

Release v0.5.0 addresses this.

Copy link

github-actions bot commented Jun 6, 2025

Release v0.5.0 addresses this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants