Skip to content

Commit

Permalink
add DeleteMany method
Browse files Browse the repository at this point in the history
  • Loading branch information
stsem committed Mar 16, 2024
1 parent 95afdc5 commit 589cc25
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 1 deletion.
36 changes: 36 additions & 0 deletions example/case_one/db/addresses.db.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type AddressStorage interface {
Create(ctx context.Context, model *Address, opts ...Option) (*string, error)
Update(ctx context.Context, id string, updateData *AddressUpdate) error
DeleteById(ctx context.Context, id string, opts ...Option) error
DeleteMany(ctx context.Context, builders ...*QueryBuilder) error
FindById(ctx context.Context, id string, opts ...Option) (*Address, error)
FindMany(ctx context.Context, builder ...*QueryBuilder) ([]*Address, error)
FindOne(ctx context.Context, builders ...*QueryBuilder) (*Address, error)
Expand Down Expand Up @@ -464,6 +465,41 @@ func (t *addressStorage) DeleteById(ctx context.Context, id string, opts ...Opti
return nil
}

// DeleteMany removes entries from the addresses table using the provided filters
func (t *addressStorage) DeleteMany(ctx context.Context, builders ...*QueryBuilder) error {
// build query
query := t.queryBuilder.Delete("addresses")

var withFilter bool
for _, builder := range builders {
if builder == nil {
continue
}

// apply filter options
for _, option := range builder.filterOptions {
query = option.ApplyDelete(query)
withFilter = true
}
}

if !withFilter {
return errors.New("filters are required for delete operation")
}

sqlQuery, args, err := query.ToSql()
if err != nil {
return fmt.Errorf("failed to build query: %w", err)
}

_, err = t.DB(ctx).ExecContext(ctx, sqlQuery, args...)
if err != nil {
return fmt.Errorf("failed to delete Address: %w", err)
}

return nil
}

// FindById retrieves a Address by its id.
func (t *addressStorage) FindById(ctx context.Context, id string, opts ...Option) (*Address, error) {
builder := NewQueryBuilder()
Expand Down
2 changes: 1 addition & 1 deletion example/case_one/db/blog.db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example/case_one/db/devices.db.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type DeviceStorage interface {
UpgradeTable(ctx context.Context) error
Create(ctx context.Context, model *Device, opts ...Option) error
Update(ctx context.Context, id int64, updateData *DeviceUpdate) error
DeleteMany(ctx context.Context, builders ...*QueryBuilder) error
FindMany(ctx context.Context, builder ...*QueryBuilder) ([]*Device, error)
FindOne(ctx context.Context, builders ...*QueryBuilder) (*Device, error)
Count(ctx context.Context, builders ...*QueryBuilder) (int64, error)
Expand Down
36 changes: 36 additions & 0 deletions example/case_one/db/posts.db.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type PostStorage interface {
Create(ctx context.Context, model *Post, opts ...Option) (*int32, error)
Update(ctx context.Context, id int32, updateData *PostUpdate) error
DeleteById(ctx context.Context, id int32, opts ...Option) error
DeleteMany(ctx context.Context, builders ...*QueryBuilder) error
FindById(ctx context.Context, id int32, opts ...Option) (*Post, error)
FindMany(ctx context.Context, builder ...*QueryBuilder) ([]*Post, error)
FindOne(ctx context.Context, builders ...*QueryBuilder) (*Post, error)
Expand Down Expand Up @@ -416,6 +417,41 @@ func (t *postStorage) DeleteById(ctx context.Context, id int32, opts ...Option)
return nil
}

// DeleteMany removes entries from the posts table using the provided filters
func (t *postStorage) DeleteMany(ctx context.Context, builders ...*QueryBuilder) error {
// build query
query := t.queryBuilder.Delete("posts")

var withFilter bool
for _, builder := range builders {
if builder == nil {
continue
}

// apply filter options
for _, option := range builder.filterOptions {
query = option.ApplyDelete(query)
withFilter = true
}
}

if !withFilter {
return errors.New("filters are required for delete operation")
}

sqlQuery, args, err := query.ToSql()
if err != nil {
return fmt.Errorf("failed to build query: %w", err)
}

_, err = t.DB(ctx).ExecContext(ctx, sqlQuery, args...)
if err != nil {
return fmt.Errorf("failed to delete Address: %w", err)
}

return nil
}

// FindById retrieves a Post by its id.
func (t *postStorage) FindById(ctx context.Context, id int32, opts ...Option) (*Post, error) {
builder := NewQueryBuilder()
Expand Down
36 changes: 36 additions & 0 deletions example/case_one/db/settings.db.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type SettingStorage interface {
Create(ctx context.Context, model *Setting, opts ...Option) (*int32, error)
Update(ctx context.Context, id int32, updateData *SettingUpdate) error
DeleteById(ctx context.Context, id int32, opts ...Option) error
DeleteMany(ctx context.Context, builders ...*QueryBuilder) error
FindById(ctx context.Context, id int32, opts ...Option) (*Setting, error)
FindMany(ctx context.Context, builder ...*QueryBuilder) ([]*Setting, error)
FindOne(ctx context.Context, builders ...*QueryBuilder) (*Setting, error)
Expand Down Expand Up @@ -413,6 +414,41 @@ func (t *settingStorage) DeleteById(ctx context.Context, id int32, opts ...Optio
return nil
}

// DeleteMany removes entries from the settings table using the provided filters
func (t *settingStorage) DeleteMany(ctx context.Context, builders ...*QueryBuilder) error {
// build query
query := t.queryBuilder.Delete("settings")

var withFilter bool
for _, builder := range builders {
if builder == nil {
continue
}

// apply filter options
for _, option := range builder.filterOptions {
query = option.ApplyDelete(query)
withFilter = true
}
}

if !withFilter {
return errors.New("filters are required for delete operation")
}

sqlQuery, args, err := query.ToSql()
if err != nil {
return fmt.Errorf("failed to build query: %w", err)
}

_, err = t.DB(ctx).ExecContext(ctx, sqlQuery, args...)
if err != nil {
return fmt.Errorf("failed to delete Address: %w", err)
}

return nil
}

// FindById retrieves a Setting by its id.
func (t *settingStorage) FindById(ctx context.Context, id int32, opts ...Option) (*Setting, error) {
builder := NewQueryBuilder()
Expand Down
36 changes: 36 additions & 0 deletions example/case_one/db/users.db.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type UserStorage interface {
Create(ctx context.Context, model *User, opts ...Option) (*string, error)
Update(ctx context.Context, id string, updateData *UserUpdate) error
DeleteById(ctx context.Context, id string, opts ...Option) error
DeleteMany(ctx context.Context, builders ...*QueryBuilder) error
FindById(ctx context.Context, id string, opts ...Option) (*User, error)
FindMany(ctx context.Context, builder ...*QueryBuilder) ([]*User, error)
FindOne(ctx context.Context, builders ...*QueryBuilder) (*User, error)
Expand Down Expand Up @@ -788,6 +789,41 @@ func (t *userStorage) DeleteById(ctx context.Context, id string, opts ...Option)
return nil
}

// DeleteMany removes entries from the users table using the provided filters
func (t *userStorage) DeleteMany(ctx context.Context, builders ...*QueryBuilder) error {
// build query
query := t.queryBuilder.Delete("users")

var withFilter bool
for _, builder := range builders {
if builder == nil {
continue
}

// apply filter options
for _, option := range builder.filterOptions {
query = option.ApplyDelete(query)
withFilter = true
}
}

if !withFilter {
return errors.New("filters are required for delete operation")
}

sqlQuery, args, err := query.ToSql()
if err != nil {
return fmt.Errorf("failed to build query: %w", err)
}

_, err = t.DB(ctx).ExecContext(ctx, sqlQuery, args...)
if err != nil {
return fmt.Errorf("failed to delete Address: %w", err)
}

return nil
}

// FindById retrieves a User by its id.
func (t *userStorage) FindById(ctx context.Context, id string, opts ...Option) (*User, error) {
builder := NewQueryBuilder()
Expand Down
36 changes: 36 additions & 0 deletions plugin/provider/postgres/tmpl/table.templater.tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,41 @@ func (t *{{ storageName | lowerCamelCase }}) DeleteBy{{ getPrimaryKey.GetName |
return nil
}
// DeleteMany removes entries from the {{ tableName }} table using the provided filters
func (t *{{ storageName | lowerCamelCase }}) DeleteMany(ctx context.Context, builders ...*QueryBuilder) error {
// build query
query := t.queryBuilder.Delete("{{ tableName }}")
var withFilter bool
for _, builder := range builders {
if builder == nil {
continue
}
// apply filter options
for _, option := range builder.filterOptions {
query = option.ApplyDelete(query)
withFilter = true
}
}
if !withFilter {
return errors.New("filters are required for delete operation")
}
sqlQuery, args, err := query.ToSql()
if err != nil {
return fmt.Errorf("failed to build query: %w", err)
}
_, err = t.DB(ctx).ExecContext(ctx, sqlQuery, args...)
if err != nil {
return fmt.Errorf("failed to delete Address: %w", err)
}
return nil
}
`

const TableUpdateMethodTemplate = `
Expand Down Expand Up @@ -660,6 +695,7 @@ type {{ storageName }} interface {
{{- if (hasPrimaryKey) }}
DeleteBy{{ getPrimaryKey.GetName | camelCase }}(ctx context.Context, {{getPrimaryKey.GetName}} {{IDType}}, opts ...Option) error
{{- end }}
DeleteMany(ctx context.Context, builders ...*QueryBuilder) error
{{- if (hasPrimaryKey) }}
FindBy{{ getPrimaryKey.GetName | camelCase }}(ctx context.Context, id {{IDType}}, opts ...Option) (*{{ structureName }}, error)
{{- end }}
Expand Down

0 comments on commit 589cc25

Please sign in to comment.