diff --git a/client/orm/db_tables.go b/client/orm/db_tables.go index 34fa0f07..94450f94 100644 --- a/client/orm/db_tables.go +++ b/client/orm/db_tables.go @@ -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 { diff --git a/client/orm/orm.go b/client/orm/orm.go index d1a66b88..27ae1fc2 100644 --- a/client/orm/orm.go +++ b/client/orm/orm.go @@ -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) diff --git a/client/orm/orm_conds.go b/client/orm/orm_conds.go index 7a798c6f..a2f6743c 100644 --- a/client/orm/orm_conds.go +++ b/client/orm/orm_conds.go @@ -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 { diff --git a/client/orm/orm_queryset.go b/client/orm/orm_queryset.go index e6493d8f..29b65b1a 100644 --- a/client/orm/orm_queryset.go +++ b/client/orm/orm_queryset.go @@ -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 } diff --git a/client/orm/orm_test.go b/client/orm/orm_test.go index 8f057df1..e6041396 100644 --- a/client/orm/orm_test.go +++ b/client/orm/orm_test.go @@ -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) diff --git a/client/orm/structs/clauses/order.go b/client/orm/structs/clauses/order/order.go similarity index 54% rename from client/orm/structs/clauses/order.go rename to client/orm/structs/clauses/order/order.go index 432bb8f4..c07202d5 100644 --- a/client/orm/structs/clauses/order.go +++ b/client/orm/structs/clauses/order/order.go @@ -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 } diff --git a/client/orm/structs/clauses/order/order_test.go b/client/orm/structs/clauses/order/order_test.go new file mode 100644 index 00000000..93072960 --- /dev/null +++ b/client/orm/structs/clauses/order/order_test.go @@ -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() + } + +} diff --git a/client/orm/structs/clauses/order_test.go b/client/orm/structs/clauses/order_test.go deleted file mode 100644 index dbb37a0a..00000000 --- a/client/orm/structs/clauses/order_test.go +++ /dev/null @@ -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() - } -} - diff --git a/client/orm/structs/clauses/const.go b/client/orm/structs/const.go similarity index 81% rename from client/orm/structs/clauses/const.go rename to client/orm/structs/const.go index 747d3fd7..42a1845a 100644 --- a/client/orm/structs/clauses/const.go +++ b/client/orm/structs/const.go @@ -1,4 +1,4 @@ -package clauses +package structs const ( ExprSep = "__" diff --git a/client/orm/types.go b/client/orm/types.go index 14a34025..d39aa75c 100644 --- a/client/orm/types.go +++ b/client/orm/types.go @@ -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`)