Skip to content

Commit

Permalink
minor: add Mutate and MutateSingle shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Oct 26, 2024
1 parent 05847e0 commit 405d793
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
16 changes: 16 additions & 0 deletions db_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,22 @@ func (db *DB) insertTableRecord(ctx context.Context, td *desc.Table, structValue
return nil
}

// Mutate executes a query that modifies the database and returns the number of rows affected.
func (db *DB) Mutate(ctx context.Context, query string, args ...any) (int64, error) {
tag, err := db.Exec(ctx, query, args...)
if err != nil {
return 0, err
}

return tag.RowsAffected(), nil
}

// MutateSingle executes a query that modifies the database and returns true if at least one row was affected.
func (db *DB) MutateSingle(ctx context.Context, query string, args ...any) (bool, error) {
rowsAffected, err := db.Mutate(ctx, query, args...)
return rowsAffected > 0, err
}

// Delete deletes one or more values from the database by building and executing an
// SQL query based on the values and the table definition.
func (db *DB) Delete(ctx context.Context, values ...any) (int64, error) {
Expand Down
10 changes: 10 additions & 0 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ func (repo *Repository[T]) Exec(ctx context.Context, query string, args ...any)
return repo.db.Exec(ctx, query, args...)
}

// Mutate executes a query that returns the number of affected rows and returns it and an error.
func (repo *Repository[T]) Mutate(ctx context.Context, query string, args ...any) (int64, error) {
return repo.db.Mutate(ctx, query, args...)
}

// MutateSingle executes a query that modifies the database and returns true if at least one row was affected.
func (repo *Repository[T]) MutateSingle(ctx context.Context, query string, args ...any) (bool, error) {
return repo.db.MutateSingle(ctx, query, args...)
}

// === //

// InTransaction runs a function within a database transaction and commits or
Expand Down

0 comments on commit 405d793

Please sign in to comment.