sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Add sqlc.slice() to support IN clauses in MySQL (#695)

Open Jille opened this issue 2 years ago • 2 comments

This feature allows passing in a slice to an IN clause. Adding the new function sqlc.slice() as opposed to overloading the parsing of "IN (?)" was chosen to guarantee backwards compatibility.

SELECT * FROM tab WHERE col IN (sqlc.slice("go_param_name"))

This commit is based on https://github.com/kyleconroy/sqlc/pull/1312 by Paul Cameron. I just rebased and did some cleanup.

Co-authored-by: Paul Cameron [email protected]

Jille avatar Aug 23 '22 08:08 Jille

I'm not sure why the tests fail. It complains about codegen.json mismatching and says there's thousands of {mis,}matching lines, but the file is only ~500 lines. I suspect something regenerates it, but I failed to find the culprit.

Jille avatar Aug 23 '22 09:08 Jille

Does this have a path towards supporting multiple IN args? e.g. (col1, col2, ...) IN ((?,?,...), (?,?,...))

derekperkins avatar Aug 25 '22 13:08 derekperkins

what's the status of this PR? What's missing?

Rhymond avatar Oct 21 '22 16:10 Rhymond

I'm waiting for @kyleconroy to review. I'm hoping he can give me a pointer for the failing test.

Does this have a path towards supporting multiple IN args?

That should be doable to add on top of this PR.

Jille avatar Oct 21 '22 17:10 Jille

I'm waiting for @kyleconroy to review. I'm hoping he can give me a pointer for the failing test.

Does this have a path towards supporting multiple IN args?

That should be doable to add on top of this PR.

I'm looking at your code and thinking would it be better to do what sqlx did for for queries with IN (please check https://jmoiron.github.io/sqlx/ "in Queries"). In that way you would keep original query as parameter and you wouldn't need to use custom placeholder in your case /*SLICE:ids*/? You could replace all sqlc.slice() funcs with a question mark (same as sqlc.arg does) and then only if sqlc.slice() exists call sqlx.in in the generated function to replace query question marks and prepare argument list. What do you think?

Rhymond avatar Oct 23 '22 18:10 Rhymond

@Rhymond apologies for the late reply

Your proposal consists of two parts: a) Keep using ? as the place holder, and b) Factor out the generated code and call a library instead.

a) I don't mind much which way to go. /*SLICE:ids*/ was easier to work with in fewer lines of generated code. b) We currently don't ever import github.com/kyleconroy/sqlc, I'd like Kyle's opinion on it before going that direction.

I'd propose to first get this in, and we can easily refactor later.

Jille avatar Jan 02 '23 08:01 Jille

@kyleconroy this is ready to be merged

I've rebased and the test failures magically went away. Maybe I was based on a bad commit before ¯\(ツ)

Jille avatar Jan 02 '23 08:01 Jille

I've tested this branch out in a new project and found it works as expected so far. Would love to see this merged! Thank you @Jille for the work!

ajaxify avatar Jan 08 '23 22:01 ajaxify

Hi @kyleconroy we're sponsoring this project and would really love to see this feature. It's also been requested a lot (https://github.com/kyleconroy/sqlc/issues/695) by other people.

Is there anything missing that prevents this from getting merged? Happy to help as well.

Thanks for your work!

roccoblues avatar Jan 11 '23 09:01 roccoblues

appreciated if this feature get supported as soon as possible

johnwayne19860314 avatar Feb 21 '23 02:02 johnwayne19860314

this feature will be merged on the next release?

uasouz avatar Feb 23 '23 17:02 uasouz

How can we move this forward? What's missing? I'm keen to help

Rhymond avatar Mar 06 '23 10:03 Rhymond

I'm looking forward this feature!

ogugu9 avatar Mar 14 '23 02:03 ogugu9

@kyleconroy

koolay avatar Apr 02 '23 15:04 koolay

I've rebased to main.

Jille avatar Apr 03 '23 07:04 Jille

would it be possible to create a new release already so this important feature included?

ecornelisse avatar Apr 14 '23 14:04 ecornelisse