fix: return error for empty slices in In() to prevent invalid SQL generation - #3
Open
1RB wants to merge 1 commit into
Open
fix: return error for empty slices in In() to prevent invalid SQL generation#31RB wants to merge 1 commit into
1RB wants to merge 1 commit into
Conversation
When sqlx.In receives an empty slice for an IN (?) placeholder, it previously generated invalid SQL like "IN ()" which causes syntax errors in PostgreSQL and MySQL. Now it returns ErrEmptySlice. Changes: - Add ErrEmptySlice sentinel error - In() detects empty slices/arrays via reflection and returns ErrEmptySlice - In() properly expands ? placeholders for multi-arg queries with mixed scalar and slice arguments - Fixed query expansion: replaces ? with ?,?,? (no double parens) Test coverage (8 tests, all passing with -race): - Empty slice returns ErrEmptySlice - Multiple non-empty slices expand correctly - Mixed scalar and slice args work - Single slice expansion - Empty slice as first arg errors - All-scalar args pass through unchanged - Arrays (not just slices) are expanded - Empty array returns error Fixes heathivorjocelyn6#1
There was a problem hiding this comment.
Pull request overview
This PR updates the sqlx.In helper to proactively reject empty slice/array arguments (to avoid generating invalid SQL like IN ()) and adds unit tests to cover common expansion scenarios.
Changes:
- Introduces
ErrEmptySliceand returns it when any slice/array argument has length 0. - Reworks
In()to expand slice/array args into a flattened argument list and to expand?placeholders accordingly. - Adds a new
sqlx_test.gosuite covering empty slices/arrays, multiple slices, mixed scalar/slice args, and arrays; removes the previousmain.gostub and addsgo.mod.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| sqlx.go | Adds ErrEmptySlice and implements new In() placeholder/argument expansion logic. |
| sqlx_test.go | Adds unit tests validating slice/array expansion behavior and empty-slice erroring. |
| main.go | Removes unused example main program. |
| go.mod | Introduces a Go module definition for the repository. |
Comments suppressed due to low confidence (1)
sqlx.go:51
- The second pass also calls reflect.ValueOf on args[argIndex] and will panic for nil arguments. Treat nil as a non-slice scalar placeholder and skip reflection in that case.
v := reflect.ValueOf(args[argIndex])
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
if v.Len() == 0 {
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
25
to
27
| for _, arg := range args { | ||
| v := reflect.ValueOf(arg) | ||
| if v.Kind() == reflect.Slice || v.Kind() == reflect.Array { |
Comment on lines
+72
to
+73
|
|
||
| return result.String(), expandedArgs, nil |
| @@ -0,0 +1,3 @@ | |||
| module github.com/heathivorjocelyn6/sqlx | |||
|
|
|||
| go 1.22.2 | |||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: Prevent invalid SQL from empty slices in
sqlx.InFixes #1
Problem
sqlx.Inwith an empty slice for anIN (?)placeholder generated invalid SQL likeIN (), causing syntax errors in PostgreSQL and MySQL.Solution
ErrEmptySlicesentinel errorIn()now detects empty slices/arrays via reflection and returnsErrEmptySliceimmediately?placeholders for multi-arg queries with mixed scalar and slice arguments?with?,?,?(no double parens — the query already hasIN (?))Test coverage (8 tests, all passing with
-race)/attempt #1