Skip to content

Commit

Permalink
fix int32Access.Equals(), upgrade pgx, update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
emicklei committed Jan 23, 2025
1 parent 761cef4 commit c75d4da
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file.

## [v1.8.1] - 2025-01-23

- fix int32Access.Equals to accept int32 too
- add NewColumns()

## [v1.8.0] - 2025-01-22

- add access to CommandTag in iterator to get RowsAffected value
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ or by example
Where(products.ID.Equals(p.ID)).
Returning(products.Code)

or by collecting columns first

cols := NewColumns()
cols.Add(products.Code.Set("testme"))
if categoryWhasChanged {
cols.Add(products.CategoryID.Set(changedValue))
}
m := products.Update(cols...).Where(products.ID.Equals(10)).

### Delete

m := products.Delete().Where(products.ID.Equals(10))
Expand Down
2 changes: 1 addition & 1 deletion access.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type ColumnAccessorSlice []ColumnAccessor
// This can be used to build a list of columns e.g when updating.
func NewColumns() (list ColumnAccessorSlice) { return }

// Add appends a non-nil column to the list
// Add appends a non-nil column accessor to the list
func (s *ColumnAccessorSlice) Add(c ColumnAccessor) {
if c != nil {
*s = append(*s, c)
Expand Down
5 changes: 4 additions & 1 deletion access_int32.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (a int32Access) Equals(intLike any) binaryExpression {
if i, ok := intLike.(int); ok {
return makeBinaryOperator(a, "=", valuePrinter{v: i})
}
if i, ok := intLike.(int32); ok {
return makeBinaryOperator(a, "=", valuePrinter{v: i})
}
if ia, ok := intLike.(int32Access); ok {
return makeBinaryOperator(a, "=", ia)
}
Expand All @@ -64,7 +67,7 @@ func (a int32Access) Equals(intLike any) binaryExpression {
if p, ok := intLike.(FieldAccess[pgtype.Int4]); ok {
return makeBinaryOperator(a, "=", p)
}
panic(fmt.Sprintf("int or Int32Access or *QueryParameter expected, got %T", intLike))
panic(fmt.Sprintf("int, int32, Int32Access or *QueryParameter expected, got %T", intLike))
}

func (a int32Access) Compare(op string, i int) binaryExpression {
Expand Down
16 changes: 16 additions & 0 deletions access_int32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,19 @@ func TestInt32InEmpty(t *testing.T) {
t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
}
}
func TestInt32Euals(t *testing.T) {
ta := NewInt32Access(ci, nil)
in := ta.Equals(int32(1))
sql := SQL(in)
if got, want := sql, "(t1.label = 1)"; got != want {
t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
}
}
func TestIntEuals(t *testing.T) {
ta := NewInt32Access(ci, nil)
in := ta.Equals(int(1))
sql := SQL(in)
if got, want := sql, "(t1.label = 1)"; got != want {
t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
}
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ require (
github.com/emicklei/tre v1.7.0
github.com/google/uuid v1.6.0
github.com/iancoleman/strcase v0.3.0
github.com/jackc/pgx/v5 v5.7.1
github.com/jackc/pgx/v5 v5.7.2
github.com/shopspring/decimal v1.4.0
)

require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/text v0.21.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7Ulw
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs=
github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA=
github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI=
github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -26,10 +28,14 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down

0 comments on commit c75d4da

Please sign in to comment.