2023-06-23 15:09:36 +00:00
|
|
|
package mdbc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"gitter.top/common/goref"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
)
|
|
|
|
|
|
|
|
type deleteScope struct {
|
2023-06-23 15:56:52 +00:00
|
|
|
scope *scope
|
|
|
|
mdbc *mdbc
|
2023-06-23 15:09:36 +00:00
|
|
|
ctx context.Context
|
|
|
|
opts *options.DeleteOptions
|
|
|
|
filter interface{}
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *deleteScope) SetContext(ctx context.Context) *deleteScope {
|
|
|
|
ds.ctx = ctx
|
|
|
|
return ds
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *deleteScope) SetOptions(opts options.DeleteOptions) *deleteScope {
|
|
|
|
ds.opts = &opts
|
|
|
|
return ds
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetID delete documents based on ID, has the highest priority.
|
2023-06-23 15:56:52 +00:00
|
|
|
func (ds *deleteScope) SetID(id ...any) *deleteScope {
|
2023-06-23 15:09:36 +00:00
|
|
|
if len(id) == 0 {
|
|
|
|
return ds
|
|
|
|
}
|
|
|
|
if len(id) == 1 {
|
|
|
|
ds.filter = bson.M{
|
2023-06-23 15:56:52 +00:00
|
|
|
"_id": id[0],
|
2023-06-23 15:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(id) > 1 {
|
|
|
|
ds.filter = bson.M{
|
|
|
|
"_id": bson.M{"$in": id},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ds
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFilter delete documents based on filter condition, filter type only support map or struct
|
|
|
|
func (ds *deleteScope) SetFilter(filter interface{}) *deleteScope {
|
|
|
|
if ds.filter != nil {
|
|
|
|
return ds
|
|
|
|
}
|
|
|
|
|
|
|
|
if filter == nil {
|
|
|
|
ds.filter = bson.M{}
|
|
|
|
return ds
|
|
|
|
}
|
|
|
|
|
|
|
|
if goref.IsBaseStruct(filter) {
|
|
|
|
ds.filter, ds.err = goref.StructToMap(filter, goref.WithOmitempty(true), goref.WithTag("bson"))
|
|
|
|
return ds
|
|
|
|
}
|
|
|
|
|
|
|
|
if goref.IsBaseMap(filter) {
|
|
|
|
ds.filter = filter
|
|
|
|
return ds
|
|
|
|
}
|
|
|
|
|
|
|
|
return ds
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *deleteScope) mergeOptins() {
|
|
|
|
if ds.ctx == nil {
|
|
|
|
ds.ctx = context.Background()
|
|
|
|
}
|
|
|
|
if ds.opts == nil {
|
|
|
|
ds.opts = new(options.DeleteOptions)
|
|
|
|
}
|
|
|
|
if ds.filter == nil {
|
|
|
|
ds.filter = bson.M{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *deleteScope) One() (int64, error) {
|
|
|
|
ds.mergeOptins()
|
|
|
|
if ds.err != nil {
|
|
|
|
return 0, ds.err
|
|
|
|
}
|
2023-06-23 15:56:52 +00:00
|
|
|
result, err := ds.mdbc.database.Collection(ds.scope.tableName).DeleteOne(ds.ctx, ds.filter, ds.opts)
|
2023-06-23 15:09:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return result.DeletedCount, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *deleteScope) Many() (int64, error) {
|
|
|
|
ds.mergeOptins()
|
|
|
|
if ds.err != nil {
|
|
|
|
return 0, ds.err
|
|
|
|
}
|
2023-06-23 15:56:52 +00:00
|
|
|
result, err := ds.mdbc.database.Collection(ds.scope.tableName).DeleteMany(ds.ctx, ds.filter, ds.opts)
|
2023-06-23 15:09:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return result.DeletedCount, nil
|
|
|
|
}
|