mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
Merge pull request #1529 from zxr615/feature-groupRaw
Added Order() method support for gdb.
This commit is contained in:
@ -6,15 +6,19 @@
|
||||
|
||||
package gdb
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Order sets the "ORDER BY" statement for the model.
|
||||
//
|
||||
// Eg:
|
||||
// Order("id desc")
|
||||
// Order("id", "desc")
|
||||
// Order("id", "desc").
|
||||
// Order("id desc,name asc").
|
||||
func (m *Model) Order(orderBy ...string) *Model {
|
||||
// Order(gdb.Raw("field(id, 3,1,2)")).
|
||||
func (m *Model) Order(orderBy ...interface{}) *Model {
|
||||
if len(orderBy) == 0 {
|
||||
return m
|
||||
}
|
||||
@ -22,7 +26,13 @@ func (m *Model) Order(orderBy ...string) *Model {
|
||||
if model.orderBy != "" {
|
||||
model.orderBy += ","
|
||||
}
|
||||
model.orderBy += model.db.GetCore().QuoteString(strings.Join(orderBy, " "))
|
||||
for _, o := range orderBy {
|
||||
if v, ok := o.(Raw); ok {
|
||||
model.orderBy += gconv.String(v)
|
||||
return model
|
||||
}
|
||||
}
|
||||
model.orderBy += model.db.GetCore().QuoteString(strings.Join(gconv.SliceStr(orderBy), " "))
|
||||
return model
|
||||
}
|
||||
|
||||
|
||||
@ -442,10 +442,10 @@ func Test_Model_Clone(t *testing.T) {
|
||||
count, err := md.Count()
|
||||
t.AssertNil(err)
|
||||
|
||||
record, err := md.Order("id DESC").One()
|
||||
record, err := md.Safe(true).Order("id DESC").One()
|
||||
t.AssertNil(err)
|
||||
|
||||
result, err := md.Order("id ASC").All()
|
||||
result, err := md.Safe(true).Order("id ASC").All()
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(count, 2)
|
||||
@ -1114,6 +1114,15 @@ func Test_Model_OrderBy(t *testing.T) {
|
||||
t.Assert(len(result), TableSize)
|
||||
t.Assert(result[0]["nickname"].String(), "name_1")
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Order(gdb.Raw("field(id, 10,1,2,3,4,5,6,7,8,9)")).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), TableSize)
|
||||
t.Assert(result[0]["nickname"].String(), "name_10")
|
||||
t.Assert(result[1]["nickname"].String(), "name_1")
|
||||
t.Assert(result[2]["nickname"].String(), "name_2")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_GroupBy(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user