sql-builder/db/build.go

64 lines
1.8 KiB
Go

package db
import (
"fmt"
"strings"
"git.botann.com/lijun/sql-builder/util"
)
func (s *SqlBuilder[T]) buildSql() string {
s.DeletePlaceholderSymbol()
return s.Sql
}
func (s *SqlBuilder[T]) buildPaginateSql(sqls string) string {
s.Opts.Page.Init()
pageIndex := s.Opts.Page.GetPageIndex()
pageSize := s.Opts.Page.GetPageSize()
return fmt.Sprintf("%s limit %d offset %d", sqls, pageSize, (pageIndex-1)*pageSize)
}
func (s *SqlBuilder[T]) buildCountSql(sqls string) string {
return fmt.Sprintf("select count(1) from (%s) as t", sqls)
}
func (s *SqlBuilder[T]) buildSelect(selects ...string) {
for _, v := range selects {
s.Sql = strings.Replace(s.Sql, util.SelectPlaceholder, fmt.Sprintf(",%s", v), 1)
}
}
func (s *SqlBuilder[T]) buildCondition(conds ...string) {
for _, v := range conds {
s.Sql = strings.Replace(s.Sql, util.ConditionPlaceholder, fmt.Sprintf(" and %s", v), 1)
}
}
func (s *SqlBuilder[T]) buildGroup(groups ...string) {
for _, v := range groups {
s.Sql = strings.Replace(s.Sql, util.GroupPlaceholder, fmt.Sprintf(",%s", v), 1)
}
}
func (s *SqlBuilder[T]) buildOrder(orders ...string) {
for _, v := range orders {
s.Sql = strings.Replace(s.Sql, util.OrderPlaceholder, fmt.Sprintf(",%s", v), 1)
}
}
// tdengine专用
func (s *SqlBuilder[T]) buildPartition(partitions ...string) {
for _, v := range partitions {
s.Sql = strings.Replace(s.Sql, util.PartitionPlaceholder, fmt.Sprintf(",%s", v), 1)
}
}
func (s *SqlBuilder[T]) DeletePlaceholderSymbol() {
s.Sql = strings.Replace(s.Sql, util.SelectPlaceholder, "", -1)
s.Sql = strings.Replace(s.Sql, util.ConditionPlaceholder, "", -1)
s.Sql = strings.Replace(s.Sql, util.GroupPlaceholder, "", -1)
s.Sql = strings.Replace(s.Sql, util.OrderPlaceholder, "", -1)
s.Sql = strings.Replace(s.Sql, util.PartitionPlaceholder, "", -1)
}