OrderBy support builder.Express

This commit is contained in:
Lunny Xiao 2022-05-31 10:02:45 +08:00
parent c78313e08d
commit e2d1e0c83b
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
6 changed files with 19 additions and 8 deletions

View File

@ -1006,7 +1006,7 @@ func (engine *Engine) Asc(colNames ...string) *Session {
}
// OrderBy will generate "ORDER BY order"
func (engine *Engine) OrderBy(order string, args ...interface{}) *Session {
func (engine *Engine) OrderBy(order interface{}, args ...interface{}) *Session {
session := engine.NewSession()
session.isAutoClose = true
return session.OrderBy(order, args...)

2
go.mod
View File

@ -17,5 +17,5 @@ require (
github.com/syndtr/goleveldb v1.0.0
github.com/ziutek/mymysql v1.5.4
modernc.org/sqlite v1.14.2
xorm.io/builder v0.3.9
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978
)

4
go.sum
View File

@ -659,5 +659,5 @@ modernc.org/z v1.2.19 h1:BGyRFWhDVn5LFS5OcX4Yd/MlpRTOc7hOPTdcIpCiUao=
modernc.org/z v1.2.19/go.mod h1:+ZpP0pc4zz97eukOzW3xagV/lS82IpPN9NGG5pNF9vY=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=
xorm.io/builder v0.3.9 h1:Sd65/LdWyO7LR8+Cbd+e7mm3sK/7U9k0jS3999IDHMc=
xorm.io/builder v0.3.9/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE=
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 h1:bvLlAPW1ZMTWA32LuZMBEGHAUOcATZjzHcotf3SWweM=
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE=

View File

@ -54,7 +54,7 @@ type Interface interface {
Nullable(...string) *Session
Join(joinOperator string, tablename interface{}, condition string, args ...interface{}) *Session
Omit(columns ...string) *Session
OrderBy(order string, args ...interface{}) *Session
OrderBy(order interface{}, args ...interface{}) *Session
Ping() error
Query(sqlOrArgs ...interface{}) (resultsSlice []map[string][]byte, err error)
QueryInterface(sqlOrArgs ...interface{}) ([]map[string]interface{}, error)

View File

@ -33,11 +33,22 @@ func (statement *Statement) WriteOrderBy(w builder.Writer) error {
}
// OrderBy generate "Order By order" statement
func (statement *Statement) OrderBy(order string, args ...interface{}) *Statement {
func (statement *Statement) OrderBy(order interface{}, args ...interface{}) *Statement {
if len(statement.orderStr) > 0 {
statement.orderStr += ", "
}
statement.orderStr += statement.ReplaceQuote(order)
var rawOrder string
switch t := order.(type) {
case (*builder.Expression):
rawOrder = t.Content()
args = t.Args()
case string:
rawOrder = t
default:
statement.LastError = ErrUnSupportedSQLType
return statement
}
statement.orderStr += statement.ReplaceQuote(rawOrder)
if len(args) > 0 {
statement.orderArgs = append(statement.orderArgs, args...)
}

View File

@ -275,7 +275,7 @@ func (session *Session) Limit(limit int, start ...int) *Session {
// OrderBy provide order by query condition, the input parameter is the content
// after order by on a sql statement.
func (session *Session) OrderBy(order string, args ...interface{}) *Session {
func (session *Session) OrderBy(order interface{}, args ...interface{}) *Session {
session.statement.OrderBy(order, args...)
return session
}