From 9bd2934e42b930157ec085108a90e0294264bab0 Mon Sep 17 00:00:00 2001 From: Anker Jam Date: Sun, 25 Oct 2020 21:07:03 +0800 Subject: [PATCH 01/16] add order clause --- client/orm/db_tables.go | 13 ++++---- client/orm/orm.go | 3 +- client/orm/orm_queryset.go | 11 ++++--- client/orm/structs/order_phrase.go | 48 ++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 client/orm/structs/order_phrase.go diff --git a/client/orm/db_tables.go b/client/orm/db_tables.go index 5fd472d1..c67af052 100644 --- a/client/orm/db_tables.go +++ b/client/orm/db_tables.go @@ -16,6 +16,7 @@ package orm import ( "fmt" + "github.com/astaxie/beego/client/orm/structs" "strings" "time" ) @@ -421,7 +422,7 @@ func (t *dbTables) getGroupSQL(groups []string) (groupSQL string) { } // generate order sql. -func (t *dbTables) getOrderSQL(orders []string) (orderSQL string) { +func (t *dbTables) getOrderSQL(orders []*structs.OrderClause) (orderSQL string) { if len(orders) == 0 { return } @@ -430,16 +431,16 @@ func (t *dbTables) getOrderSQL(orders []string) (orderSQL string) { orderSqls := make([]string, 0, len(orders)) for _, order := range orders { + column := order.Column asc := "ASC" - if order[0] == '-' { + if order.Sort == structs.DESCENDING { asc = "DESC" - order = order[1:] } - exprs := strings.Split(order, ExprSep) + clause := strings.Split(column, ExprSep) - index, _, fi, suc := t.parseExprs(t.mi, exprs) + index, _, fi, suc := t.parseExprs(t.mi, clause) if !suc { - panic(fmt.Errorf("unknown field/column name `%s`", strings.Join(exprs, ExprSep))) + 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)) diff --git a/client/orm/orm.go b/client/orm/orm.go index a83faeb2..52a99572 100644 --- a/client/orm/orm.go +++ b/client/orm/orm.go @@ -58,6 +58,7 @@ import ( "database/sql" "errors" "fmt" + "github.com/astaxie/beego/client/orm/structs" "os" "reflect" "time" @@ -351,7 +352,7 @@ func (o *ormBase) LoadRelatedWithCtx(ctx context.Context, md interface{}, name s qs.relDepth = relDepth if len(order) > 0 { - qs.orders = []string{order} + qs.orders = structs.ParseOrderClause(order) } find := ind.FieldByIndex(fi.fieldIndex) diff --git a/client/orm/orm_queryset.go b/client/orm/orm_queryset.go index ed223e24..480dc561 100644 --- a/client/orm/orm_queryset.go +++ b/client/orm/orm_queryset.go @@ -17,8 +17,8 @@ package orm import ( "context" "fmt" - "github.com/astaxie/beego/client/orm/hints" + "github.com/astaxie/beego/client/orm/structs" ) type colValue struct { @@ -71,7 +71,7 @@ type querySet struct { limit int64 offset int64 groups []string - orders []string + orders []*structs.OrderClause distinct bool forUpdate bool useIndex int @@ -139,8 +139,11 @@ func (o querySet) GroupBy(exprs ...string) QuerySeter { // add ORDER expression. // "column" means ASC, "-column" means DESC. -func (o querySet) OrderBy(exprs ...string) QuerySeter { - o.orders = exprs +func (o querySet) OrderBy(expressions ...string) QuerySeter { + if len(expressions) <= 0 { + return &o + } + o.orders = structs.ParseOrderClause(expressions...) return &o } diff --git a/client/orm/structs/order_phrase.go b/client/orm/structs/order_phrase.go new file mode 100644 index 00000000..30faf429 --- /dev/null +++ b/client/orm/structs/order_phrase.go @@ -0,0 +1,48 @@ +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 +} From 544c621017dd82aa184bced157cb82fe201a0f4a Mon Sep 17 00:00:00 2001 From: Anker Jam Date: Sun, 25 Oct 2020 23:11:05 +0800 Subject: [PATCH 02/16] move dir & delete some useless function --- client/orm/db_tables.go | 17 +++++----- client/orm/orm.go | 4 +-- client/orm/orm_conds.go | 3 +- client/orm/orm_queryset.go | 6 ++-- client/orm/structs/clauses/const.go | 5 +++ client/orm/structs/clauses/order.go | 40 ++++++++++++++++++++++++ client/orm/structs/order_phrase.go | 48 ----------------------------- 7 files changed, 62 insertions(+), 61 deletions(-) create mode 100644 client/orm/structs/clauses/const.go create mode 100644 client/orm/structs/clauses/order.go delete mode 100644 client/orm/structs/order_phrase.go diff --git a/client/orm/db_tables.go b/client/orm/db_tables.go index c67af052..7c6e6fff 100644 --- a/client/orm/db_tables.go +++ b/client/orm/db_tables.go @@ -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, ", ")) diff --git a/client/orm/orm.go b/client/orm/orm.go index 52a99572..d1a66b88 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" + "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) diff --git a/client/orm/orm_conds.go b/client/orm/orm_conds.go index f3fd66f0..7a798c6f 100644 --- a/client/orm/orm_conds.go +++ b/client/orm/orm_conds.go @@ -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 { diff --git a/client/orm/orm_queryset.go b/client/orm/orm_queryset.go index 480dc561..5ca10421 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" + "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 } diff --git a/client/orm/structs/clauses/const.go b/client/orm/structs/clauses/const.go new file mode 100644 index 00000000..a0574a64 --- /dev/null +++ b/client/orm/structs/clauses/const.go @@ -0,0 +1,5 @@ +package clauses + +const ( + ExprSep = "__" +) diff --git a/client/orm/structs/clauses/order.go b/client/orm/structs/clauses/order.go new file mode 100644 index 00000000..050bcf1b --- /dev/null +++ b/client/orm/structs/clauses/order.go @@ -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 +} diff --git a/client/orm/structs/order_phrase.go b/client/orm/structs/order_phrase.go deleted file mode 100644 index 30faf429..00000000 --- a/client/orm/structs/order_phrase.go +++ /dev/null @@ -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 -} From b1d5ba8ece68900ce448cf8cd79fc42f76ccdda0 Mon Sep 17 00:00:00 2001 From: Anker Jam Date: Sun, 25 Oct 2020 23:16:41 +0800 Subject: [PATCH 03/16] support sort none --- client/orm/db_tables.go | 4 ++-- client/orm/structs/clauses/order.go | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/client/orm/db_tables.go b/client/orm/db_tables.go index 7c6e6fff..5c330f4e 100644 --- a/client/orm/db_tables.go +++ b/client/orm/db_tables.go @@ -434,9 +434,9 @@ func (t *dbTables) getOrderSQL(orders []*clauses.Order) (orderSQL string) { column := order.GetColumn() var sort string switch order.GetSort() { - case clauses.ASCENDING: + case clauses.SortAscending: sort = "ASC" - case clauses.DESCENDING: + case clauses.SortDescending: sort = "DESC" } clause := strings.Split(column, ExprSep) diff --git a/client/orm/structs/clauses/order.go b/client/orm/structs/clauses/order.go index 050bcf1b..66e8fe73 100644 --- a/client/orm/structs/clauses/order.go +++ b/client/orm/structs/clauses/order.go @@ -3,8 +3,9 @@ package clauses type Sort int8 const ( - ASCENDING Sort = 1 - DESCENDING Sort = 2 + SortNone Sort = 0 + SortAscending Sort = 1 + SortDescending Sort = 2 ) type Order struct { @@ -23,10 +24,10 @@ func (o *Order) GetSort() Sort { func ParseOrder(expressions ...string) []*Order { var orders []*Order for _, expression := range expressions { - sort := ASCENDING + sort := SortAscending column := expression if expression[0] == '-' { - sort = DESCENDING + sort = SortDescending column = expression[1:] } From 56fa213a6edf0c9ae0421ebf59683cbe6c41c8ca Mon Sep 17 00:00:00 2001 From: Anker Jam Date: Sun, 25 Oct 2020 23:49:21 +0800 Subject: [PATCH 04/16] support OrderClauses for QuerySeter --- client/orm/orm_queryset.go | 9 +++++++ client/orm/structs/clauses/order.go | 34 ++++++++++++++++++++++++ client/orm/structs/clauses/order_test.go | 29 ++++++++++++++++++++ client/orm/types.go | 19 +++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 client/orm/structs/clauses/order_test.go diff --git a/client/orm/orm_queryset.go b/client/orm/orm_queryset.go index 5ca10421..e6493d8f 100644 --- a/client/orm/orm_queryset.go +++ b/client/orm/orm_queryset.go @@ -147,6 +147,15 @@ func (o querySet) OrderBy(expressions ...string) QuerySeter { return &o } +// add ORDER expression. +func (o querySet) OrderClauses(orders ...*clauses.Order) QuerySeter { + if len(orders) <= 0 { + return &o + } + o.orders = orders + return &o +} + // add DISTINCT to SELECT func (o querySet) Distinct() QuerySeter { o.distinct = true diff --git a/client/orm/structs/clauses/order.go b/client/orm/structs/clauses/order.go index 66e8fe73..64e4ebbd 100644 --- a/client/orm/structs/clauses/order.go +++ b/client/orm/structs/clauses/order.go @@ -8,9 +8,21 @@ const ( SortDescending Sort = 2 ) +type OrderOption func(order *Order) + type Order struct { column string sort Sort + isRaw bool +} + +func OrderClause(options ...OrderOption) *Order { + o := &Order{} + for _, option := range options { + option(o) + } + + return o } func (o *Order) GetColumn() string { @@ -21,6 +33,10 @@ func (o *Order) GetSort() Sort { return o.sort } +func (o *Order) IsRaw() bool { + return o.isRaw +} + func ParseOrder(expressions ...string) []*Order { var orders []*Order for _, expression := range expressions { @@ -39,3 +55,21 @@ func ParseOrder(expressions ...string) []*Order { return orders } + +func OrderColumn(column string) OrderOption { + return func(order *Order) { + order.column = column + } +} + +func OrderSort(sort Sort) OrderOption { + return func(order *Order) { + order.sort = sort + } +} + +func OrderRaw(isRaw bool) OrderOption { + return func(order *Order) { + order.isRaw = isRaw + } +} diff --git a/client/orm/structs/clauses/order_test.go b/client/orm/structs/clauses/order_test.go new file mode 100644 index 00000000..2f44975d --- /dev/null +++ b/client/orm/structs/clauses/order_test.go @@ -0,0 +1,29 @@ +package clauses + +import "testing" + +func TestOrderClause(t *testing.T) { + var ( + column = `a` + sort = SortDescending + raw = true + ) + + o := OrderClause( + OrderColumn(column), + OrderSort(sort), + OrderRaw(raw), + ) + + if o.GetColumn() != column { + t.Error() + } + + if o.GetSort() != sort { + t.Error() + } + + if o.IsRaw() != raw { + t.Error() + } +} diff --git a/client/orm/types.go b/client/orm/types.go index 34c61d51..f1d6def7 100644 --- a/client/orm/types.go +++ b/client/orm/types.go @@ -17,6 +17,7 @@ package orm import ( "context" "database/sql" + "github.com/astaxie/beego/client/orm/structs/clauses" "reflect" "time" @@ -289,6 +290,24 @@ type QuerySeter interface { // for example: // qs.OrderBy("-status") OrderBy(exprs ...string) QuerySeter + // add ORDER expression by order clauses + // for example: + // OrderClauses(clauses.OrderClause( + // clauses.OrderColumn(`status`), + // clauses.OrderSort(clauses.SortAscending), + // clauses.OrderRaw(false), + // )) + // OrderClauses(clauses.OrderClause( + // clauses.OrderColumn(`user__status`), + // clauses.OrderSort(clauses.SortAscending), + // clauses.OrderRaw(false), + // )) + // OrderClauses(clauses.OrderClause( + // clauses.OrderColumn(`random()`), + // clauses.OrderSort(clauses.SortNone), + // clauses.OrderRaw(true), + // )) + OrderClauses(orders ...*clauses.Order) QuerySeter // add FORCE INDEX expression. // for example: // qs.ForceIndex(`idx_name1`,`idx_name2`) From 7d4e88c1b951db2ac778feaf02ac530ff171c212 Mon Sep 17 00:00:00 2001 From: Anker Jam Date: Mon, 26 Oct 2020 00:01:15 +0800 Subject: [PATCH 05/16] support raw order --- client/orm/db_tables.go | 29 ++++++++++++++++------------- client/orm/structs/clauses/const.go | 1 + client/orm/structs/clauses/order.go | 20 +++++++++++++++++--- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/client/orm/db_tables.go b/client/orm/db_tables.go index 5c330f4e..34fa0f07 100644 --- a/client/orm/db_tables.go +++ b/client/orm/db_tables.go @@ -432,21 +432,24 @@ func (t *dbTables) getOrderSQL(orders []*clauses.Order) (orderSQL string) { orderSqls := make([]string, 0, len(orders)) for _, order := range orders { column := order.GetColumn() - var sort string - switch order.GetSort() { - case clauses.SortAscending: - sort = "ASC" - case clauses.SortDescending: - sort = "DESC" - } - clause := strings.Split(column, ExprSep) + clause := strings.Split(column, clauses.ExprDot) - index, _, fi, suc := t.parseExprs(t.mi, clause) - if !suc { - panic(fmt.Errorf("unknown field/column name `%s`", strings.Join(clause, ExprSep))) - } + if order.IsRaw() { + if len(clause) == 2 { + orderSqls = append(orderSqls, fmt.Sprintf("%s.%s%s%s %s", clause[0], Q, clause[1], Q, order.SortString())) + } else if len(clause) == 1 { + orderSqls = append(orderSqls, fmt.Sprintf("%s%s%s %s", Q, clause[0], Q, order.SortString())) + } else { + panic(fmt.Errorf("unknown field/column name `%s`", strings.Join(clause, ExprSep))) + } + } else { + index, _, fi, suc := t.parseExprs(t.mi, clause) + if !suc { + 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, sort)) + orderSqls = append(orderSqls, fmt.Sprintf("%s.%s%s%s %s", index, Q, fi.column, Q, order.SortString())) + } } orderSQL = fmt.Sprintf("ORDER BY %s ", strings.Join(orderSqls, ", ")) diff --git a/client/orm/structs/clauses/const.go b/client/orm/structs/clauses/const.go index a0574a64..747d3fd7 100644 --- a/client/orm/structs/clauses/const.go +++ b/client/orm/structs/clauses/const.go @@ -2,4 +2,5 @@ package clauses const ( ExprSep = "__" + ExprDot = "." ) diff --git a/client/orm/structs/clauses/order.go b/client/orm/structs/clauses/order.go index 64e4ebbd..2f7ff6ba 100644 --- a/client/orm/structs/clauses/order.go +++ b/client/orm/structs/clauses/order.go @@ -1,5 +1,7 @@ package clauses +import "strings" + type Sort int8 const ( @@ -33,6 +35,18 @@ func (o *Order) GetSort() Sort { return o.sort } +func (o *Order) SortString() string { + switch o.GetSort() { + case SortAscending: + return "ASC" + case SortDescending: + return "DESC" + } + + return `` +} + + func (o *Order) IsRaw() bool { return o.isRaw } @@ -41,10 +55,10 @@ func ParseOrder(expressions ...string) []*Order { var orders []*Order for _, expression := range expressions { sort := SortAscending - column := expression - if expression[0] == '-' { + column := strings.ReplaceAll(expression, ExprSep, ExprDot) + if column[0] == '-' { sort = SortDescending - column = expression[1:] + column = column[1:] } orders = append(orders, &Order{ From d24388ad819916bf1bea2920f0bd8577c4cb3ea9 Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Mon, 26 Oct 2020 18:57:01 +0800 Subject: [PATCH 06/16] opt Methods --- client/orm/structs/clauses/order.go | 19 +++++-- client/orm/structs/clauses/order_test.go | 67 +++++++++++++++++++++--- client/orm/types.go | 10 ++-- 3 files changed, 78 insertions(+), 18 deletions(-) diff --git a/client/orm/structs/clauses/order.go b/client/orm/structs/clauses/order.go index 2f7ff6ba..e1110eec 100644 --- a/client/orm/structs/clauses/order.go +++ b/client/orm/structs/clauses/order.go @@ -46,7 +46,6 @@ func (o *Order) SortString() string { return `` } - func (o *Order) IsRaw() bool { return o.isRaw } @@ -76,14 +75,26 @@ func OrderColumn(column string) OrderOption { } } -func OrderSort(sort Sort) OrderOption { +func sort(sort Sort) OrderOption { return func(order *Order) { order.sort = sort } } -func OrderRaw(isRaw bool) OrderOption { +func OrderSortAscending() OrderOption { + return sort(SortAscending) +} + +func OrderSortDescending() OrderOption { + return sort(SortDescending) +} + +func OrderSortNone() OrderOption { + return sort(SortNone) +} + +func OrderRaw() OrderOption { return func(order *Order) { - order.isRaw = isRaw + order.isRaw = true } } diff --git a/client/orm/structs/clauses/order_test.go b/client/orm/structs/clauses/order_test.go index 2f44975d..dbb37a0a 100644 --- a/client/orm/structs/clauses/order_test.go +++ b/client/orm/structs/clauses/order_test.go @@ -5,25 +5,76 @@ import "testing" func TestOrderClause(t *testing.T) { var ( column = `a` - sort = SortDescending - raw = true ) o := OrderClause( OrderColumn(column), - OrderSort(sort), - OrderRaw(raw), ) if o.GetColumn() != column { t.Error() } +} - if o.GetSort() != sort { - t.Error() - } +func TestOrderSortAscending(t *testing.T) { + o := OrderClause( + OrderSortAscending(), + ) - if o.IsRaw() != raw { + 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/types.go b/client/orm/types.go index f1d6def7..14a34025 100644 --- a/client/orm/types.go +++ b/client/orm/types.go @@ -294,18 +294,16 @@ type QuerySeter interface { // for example: // OrderClauses(clauses.OrderClause( // clauses.OrderColumn(`status`), - // clauses.OrderSort(clauses.SortAscending), - // clauses.OrderRaw(false), + // clauses.OrderSortAscending(),//default None // )) // OrderClauses(clauses.OrderClause( // clauses.OrderColumn(`user__status`), - // clauses.OrderSort(clauses.SortAscending), - // clauses.OrderRaw(false), + // clauses.OrderSortDescending(),//default None // )) // OrderClauses(clauses.OrderClause( // clauses.OrderColumn(`random()`), - // clauses.OrderSort(clauses.SortNone), - // clauses.OrderRaw(true), + // 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. From beedfa1b53d1847f6cb1f31b0cafac184fbee156 Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Mon, 26 Oct 2020 19:52:21 +0800 Subject: [PATCH 07/16] add UT --- client/orm/orm_test.go | 34 +++++++++++++++++++++++++++++ client/orm/structs/clauses/order.go | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/client/orm/orm_test.go b/client/orm/orm_test.go index 565f6c60..8f057df1 100644 --- a/client/orm/orm_test.go +++ b/client/orm/orm_test.go @@ -21,6 +21,7 @@ import ( "context" "database/sql" "fmt" + "github.com/astaxie/beego/client/orm/structs/clauses" "io/ioutil" "math" "os" @@ -1077,6 +1078,26 @@ func TestOrderBy(t *testing.T) { num, err = qs.OrderBy("-profile__age").Filter("user_name", "astaxie").Count() throwFail(t, err) throwFail(t, AssertIs(num, 1)) + + num, err = qs.OrderClauses( + clauses.OrderClause( + clauses.OrderColumn(`profile__age`), + clauses.OrderSortDescending(), + ), + ).Filter("user_name", "astaxie").Count() + throwFail(t, err) + throwFail(t, AssertIs(num, 1)) + + if IsMysql { + num, err = qs.OrderClauses( + clauses.OrderClause( + clauses.OrderColumn(`rand()`), + clauses.OrderRaw(), + ), + ).Filter("user_name", "astaxie").Count() + throwFail(t, err) + throwFail(t, AssertIs(num, 1)) + } } func TestAll(t *testing.T) { @@ -1163,6 +1184,19 @@ func TestValues(t *testing.T) { throwFail(t, AssertIs(maps[2]["Profile"], nil)) } + num, err = qs.OrderClauses( + clauses.OrderClause( + clauses.OrderColumn("Id"), + clauses.OrderSortAscending(), + ), + ).Values(&maps) + throwFail(t, err) + throwFail(t, AssertIs(num, 3)) + if num == 3 { + throwFail(t, AssertIs(maps[0]["UserName"], "slene")) + throwFail(t, AssertIs(maps[2]["Profile"], nil)) + } + num, err = qs.OrderBy("Id").Values(&maps, "UserName", "Profile__Age") throwFail(t, err) throwFail(t, AssertIs(num, 3)) diff --git a/client/orm/structs/clauses/order.go b/client/orm/structs/clauses/order.go index e1110eec..432bb8f4 100644 --- a/client/orm/structs/clauses/order.go +++ b/client/orm/structs/clauses/order.go @@ -71,7 +71,7 @@ func ParseOrder(expressions ...string) []*Order { func OrderColumn(column string) OrderOption { return func(order *Order) { - order.column = column + order.column = strings.ReplaceAll(column, ExprSep, ExprDot) } } From 6d828e793968d982bd623ee9cae6e8dbc433e363 Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Tue, 27 Oct 2020 19:32:56 +0800 Subject: [PATCH 08/16] move dir and add ut for ParseOrder --- client/orm/db_tables.go | 7 +- client/orm/orm.go | 4 +- client/orm/orm_conds.go | 4 +- client/orm/orm_queryset.go | 8 +- client/orm/orm_test.go | 20 ++-- .../orm/structs/clauses/{ => order}/order.go | 47 ++++---- .../orm/structs/clauses/order/order_test.go | 112 ++++++++++++++++++ client/orm/structs/clauses/order_test.go | 80 ------------- client/orm/structs/{clauses => }/const.go | 2 +- client/orm/types.go | 32 +++-- 10 files changed, 179 insertions(+), 137 deletions(-) rename client/orm/structs/clauses/{ => order}/order.go (54%) create mode 100644 client/orm/structs/clauses/order/order_test.go delete mode 100644 client/orm/structs/clauses/order_test.go rename client/orm/structs/{clauses => }/const.go (81%) 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`) From d147f4a0184abaf759b7c1f30eb7242b03b439df Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Tue, 27 Oct 2020 19:35:35 +0800 Subject: [PATCH 09/16] format comment --- client/orm/types.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/orm/types.go b/client/orm/types.go index d39aa75c..6630e14f 100644 --- a/client/orm/types.go +++ b/client/orm/types.go @@ -302,11 +302,11 @@ type QuerySeter interface { // order.SortDescending(), // ), // ) - // OrderClauses(order.Clause( + // OrderClauses(order.Clause( // order.Column(`user__status`), // order.SortDescending(),//default None // )) - // OrderClauses(order.Clause( + // OrderClauses(order.Clause( // order.Column(`random()`), // order.SortNone(),//default None // order.Raw(),//default false.if true, do not check field is valid or not From 759982b3b856f322a0d600487a627dac81bd09c4 Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Tue, 27 Oct 2020 20:01:53 +0800 Subject: [PATCH 10/16] add more UT --- .../orm/structs/clauses/order/order_test.go | 44 ++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/client/orm/structs/clauses/order/order_test.go b/client/orm/structs/clauses/order/order_test.go index 93072960..0e9356f0 100644 --- a/client/orm/structs/clauses/order/order_test.go +++ b/client/orm/structs/clauses/order/order_test.go @@ -4,7 +4,7 @@ import ( "testing" ) -func TestOrderClause(t *testing.T) { +func TestClause(t *testing.T) { var ( column = `a` ) @@ -18,7 +18,7 @@ func TestOrderClause(t *testing.T) { } } -func TestOrderSortAscending(t *testing.T) { +func TestSortAscending(t *testing.T) { o := Clause( SortAscending(), ) @@ -28,7 +28,7 @@ func TestOrderSortAscending(t *testing.T) { } } -func TestOrderSortDescending(t *testing.T) { +func TestSortDescending(t *testing.T) { o := Clause( SortDescending(), ) @@ -38,7 +38,7 @@ func TestOrderSortDescending(t *testing.T) { } } -func TestOrderSortNone(t *testing.T) { +func TestSortNone(t *testing.T) { o1 := Clause( SortNone(), ) @@ -54,7 +54,7 @@ func TestOrderSortNone(t *testing.T) { } } -func TestOrderRaw(t *testing.T) { +func TestRaw(t *testing.T) { o1 := Clause() if o1.IsRaw() { @@ -70,7 +70,7 @@ func TestOrderRaw(t *testing.T) { } } -func TestOrderColumn(t *testing.T) { +func TestColumn(t *testing.T) { o1 := Clause( Column(`aaa`), ) @@ -110,3 +110,35 @@ func TestParseOrder(t *testing.T) { } } + +func TestOrder_GetColumn(t *testing.T) { + o := Clause( + Column(`user__id`), + ) + if o.GetColumn() != `user.id` { + t.Error() + } +} + +func TestOrder_GetSort(t *testing.T) { + o := Clause( + SortDescending(), + ) + if o.GetSort() != Descending { + t.Error() + } +} + +func TestOrder_IsRaw(t *testing.T) { + o1 := Clause() + if o1.IsRaw() { + t.Error() + } + + o2 := Clause( + Raw(), + ) + if !o2.IsRaw() { + t.Error() + } +} From 508105d32ac0b4e34d1094535e577a8d9f90ee34 Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Thu, 29 Oct 2020 18:57:16 +0800 Subject: [PATCH 11/16] fix UT:concurrent map iteration and map write --- task/task.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/task/task.go b/task/task.go index 8f25a0f3..00cbbfa7 100644 --- a/task/task.go +++ b/task/task.go @@ -452,9 +452,11 @@ func (m *taskManager) StartTask() { func (m *taskManager) run() { now := time.Now().Local() + m.taskLock.Lock() for _, t := range m.adminTaskList { t.SetNext(nil, now) } + m.taskLock.Unlock() for { // we only use RLock here because NewMapSorter copy the reference, do not change any thing From f8fb50999bc7305256d6c1844313169aab53a9c8 Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Thu, 29 Oct 2020 19:05:32 +0800 Subject: [PATCH 12/16] move dir --- client/orm/{structs => clauses}/const.go | 2 +- .../order => clauses/order_clause}/order.go | 8 +++---- .../order_clause}/order_test.go | 2 +- client/orm/db_tables.go | 7 +++--- client/orm/orm.go | 4 ++-- client/orm/orm_conds.go | 4 ++-- client/orm/orm_queryset.go | 8 +++---- client/orm/orm_test.go | 20 ++++++++--------- client/orm/types.go | 22 +++++++++---------- 9 files changed, 38 insertions(+), 39 deletions(-) rename client/orm/{structs => clauses}/const.go (81%) rename client/orm/{structs/clauses/order => clauses/order_clause}/order.go (85%) rename client/orm/{structs/clauses/order => clauses/order_clause}/order_test.go (98%) diff --git a/client/orm/structs/const.go b/client/orm/clauses/const.go similarity index 81% rename from client/orm/structs/const.go rename to client/orm/clauses/const.go index 42a1845a..747d3fd7 100644 --- a/client/orm/structs/const.go +++ b/client/orm/clauses/const.go @@ -1,4 +1,4 @@ -package structs +package clauses const ( ExprSep = "__" diff --git a/client/orm/structs/clauses/order/order.go b/client/orm/clauses/order_clause/order.go similarity index 85% rename from client/orm/structs/clauses/order/order.go rename to client/orm/clauses/order_clause/order.go index c07202d5..510c5505 100644 --- a/client/orm/structs/clauses/order/order.go +++ b/client/orm/clauses/order_clause/order.go @@ -1,7 +1,7 @@ -package order +package order_clause import ( - "github.com/astaxie/beego/client/orm/structs" + "github.com/astaxie/beego/client/orm/clauses" "strings" ) @@ -57,7 +57,7 @@ func ParseOrder(expressions ...string) []*Order { var orders []*Order for _, expression := range expressions { sort := Ascending - column := strings.ReplaceAll(expression, structs.ExprSep, structs.ExprDot) + column := strings.ReplaceAll(expression, clauses.ExprSep, clauses.ExprDot) if column[0] == '-' { sort = Descending column = column[1:] @@ -74,7 +74,7 @@ func ParseOrder(expressions ...string) []*Order { func Column(column string) Option { return func(order *Order) { - order.column = strings.ReplaceAll(column, structs.ExprSep, structs.ExprDot) + order.column = strings.ReplaceAll(column, clauses.ExprSep, clauses.ExprDot) } } diff --git a/client/orm/structs/clauses/order/order_test.go b/client/orm/clauses/order_clause/order_test.go similarity index 98% rename from client/orm/structs/clauses/order/order_test.go rename to client/orm/clauses/order_clause/order_test.go index 0e9356f0..172e7492 100644 --- a/client/orm/structs/clauses/order/order_test.go +++ b/client/orm/clauses/order_clause/order_test.go @@ -1,4 +1,4 @@ -package order +package order_clause import ( "testing" diff --git a/client/orm/db_tables.go b/client/orm/db_tables.go index 94450f94..76a14fd0 100644 --- a/client/orm/db_tables.go +++ b/client/orm/db_tables.go @@ -16,8 +16,7 @@ package orm import ( "fmt" - "github.com/astaxie/beego/client/orm/structs" - "github.com/astaxie/beego/client/orm/structs/clauses/order" + "github.com/astaxie/beego/client/orm/clauses" "strings" "time" ) @@ -423,7 +422,7 @@ func (t *dbTables) getGroupSQL(groups []string) (groupSQL string) { } // generate order sql. -func (t *dbTables) getOrderSQL(orders []*order.Order) (orderSQL string) { +func (t *dbTables) getOrderSQL(orders []*order_clause.Order) (orderSQL string) { if len(orders) == 0 { return } @@ -433,7 +432,7 @@ func (t *dbTables) getOrderSQL(orders []*order.Order) (orderSQL string) { orderSqls := make([]string, 0, len(orders)) for _, order := range orders { column := order.GetColumn() - clause := strings.Split(column, structs.ExprDot) + clause := strings.Split(column, clauses.ExprDot) if order.IsRaw() { if len(clause) == 2 { diff --git a/client/orm/orm.go b/client/orm/orm.go index 27ae1fc2..a228c626 100644 --- a/client/orm/orm.go +++ b/client/orm/orm.go @@ -58,7 +58,7 @@ import ( "database/sql" "errors" "fmt" - order2 "github.com/astaxie/beego/client/orm/structs/clauses/order" + "github.com/astaxie/beego/client/orm/clauses/order_clause" "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 = order2.ParseOrder(order) + qs.orders = order_clause.ParseOrder(order) } find := ind.FieldByIndex(fi.fieldIndex) diff --git a/client/orm/orm_conds.go b/client/orm/orm_conds.go index a2f6743c..fbf62c5a 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" + "github.com/astaxie/beego/client/orm/clauses" "strings" ) // ExprSep define the expression separation const ( - ExprSep = structs.ExprSep + ExprSep = clauses.ExprSep ) type condValue struct { diff --git a/client/orm/orm_queryset.go b/client/orm/orm_queryset.go index 29b65b1a..45900487 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/order" + "github.com/astaxie/beego/client/orm/clauses/order_clause" ) type colValue struct { @@ -71,7 +71,7 @@ type querySet struct { limit int64 offset int64 groups []string - orders []*order.Order + orders []*order_clause.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 = order.ParseOrder(expressions...) + o.orders = order_clause.ParseOrder(expressions...) return &o } // add ORDER expression. -func (o querySet) OrderClauses(orders ...*order.Order) QuerySeter { +func (o querySet) OrderClauses(orders ...*order_clause.Order) QuerySeter { if len(orders) <= 0 { return &o } diff --git a/client/orm/orm_test.go b/client/orm/orm_test.go index e6041396..2f3f9d55 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/order" + "github.com/astaxie/beego/client/orm/clauses/order_clause" "io/ioutil" "math" "os" @@ -1080,9 +1080,9 @@ func TestOrderBy(t *testing.T) { throwFail(t, AssertIs(num, 1)) num, err = qs.OrderClauses( - order.Clause( - order.Column(`profile__age`), - order.SortDescending(), + order_clause.Clause( + order_clause.Column(`profile__age`), + order_clause.SortDescending(), ), ).Filter("user_name", "astaxie").Count() throwFail(t, err) @@ -1090,9 +1090,9 @@ func TestOrderBy(t *testing.T) { if IsMysql { num, err = qs.OrderClauses( - order.Clause( - order.Column(`rand()`), - order.Raw(), + order_clause.Clause( + order_clause.Column(`rand()`), + order_clause.Raw(), ), ).Filter("user_name", "astaxie").Count() throwFail(t, err) @@ -1185,9 +1185,9 @@ func TestValues(t *testing.T) { } num, err = qs.OrderClauses( - order.Clause( - order.Column("Id"), - order.SortAscending(), + order_clause.Clause( + order_clause.Column("Id"), + order_clause.SortAscending(), ), ).Values(&maps) throwFail(t, err) diff --git a/client/orm/types.go b/client/orm/types.go index 6630e14f..5da40830 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/order" + "github.com/astaxie/beego/client/orm/clauses/order_clause" "reflect" "time" @@ -293,25 +293,25 @@ type QuerySeter interface { // add ORDER expression by order clauses // for example: // OrderClauses( - // order.Clause( + // order_clause.Clause( // order.Column("Id"), // order.SortAscending(), // ), - // order.Clause( + // order_clause.Clause( // order.Column("status"), // order.SortDescending(), // ), // ) - // OrderClauses(order.Clause( - // order.Column(`user__status`), - // order.SortDescending(),//default None + // OrderClauses(order_clause.Clause( + // order_clause.Column(`user__status`), + // order_clause.SortDescending(),//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(order_clause.Clause( + // order_clause.Column(`random()`), + // order_clause.SortNone(),//default None + // order_clause.Raw(),//default false.if true, do not check field is valid or not // )) - OrderClauses(orders ...*order.Order) QuerySeter + OrderClauses(orders ...*order_clause.Order) QuerySeter // add FORCE INDEX expression. // for example: // qs.ForceIndex(`idx_name1`,`idx_name2`) From f864e585e1b82815816e9acfbdfddad8dc9986b9 Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Thu, 29 Oct 2020 19:07:42 +0800 Subject: [PATCH 13/16] fix import --- client/orm/db_tables.go | 1 + 1 file changed, 1 insertion(+) diff --git a/client/orm/db_tables.go b/client/orm/db_tables.go index 76a14fd0..82b8ab7e 100644 --- a/client/orm/db_tables.go +++ b/client/orm/db_tables.go @@ -17,6 +17,7 @@ package orm import ( "fmt" "github.com/astaxie/beego/client/orm/clauses" + "github.com/astaxie/beego/client/orm/clauses/order_clause" "strings" "time" ) From 3150285542bd2339030a92e8cfedf6736038f6ff Mon Sep 17 00:00:00 2001 From: Anker Jam Date: Sat, 9 Jan 2021 21:27:30 +0800 Subject: [PATCH 14/16] fix imports --- client/orm/db_tables.go | 4 ++-- client/orm/orm_queryset.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/client/orm/db_tables.go b/client/orm/db_tables.go index 82b8ab7e..d62d8106 100644 --- a/client/orm/db_tables.go +++ b/client/orm/db_tables.go @@ -16,8 +16,8 @@ package orm import ( "fmt" - "github.com/astaxie/beego/client/orm/clauses" - "github.com/astaxie/beego/client/orm/clauses/order_clause" + "github.com/beego/beego/v2/client/orm/clauses" + "github.com/beego/beego/v2/client/orm/clauses/order_clause" "strings" "time" ) diff --git a/client/orm/orm_queryset.go b/client/orm/orm_queryset.go index 9f7a3c1e..66e1c442 100644 --- a/client/orm/orm_queryset.go +++ b/client/orm/orm_queryset.go @@ -17,8 +17,8 @@ package orm import ( "context" "fmt" - "github.com/astaxie/beego/v2/client/orm/hints" - "github.com/astaxie/beego/v2/client/orm/clauses/order_clause" + "github.com/beego/beego/v2/client/orm/hints" + "github.com/beego/beego/v2/client/orm/clauses/order_clause" ) type colValue struct { From 791c28b1d4a5ec03207d912d550f9cf8e8fec238 Mon Sep 17 00:00:00 2001 From: Anker Jam Date: Sat, 9 Jan 2021 22:16:21 +0800 Subject: [PATCH 15/16] fix imports --- client/orm/clauses/order_clause/order.go | 2 +- client/orm/orm.go | 2 +- client/orm/orm_conds.go | 2 +- client/orm/orm_test.go | 2 +- client/orm/types.go | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/client/orm/clauses/order_clause/order.go b/client/orm/clauses/order_clause/order.go index 510c5505..e45c2f85 100644 --- a/client/orm/clauses/order_clause/order.go +++ b/client/orm/clauses/order_clause/order.go @@ -1,7 +1,7 @@ package order_clause import ( - "github.com/astaxie/beego/client/orm/clauses" + "github.com/beego/beego/v2/client/orm/clauses" "strings" ) diff --git a/client/orm/orm.go b/client/orm/orm.go index f4422648..f33c4b51 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/clauses/order_clause" + "github.com/beego/beego/v2/client/orm/clauses/order_clause" "os" "reflect" "time" diff --git a/client/orm/orm_conds.go b/client/orm/orm_conds.go index 99828f3c..0080d53c 100644 --- a/client/orm/orm_conds.go +++ b/client/orm/orm_conds.go @@ -16,7 +16,7 @@ package orm import ( "fmt" - "github.com/astaxie/beego/client/orm/clauses" + "github.com/beego/beego/v2/client/orm/clauses" "strings" ) diff --git a/client/orm/orm_test.go b/client/orm/orm_test.go index 1dd3bc8d..f2943739 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/clauses/order_clause" + "github.com/beego/beego/v2/client/orm/clauses/order_clause" "io/ioutil" "math" "os" diff --git a/client/orm/types.go b/client/orm/types.go index 37aa9f3f..b22848fe 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/clauses/order_clause" + "github.com/beego/beego/v2/client/orm/clauses/order_clause" "reflect" "time" From 25bf1259c61bea15c04c92228cc15f64dc18dd66 Mon Sep 17 00:00:00 2001 From: Anker Jam Date: Sat, 9 Jan 2021 23:55:18 +0800 Subject: [PATCH 16/16] add change log --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a259efc..28cdd2f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,4 +3,5 @@ - Remove `duration` from prometheus labels. [4391](https://github.com/beego/beego/pull/4391) - Fix `unknown escape sequence` in generated code. [4385](https://github.com/beego/beego/pull/4385) - Using fixed name `commentRouter.go` as generated file name. [4385](https://github.com/beego/beego/pull/4385) -- Fix 4383: ORM Adapter produces panic when using orm.RegisterModelWithPrefix. [4386](https://github.com/beego/beego/pull/4386) \ No newline at end of file +- Fix 4383: ORM Adapter produces panic when using orm.RegisterModelWithPrefix. [4386](https://github.com/beego/beego/pull/4386) +- Support 4144: Add new api for order by for supporting multiple way to query [4294](https://github.com/beego/beego/pull/4294) \ No newline at end of file