Skip to content

Commit

Permalink
Move Sub(s string, sub func(sb *BuilderX)) from BuilderX to CondBuild…
Browse files Browse the repository at this point in the history
…erX (#65)
  • Loading branch information
sim-wangyan committed Dec 14, 2023
1 parent b9a90b7 commit a4a9535
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func main() {
}

builder := Of(nil).
Select("p.id").
Select("p.id","p.weight").
FromX(func(fb *FromBuilder) {
fb.
Sub(sub).As("p").
Expand All @@ -162,7 +162,12 @@ func main() {
on.Gt("c.id", ro.MinCatId)
})
}).
Ne("p.type","PIG")
Ne("p.type","PIG").
Having(func(cb *CondBuilderX) {
cb.Sub("p.weight > ?", func(sb *BuilderX) {
sb.Select("AVG(weight)").From("t_dog")
})
})

}

Expand Down
16 changes: 4 additions & 12 deletions builder_x.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package sqlxb
//
// @author Sim
type BuilderX struct {
CondBuilder
CondBuilderX
pageBuilder *PageBuilder

sorts []Sort
Expand Down Expand Up @@ -93,8 +93,8 @@ func (x *BuilderX) Select(resultKeys ...string) *BuilderX {
return x
}

func (x *BuilderX) Having(cond func(cb *CondBuilder)) *BuilderX {
var cb = new(CondBuilder)
func (x *BuilderX) Having(cond func(cb *CondBuilderX)) *BuilderX {
var cb = new(CondBuilderX)
cond(cb)
x.havings = cb.bbs
return x
Expand Down Expand Up @@ -122,15 +122,7 @@ func (x *BuilderX) Agg(fn string, vs ...interface{}) *BuilderX {
}

func (x *BuilderX) Sub(s string, sub func(sb *BuilderX)) *BuilderX {

b := new(BuilderX)
sub(b)
bb := Bb{
op: SUB,
key: s,
value: b,
}
x.bbs = append(x.bbs, bb)
x.CondBuilderX.Sub(s, sub)
return x
}

Expand Down
34 changes: 34 additions & 0 deletions cond_builder_x.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2020 io.xream.sqlxb
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sqlxb

type CondBuilderX struct {
CondBuilder
}

func (x *CondBuilderX) Sub(s string, sub func(sb *BuilderX)) *CondBuilderX {

b := new(BuilderX)
sub(b)
bb := Bb{
op: SUB,
key: s,
value: b,
}
x.bbs = append(x.bbs, bb)
return x
}
1 change: 1 addition & 0 deletions to_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (built *Built) toBb(bb Bb, bp *strings.Builder, vs *[]interface{}) {
var bx = *bb.value.(*BuilderX)
ss, _ := bx.Build().sqlData(vs, nil)
ss = BEGIN_SUB + ss + END_SUB
ss = SPACE + ss
if bb.key != "" {
if strings.Contains(bb.key, PLACE_HOLDER) {
bp.WriteString(strings.ReplaceAll(bb.key, PLACE_HOLDER, ss))
Expand Down

0 comments on commit a4a9535

Please sign in to comment.