Skip to content

Commit

Permalink
add for update, skip locked. updated deps
Browse files Browse the repository at this point in the history
  • Loading branch information
emicklei committed Sep 25, 2024
1 parent 9816503 commit 0d791e0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 28 deletions.
16 changes: 9 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
module github.com/emicklei/pgtalk

go 1.18
go 1.21

toolchain go1.23.1

require (
github.com/emicklei/tre v1.5.0
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.5.2
github.com/shopspring/decimal v1.3.1
github.com/jackc/pgx/v5 v5.7.1
github.com/shopspring/decimal v1.4.0
)

require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/text v0.14.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
)
35 changes: 19 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/emicklei/tre v1.5.0 h1:TrxCR3qvBSPovV4cxuLTWmbjghwHU6og3gXiQStU+QU=
github.com/emicklei/tre v1.5.0/go.mod h1:3e+DTVKARET1BwLFwOIJwgYLUtkN+X9XZT0JDnb+OYY=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emicklei/tre v1.7.0 h1:bEZhzT3o+9lcJ4KQh+uf0mv7rmMe0LEtVDa/7F5iq/c=
github.com/emicklei/tre v1.7.0/go.mod h1:3e+DTVKARET1BwLFwOIJwgYLUtkN+X9XZT0JDnb+OYY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA=
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.2 h1:iLlpgp4Cp/gC9Xuscl7lFL1PhhW+ZLtXZcrfCt4C3tA=
github.com/jackc/pgx/v5 v5.5.2/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
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/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=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
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/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=
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=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 1 addition & 5 deletions operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,13 @@ func (o binaryExpression) Like(pattern string) SQLExpression {
type betweenAnd struct {
}

func makeBetweenAnd(reader ColumnAccessor, begin, end SQLExpression) betweenAnd { return betweenAnd{} }
func makeBetweenAnd(_ ColumnAccessor, _, _ SQLExpression) betweenAnd { return betweenAnd{} }

type unaryExpression struct {
Operator string
Operand SQLExpression
}

func makeUnaryOperator(operator string, operand SQLExpression) unaryExpression {
return unaryExpression{Operator: operator, Operand: operand}
}

func (u unaryExpression) SQLOn(w WriteContext) {
fmt.Fprintf(w, "%s (", u.Operator)
u.Operand.SQLOn(w)
Expand Down
27 changes: 27 additions & 0 deletions queryset.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type QuerySet[T any] struct {
having SQLExpression
orderBy []SQLWriter
sortOption string
selectFor string
skipLocked bool
}

func MakeQuerySet[T any](tableInfo TableInfo, selectors []ColumnAccessor) QuerySet[T] {
Expand Down Expand Up @@ -79,6 +81,12 @@ func (q QuerySet[T]) SQLOn(w WriteContext) {
if q.offset > 0 {
fmt.Fprintf(w, "\nOFFSET %d", q.offset)
}
if q.selectFor != "" {
fmt.Fprintf(w, "\nFOR %s", q.selectFor)
}
if q.skipLocked {
fmt.Fprint(w, "\nSKIP LOCKED")
}
}

// TableAlias will override the default table or view alias
Expand Down Expand Up @@ -220,3 +228,22 @@ func (d QuerySet[T]) FullJoin(otherQuerySet querySet) join {
joinType: fullOuterJoinType,
}
}

type SQL_FOR string

const (
FOR_UPDATE SQL_FOR = "UPDATE"
FOR_NO_KEY_UPDATE SQL_FOR = "NO KEY UPDATE"
FOR_SHARE SQL_FOR = "SHARE"
FOR_KEY_SHARE SQL_FOR = "KEY SHARE"
)

func (d QuerySet[T]) For(f SQL_FOR) QuerySet[T] {
d.selectFor = string(f)
return d
}

func (d QuerySet[T]) SkipLocked() QuerySet[T] {
d.skipLocked = true
return d
}
10 changes: 10 additions & 0 deletions queryset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,13 @@ func TestQueryWithParameter(t *testing.T) {
mock := newMockConnection(t)
q.Exec(mock.ctx(), mock, i42)
}

func TestSelectForSkipLocked(t *testing.T) {
q := MakeQuerySet[poly](polyTable, polyTable.Columns)
q = q.For(FOR_UPDATE)
q = q.SkipLocked()
q = q.Where(polyFUUID.Equals(1))
if got, want := oneliner(SQL(q)), "SELECT FROM public.polies p1 WHERE (p1.fuuid = 1) FOR UPDATE SKIP LOCKED"; got != want {
t.Errorf("got [%v]:%T want [%v]:%T", got, got, want, want)
}
}

0 comments on commit 0d791e0

Please sign in to comment.