Skip to content

Commit f1a1f7e

Browse files
yhabteaboxzi
authored andcommitted
database: Generalize ForEachRow into ExecAndApply and ForEachRow
Imported from Icinga/icinga-notifications@f8c3125
1 parent f330f2e commit f1a1f7e

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

database/utils.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,16 @@ func BuildInsertStmtWithout(db *DB, into interface{}, withoutColumn string) stri
6565
)
6666
}
6767

68-
// ForEachRow applies the provided restoreFunc callback for each successfully retrieved row of the specified type.
69-
// It will bulk SELECT the data from the database scoped to the specified ids and scans into the provided Row type.
68+
// ExecAndApply applies the provided restoreFunc callback for each successfully retrieved row of the specified type.
7069
// Returns error on any database failure or fails to acquire the table semaphore.
71-
func ForEachRow[Row, Id any](ctx context.Context, db *DB, idColumn string, ids []Id, restoreFunc func(*Row)) error {
72-
subject := new(Row)
73-
table := TableName(subject)
70+
func ExecAndApply[Row any](ctx context.Context, db *DB, stmt string, args []interface{}, restoreFunc func(*Row)) error {
71+
table := TableName(new(Row))
7472
sem := db.GetSemaphoreForTable(table)
7573
if err := sem.Acquire(ctx, 1); err != nil {
7674
return errors.Wrapf(err, "cannot acquire semaphore for table %q", table)
7775
}
7876
defer sem.Release(1)
7977

80-
query := fmt.Sprintf("%s WHERE %q IN (?)", db.BuildSelectStmt(subject, subject), idColumn)
81-
stmt, args, err := sqlx.In(query, ids)
82-
if err != nil {
83-
return errors.Wrapf(err, "cannot build placeholders for %q", query)
84-
}
85-
8678
rows, err := db.QueryxContext(ctx, db.Rebind(stmt), args...)
8779
if err != nil {
8880
return err
@@ -104,6 +96,20 @@ func ForEachRow[Row, Id any](ctx context.Context, db *DB, idColumn string, ids [
10496
return rows.Err()
10597
}
10698

99+
// ForEachRow applies the provided restoreFunc callback for each successfully retrieved row of the specified type.
100+
// It will bulk SELECT the data from the database scoped to the specified ids and scans into the provided Row type.
101+
// Returns error on any database failure or fails to acquire the table semaphore.
102+
func ForEachRow[Row, Id any](ctx context.Context, db *DB, idColumn string, ids []Id, restoreFunc func(*Row)) error {
103+
subject := new(Row)
104+
query := fmt.Sprintf("%s WHERE %q IN (?)", db.BuildSelectStmt(subject, subject), idColumn)
105+
stmt, args, err := sqlx.In(query, ids)
106+
if err != nil {
107+
return errors.Wrapf(err, "cannot build placeholders for %q", query)
108+
}
109+
110+
return ExecAndApply(ctx, db, stmt, args, restoreFunc)
111+
}
112+
107113
// ToDBString transforms the given string to types.String.
108114
func ToDBString(value string) types.String {
109115
str := types.String{NullString: sql.NullString{String: value}}

0 commit comments

Comments
 (0)