Skip to content

Commit f918734

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

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
@@ -94,24 +94,16 @@ func InsertAndFetchId(db *DB, stmt string, args any) (int64, error) {
9494
return lastInsertId, nil
9595
}
9696

97-
// ForEachRow applies the provided restoreFunc callback for each successfully retrieved row of the specified type.
98-
// It will bulk SELECT the data from the database scoped to the specified ids and scans into the provided Row type.
97+
// ExecAndApply applies the provided restoreFunc callback for each successfully retrieved row of the specified type.
9998
// Returns error on any database failure or fails to acquire the table semaphore.
100-
func ForEachRow[Row, Id any](ctx context.Context, db *DB, idColumn string, ids []Id, restoreFunc func(*Row)) error {
101-
subject := new(Row)
102-
table := TableName(subject)
99+
func ExecAndApply[Row any](ctx context.Context, db *DB, stmt string, args []interface{}, restoreFunc func(*Row)) error {
100+
table := TableName(new(Row))
103101
sem := db.GetSemaphoreForTable(table)
104102
if err := sem.Acquire(ctx, 1); err != nil {
105103
return errors.Wrapf(err, "cannot acquire semaphore for table %q", table)
106104
}
107105
defer sem.Release(1)
108106

109-
query := fmt.Sprintf("%s WHERE %q IN (?)", db.BuildSelectStmt(subject, subject), idColumn)
110-
stmt, args, err := sqlx.In(query, ids)
111-
if err != nil {
112-
return errors.Wrapf(err, "cannot build placeholders for %q", query)
113-
}
114-
115107
rows, err := db.QueryxContext(ctx, db.Rebind(stmt), args...)
116108
if err != nil {
117109
return err
@@ -133,6 +125,20 @@ func ForEachRow[Row, Id any](ctx context.Context, db *DB, idColumn string, ids [
133125
return rows.Err()
134126
}
135127

128+
// ForEachRow applies the provided restoreFunc callback for each successfully retrieved row of the specified type.
129+
// It will bulk SELECT the data from the database scoped to the specified ids and scans into the provided Row type.
130+
// Returns error on any database failure or fails to acquire the table semaphore.
131+
func ForEachRow[Row, Id any](ctx context.Context, db *DB, idColumn string, ids []Id, restoreFunc func(*Row)) error {
132+
subject := new(Row)
133+
query := fmt.Sprintf("%s WHERE %q IN (?)", db.BuildSelectStmt(subject, subject), idColumn)
134+
stmt, args, err := sqlx.In(query, ids)
135+
if err != nil {
136+
return errors.Wrapf(err, "cannot build placeholders for %q", query)
137+
}
138+
139+
return ExecAndApply(ctx, db, stmt, args, restoreFunc)
140+
}
141+
136142
// ToDBString transforms the given string to types.String.
137143
func ToDBString(value string) types.String {
138144
str := types.String{NullString: sql.NullString{String: value}}

0 commit comments

Comments
 (0)