Skip to content

feat: Implement joins with golang templates #6429

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 43 commits into from
Mar 10, 2023

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Mar 2, 2023

Golang template alternative to #6371

What this does

This adds database joins and dynamic SQL queries. I only did this for workspace builds in this PR. Read the other PR and this issue: #6426.

Fixes: #6426

Next work

I will use templates for authorized queries next, remove the strings replace.

@Emyrk Emyrk marked this pull request as ready for review March 8, 2023 15:22
// "$1, $2" instead of "$1, $1".
// 2. SQLx does not handle uuid arrays.
// 3. SQLx only supports ":name" style arguments and breaks "::" type casting.
func bindNamed(query string, arg interface{}) (newQuery string, args []interface{}, err error) {
Copy link
Member

Choose a reason for hiding this comment

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

Is there no other way around this? I really dislike the regexp rewriting and reflection on every single query.

Is sqlx not smarter than this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually SQLx is a bit dummber. I took this code implementation from SQLx and changed some things. The usage for SQLx is sqlx.Named(). But it doesn't handle things that I put in the function comments, so I adjusted it.

https://github.com/jmoiron/sqlx/blob/master/named.go#LL421-L436C2

Instead of a regex, they wrote a more proper parser: https://github.com/jmoiron/sqlx/blob/master/named.go#L331

But the parser doesn't work. It parses comments and they use :arg as arguments, so when we typecast with ::, it's seen as escaping a colon literal. So our sql queries would have to do ::::.

The SQLx parser is honestly really dumb with actual SQL.

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

Looks fine to me except for loadQueries, this should happen as close to startup as possible.

@Emyrk Emyrk merged commit 8b125d6 into main Mar 10, 2023
@Emyrk Emyrk deleted the stevenmasley/build_with_template branch March 10, 2023 15:44
@github-actions github-actions bot locked and limited conversation to collaborators Mar 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Discussion: How should we handle joins?
3 participants