Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: facades.Orm supports sum #241

Merged
merged 4 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions contracts/database/orm/mocks/Query.go

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

1 change: 1 addition & 0 deletions contracts/database/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Query interface {
Scopes(funcs ...func(Query) Query) Query
Select(query any, args ...any) Query
SharedLock() Query
Sum(column string, dest any) error
Table(name string, args ...any) Query
Update(column any, value ...any) (*Result, error)
UpdateOrCreate(dest any, attributes any, values any) error
Expand Down
4 changes: 4 additions & 0 deletions database/gorm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,10 @@ func (r *QueryImpl) SharedLock() ormcontract.Query {
return r
}

func (r *QueryImpl) Sum(column string, dest any) error {
return r.instance.Select("SUM(" + column + ")").Row().Scan(dest)
}

func (r *QueryImpl) Table(name string, args ...any) ormcontract.Query {
tx := r.instance.Table(name, args...)

Expand Down
19 changes: 19 additions & 0 deletions database/gorm/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2444,6 +2444,25 @@ func (s *QueryTestSuite) TestSoftDelete() {
}
}

func (s *QueryTestSuite) TestSum() {
for driver, query := range s.queries {
s.Run(driver.String(), func() {
user := User{Name: "count_user", Avatar: "count_avatar"}
s.Nil(query.Create(&user))
s.True(user.ID > 0)

user1 := User{Name: "count_user", Avatar: "count_avatar1"}
s.Nil(query.Create(&user1))
s.True(user1.ID > 0)

var value float64
err := query.Table("users").Sum("id", &value)
s.Nil(err)
s.True(value > 0)
})
}
}

func (s *QueryTestSuite) TestTransactionSuccess() {
for driver, query := range s.queries {
s.Run(driver.String(), func() {
Expand Down