feat: 去掉了批量查询时,结果为空的报错

This commit is contained in:
2024-09-19 14:11:17 +08:00
parent 1f9ff72824
commit dde9b6f74e
2 changed files with 5 additions and 9 deletions

View File

@@ -247,10 +247,6 @@ func (b *Mapper) ScanRowsWithContext(ctx context.Context, target any, rows *sql.
// 将新实例添加到slice中
vf.Set(reflect.Append(vf, newStruct))
}
if vf.Len() == 0 { // 如果slice为空返回 sql.ErrNoRows
return sql.ErrNoRows
}
return nil
default:
return errors.New("target must be a pointer to a struct or a slice of structs")
@@ -277,11 +273,11 @@ func (b *Mapper) scanRow(target reflect.Value, rows *sql.Rows) error {
dest := make([]interface{}, len(columns))
for i, colName := range columns {
// 拿到缓存的字段索引
if idx, ok := mate.DBName2IndexCache[colName]; ok {
dest[i] = target.FieldByIndex(idx).Addr().Interface()
} else {
idx, ok := mate.DBName2IndexCache[colName]
if !ok {
return fmt.Errorf("no corresponding field found for column %s", colName)
}
dest[i] = target.FieldByIndex(idx).Addr().Interface()
}
// 扫描数据