Skip to content

Commit

Permalink
add a DeleteByID simple helper
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Aug 20, 2023
1 parent ac4bc3d commit 980bd5a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
15 changes: 15 additions & 0 deletions db_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,21 @@ func (db *DB) deleteTableRecords(ctx context.Context, td *desc.Table, values []a
return tag.RowsAffected(), nil // return true and nil if at least one row was affected by the query
}

func (db *DB) deleteByID(ctx context.Context, td *desc.Table, id any) (bool, error) {
primaryKey, ok := td.PrimaryKey()
if !ok {
return false, fmt.Errorf("no primary key found in table definition: %s", td.Name)
}

query := fmt.Sprintf(`DELETE FROM "%s"."%s" WHERE "%s" = $1;`, db.searchPath, td.Name, primaryKey.Name)
tag, err := db.Exec(ctx, query, id)
if err != nil {
return false, err
}

return tag.RowsAffected() > 0, nil
}

// Update updates one or more values in the database by building and executing an
// SQL query based on the values and the table definition.
func (db *DB) Update(ctx context.Context, values ...any) (int64, error) {
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/text v0.12.0 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
9 changes: 9 additions & 0 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ func (repo *Repository[T]) Delete(ctx context.Context, values ...T) (int64, erro
return repo.db.deleteTableRecords(ctx, repo.td, valuesAsInterfaces)
}

// DeleteByID deletes a single row from a table by matching the id column with the given argument and
// reports whether the entry was removed or not.
//
// The difference between Delete and DeleteByID is that
// DeleteByID accepts just the id value instead of the whole entity structure value.
func (repo *Repository[T]) DeleteByID(ctx context.Context, id any) (bool, error) {
return repo.db.deleteByID(ctx, repo.td, id)
}

// Update updates one or more values of type T in the database by their primary key values.
func (repo *Repository[T]) Update(ctx context.Context, values ...T) (int64, error) {
return repo.UpdateOnlyColumns(ctx, nil, values...)
Expand Down

0 comments on commit 980bd5a

Please sign in to comment.