2
0

Replace QueryFunc with ForEachScannedRow

This commit is contained in:
Jack Christensen
2022-07-07 20:29:04 -05:00
parent a86f4f3db9
commit 76946fb5a3
16 changed files with 301 additions and 469 deletions
+25
View File
@@ -361,3 +361,28 @@ func RowsFromResultReader(typeMap *pgtype.Map, resultReader *pgconn.ResultReader
resultReader: resultReader,
}
}
// ForEachScannedRow iterates through rows. For each row it scans into the elements of scans and calls fn. If any row
// fails to scan or fn returns an error the query will be aborted and the error will be returned. Rows will be closed
// when ForEachScannedRow returns.
func ForEachScannedRow(rows Rows, scans []any, fn func() error) (pgconn.CommandTag, error) {
defer rows.Close()
for rows.Next() {
err := rows.Scan(scans...)
if err != nil {
return pgconn.CommandTag{}, err
}
err = fn()
if err != nil {
return pgconn.CommandTag{}, err
}
}
if err := rows.Err(); err != nil {
return pgconn.CommandTag{}, err
}
return rows.CommandTag(), nil
}