Skip to content
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

Is there a way to parse existing sql queries into sqlbuilder and then add more on top of the parsed query? #115

Closed
vamshiaruru32 opened this issue Jul 14, 2023 · 2 comments

Comments

@vamshiaruru32
Copy link

For example, let us say I have a query like "SELECT id, name, slug FROM product" which I am already using in my code else where. Now I want to add more conditions on top of that using sqlbuilder. Is there a way to parse this into a select statement? I have tried the following

	sb := sqlbuilder.NewSelectBuilder()
	sb.SQL(query).Where(
		sb.In("id", 1, 2, 3),
	)
	q, args = sb.Build()
	fmt.Println(q, args)

But this prints

SELECT id, name, slug FROM page SELECT  FROM  WHERE id IN (?, ?, ?) [1 2 3]

With an extra select from. Is this a valid usecase for this library?

Thanks for any help, the library seems awesome!

@huandu
Copy link
Owner

huandu commented Jul 15, 2023

There is no such feature right now. I'm considering how to support it. One possible design is that: When building a SQL, Builders ignore all parts which is not set.

For instance, if we don't call Select and From on a SelectBuilder, the SELECT and FROM clause are completely missing in the final output.

@vamshiaruru32
Copy link
Author

Thanks, that sounds useful. one very useful thing this unlocks is being able to use this sqlc. Use sqlc to generate models from a query, and add dynamic where and pagination clauses using this library.

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

No branches or pull requests

2 participants