Add limit, order builder

This commit is contained in:
Jinzhu 2018-03-03 14:29:07 +08:00
parent 1f501c36d7
commit f0a88b68a9
2 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,26 @@
package sqlbuilder
import (
"fmt"
"github.com/jinzhu/gorm"
)
// BuildLimitCondition build limit condition
func BuildLimitCondition(tx *gorm.DB) chan *Builder {
limitChan := make(chan *Builder)
go func() {
builder := &Builder{}
if limit := tx.Statement.Limit; limit.Limit != nil {
builder.SQL.WriteString(fmt.Sprintf(" LIMIT %d", *limit.Limit))
if limit.Offset != nil {
builder.SQL.WriteString(fmt.Sprintf(" OFFSET %d", *limit.Offset))
}
}
limitChan <- builder
}()
return limitChan
}

View File

@ -0,0 +1,30 @@
package sqlbuilder
import "github.com/jinzhu/gorm"
// BuildOrderCondition build order condition
func BuildOrderCondition(tx *gorm.DB) chan *Builder {
orderChan := make(chan *Builder)
go func() {
builder := &Builder{}
if orderBy := tx.Statement.OrderBy; len(orderBy) > 0 {
builder.SQL.WriteString(" ORDER BY ")
for i, by := range orderBy {
if i > 0 {
builder.SQL.WriteString(", ")
}
if str, ok := by.(string); ok {
builder.SQL.WriteString(str)
} else {
buildCondition(tx, by, builder)
}
}
}
orderChan <- builder
}()
return orderChan
}