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: [#614] Optimize facades.Orm().Query().Exists(), Order() and Count() methods #929

Merged
merged 4 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 5 additions & 2 deletions contracts/database/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Query interface {
// Commit commits the changes in a transaction.
Commit() error
// Count retrieve the "count" result of the query.
Count(count *int64) error
Count() (int64, error)
// Create inserts new record into the database.
Create(value any) error
// Cursor returns a cursor, use scan to iterate over the returned rows.
Expand All @@ -60,7 +60,7 @@ type Query interface {
// Exec executes raw sql
Exec(sql string, values ...any) (*Result, error)
// Exists returns true if matching records exist; otherwise, it returns false.
Exists(exists *bool) error
Exists() (bool, error)
// Find finds records that match given conditions.
Find(dest any, conds ...any) error
// FindOrFail finds records that match given conditions or throws an error.
Expand Down Expand Up @@ -107,11 +107,14 @@ type Query interface {
// Omit specifies columns that should be omitted from the query.
Omit(columns ...string) Query
// Order specifies the order in which the results should be returned.
// DEPRECATED Use OrderByRaw instead.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistent with Laravel

Order(value any) Query
// OrderBy specifies the order should be ascending.
OrderBy(column string, direction ...string) Query
// OrderByDesc specifies the order should be descending.
OrderByDesc(column string) Query
// OrderByRaw specifies the order should be raw.
OrderByRaw(raw string) Query
// OrWhere add an "or where" clause to the query.
OrWhere(query any, args ...any) Query
// OrWhereIn adds an "or where column in" clause to the query.
Expand Down
4 changes: 2 additions & 2 deletions database/console/show_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ func (r *ShowCommand) display(ctx console.Context, info databaseInfo) {
ctx.TwoColumnDetail("<fg=green;op=bold>Views</>", "<fg=yellow;op=bold>Rows</>")
for i := range info.Views {
if !str.Of(info.Views[i].Name).StartsWith("pg_catalog", "information_schema", "spt_") {
var rows int64
if err := r.schema.Orm().Query().Table(info.Views[i].Name).Count(&rows); err != nil {
rows, err := r.schema.Orm().Query().Table(info.Views[i].Name).Count()
if err != nil {
ctx.Error(err.Error())
return
}
Expand Down
3 changes: 1 addition & 2 deletions database/console/show_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ func TestShowCommand(t *testing.T) {
mockOrm.EXPECT().Query().Return(mockQuery).Once()
mockQuery.EXPECT().Table("test").Return(mockQuery).Once()

var rows int64
mockQuery.EXPECT().Count(&rows).Return(nil).Once()
mockQuery.EXPECT().Count().Return(int64(1), nil).Once()
mockContext.EXPECT().NewLine().Times(4)
for i := range successCaseExpected {
mockContext.EXPECT().TwoColumnDetail(successCaseExpected[i][0], successCaseExpected[i][1]).Once()
Expand Down
43 changes: 34 additions & 9 deletions database/gorm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,17 @@ func (r *Query) Commit() error {
return r.instance.Commit().Error
}

func (r *Query) Count(count *int64) error {
func (r *Query) Count() (int64, error) {
query := r.buildConditions()

return query.instance.Count(count).Error
var count int64

err := query.instance.Count(&count).Error
if err != nil {
return 0, err
}

return count, nil
}

func (r *Query) Create(value any) error {
Expand Down Expand Up @@ -219,10 +226,16 @@ func (r *Query) Exec(sql string, values ...any) (*contractsorm.Result, error) {
}, result.Error
}

func (r *Query) Exists(exists *bool) error {
func (r *Query) Exists() (bool, error) {
query := r.buildConditions()

return query.instance.Select("1").Limit(1).Find(exists).Error
var exists bool
err := query.instance.Select("1").Limit(1).Find(&exists).Error
if err != nil {
return false, err
}

return exists, nil
}

func (r *Query) Find(dest any, conds ...any) error {
Expand Down Expand Up @@ -555,6 +568,7 @@ func (r *Query) Omit(columns ...string) contractsorm.Query {
return r.setConditions(conditions)
}

// DEPRECATED: Use OrderByRaw instead
func (r *Query) Order(value any) contractsorm.Query {
conditions := r.conditions
conditions.order = append(r.conditions.order, value)
Expand All @@ -569,19 +583,26 @@ func (r *Query) OrderBy(column string, direction ...string) contractsorm.Query {
} else {
orderDirection = "ASC"
}
return r.Order(fmt.Sprintf("%s %s", column, orderDirection))
return r.OrderByRaw(fmt.Sprintf("%s %s", column, orderDirection))
}

func (r *Query) OrderByDesc(column string) contractsorm.Query {
return r.Order(fmt.Sprintf("%s DESC", column))
return r.OrderByRaw(fmt.Sprintf("%s DESC", column))
}

func (r *Query) OrderByRaw(raw string) contractsorm.Query {
conditions := r.conditions
conditions.order = append(r.conditions.order, raw)

return r.setConditions(conditions)
}

func (r *Query) Instance() *gormio.DB {
return r.instance
}

func (r *Query) InRandomOrder() contractsorm.Query {
return r.Order(r.gormQuery.RandomOrder())
return r.OrderByRaw(r.gormQuery.RandomOrder())
}

func (r *Query) InTransaction() bool {
Expand Down Expand Up @@ -612,13 +633,17 @@ func (r *Query) Paginate(page, limit int, dest any, total *int64) error {
offset := (page - 1) * limit
if total != nil {
if query.conditions.table == nil && query.conditions.model == nil {
if err := query.Model(dest).Count(total); err != nil {
count, err := query.Model(dest).Count()
if err != nil {
return err
}
*total = count
} else {
if err := query.Count(total); err != nil {
count, err := query.Count()
if err != nil {
return err
}
*total = count
}
}

Expand Down
130 changes: 98 additions & 32 deletions mocks/database/orm/Query.go

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

Loading
Loading