move dir & delete some useless function
This commit is contained in:
parent
9bd2934e42
commit
544c621017
@ -16,7 +16,7 @@ package orm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/astaxie/beego/client/orm/structs"
|
||||
"github.com/astaxie/beego/client/orm/structs/clauses"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@ -422,7 +422,7 @@ func (t *dbTables) getGroupSQL(groups []string) (groupSQL string) {
|
||||
}
|
||||
|
||||
// generate order sql.
|
||||
func (t *dbTables) getOrderSQL(orders []*structs.OrderClause) (orderSQL string) {
|
||||
func (t *dbTables) getOrderSQL(orders []*clauses.Order) (orderSQL string) {
|
||||
if len(orders) == 0 {
|
||||
return
|
||||
}
|
||||
@ -431,10 +431,13 @@ func (t *dbTables) getOrderSQL(orders []*structs.OrderClause) (orderSQL string)
|
||||
|
||||
orderSqls := make([]string, 0, len(orders))
|
||||
for _, order := range orders {
|
||||
column := order.Column
|
||||
asc := "ASC"
|
||||
if order.Sort == structs.DESCENDING {
|
||||
asc = "DESC"
|
||||
column := order.GetColumn()
|
||||
var sort string
|
||||
switch order.GetSort() {
|
||||
case clauses.ASCENDING:
|
||||
sort = "ASC"
|
||||
case clauses.DESCENDING:
|
||||
sort = "DESC"
|
||||
}
|
||||
clause := strings.Split(column, ExprSep)
|
||||
|
||||
@ -443,7 +446,7 @@ func (t *dbTables) getOrderSQL(orders []*structs.OrderClause) (orderSQL string)
|
||||
panic(fmt.Errorf("unknown field/column name `%s`", strings.Join(clause, ExprSep)))
|
||||
}
|
||||
|
||||
orderSqls = append(orderSqls, fmt.Sprintf("%s.%s%s%s %s", index, Q, fi.column, Q, asc))
|
||||
orderSqls = append(orderSqls, fmt.Sprintf("%s.%s%s%s %s", index, Q, fi.column, Q, sort))
|
||||
}
|
||||
|
||||
orderSQL = fmt.Sprintf("ORDER BY %s ", strings.Join(orderSqls, ", "))
|
||||
|
||||
@ -58,7 +58,7 @@ import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/astaxie/beego/client/orm/structs"
|
||||
"github.com/astaxie/beego/client/orm/structs/clauses"
|
||||
"os"
|
||||
"reflect"
|
||||
"time"
|
||||
@ -352,7 +352,7 @@ func (o *ormBase) LoadRelatedWithCtx(ctx context.Context, md interface{}, name s
|
||||
qs.relDepth = relDepth
|
||||
|
||||
if len(order) > 0 {
|
||||
qs.orders = structs.ParseOrderClause(order)
|
||||
qs.orders = clauses.ParseOrder(order)
|
||||
}
|
||||
|
||||
find := ind.FieldByIndex(fi.fieldIndex)
|
||||
|
||||
@ -16,12 +16,13 @@ package orm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/astaxie/beego/client/orm/structs/clauses"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ExprSep define the expression separation
|
||||
const (
|
||||
ExprSep = "__"
|
||||
ExprSep = clauses.ExprSep
|
||||
)
|
||||
|
||||
type condValue struct {
|
||||
|
||||
@ -18,7 +18,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/astaxie/beego/client/orm/hints"
|
||||
"github.com/astaxie/beego/client/orm/structs"
|
||||
"github.com/astaxie/beego/client/orm/structs/clauses"
|
||||
)
|
||||
|
||||
type colValue struct {
|
||||
@ -71,7 +71,7 @@ type querySet struct {
|
||||
limit int64
|
||||
offset int64
|
||||
groups []string
|
||||
orders []*structs.OrderClause
|
||||
orders []*clauses.Order
|
||||
distinct bool
|
||||
forUpdate bool
|
||||
useIndex int
|
||||
@ -143,7 +143,7 @@ func (o querySet) OrderBy(expressions ...string) QuerySeter {
|
||||
if len(expressions) <= 0 {
|
||||
return &o
|
||||
}
|
||||
o.orders = structs.ParseOrderClause(expressions...)
|
||||
o.orders = clauses.ParseOrder(expressions...)
|
||||
return &o
|
||||
}
|
||||
|
||||
|
||||
5
client/orm/structs/clauses/const.go
Normal file
5
client/orm/structs/clauses/const.go
Normal file
@ -0,0 +1,5 @@
|
||||
package clauses
|
||||
|
||||
const (
|
||||
ExprSep = "__"
|
||||
)
|
||||
40
client/orm/structs/clauses/order.go
Normal file
40
client/orm/structs/clauses/order.go
Normal file
@ -0,0 +1,40 @@
|
||||
package clauses
|
||||
|
||||
type Sort int8
|
||||
|
||||
const (
|
||||
ASCENDING Sort = 1
|
||||
DESCENDING Sort = 2
|
||||
)
|
||||
|
||||
type Order struct {
|
||||
column string
|
||||
sort Sort
|
||||
}
|
||||
|
||||
func (o *Order) GetColumn() string {
|
||||
return o.column
|
||||
}
|
||||
|
||||
func (o *Order) GetSort() Sort {
|
||||
return o.sort
|
||||
}
|
||||
|
||||
func ParseOrder(expressions ...string) []*Order {
|
||||
var orders []*Order
|
||||
for _, expression := range expressions {
|
||||
sort := ASCENDING
|
||||
column := expression
|
||||
if expression[0] == '-' {
|
||||
sort = DESCENDING
|
||||
column = expression[1:]
|
||||
}
|
||||
|
||||
orders = append(orders, &Order{
|
||||
column: column,
|
||||
sort: sort,
|
||||
})
|
||||
}
|
||||
|
||||
return orders
|
||||
}
|
||||
@ -1,48 +0,0 @@
|
||||
package structs
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Sort int8
|
||||
|
||||
const (
|
||||
ASCENDING Sort = 1
|
||||
DESCENDING Sort = 2
|
||||
)
|
||||
|
||||
type OrderClause struct {
|
||||
Column string
|
||||
Sort Sort
|
||||
}
|
||||
|
||||
var _ fmt.Stringer = new(OrderClause)
|
||||
|
||||
func (o *OrderClause) String() string {
|
||||
sort := ``
|
||||
if o.Sort == ASCENDING {
|
||||
sort = `ASC`
|
||||
} else if o.Sort == DESCENDING {
|
||||
sort = `DESC`
|
||||
} else {
|
||||
return fmt.Sprintf("%s", o.Column)
|
||||
}
|
||||
return fmt.Sprintf("%s %s", o.Column, sort)
|
||||
}
|
||||
|
||||
func ParseOrderClause(expressions ...string) []*OrderClause {
|
||||
var orders []*OrderClause
|
||||
for _, expression := range expressions {
|
||||
sort := ASCENDING
|
||||
column := expression
|
||||
if expression[0] == '-' {
|
||||
sort = DESCENDING
|
||||
column = expression[1:]
|
||||
}
|
||||
|
||||
orders = append(orders, &OrderClause{
|
||||
Column: column,
|
||||
Sort: sort,
|
||||
})
|
||||
}
|
||||
|
||||
return orders
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user