Skip to content

Commit

Permalink
add the try functions to bind rows to map
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Jul 18, 2024
1 parent 0102732 commit 9f9022b
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions dml_select_bind_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,36 @@

package sqlx

// TryBindToMapKV is the same as BindToMapKV, but calls it only if err==nil.
func TryBindToMapKV[M ~map[K]V, K comparable, V any](rows Rows, err error, initcap int) (m M, e error) {
if err == nil {
m, err = BindToMapKV[M](rows, initcap)
}

e = err
return
}

// TryBindToMapBool is the same as BindToMapBool, but calls it only if err==nil.
func TryBindToMapBool[M ~map[K]bool, K comparable](rows Rows, err error, initcap int) (m M, e error) {
if err == nil {
m, err = BindToMapBool[M](rows, initcap)
}

e = err
return
}

// TryBindToMapEmptyStruct is the same as BindToMapEmptyStruct, but calls it only if err==nil.
func TryBindToMapEmptyStruct[M ~map[K]struct{}, K comparable](rows Rows, err error, initcap int) (m M, e error) {
if err == nil {
m, err = BindToMapEmptyStruct[M](rows, initcap)
}

e = err
return
}

// BindToMapKV scans two columns as key and value, and inserts them into m.
//
// NOTICE: If rows.Rows is nil, do nothing. Or, it will close the rows.
Expand Down

0 comments on commit 9f9022b

Please sign in to comment.