orderBy raw()

This commit is contained in:
fanwei
2022-01-07 21:06:49 +08:00
parent b39b2374c4
commit 0dc1adb672
2 changed files with 10 additions and 9 deletions

View File

@ -15,8 +15,9 @@ import (
//
// Eg:
// Order("id desc")
// Order("id", "desc")
// Order("id desc,name asc").
// Order("id", "desc",gdb.Raw("field(id, 3,1,2)"))
// Order(gdb.Raw("field(id, 3,1,2)"))
func (m *Model) Order(orderBy ...interface{}) *Model {
if len(orderBy) == 0 {
return m
@ -28,15 +29,15 @@ func (m *Model) Order(orderBy ...interface{}) *Model {
}
for _, o := range orderBy {
switch o.(type) {
case Raw:
model.orderBy += gconv.String(o)
default:
model.orderBy += model.db.GetCore().QuoteString(gconv.String(o))
if v, ok := o.(Raw); ok {
model.orderBy += gconv.String(v)
return model
}
}
return m
model.orderBy += model.db.GetCore().QuoteString(strings.Join(gconv.SliceStr(orderBy), " "))
return model
}
// OrderAsc sets the "ORDER BY xxx ASC" statement for the model.

View File

@ -441,10 +441,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)