From 80b80e01eb5ee865c0cd06c2a54098396fc6f24a Mon Sep 17 00:00:00 2001 From: lj-wsdj <1134294381@qq.com> Date: Thu, 9 May 2024 09:51:58 +0800 Subject: [PATCH] fix --- db/build.go | 33 +++++++++++++---------------- main_test.go | 56 ++++++++++++++++++++++++++++++++++++++++---------- sql_builder.go | 2 +- 3 files changed, 60 insertions(+), 31 deletions(-) diff --git a/db/build.go b/db/build.go index ff21472..f59d9c5 100644 --- a/db/build.go +++ b/db/build.go @@ -29,33 +29,30 @@ func (s *SqlBuilder[T]) buildCountSql(sqls string) string { func (s *SqlBuilder[T]) buildSelect() { for i, v := range s.Opts.selects { - n := i + 1 // 如果是最后一个select字段,则不加逗号 - if i == len(s.Opts.selects) { - s.Sql = strings.Replace(s.Sql, util.SelectPlaceholder, fmt.Sprintf("%s", v), n) + if i == len(s.Opts.selects)-1 { + 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), n) + s.Sql = strings.Replace(s.Sql, util.SelectPlaceholder, fmt.Sprintf("%s,", v), 1) } } s.Sql = strings.Replace(s.Sql, util.SelectPlaceholder, "", -1) } func (s *SqlBuilder[T]) buildCondition() { - for i, v := range s.Opts.conditions { - n := i + 1 - s.Sql = strings.Replace(s.Sql, util.ConditionPlaceholder, fmt.Sprintf(" and %s", v), n) + for _, v := range s.Opts.conditions { + s.Sql = strings.Replace(s.Sql, util.ConditionPlaceholder, fmt.Sprintf(" and %s", v), 1) } s.Sql = strings.Replace(s.Sql, util.ConditionPlaceholder, "", -1) } func (s *SqlBuilder[T]) buildGroup() { for i, v := range s.Opts.groups { - n := i + 1 // 如果是最后一个group字段,则不加逗号 - if n == len(s.Opts.groups) { - s.Sql = strings.Replace(s.Sql, util.GroupPlaceholder, fmt.Sprintf(" %s", v), n) + if i == len(s.Opts.groups)-1 { + 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), n) + s.Sql = strings.Replace(s.Sql, util.GroupPlaceholder, fmt.Sprintf(" %s,", v), 1) } } s.Sql = strings.Replace(s.Sql, util.GroupPlaceholder, "", -1) @@ -63,12 +60,11 @@ func (s *SqlBuilder[T]) buildGroup() { func (s *SqlBuilder[T]) buildOrder() { for i, v := range s.Opts.orders { - n := i + 1 // 如果是最后一个order字段,则不加逗号 - if n == len(s.Opts.orders) { - s.Sql = strings.Replace(s.Sql, util.OrderPlaceholder, fmt.Sprintf(" %s", v), n) + if i == len(s.Opts.orders)-1 { + 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), n) + s.Sql = strings.Replace(s.Sql, util.OrderPlaceholder, fmt.Sprintf(" %s,", v), 1) } } s.Sql = strings.Replace(s.Sql, util.OrderPlaceholder, "", -1) @@ -77,12 +73,11 @@ func (s *SqlBuilder[T]) buildOrder() { // tdengine专用 func (s *SqlBuilder[T]) buildPartition() { for i, v := range s.Opts.partitions { - n := i + 1 // 如果是最后一个partition字段,则不加逗号 - if n == len(s.Opts.partitions) { - s.Sql = strings.Replace(s.Sql, util.PartitionPlaceholder, fmt.Sprintf(" %s", v), n) + if i == len(s.Opts.partitions)-1 { + 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), n) + s.Sql = strings.Replace(s.Sql, util.PartitionPlaceholder, fmt.Sprintf(" %s,", v), 1) } } s.Sql = strings.Replace(s.Sql, util.PartitionPlaceholder, "", -1) diff --git a/main_test.go b/main_test.go index 0b309aa..4b8b36e 100644 --- a/main_test.go +++ b/main_test.go @@ -1,8 +1,9 @@ -package sqlbuilder +package sql_builder import ( "fmt" "testing" + "time" _ "git.botann.com/lijun/sql-builder/conf" ) @@ -48,17 +49,50 @@ func (p *Page) SetPageSize(pageSize int) { } func TestMain(m *testing.M) { - var data []struct { - Autoseq int - Name string + type InitialProcedureRequest struct { + Page + ProjectNo string `json:"project_no"` + ProjectName string `json:"project_name"` + ProjectUnit *int `json:"project_unit"` + InvestType *int `json:"invest_type"` } + type InitialProcedureResponse struct { + InitiationTime time.Time `json:"initiation_time"` + ProjectNo string `json:"project_no"` + ProjectName string `json:"project_name"` + ProjectUnit string `json:"project_unit"` + ProjectType string `json:"project_type"` + Allnum int `json:"all_num"` + NotHandleNum int `json:"not_handle_num"` + NoHandleNum int `json:"no_handle_num"` + HandlingNum int `json:"handling_num"` + HandledNum int `json:"handled_num"` + } + var data []InitialProcedureResponse + project_unit := 1 + sqlb := DefaultSqlBuilder() + slqs := `select max(t1.establishment_time),max(t1.number),max(t1.name),max(t2.name) as project_unit,max(t3.name) as project_type, + count(distinct t6.autoseq) all_num, + count(distinct case t6.state when 1 then t6.autoseq end) not_handle_num, + count(distinct case t6.state when 2 then t6.autoseq end) no_handle_num, + count(distinct case t6.state when 3 then t6.autoseq end) handling_num, + count(distinct case t6.state when 4 then t6.autoseq end) handled_num + from tproject_info t1 + left join tcompany t2 on t1.investor_id = t2.autoseq and t2.del = 0 + left join tinvestment_type t3 on t1.industry_category_id = t3.autoseq and t3.del = 0 + left join tinvestment_type_bind_procedure t4 on t3.autoseq = t4.investment_type_id and t4.del = 0 + left join tprocedure t5 on t4.procedure_id = t5.autoseq and t5.del = 0 @c + left join tpreliminary_procedures t6 on t5.autoseq = t6.procedures_id + where t1.del = 0 @c @c @c @c + group by t1.autoseq` + sqlb.Sql = slqs + sqlb.Conditions("t5.type = ?", 1) + if project_unit != 1 { + sqlb.Conditions("t2.autoseq = ?", project_unit) + } + var err error var page Page - sql_builder := DefaultSqlBuilder() - ids := []int{1, 2, 3, 4, 5} - sql_builder.Sql = `select autoseq,name from tsysuser where del = 0 @c order by @o` - sql_builder.Conditions("autoseq in (?)", ids) - sql_builder.Orders("autoseq desc") page.Data = &data - err := sql_builder.PaginateBySql(page.Data, &page) - fmt.Println(err, page) + err = sqlb.PaginateBySql(page.Data, &page) + fmt.Println(err, page.Data) } diff --git a/sql_builder.go b/sql_builder.go index 6a43952..b22329e 100644 --- a/sql_builder.go +++ b/sql_builder.go @@ -1,4 +1,4 @@ -package sqlbuilder +package sql_builder import ( "git.botann.com/lijun/sql-builder/db"