Describe the feature
The order func is this:
func (db *DB) Order(value interface{}) (tx *DB) {
tx = db.getInstance()
switch v := value.(type) {
case clause.OrderBy:
tx.Statement.AddClause(v)
case clause.OrderByColumn:
tx.Statement.AddClause(clause.OrderBy{
Columns: []clause.OrderByColumn{v},
})
case string:
if v != "" {
tx.Statement.AddClause(clause.OrderBy{
Columns: []clause.OrderByColumn{{
Column: clause.Column{Name: v, Raw: true},
}},
})
}
}
return
}
The input is an interface while the switch has no default branch.
So why don't give a func OrderBy() and func OrderByColumn()
Motivation
Some times we use a string-builder to build the order stmt.
While when we pass the custom string-builder type to the Order func. it not return error and not do the order.
For example.
type A struct {
orderByString string
}
func (a *A) AppendOrder(orderString string){
if a.orderByString != ""{
a.orderByString += ","
}
a.orderByString += orderString
}
var a = &A{} a.AppendOrder("name asc") a.AppendOrder("age desc")
db.Where(...).Order(a).Find(...).Error
same with the logic:
db.Where(...).Find(...).Error
which means no order.