diff --git a/source_builder.go b/source_builder.go index d18da67..ce4d4e2 100644 --- a/source_builder.go +++ b/source_builder.go @@ -80,7 +80,6 @@ func (sb *SourceBuilder) Join(join JOIN) *SourceBuilder { } x := SourceX{} - *sb.xs = append(*sb.xs, &x) sb.x = &x @@ -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 } diff --git a/source_builder_optimization.go b/source_builder_optimization.go index 5f96576..6ccf035 100644 --- a/source_builder_optimization.go +++ b/source_builder_optimization.go @@ -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 } diff --git a/to_source_sql_by_builder.go b/to_source_sql_by_builder.go index bcbf9f2..6c2f1c5 100644 --- a/to_source_sql_by_builder.go +++ b/to_source_sql_by_builder.go @@ -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) } } }