Skip to content

Commit f54d157

Browse files
committed
added db.QueryStructOrNil
1 parent 2d1ca47 commit f54d157

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

db/queryrow.go renamed to db/querystruct.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ func QueryStruct[S any](ctx context.Context, pkValues ...any) (row *S, err error
4040
return row, nil
4141
}
4242

43+
// QueryStructOrNil uses the passed pkValues to query a table row
44+
// and scan it into a struct of type S that must have tagged fields
45+
// with primary key flags to identify the primary key column names
46+
// for the passed pkValues and a table name.
47+
// Returns nil as row and error if no row could be found with the
48+
// passed pkValues.
49+
func QueryStructOrNil[S any](ctx context.Context, pkValues ...any) (row *S, err error) {
50+
row, err = QueryStruct[S](ctx, pkValues...)
51+
return row, ReplaceErrNoRows(err, nil)
52+
}
53+
4354
func pkColumnsOfStruct(conn sqldb.Connection, t reflect.Type) (table string, columns []string, err error) {
4455
mapper := conn.StructFieldMapper()
4556
for i := 0; i < t.NumField(); i++ {

0 commit comments

Comments
 (0)