Skip to content

Commit

Permalink
Add API: SortRand() (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
sim-wangyan committed Feb 1, 2024
1 parent 76b273d commit 9acd07d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
12 changes: 9 additions & 3 deletions builder_x.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,17 @@ func (x *BuilderX) X(k string, vs ...interface{}) *BuilderX {
}

func (x *BuilderX) Sort(orderBy string, direction Direction) *BuilderX {
if orderBy == "" || direction == nil {
if orderBy == "" {
return x
}
sort := Sort{orderBy: orderBy, direction: direction()}
x.sorts = append(x.sorts, sort)
if direction == nil {
sort := Sort{orderBy: orderBy, direction: ""}
x.sorts = append(x.sorts, sort)
} else {
sort := Sort{orderBy: orderBy, direction: direction()}
x.sorts = append(x.sorts, sort)
}

return x
}

Expand Down
5 changes: 0 additions & 5 deletions sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package sqlxb
const (
asc = "ASC"
desc = "DESC"
nul = "NULL"
)

type Sort struct {
Expand All @@ -36,7 +35,3 @@ func ASC() string {
func DESC() string {
return desc
}

func NULL() string {
return nul
}
6 changes: 4 additions & 2 deletions to_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,10 @@ func (built *Built) toSortSql(bp *strings.Builder) {
for i := 0; i < length; i++ {
sort := built.Sorts[i]
bp.WriteString(sort.orderBy)
bp.WriteString(SPACE)
bp.WriteString(sort.direction)
if sort.direction != "" {
bp.WriteString(SPACE)
bp.WriteString(sort.direction)
}
if i < length-1 {
bp.WriteString(COMMA)
}
Expand Down

0 comments on commit 9acd07d

Please sign in to comment.