From d06f44e6710eaa74900b8cb057fadf0ced772393 Mon Sep 17 00:00:00 2001 From: lj-wsdj <1134294381@qq.com> Date: Tue, 28 May 2024 14:18:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Clear=E6=96=B9=E6=B3=95?= =?UTF-8?q?=EF=BC=9B=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= =?UTF-8?q?=EF=BC=8C=E5=8F=AF=E4=BB=A5=E6=B7=BB=E5=8A=A0=E4=BB=BB=E6=84=8F?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/add_sql.go | 5 ++++ db/build.go | 65 +++++++++++++++++---------------------------------- db/db.go | 5 ++++ main_test.go | 54 ++++++++++++++++++------------------------ 4 files changed, 55 insertions(+), 74 deletions(-) diff --git a/db/add_sql.go b/db/add_sql.go index 2e4cc9c..10b994b 100644 --- a/db/add_sql.go +++ b/db/add_sql.go @@ -18,22 +18,27 @@ func (s *SqlBuilder[T]) Conditions(cond string, args ...interface{}) { cond, args = dealInCondition(cond, args) s.Opts.conditions = append(s.Opts.conditions, cond) s.Opts.args = append(s.Opts.args, args...) + s.buildCondition(cond) } func (s *SqlBuilder[T]) Selects(selects ...string) { s.Opts.selects = append(s.Opts.selects, selects...) + s.buildSelect(selects...) } func (s *SqlBuilder[T]) Groups(groups ...string) { s.Opts.groups = append(s.Opts.groups, groups...) + s.buildGroup(groups...) } func (s *SqlBuilder[T]) Orders(orders ...string) { s.Opts.orders = append(s.Opts.orders, orders...) + s.buildOrder(orders...) } func (s *SqlBuilder[T]) Partitions(partitions ...string) { s.Opts.partitions = append(s.Opts.partitions, partitions...) + s.buildPartition(partitions...) } func dealInCondition(cond string, args []interface{}) (string, []interface{}) { diff --git a/db/build.go b/db/build.go index f59d9c5..ff0106f 100644 --- a/db/build.go +++ b/db/build.go @@ -8,11 +8,7 @@ import ( ) func (s *SqlBuilder[T]) buildSql() string { - s.buildSelect() - s.buildGroup() - s.buildOrder() - s.buildPartition() - s.buildCondition() + s.DeletePlaceholderSymbol() return s.Sql } @@ -27,58 +23,41 @@ func (s *SqlBuilder[T]) buildCountSql(sqls string) string { return fmt.Sprintf("select count(1) from (%s) as t", sqls) } -func (s *SqlBuilder[T]) buildSelect() { - for i, v := range s.Opts.selects { - // 如果是最后一个select字段,则不加逗号 - 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), 1) - } +func (s *SqlBuilder[T]) buildSelect(selects ...string) { + for _, v := range selects { + 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 _, v := range s.Opts.conditions { +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) } - s.Sql = strings.Replace(s.Sql, util.ConditionPlaceholder, "", -1) } -func (s *SqlBuilder[T]) buildGroup() { - for i, v := range s.Opts.groups { - // 如果是最后一个group字段,则不加逗号 - 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), 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) } - s.Sql = strings.Replace(s.Sql, util.GroupPlaceholder, "", -1) } -func (s *SqlBuilder[T]) buildOrder() { - for i, v := range s.Opts.orders { - // 如果是最后一个order字段,则不加逗号 - 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), 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) } - s.Sql = strings.Replace(s.Sql, util.OrderPlaceholder, "", -1) } // tdengine专用 -func (s *SqlBuilder[T]) buildPartition() { - for i, v := range s.Opts.partitions { - // 如果是最后一个partition字段,则不加逗号 - 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), 1) - } +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) } diff --git a/db/db.go b/db/db.go index c3e1bec..a619f8f 100644 --- a/db/db.go +++ b/db/db.go @@ -27,6 +27,11 @@ type Options struct { partitions []string // tdengine专用 } +func (s *SqlBuilder[T]) Clear() { + s.Sql = "" + s.Opts = Options{} +} + func (s *SqlBuilder[T]) FindBySql(data interface{}) error { sqls := s.buildSql() return s.Connector.QueryEx(sqls, data, s.Opts.args...) diff --git a/main_test.go b/main_test.go index 4b8b36e..3ca26f9 100644 --- a/main_test.go +++ b/main_test.go @@ -49,50 +49,42 @@ func (p *Page) SetPageSize(pageSize int) { } func TestMain(m *testing.M) { - 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 PreliminaryProcedureResponse struct { + ProjectId int `json:"project_id"` + InitiationTime time.Time `json:"initiation_time" export:"x:1,y:2"` + ProjectNo string `json:"project_no" export:"x:2,y:2"` + ProjectName string `json:"project_name" export:"x:3,y:2"` + ProjectUnit string `json:"project_unit" export:"x:4,y:2"` + ProjectType string `json:"project_type" export:"x:5,y:2"` + Allnum int `json:"all_num" export:"x:6,y:2"` + NotHandleNum int `json:"not_handle_num" export:"x:7,y:2"` + NoHandleNum int `json:"no_handle_num" export:"x:8,y:2"` + HandlingNum int `json:"handling_num" export:"x:9,y:2"` + HandledNum int `json:"handled_num" export:"x:10,y:2"` } - 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 + var data []PreliminaryProcedureResponse + company_id := 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, + slqs := `select t1.autoseq,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 + inner join tcompany t2 on t1.investor_id = t2.autoseq and t2.del = 0 @c 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 + left join tpreliminary_procedures t6 on t1.autoseq = t6.project_id + where t1.del = 0 @c @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) + if company_id != 0 { + sqlb.Conditions("t2.autoseq = ?", company_id) } + fmt.Println(sqlb) var err error - var page Page + page := Page{PageIndex: 2, PageSize: 10} + err = sqlb.PaginateBySql(&data, &page) page.Data = &data - err = sqlb.PaginateBySql(page.Data, &page) fmt.Println(err, page.Data) }