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 (
"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"
"time"
)
@ -422,7 +423,7 @@ func (t *dbTables) getGroupSQL(groups []string) (groupSQL string) {
}
// 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 {
return
}
@ -432,7 +433,7 @@ func (t *dbTables) getOrderSQL(orders []*clauses.Order) (orderSQL string) {
orderSqls := make([]string, 0, len(orders))
for _, order := range orders {
column := order.GetColumn()
clause := strings.Split(column, clauses.ExprDot)
clause := strings.Split(column, structs.ExprDot)
if order.IsRaw() {
if len(clause) == 2 {

View File

@ -58,7 +58,7 @@ import (
"database/sql"
"errors"
"fmt"
"github.com/astaxie/beego/client/orm/structs/clauses"
order2 "github.com/astaxie/beego/client/orm/structs/clauses/order"
"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 = clauses.ParseOrder(order)
qs.orders = order2.ParseOrder(order)
}
find := ind.FieldByIndex(fi.fieldIndex)

View File

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

View File

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

View File

@ -21,7 +21,7 @@ import (
"context"
"database/sql"
"fmt"
"github.com/astaxie/beego/client/orm/structs/clauses"
"github.com/astaxie/beego/client/orm/structs/clauses/order"
"io/ioutil"
"math"
"os"
@ -1080,9 +1080,9 @@ func TestOrderBy(t *testing.T) {
throwFail(t, AssertIs(num, 1))
num, err = qs.OrderClauses(
clauses.OrderClause(
clauses.OrderColumn(`profile__age`),
clauses.OrderSortDescending(),
order.Clause(
order.Column(`profile__age`),
order.SortDescending(),
),
).Filter("user_name", "astaxie").Count()
throwFail(t, err)
@ -1090,9 +1090,9 @@ func TestOrderBy(t *testing.T) {
if IsMysql {
num, err = qs.OrderClauses(
clauses.OrderClause(
clauses.OrderColumn(`rand()`),
clauses.OrderRaw(),
order.Clause(
order.Column(`rand()`),
order.Raw(),
),
).Filter("user_name", "astaxie").Count()
throwFail(t, err)
@ -1185,9 +1185,9 @@ func TestValues(t *testing.T) {
}
num, err = qs.OrderClauses(
clauses.OrderClause(
clauses.OrderColumn("Id"),
clauses.OrderSortAscending(),
order.Clause(
order.Column("Id"),
order.SortAscending(),
),
).Values(&maps)
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
const (
SortNone Sort = 0
SortAscending Sort = 1
SortDescending Sort = 2
None Sort = 0
Ascending Sort = 1
Descending Sort = 2
)
type OrderOption func(order *Order)
type Option func(order *Order)
type Order struct {
column string
@ -18,7 +21,7 @@ type Order struct {
isRaw bool
}
func OrderClause(options ...OrderOption) *Order {
func Clause(options ...Option) *Order {
o := &Order{}
for _, option := range options {
option(o)
@ -37,9 +40,9 @@ func (o *Order) GetSort() Sort {
func (o *Order) SortString() string {
switch o.GetSort() {
case SortAscending:
case Ascending:
return "ASC"
case SortDescending:
case Descending:
return "DESC"
}
@ -53,10 +56,10 @@ func (o *Order) IsRaw() bool {
func ParseOrder(expressions ...string) []*Order {
var orders []*Order
for _, expression := range expressions {
sort := SortAscending
column := strings.ReplaceAll(expression, ExprSep, ExprDot)
sort := Ascending
column := strings.ReplaceAll(expression, structs.ExprSep, structs.ExprDot)
if column[0] == '-' {
sort = SortDescending
sort = Descending
column = column[1:]
}
@ -69,31 +72,31 @@ func ParseOrder(expressions ...string) []*Order {
return orders
}
func OrderColumn(column string) OrderOption {
func Column(column string) Option {
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) {
order.sort = sort
}
}
func OrderSortAscending() OrderOption {
return sort(SortAscending)
func SortAscending() Option {
return sort(Ascending)
}
func OrderSortDescending() OrderOption {
return sort(SortDescending)
func SortDescending() Option {
return sort(Descending)
}
func OrderSortNone() OrderOption {
return sort(SortNone)
func SortNone() Option {
return sort(None)
}
func OrderRaw() OrderOption {
func Raw() Option {
return func(order *Order) {
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 (
ExprSep = "__"

View File

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