lj-wsdj 2024-06-03 09:53:03 +08:00
parent d06f44e671
commit 6439b9e69c
2 changed files with 27 additions and 6 deletions

View File

@ -25,34 +25,54 @@ func (s *SqlBuilder[T]) buildCountSql(sqls string) string {
func (s *SqlBuilder[T]) buildSelect(selects ...string) { func (s *SqlBuilder[T]) buildSelect(selects ...string) {
for _, v := range selects { for _, v := range selects {
if strings.HasPrefix(v, "select") {
s.Sql = strings.Replace(s.Sql, util.SelectPlaceholder, fmt.Sprintf("%s", v), 1)
} else {
s.Sql = strings.Replace(s.Sql, util.SelectPlaceholder, fmt.Sprintf(",%s", v), 1) s.Sql = strings.Replace(s.Sql, util.SelectPlaceholder, fmt.Sprintf(",%s", v), 1)
} }
} }
}
func (s *SqlBuilder[T]) buildCondition(conds ...string) { func (s *SqlBuilder[T]) buildCondition(conds ...string) {
for _, v := range conds { for _, v := range conds {
if strings.HasPrefix(v, "where") {
s.Sql = strings.Replace(s.Sql, util.ConditionPlaceholder, fmt.Sprintf(" %s", v), 1)
} else {
s.Sql = strings.Replace(s.Sql, util.ConditionPlaceholder, fmt.Sprintf(" and %s", v), 1) s.Sql = strings.Replace(s.Sql, util.ConditionPlaceholder, fmt.Sprintf(" and %s", v), 1)
} }
} }
}
func (s *SqlBuilder[T]) buildGroup(groups ...string) { func (s *SqlBuilder[T]) buildGroup(groups ...string) {
for _, v := range groups { for _, v := range groups {
if strings.HasPrefix(v, "group by") {
s.Sql = strings.Replace(s.Sql, util.GroupPlaceholder, fmt.Sprintf("%s", v), 1)
} else {
s.Sql = strings.Replace(s.Sql, util.GroupPlaceholder, fmt.Sprintf(",%s", v), 1) s.Sql = strings.Replace(s.Sql, util.GroupPlaceholder, fmt.Sprintf(",%s", v), 1)
} }
} }
}
func (s *SqlBuilder[T]) buildOrder(orders ...string) { func (s *SqlBuilder[T]) buildOrder(orders ...string) {
for _, v := range orders { for _, v := range orders {
if strings.HasPrefix(v, "order by") {
s.Sql = strings.Replace(s.Sql, util.OrderPlaceholder, fmt.Sprintf("%s", v), 1)
} else {
s.Sql = strings.Replace(s.Sql, util.OrderPlaceholder, fmt.Sprintf(",%s", v), 1) s.Sql = strings.Replace(s.Sql, util.OrderPlaceholder, fmt.Sprintf(",%s", v), 1)
} }
} }
}
// tdengine专用 // tdengine专用
func (s *SqlBuilder[T]) buildPartition(partitions ...string) { func (s *SqlBuilder[T]) buildPartition(partitions ...string) {
for _, v := range partitions { for _, v := range partitions {
if strings.HasPrefix(v, "partition by") {
s.Sql = strings.Replace(s.Sql, util.PartitionPlaceholder, fmt.Sprintf("%s", v), 1)
} else {
s.Sql = strings.Replace(s.Sql, util.PartitionPlaceholder, fmt.Sprintf(",%s", v), 1) s.Sql = strings.Replace(s.Sql, util.PartitionPlaceholder, fmt.Sprintf(",%s", v), 1)
} }
} }
}
func (s *SqlBuilder[T]) DeletePlaceholderSymbol() { func (s *SqlBuilder[T]) DeletePlaceholderSymbol() {
s.Sql = strings.Replace(s.Sql, util.SelectPlaceholder, "", -1) s.Sql = strings.Replace(s.Sql, util.SelectPlaceholder, "", -1)

View File

@ -76,11 +76,12 @@ func TestMain(m *testing.M) {
left join tinvestment_type t3 on t1.industry_category_id = t3.autoseq and t3.del = 0 left join tinvestment_type t3 on t1.industry_category_id = t3.autoseq and t3.del = 0
left join tpreliminary_procedures t6 on t1.autoseq = t6.project_id left join tpreliminary_procedures t6 on t1.autoseq = t6.project_id
where t1.del = 0 @c @c @c @c @c where t1.del = 0 @c @c @c @c @c
group by t1.autoseq` @g`
sqlb.Sql = slqs sqlb.Sql = slqs
if company_id != 0 { if company_id != 0 {
sqlb.Conditions("t2.autoseq = ?", company_id) sqlb.Conditions("t2.autoseq = ?", company_id)
} }
sqlb.Groups("group by t1.autoseq")
fmt.Println(sqlb) fmt.Println(sqlb)
var err error var err error
page := Page{PageIndex: 2, PageSize: 10} page := Page{PageIndex: 2, PageSize: 10}