Check orderby validate

This commit is contained in:
Lunny Xiao 2023-07-25 20:38:41 +08:00
parent cb4f310151
commit e8facc9984
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
1 changed files with 49 additions and 3 deletions

View File

@ -5,6 +5,7 @@
package statements package statements
import ( import (
"errors"
"fmt" "fmt"
"xorm.io/builder" "xorm.io/builder"
@ -16,6 +17,26 @@ type orderBy struct {
direction string // ASC, DESC or "", "" means raw orderStr direction string // ASC, DESC or "", "" means raw orderStr
} }
func (ob orderBy) CheckValid() error {
if ob.orderStr == nil {
return fmt.Errorf("order by string is nil")
}
switch t := ob.orderStr.(type) {
case string:
if t == "" {
return fmt.Errorf("order by string is empty")
}
return nil
case *builder.Expression:
if t.Content() == "" {
return fmt.Errorf("order by string is empty")
}
return nil
default:
return fmt.Errorf("order by string is not string or builder.Expression")
}
}
func (statement *Statement) HasOrderBy() bool { func (statement *Statement) HasOrderBy() bool {
return len(statement.orderBy) > 0 return len(statement.orderBy) > 0
} }
@ -25,6 +46,8 @@ func (statement *Statement) ResetOrderBy() {
statement.orderBy = []orderBy{} statement.orderBy = []orderBy{}
} }
var ErrNoColumnName = errors.New("no column name")
func (statement *Statement) writeOrderBy(w *builder.BytesWriter, orderBy orderBy) error { func (statement *Statement) writeOrderBy(w *builder.BytesWriter, orderBy orderBy) error {
switch t := orderBy.orderStr.(type) { switch t := orderBy.orderStr.(type) {
case (*builder.Expression): case (*builder.Expression):
@ -75,22 +98,45 @@ 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 {
statement.orderBy = append(statement.orderBy, orderBy{order, args, ""}) ob := orderBy{order, args, ""}
if err := ob.CheckValid(); err != nil {
statement.LastError = err
return statement
}
statement.orderBy = append(statement.orderBy, ob)
return statement return statement
} }
// Desc generate `ORDER BY xx DESC` // Desc generate `ORDER BY xx DESC`
func (statement *Statement) Desc(colNames ...string) *Statement { func (statement *Statement) Desc(colNames ...string) *Statement {
if len(colNames) == 0 {
statement.LastError = ErrNoColumnName
return statement
}
for _, colName := range colNames { for _, colName := range colNames {
statement.orderBy = append(statement.orderBy, orderBy{colName, nil, "DESC"}) ob := orderBy{colName, nil, "DESC"}
statement.orderBy = append(statement.orderBy, ob)
if err := ob.CheckValid(); err != nil {
statement.LastError = err
return statement
}
} }
return statement return statement
} }
// Asc provide asc order by query condition, the input parameters are columns. // Asc provide asc order by query condition, the input parameters are columns.
func (statement *Statement) Asc(colNames ...string) *Statement { func (statement *Statement) Asc(colNames ...string) *Statement {
if len(colNames) == 0 {
statement.LastError = ErrNoColumnName
return statement
}
for _, colName := range colNames { for _, colName := range colNames {
statement.orderBy = append(statement.orderBy, orderBy{colName, nil, "ASC"}) ob := orderBy{colName, nil, "ASC"}
statement.orderBy = append(statement.orderBy, ob)
if err := ob.CheckValid(); err != nil {
statement.LastError = err
return statement
}
} }
return statement return statement
} }