mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
orderBy raw()
This commit is contained in:
@ -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.
|
||||
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user