Skip to content

Commit

Permalink
Merge pull request #40 from rajatgpt1521/feature-delete-num
Browse files Browse the repository at this point in the history
Added deleteNumMethod in queryset.go
  • Loading branch information
jirfag authored Aug 25, 2018
2 parents 7510503 + fdee4c1 commit d75411b
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 14 deletions.
14 changes: 14 additions & 0 deletions examples/comparison/gorm4/autogenerated_gorm4.go

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

Binary file added queryset/compile
Binary file not shown.
55 changes: 54 additions & 1 deletion queryset/methods/queryset.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,20 +253,73 @@ func NewGetUpdaterMethod(qsTypeName, updaterTypeMethod string) GetUpdaterMethod
// DeleteMethod creates Delete method
type DeleteMethod struct {
baseQuerySetMethod

namedMethod
noArgsMethod

gormErroredMethod
}

// NewDeleteMethod creates Delete method
func NewDeleteMethod(qsTypeName, structTypeName string) DeleteMethod {
return DeleteMethod{
baseQuerySetMethod: newBaseQuerySetMethod(qsTypeName),

namedMethod: newNamedMethod("Delete"),
baseQuerySetMethod: newBaseQuerySetMethod(qsTypeName),
gormErroredMethod: newGormErroredMethod("Delete", structTypeName+"{}", qsDbName),
}
}

// DeleteNumMethod creates DeleteNum method
type DeleteNumMethod struct {
namedMethod
baseQuerySetMethod

noArgsMethod
constBodyMethod
constRetMethod
}

// NewDeleteNumMethod delete row count
func NewDeleteNumMethod(qsTypeName, structTypeName string) DeleteNumMethod {
return DeleteNumMethod{
namedMethod: newNamedMethod("DeleteNum"),
baseQuerySetMethod: newBaseQuerySetMethod(qsTypeName),
constRetMethod: newConstRetMethod("(int64, error)"),
constBodyMethod: newConstBodyMethod(
strings.Join([]string{
"db := qs.db.Delete(" + structTypeName + "{}" + ")",
"return db.RowsAffected, db.Error",
}, "\n"),
),
}
}

// DeleteNumUnscopedMethod creates DeleteNumUnscoped method for performing hard deletes
type DeleteNumUnscopedMethod struct {
namedMethod
baseQuerySetMethod

noArgsMethod
constBodyMethod
constRetMethod
}

// NewDeleteNumUnscopedMethod delete row count for hard deletes
func NewDeleteNumUnscopedMethod(qsTypeName, structTypeName string) DeleteNumUnscopedMethod {
return DeleteNumUnscopedMethod{
namedMethod: newNamedMethod("DeleteNumUnscoped"),
baseQuerySetMethod: newBaseQuerySetMethod(qsTypeName),
constRetMethod: newConstRetMethod("(int64, error)"),
constBodyMethod: newConstBodyMethod(
strings.Join([]string{
"db := qs.db.Unscoped().Delete(" + structTypeName + "{}" + ")",
"return db.RowsAffected, db.Error",
}, "\n"),
),
}
}

// CountMethod creates Count method
type CountMethod struct {
baseQuerySetMethod
Expand Down
4 changes: 3 additions & 1 deletion queryset/methodsbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ func (b *methodsBuilder) buildCRUDMethods() *methodsBuilder {
methods.NewGetUpdaterMethod(b.qsTypeName(), getUpdaterTypeName(b.s.TypeName)),
methods.NewDeleteMethod(b.qsTypeName(), b.s.TypeName),
methods.NewStructModifierMethod("Create", b.s.TypeName),
methods.NewStructModifierMethod("Delete", b.s.TypeName))
methods.NewStructModifierMethod("Delete", b.s.TypeName),
methods.NewDeleteNumMethod(b.qsTypeName(), b.s.TypeName),
methods.NewDeleteNumUnscopedMethod(b.qsTypeName(), b.s.TypeName))
return b
}

Expand Down
32 changes: 32 additions & 0 deletions queryset/queryset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ func TestQueries(t *testing.T) {
testUserQueryFilters,
testUsersCount,
testUsersUpdateNum,
testUsersDeleteNum,
testUsersDeleteNumUnscoped,
}
for _, f := range funcs {
f := f // save range var
Expand Down Expand Up @@ -328,6 +330,36 @@ func testUserDeleteByPK(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
assert.Nil(t, u.Delete(db))
}

func testUsersDeleteNum(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
usersNum := 2
users := getTestUsers(usersNum)
req := "UPDATE `users` SET deleted_at=? WHERE `users`.deleted_at IS NULL AND ((email IN (?,?)))"
m.ExpectExec(fixedFullRe(req)).
WithArgs(sqlmock.AnyArg(), users[0].Email, users[1].Email).
WillReturnResult(sqlmock.NewResult(0, int64(usersNum)))

num, err := test.NewUserQuerySet(db).
EmailIn(users[0].Email, users[1].Email).
DeleteNum()
assert.Nil(t, err)
assert.Equal(t, int64(usersNum), num)
}

func testUsersDeleteNumUnscoped(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
usersNum := 2
users := getTestUsers(usersNum)
req := "DELETE FROM `users` WHERE (email IN (?,?))"
m.ExpectExec(fixedFullRe(req)).
WithArgs(users[0].Email, users[1].Email).
WillReturnResult(sqlmock.NewResult(0, int64(usersNum)))

num, err := test.NewUserQuerySet(db).
EmailIn(users[0].Email, users[1].Email).
DeleteNumUnscoped()
assert.Nil(t, err)
assert.Equal(t, int64(usersNum), num)
}

func testUsersUpdateNum(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
usersNum := 2
users := getTestUsers(usersNum)
Expand Down
74 changes: 65 additions & 9 deletions queryset/test/autogenerated_models.go

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

20 changes: 17 additions & 3 deletions queryset/test/pkgimport/autogenerated_models.go

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

0 comments on commit d75411b

Please sign in to comment.