move dir and add ut for ParseOrder

This commit is contained in:
jianzhiyao 2020-10-27 19:32:56 +08:00
parent beedfa1b53
commit 6d828e7939
10 changed files with 179 additions and 137 deletions

View File

@ -16,7 +16,8 @@ package orm
import ( import (
"fmt" "fmt"
"github.com/astaxie/beego/client/orm/structs/clauses" "github.com/astaxie/beego/client/orm/structs"
"github.com/astaxie/beego/client/orm/structs/clauses/order"
"strings" "strings"
"time" "time"
) )
@ -422,7 +423,7 @@ func (t *dbTables) getGroupSQL(groups []string) (groupSQL string) {
} }
// generate order sql. // generate order sql.
func (t *dbTables) getOrderSQL(orders []*clauses.Order) (orderSQL string) { func (t *dbTables) getOrderSQL(orders []*order.Order) (orderSQL string) {
if len(orders) == 0 { if len(orders) == 0 {
return return
} }
@ -432,7 +433,7 @@ func (t *dbTables) getOrderSQL(orders []*clauses.Order) (orderSQL string) {
orderSqls := make([]string, 0, len(orders)) orderSqls := make([]string, 0, len(orders))
for _, order := range orders { for _, order := range orders {
column := order.GetColumn() column := order.GetColumn()
clause := strings.Split(column, clauses.ExprDot) clause := strings.Split(column, structs.ExprDot)
if order.IsRaw() { if order.IsRaw() {
if len(clause) == 2 { if len(clause) == 2 {

View File

@ -58,7 +58,7 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"github.com/astaxie/beego/client/orm/structs/clauses" order2 "github.com/astaxie/beego/client/orm/structs/clauses/order"
"os" "os"
"reflect" "reflect"
"time" "time"
@ -352,7 +352,7 @@ func (o *ormBase) LoadRelatedWithCtx(ctx context.Context, md interface{}, name s
qs.relDepth = relDepth qs.relDepth = relDepth
if len(order) > 0 { if len(order) > 0 {
qs.orders = clauses.ParseOrder(order) qs.orders = order2.ParseOrder(order)
} }
find := ind.FieldByIndex(fi.fieldIndex) find := ind.FieldByIndex(fi.fieldIndex)

View File

@ -16,13 +16,13 @@ package orm
import ( import (
"fmt" "fmt"
"github.com/astaxie/beego/client/orm/structs/clauses" "github.com/astaxie/beego/client/orm/structs"
"strings" "strings"
) )
// ExprSep define the expression separation // ExprSep define the expression separation
const ( const (
ExprSep = clauses.ExprSep ExprSep = structs.ExprSep
) )
type condValue struct { type condValue struct {

View File

@ -18,7 +18,7 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/astaxie/beego/client/orm/hints" "github.com/astaxie/beego/client/orm/hints"
"github.com/astaxie/beego/client/orm/structs/clauses" "github.com/astaxie/beego/client/orm/structs/clauses/order"
) )
type colValue struct { type colValue struct {
@ -71,7 +71,7 @@ type querySet struct {
limit int64 limit int64
offset int64 offset int64
groups []string groups []string
orders []*clauses.Order orders []*order.Order
distinct bool distinct bool
forUpdate bool forUpdate bool
useIndex int useIndex int
@ -143,12 +143,12 @@ func (o querySet) OrderBy(expressions ...string) QuerySeter {
if len(expressions) <= 0 { if len(expressions) <= 0 {
return &o return &o
} }
o.orders = clauses.ParseOrder(expressions...) o.orders = order.ParseOrder(expressions...)
return &o return &o
} }
// add ORDER expression. // add ORDER expression.
func (o querySet) OrderClauses(orders ...*clauses.Order) QuerySeter { func (o querySet) OrderClauses(orders ...*order.Order) QuerySeter {
if len(orders) <= 0 { if len(orders) <= 0 {
return &o return &o
} }

View File

@ -21,7 +21,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"fmt" "fmt"
"github.com/astaxie/beego/client/orm/structs/clauses" "github.com/astaxie/beego/client/orm/structs/clauses/order"
"io/ioutil" "io/ioutil"
"math" "math"
"os" "os"
@ -1080,9 +1080,9 @@ func TestOrderBy(t *testing.T) {
throwFail(t, AssertIs(num, 1)) throwFail(t, AssertIs(num, 1))
num, err = qs.OrderClauses( num, err = qs.OrderClauses(
clauses.OrderClause( order.Clause(
clauses.OrderColumn(`profile__age`), order.Column(`profile__age`),
clauses.OrderSortDescending(), order.SortDescending(),
), ),
).Filter("user_name", "astaxie").Count() ).Filter("user_name", "astaxie").Count()
throwFail(t, err) throwFail(t, err)
@ -1090,9 +1090,9 @@ func TestOrderBy(t *testing.T) {
if IsMysql { if IsMysql {
num, err = qs.OrderClauses( num, err = qs.OrderClauses(
clauses.OrderClause( order.Clause(
clauses.OrderColumn(`rand()`), order.Column(`rand()`),
clauses.OrderRaw(), order.Raw(),
), ),
).Filter("user_name", "astaxie").Count() ).Filter("user_name", "astaxie").Count()
throwFail(t, err) throwFail(t, err)
@ -1185,9 +1185,9 @@ func TestValues(t *testing.T) {
} }
num, err = qs.OrderClauses( num, err = qs.OrderClauses(
clauses.OrderClause( order.Clause(
clauses.OrderColumn("Id"), order.Column("Id"),
clauses.OrderSortAscending(), order.SortAscending(),
), ),
).Values(&maps) ).Values(&maps)
throwFail(t, err) throwFail(t, err)

View File

@ -1,16 +1,19 @@
package clauses package order
import "strings" import (
"github.com/astaxie/beego/client/orm/structs"
"strings"
)
type Sort int8 type Sort int8
const ( const (
SortNone Sort = 0 None Sort = 0
SortAscending Sort = 1 Ascending Sort = 1
SortDescending Sort = 2 Descending Sort = 2
) )
type OrderOption func(order *Order) type Option func(order *Order)
type Order struct { type Order struct {
column string column string
@ -18,7 +21,7 @@ type Order struct {
isRaw bool isRaw bool
} }
func OrderClause(options ...OrderOption) *Order { func Clause(options ...Option) *Order {
o := &Order{} o := &Order{}
for _, option := range options { for _, option := range options {
option(o) option(o)
@ -37,9 +40,9 @@ func (o *Order) GetSort() Sort {
func (o *Order) SortString() string { func (o *Order) SortString() string {
switch o.GetSort() { switch o.GetSort() {
case SortAscending: case Ascending:
return "ASC" return "ASC"
case SortDescending: case Descending:
return "DESC" return "DESC"
} }
@ -53,10 +56,10 @@ func (o *Order) IsRaw() bool {
func ParseOrder(expressions ...string) []*Order { func ParseOrder(expressions ...string) []*Order {
var orders []*Order var orders []*Order
for _, expression := range expressions { for _, expression := range expressions {
sort := SortAscending sort := Ascending
column := strings.ReplaceAll(expression, ExprSep, ExprDot) column := strings.ReplaceAll(expression, structs.ExprSep, structs.ExprDot)
if column[0] == '-' { if column[0] == '-' {
sort = SortDescending sort = Descending
column = column[1:] column = column[1:]
} }
@ -69,31 +72,31 @@ func ParseOrder(expressions ...string) []*Order {
return orders return orders
} }
func OrderColumn(column string) OrderOption { func Column(column string) Option {
return func(order *Order) { return func(order *Order) {
order.column = strings.ReplaceAll(column, ExprSep, ExprDot) order.column = strings.ReplaceAll(column, structs.ExprSep, structs.ExprDot)
} }
} }
func sort(sort Sort) OrderOption { func sort(sort Sort) Option {
return func(order *Order) { return func(order *Order) {
order.sort = sort order.sort = sort
} }
} }
func OrderSortAscending() OrderOption { func SortAscending() Option {
return sort(SortAscending) return sort(Ascending)
} }
func OrderSortDescending() OrderOption { func SortDescending() Option {
return sort(SortDescending) return sort(Descending)
} }
func OrderSortNone() OrderOption { func SortNone() Option {
return sort(SortNone) return sort(None)
} }
func OrderRaw() OrderOption { func Raw() Option {
return func(order *Order) { return func(order *Order) {
order.isRaw = true order.isRaw = true
} }

View File

@ -0,0 +1,112 @@
package order
import (
"testing"
)
func TestOrderClause(t *testing.T) {
var (
column = `a`
)
o := Clause(
Column(column),
)
if o.GetColumn() != column {
t.Error()
}
}
func TestOrderSortAscending(t *testing.T) {
o := Clause(
SortAscending(),
)
if o.GetSort() != Ascending {
t.Error()
}
}
func TestOrderSortDescending(t *testing.T) {
o := Clause(
SortDescending(),
)
if o.GetSort() != Descending {
t.Error()
}
}
func TestOrderSortNone(t *testing.T) {
o1 := Clause(
SortNone(),
)
if o1.GetSort() != None {
t.Error()
}
o2 := Clause()
if o2.GetSort() != None {
t.Error()
}
}
func TestOrderRaw(t *testing.T) {
o1 := Clause()
if o1.IsRaw() {
t.Error()
}
o2 := Clause(
Raw(),
)
if !o2.IsRaw() {
t.Error()
}
}
func TestOrderColumn(t *testing.T) {
o1 := Clause(
Column(`aaa`),
)
if o1.GetColumn() != `aaa` {
t.Error()
}
}
func TestParseOrder(t *testing.T) {
orders := ParseOrder(
`-user__status`,
`status`,
`user__status`,
)
t.Log(orders)
if orders[0].GetSort() != Descending {
t.Error()
}
if orders[0].GetColumn() != `user.status` {
t.Error()
}
if orders[1].GetColumn() != `status` {
t.Error()
}
if orders[1].GetSort() != Ascending {
t.Error()
}
if orders[2].GetColumn() != `user.status` {
t.Error()
}
}

View File

@ -1,80 +0,0 @@
package clauses
import "testing"
func TestOrderClause(t *testing.T) {
var (
column = `a`
)
o := OrderClause(
OrderColumn(column),
)
if o.GetColumn() != column {
t.Error()
}
}
func TestOrderSortAscending(t *testing.T) {
o := OrderClause(
OrderSortAscending(),
)
if o.GetSort() != SortAscending {
t.Error()
}
}
func TestOrderSortDescending(t *testing.T) {
o := OrderClause(
OrderSortDescending(),
)
if o.GetSort() != SortDescending {
t.Error()
}
}
func TestOrderSortNone(t *testing.T) {
o1 := OrderClause(
OrderSortNone(),
)
if o1.GetSort() != SortNone {
t.Error()
}
o2 := OrderClause()
if o2.GetSort() != SortNone {
t.Error()
}
}
func TestOrderRaw(t *testing.T) {
o1 := OrderClause()
if o1.IsRaw() {
t.Error()
}
o2 := OrderClause(
OrderRaw(),
)
if !o2.IsRaw() {
t.Error()
}
}
func TestOrderColumn(t *testing.T) {
o1 := OrderClause(
OrderColumn(`aaa`),
)
if o1.GetColumn() != `aaa` {
t.Error()
}
}

View File

@ -1,4 +1,4 @@
package clauses package structs
const ( const (
ExprSep = "__" ExprSep = "__"

View File

@ -17,7 +17,7 @@ package orm
import ( import (
"context" "context"
"database/sql" "database/sql"
"github.com/astaxie/beego/client/orm/structs/clauses" "github.com/astaxie/beego/client/orm/structs/clauses/order"
"reflect" "reflect"
"time" "time"
@ -292,20 +292,26 @@ type QuerySeter interface {
OrderBy(exprs ...string) QuerySeter OrderBy(exprs ...string) QuerySeter
// add ORDER expression by order clauses // add ORDER expression by order clauses
// for example: // for example:
// OrderClauses(clauses.OrderClause( // OrderClauses(
// clauses.OrderColumn(`status`), // order.Clause(
// clauses.OrderSortAscending(),//default None // order.Column("Id"),
// order.SortAscending(),
// ),
// order.Clause(
// order.Column("status"),
// order.SortDescending(),
// ),
// )
// OrderClauses(order.Clause(
// order.Column(`user__status`),
// order.SortDescending(),//default None
// )) // ))
// OrderClauses(clauses.OrderClause( // OrderClauses(order.Clause(
// clauses.OrderColumn(`user__status`), // order.Column(`random()`),
// clauses.OrderSortDescending(),//default None // order.SortNone(),//default None
// order.Raw(),//default false.if true, do not check field is valid or not
// )) // ))
// OrderClauses(clauses.OrderClause( OrderClauses(orders ...*order.Order) QuerySeter
// clauses.OrderColumn(`random()`),
// clauses.OrderSortNone(),//default None
// clauses.OrderRaw(),//default false.if true, do not check field is valid or not
// ))
OrderClauses(orders ...*clauses.Order) QuerySeter
// add FORCE INDEX expression. // add FORCE INDEX expression.
// for example: // for example:
// qs.ForceIndex(`idx_name1`,`idx_name2`) // qs.ForceIndex(`idx_name1`,`idx_name2`)