move dir
This commit is contained in:
103
client/orm/clauses/order_clause/order.go
Normal file
103
client/orm/clauses/order_clause/order.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package order_clause
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego/client/orm/clauses"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Sort int8
|
||||
|
||||
const (
|
||||
None Sort = 0
|
||||
Ascending Sort = 1
|
||||
Descending Sort = 2
|
||||
)
|
||||
|
||||
type Option func(order *Order)
|
||||
|
||||
type Order struct {
|
||||
column string
|
||||
sort Sort
|
||||
isRaw bool
|
||||
}
|
||||
|
||||
func Clause(options ...Option) *Order {
|
||||
o := &Order{}
|
||||
for _, option := range options {
|
||||
option(o)
|
||||
}
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *Order) GetColumn() string {
|
||||
return o.column
|
||||
}
|
||||
|
||||
func (o *Order) GetSort() Sort {
|
||||
return o.sort
|
||||
}
|
||||
|
||||
func (o *Order) SortString() string {
|
||||
switch o.GetSort() {
|
||||
case Ascending:
|
||||
return "ASC"
|
||||
case Descending:
|
||||
return "DESC"
|
||||
}
|
||||
|
||||
return ``
|
||||
}
|
||||
|
||||
func (o *Order) IsRaw() bool {
|
||||
return o.isRaw
|
||||
}
|
||||
|
||||
func ParseOrder(expressions ...string) []*Order {
|
||||
var orders []*Order
|
||||
for _, expression := range expressions {
|
||||
sort := Ascending
|
||||
column := strings.ReplaceAll(expression, clauses.ExprSep, clauses.ExprDot)
|
||||
if column[0] == '-' {
|
||||
sort = Descending
|
||||
column = column[1:]
|
||||
}
|
||||
|
||||
orders = append(orders, &Order{
|
||||
column: column,
|
||||
sort: sort,
|
||||
})
|
||||
}
|
||||
|
||||
return orders
|
||||
}
|
||||
|
||||
func Column(column string) Option {
|
||||
return func(order *Order) {
|
||||
order.column = strings.ReplaceAll(column, clauses.ExprSep, clauses.ExprDot)
|
||||
}
|
||||
}
|
||||
|
||||
func sort(sort Sort) Option {
|
||||
return func(order *Order) {
|
||||
order.sort = sort
|
||||
}
|
||||
}
|
||||
|
||||
func SortAscending() Option {
|
||||
return sort(Ascending)
|
||||
}
|
||||
|
||||
func SortDescending() Option {
|
||||
return sort(Descending)
|
||||
}
|
||||
|
||||
func SortNone() Option {
|
||||
return sort(None)
|
||||
}
|
||||
|
||||
func Raw() Option {
|
||||
return func(order *Order) {
|
||||
order.isRaw = true
|
||||
}
|
||||
}
|
||||
144
client/orm/clauses/order_clause/order_test.go
Normal file
144
client/orm/clauses/order_clause/order_test.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package order_clause
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClause(t *testing.T) {
|
||||
var (
|
||||
column = `a`
|
||||
)
|
||||
|
||||
o := Clause(
|
||||
Column(column),
|
||||
)
|
||||
|
||||
if o.GetColumn() != column {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestSortAscending(t *testing.T) {
|
||||
o := Clause(
|
||||
SortAscending(),
|
||||
)
|
||||
|
||||
if o.GetSort() != Ascending {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestSortDescending(t *testing.T) {
|
||||
o := Clause(
|
||||
SortDescending(),
|
||||
)
|
||||
|
||||
if o.GetSort() != Descending {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestSortNone(t *testing.T) {
|
||||
o1 := Clause(
|
||||
SortNone(),
|
||||
)
|
||||
|
||||
if o1.GetSort() != None {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
o2 := Clause()
|
||||
|
||||
if o2.GetSort() != None {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestRaw(t *testing.T) {
|
||||
o1 := Clause()
|
||||
|
||||
if o1.IsRaw() {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
o2 := Clause(
|
||||
Raw(),
|
||||
)
|
||||
|
||||
if !o2.IsRaw() {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestColumn(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()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user