Skip to content

Commit

Permalink
update the try-bind error
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Jul 19, 2024
1 parent 9f9022b commit 469b89c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
39 changes: 15 additions & 24 deletions dml_select_bind_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,35 @@ 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)
if err != nil {
e = err
return
}

e = err
return
return BindToMapKV[M](rows, initcap)
}

// 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)
if err != nil {
e = err
return
}

e = err
return
return BindToMapBool[M](rows, initcap)
}

// 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)
if err != nil {
e = err
return
}

e = err
return
return BindToMapEmptyStruct[M](rows, initcap)
}

// 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.
// NOTICE: It will close the rows.
func BindToMapKV[M ~map[K]V, K comparable, V any](rows Rows, initcap int) (m M, err error) {
if rows.Rows == nil {
return
}
defer rows.Close()

if initcap == 0 {
Expand All @@ -74,22 +68,19 @@ func BindToMapKV[M ~map[K]V, K comparable, V any](rows Rows, initcap int) (m M,

// BindToMapBool scans one column as key, and insert it with the value true into m.
//
// NOTICE: if rows.Rows is nil, do nothing. Or, it will close the rows.
// NOTICE: It will close the rows.
func BindToMapBool[M ~map[K]bool, K comparable](rows Rows, initcap int) (M, error) {
return bindtomapkey[M](rows, initcap, true)
}

// BindToMapEmptyStruct scans one column as key, and insert it with the value struct{}{} into m.
//
// NOTICE: if rows.Rows is nil, do nothing. Or, it will close the rows.
// NOTICE: It will close the rows.
func BindToMapEmptyStruct[M ~map[K]struct{}, K comparable](rows Rows, initcap int) (M, error) {
return bindtomapkey[M](rows, initcap, struct{}{})
}

func bindtomapkey[M ~map[K]V, K comparable, V any](rows Rows, initcap int, v V) (m M, err error) {
if rows.Rows == nil {
return
}
defer rows.Close()

if initcap == 0 {
Expand Down
10 changes: 3 additions & 7 deletions dml_select_bind_rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,14 @@ func NewRows(rows *sql.Rows, columns ...string) Rows {
// TryBindSlice is the same as BindSlice, which binds rows to slice
// only if err is equal to nil.
func (r Rows) TryBindSlice(slice any, err error) error {
if err == nil {
err = r.BindSlice(slice)
if err != nil {
return err
}
return err
return r.BindSlice(slice)
}

// BindSlice is the same as ScanSlice, but closes sql.Rows.
func (r Rows) BindSlice(slice any) (err error) {
if r.Rows == nil {
return
}

defer r.Rows.Close()
return r.ScanSlice(slice)
}
Expand Down

0 comments on commit 469b89c

Please sign in to comment.