Skip to content

Commit

Permalink
Change SourceBuilder.Sub(sub *BuilderX) to Sub(sub func(sub *BuilderX…
Browse files Browse the repository at this point in the history
…)) (#54)
  • Loading branch information
sim-wangyan committed Nov 3, 2023
1 parent 612190e commit 9bf8e15
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
7 changes: 4 additions & 3 deletions source_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func (sb *SourceBuilder) Join(join JOIN) *SourceBuilder {
}

x := SourceX{}

*sb.xs = append(*sb.xs, &x)
sb.x = &x

Expand All @@ -90,7 +89,9 @@ func (sb *SourceBuilder) Join(join JOIN) *SourceBuilder {
return sb
}

func (sb *SourceBuilder) Sub(sub *BuilderX) *SourceBuilder {
sb.x.sub = sub
func (sb *SourceBuilder) Sub(sub func(sub *BuilderX)) *SourceBuilder {
x := new(BuilderX)
sb.x.sub = x
sub(x)
return sb
}
4 changes: 3 additions & 1 deletion source_builder_optimization.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func (x *BuilderX) optimizeSourceBuilder() {
}

x.removeSourceBuilder(x.sxs, func(useds *[]*SourceX, ele *SourceX, i int) bool {

if i == 0 {
return false
}
if ele.sub != nil && (ele.join != nil && !strings.Contains(ele.join.join, "LEFT")) {
return false
}
Expand Down
30 changes: 15 additions & 15 deletions to_source_sql_by_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,36 @@ func (built *Built) toSourceSqlBySql(bp *strings.Builder) bool {
return false
}

func (built *Built) toSourceSqlByBuilder(vs *[]interface{}, sb *SourceX, bp *strings.Builder) {
if sb.join != nil { //JOIN
func (built *Built) toSourceSqlByBuilder(vs *[]interface{}, sx *SourceX, bp *strings.Builder) {
if sx.join != nil { //JOIN
bp.WriteString(SPACE)
bp.WriteString(sb.join.join)
bp.WriteString(sx.join.join)
bp.WriteString(SPACE)
}
if sb.po != nil {
bp.WriteString(sb.po.TableName())
} else if sb.sub != nil {
dataSql, _ := sb.sub.Build().sqlData(vs, nil)
if sx.po != nil {
bp.WriteString(sx.po.TableName())
} else if sx.sub != nil {
dataSql, _ := sx.sub.Build().sqlData(vs, nil)
bp.WriteString(BEGIN_SUB)
bp.WriteString(dataSql)
bp.WriteString(END_SUB)
}
if sb.alia != "" {
if sx.alia != "" {
bp.WriteString(SPACE)
bp.WriteString(sb.alia)
bp.WriteString(sx.alia)
}
if sb.join != nil && sb.join.on != nil { //ON
if sx.join != nil && sx.join.on != nil { //ON

if sb.join.on.orUsingKey != "" {
if sx.join.on.orUsingKey != "" {
bp.WriteString(USING_SCRIPT_LEFT)
bp.WriteString(sb.join.on.orUsingKey)
bp.WriteString(sx.join.on.orUsingKey)
bp.WriteString(END_SUB)
} else if sb.s != "" {
} else if sx.s != "" {
bp.WriteString(SPACE)
bp.WriteString(sb.s)
bp.WriteString(sx.s)
} else {
bp.WriteString(ON_SCRIPT)
built.toConditionSql(sb.join.on.bbs, bp, vs, nil)
built.toConditionSql(sx.join.on.bbs, bp, vs, nil)
}
}
}

0 comments on commit 9bf8e15

Please sign in to comment.