Skip to content

Add Between/NotBetween expression helpers#400

Open
sonnemusk wants to merge 1 commit into
Masterminds:masterfrom
sonnemusk:feat/expr-between
Open

Add Between/NotBetween expression helpers#400
sonnemusk wants to merge 1 commit into
Masterminds:masterfrom
sonnemusk:feat/expr-between

Conversation

@sonnemusk

Copy link
Copy Markdown

Summary

Adds first-class BETWEEN / NOT BETWEEN support to squirrel's expression helpers, addressing #340.

Today the common workaround is:

.Where(sq.Expr("age BETWEEN ? AND ?", 18, 65))

This PR makes that consistent with Eq, Lt, Like, and friends:

.Where(sq.Between{"age": []int{18, 65}})
// → age BETWEEN ? AND ?

.Where(sq.NotBetween{"id": []int{1, 5}})
// → id NOT BETWEEN ? AND ?

// Short alias requested in #340
.Where(sq.Bt{"score": []interface{}{0, 100}})
// → score BETWEEN ? AND ?

API design

  • Between / NotBetween: map[string]interface{}, same multi-column AND style as Eq / Lt.
  • Values must be a two-element list or array (lower, upper). Other lengths, non-list values, and nil return an error.
  • Bt: type alias for Between (as suggested in the issue).
  • Empty Between{} evaluates to (1=1), matching empty Eq{}.
  • Bounds are bound as placeholders (?), so they work with PlaceholderFormat (dollar, colon, etc.).

A previous attempt in #180 used an unexported-field struct and was never usable; this follows the established map-based pattern instead.

Tests

Unit coverage in expr_test.go for:

  • basic Between / NotBetween / Bt
  • []T, []interface{}, and [2]T bounds
  • multi-key stable ordering
  • empty map
  • nil / non-list / wrong-length errors
  • use inside Select(...).Where(...)
go test ./...

Related

Closes #340

Add map-style Between and NotBetween Sqlizers that emit
`col BETWEEN ? AND ?` / `col NOT BETWEEN ? AND ?` with two bound
args per column. Values must be two-element lists or arrays.

Also provide Bt as a type alias for Between, as suggested in Masterminds#340.
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.

Add sq.Between feature

1 participant