order by function support empty value
This commit is contained in:
parent
63222312b2
commit
b48e273924
|
@ -17,23 +17,23 @@ type orderBy struct {
|
||||||
direction string // ASC, DESC or "", "" means raw orderStr
|
direction string // ASC, DESC or "", "" means raw orderStr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ob orderBy) CheckValid() error {
|
func (ob orderBy) CheckValid() (bool, error) {
|
||||||
if ob.orderStr == nil {
|
if ob.orderStr == nil {
|
||||||
return fmt.Errorf("order by string is nil")
|
return false, nil
|
||||||
}
|
}
|
||||||
switch t := ob.orderStr.(type) {
|
switch t := ob.orderStr.(type) {
|
||||||
case string:
|
case string:
|
||||||
if t == "" {
|
if t == "" {
|
||||||
return fmt.Errorf("order by string is empty")
|
return false, nil
|
||||||
}
|
}
|
||||||
return nil
|
return true, nil
|
||||||
case *builder.Expression:
|
case *builder.Expression:
|
||||||
if t.Content() == "" {
|
if t.Content() == "" {
|
||||||
return fmt.Errorf("order by string is empty")
|
return false, nil
|
||||||
}
|
}
|
||||||
return nil
|
return true, nil
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("order by string is not string or builder.Expression")
|
return false, fmt.Errorf("order by string is not string or builder.Expression")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,11 +99,14 @@ func (statement *Statement) writeOrderBys(w *builder.BytesWriter) error {
|
||||||
// OrderBy generate "Order By order" statement
|
// OrderBy generate "Order By order" statement
|
||||||
func (statement *Statement) OrderBy(order interface{}, args ...interface{}) *Statement {
|
func (statement *Statement) OrderBy(order interface{}, args ...interface{}) *Statement {
|
||||||
ob := orderBy{order, args, ""}
|
ob := orderBy{order, args, ""}
|
||||||
if err := ob.CheckValid(); err != nil {
|
notNil, err := ob.CheckValid()
|
||||||
|
if err != nil {
|
||||||
statement.LastError = err
|
statement.LastError = err
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
if notNil {
|
||||||
statement.orderBy = append(statement.orderBy, ob)
|
statement.orderBy = append(statement.orderBy, ob)
|
||||||
|
}
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,7 +119,7 @@ func (statement *Statement) Desc(colNames ...string) *Statement {
|
||||||
for _, colName := range colNames {
|
for _, colName := range colNames {
|
||||||
ob := orderBy{colName, nil, "DESC"}
|
ob := orderBy{colName, nil, "DESC"}
|
||||||
statement.orderBy = append(statement.orderBy, ob)
|
statement.orderBy = append(statement.orderBy, ob)
|
||||||
if err := ob.CheckValid(); err != nil {
|
if _, err := ob.CheckValid(); err != nil {
|
||||||
statement.LastError = err
|
statement.LastError = err
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
@ -133,7 +136,7 @@ func (statement *Statement) Asc(colNames ...string) *Statement {
|
||||||
for _, colName := range colNames {
|
for _, colName := range colNames {
|
||||||
ob := orderBy{colName, nil, "ASC"}
|
ob := orderBy{colName, nil, "ASC"}
|
||||||
statement.orderBy = append(statement.orderBy, ob)
|
statement.orderBy = append(statement.orderBy, ob)
|
||||||
if err := ob.CheckValid(); err != nil {
|
if _, err := ob.CheckValid(); err != nil {
|
||||||
statement.LastError = err
|
statement.LastError = err
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
|
@ -270,6 +270,21 @@ func TestNullStructIterate(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNullOrderBy(t *testing.T) {
|
||||||
|
assert.NoError(t, PrepareEngine())
|
||||||
|
assertSync(t, new(NullStruct))
|
||||||
|
|
||||||
|
if true {
|
||||||
|
err := testEngine.Where("`age` IS NOT NULL").OrderBy("").Iterate(new(NullStruct),
|
||||||
|
func(i int, bean any) error {
|
||||||
|
nultype := bean.(*NullStruct)
|
||||||
|
fmt.Println(i, nultype)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNullStructCount(t *testing.T) {
|
func TestNullStructCount(t *testing.T) {
|
||||||
assert.NoError(t, PrepareEngine())
|
assert.NoError(t, PrepareEngine())
|
||||||
assertSync(t, new(NullStruct))
|
assertSync(t, new(NullStruct))
|
||||||
|
|
Loading…
Reference in New Issue