Skip to content

Commit 7a66e80

Browse files
committed
first pkValue arg for QueryStruct and QueryStructOrNil
1 parent 5b41b84 commit 7a66e80

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

db/query.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ func QueryValueOrDefault[T any](ctx context.Context, query string, args ...any)
5858
return value, err
5959
}
6060

61-
// QueryStruct uses the passed pkValues to query a table row
61+
// QueryStruct uses the passed pkValue+pkValues to query a table row
6262
// and scan it into a struct of type S that must have tagged fields
6363
// with primary key flags to identify the primary key column names
64-
// for the passed pkValues and a table name.
65-
func QueryStruct[S any](ctx context.Context, pkValues ...any) (row *S, err error) {
66-
if len(pkValues) == 0 {
67-
return nil, errors.New("missing primary key values")
64+
// for the passed pkValue+pkValues and a table name.
65+
func QueryStruct[S any](ctx context.Context, pkValue any, pkValues ...any) (row *S, err error) {
66+
if len(pkValues) > 0 {
67+
pkValues = append([]any{pkValues}, pkValues...)
6868
}
6969
t := reflect.TypeOf(row).Elem()
7070
if t.Kind() != reflect.Struct {
@@ -89,14 +89,14 @@ func QueryStruct[S any](ctx context.Context, pkValues ...any) (row *S, err error
8989
return row, nil
9090
}
9191

92-
// QueryStructOrNil uses the passed pkValues to query a table row
92+
// QueryStructOrNil uses the passed pkValue+pkValues to query a table row
9393
// and scan it into a struct of type S that must have tagged fields
9494
// with primary key flags to identify the primary key column names
95-
// for the passed pkValues and a table name.
95+
// for the passed pkValue+pkValues and a table name.
9696
// Returns nil as row and error if no row could be found with the
97-
// passed pkValues.
98-
func QueryStructOrNil[S any](ctx context.Context, pkValues ...any) (row *S, err error) {
99-
row, err = QueryStruct[S](ctx, pkValues...)
97+
// passed pkValue+pkValues.
98+
func QueryStructOrNil[S any](ctx context.Context, pkValue any, pkValues ...any) (row *S, err error) {
99+
row, err = QueryStruct[S](ctx, pkValue, pkValues...)
100100
if err != nil {
101101
if errors.Is(err, sql.ErrNoRows) {
102102
return nil, nil

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ module github.com/domonda/go-sqldb
33
go 1.18
44

55
require (
6-
github.com/domonda/go-errs v0.0.0-20220527085304-63cf6ad85d71
7-
github.com/domonda/go-types v0.0.0-20220603104906-eadd2cf77191
6+
github.com/domonda/go-errs v0.0.0-20220803101145-9c94ff28df79
7+
github.com/domonda/go-types v0.0.0-20220803103538-ba179df4d6de
88
github.com/go-sql-driver/mysql v1.6.0
99
github.com/lib/pq v1.10.6
10-
github.com/stretchr/testify v1.7.1
11-
golang.org/x/exp v0.0.0-20220602145555-4a0574d9293f
10+
github.com/stretchr/testify v1.8.0
11+
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
1212
)
1313

1414
require (

go.sum

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4-
github.com/domonda/go-errs v0.0.0-20220527085304-63cf6ad85d71 h1:WRag+fUJENLRM8N/wp6gf/0i1aEkLY9prNgoFQsWeso=
5-
github.com/domonda/go-errs v0.0.0-20220527085304-63cf6ad85d71/go.mod h1:suiFfPp8l6I+OOaKgPK/bfX7Ci9ZtFRgPh5VNE0HPao=
4+
github.com/domonda/go-errs v0.0.0-20220803101145-9c94ff28df79 h1:fBQtqAIzfFx/pGWhcnask+x3OjOfJ/Io1ZY0qNEDtAQ=
5+
github.com/domonda/go-errs v0.0.0-20220803101145-9c94ff28df79/go.mod h1:WvIoE59Dfs0hhB2GYSlwowlBr2WWGXf/F74bg6HWUpQ=
66
github.com/domonda/go-pretty v0.0.0-20220317123925-dd9e6bef129a h1:6/Is0KGl5Ot3E8ZLAgAFWYiSRdU+3t3jL38+5yIlCV4=
77
github.com/domonda/go-pretty v0.0.0-20220317123925-dd9e6bef129a/go.mod h1:3QkM8UJdyJMeKZiIo7hYzSkQBpRS3k0gOHw4ysyEIB4=
8-
github.com/domonda/go-types v0.0.0-20220603104906-eadd2cf77191 h1:NcOIFS41zSztJog+aPw48HV8oVhRQPV0B6M6CshwFqc=
9-
github.com/domonda/go-types v0.0.0-20220603104906-eadd2cf77191/go.mod h1:qZTRjdjIXo3g+8PUhfpkKbMPGsLVTuF3H7/AX5CzNeQ=
8+
github.com/domonda/go-types v0.0.0-20220803103538-ba179df4d6de h1:JQpcRcKuUG15dmfT1LkrRgLl+wpRAvIcHLqOS6/Q2Lo=
9+
github.com/domonda/go-types v0.0.0-20220803103538-ba179df4d6de/go.mod h1:OBigUM85hSP8diN6QHh2PVGD0GamVO1C4TeJHWxBePA=
1010
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
1111
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
1212
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
@@ -21,12 +21,14 @@ github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
2121
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2222
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2323
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
24-
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
24+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
2525
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
26+
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
27+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
2628
github.com/ungerik/go-reflection v0.0.0-20220113085621-6c5fc1f2694a h1:9vfYtqoyrPw08TbSLxkSXEflp6iXa3RL86Qjs+DrVas=
2729
github.com/ungerik/go-reflection v0.0.0-20220113085621-6c5fc1f2694a/go.mod h1:6Hnd2/4g3Tpt6TjvxHx8wXOZziwApVxRdIGkr7vNpXs=
28-
golang.org/x/exp v0.0.0-20220602145555-4a0574d9293f h1:KK6mxegmt5hGJRcAnEDjSNLxIRhZxDcgwMbcO/lMCRM=
29-
golang.org/x/exp v0.0.0-20220602145555-4a0574d9293f/go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys=
30+
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
31+
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA=
3032
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3133
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
3234
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

0 commit comments

Comments
 (0)