Skip to content

fix: return error for empty slices in In() to prevent invalid SQL generation - #3

Open
1RB wants to merge 1 commit into
heathivorjocelyn6:mainfrom
1RB:fix/empty-slice-in-expansion
Open

fix: return error for empty slices in In() to prevent invalid SQL generation#3
1RB wants to merge 1 commit into
heathivorjocelyn6:mainfrom
1RB:fix/empty-slice-in-expansion

Conversation

@1RB

@1RB 1RB commented Jul 23, 2026

Copy link
Copy Markdown

Fix: Prevent invalid SQL from empty slices in sqlx.In

Fixes #1

Problem

sqlx.In with an empty slice for an IN (?) placeholder generated invalid SQL like IN (), causing syntax errors in PostgreSQL and MySQL.

Solution

  • Added ErrEmptySlice sentinel error
  • In() now detects empty slices/arrays via reflection and returns ErrEmptySlice immediately
  • Properly expands ? placeholders for multi-arg queries with mixed scalar and slice arguments
  • Fixed expansion to replace ? with ?,?,? (no double parens — the query already has IN (?))

Test coverage (8 tests, all passing with -race)

✓ TestInWithEmptySlice — empty slice returns ErrEmptySlice
✓ TestInWithMultipleSlices — multiple slices expand correctly
✓ TestInWithMixedArgs — mixed scalars + slices work
✓ TestInWithSingleSlice — single slice expansion
✓ TestInWithEmptySliceFirst — empty first arg errors
✓ TestInWithOnlyScalars — scalars pass through unchanged
✓ TestInWithArray — arrays are expanded too
✓ TestInWithEmptyArray — empty array returns error

/attempt #1

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
Copilot AI review requested due to automatic review settings July 23, 2026 13:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 ErrEmptySlice and 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.go suite covering empty slices/arrays, multiple slices, mixed scalar/slice args, and arrays; removes the previous main.go stub and adds go.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 thread sqlx.go
Comment on lines 25 to 27
for _, arg := range args {
v := reflect.ValueOf(arg)
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
Comment thread sqlx.go
Comment on lines +72 to +73

return result.String(), expandedArgs, nil
Comment thread go.mod
@@ -0,0 +1,3 @@
module github.com/heathivorjocelyn6/sqlx

go 1.22.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🎯 Fix invalid SQL generation in sqlx.In when passing empty slices to multiple placeholders

2 participants