Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dbutil/queryhelper: return zero-value if no rows found
Without this commit, the QueryOne function would return whatever the (DataStruct[T]).Scan function returns as the value in the sql.ErrNoRows error case. In some cases, the value returned might be an initialized value. For example, if the following pattern is followed: func (s *MyStruct) Scan(row dbutil.Scannable) (*MyStruct, error) { err := row.Scan(&s.A, &s.B, &s.C) return s, err } Note that even in the error case, the "s" variable will be initialized. The alternative would be to force the (DataStruct[T]).Scan function to return nil if there is an error, but that forces unnecessary verbosity onto the implementer of the interface since they would have to do something like: func (s *MyStruct) Scan(row dbutil.Scannable) (*MyStruct, error) { err := row.Scan(&s.A, &s.B, &s.C) if err != nil { return nil, err } return s, nil } Signed-off-by: Sumner Evans <[email protected]>
- Loading branch information