Skip to content

Commit

Permalink
Merge pull request #51 from x-ream/sourceSql
Browse files Browse the repository at this point in the history
Source sql
  • Loading branch information
sim-wangyan committed Oct 31, 2023
2 parents 9c70be6 + 796b5cf commit 1cb9757
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 120 deletions.
11 changes: 5 additions & 6 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@
// (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
// 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

//
// TO build sql, like: SELECT * FROM ....
// Can add L2Cache
//
// @author Sim
type Builder struct {
ConditionBuilder
CondBuilder
pageBuilder *PageBuilder

sorts []Sort
Expand Down Expand Up @@ -55,10 +53,11 @@ func (builder *Builder) Sort(orderBy string, direction Direction) *Builder {
return builder
}

func (builder *Builder) Paged() *PageBuilder {
func (builder *Builder) Paged(page func(pb *PageBuilder)) *Builder {
pageBuilder := new(PageBuilder)
builder.pageBuilder = pageBuilder
return pageBuilder
page(pageBuilder)
return builder
}

func (builder *Builder) Build() *Built {
Expand Down
42 changes: 30 additions & 12 deletions builder_x.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// (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
// 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,
Expand All @@ -20,14 +20,14 @@ package sqlxb
// Sql for MySQL, Clickhouse....
//
// @author Sim
//
func Sub() *BuilderX {
return NewBuilderX(nil, "")
func Sub(po Po) *BuilderX {
return NewBuilderX(po, "")
}

type BuilderX struct {
Builder
resultKeys []string
orSourceSql string
sbs []*SourceBuilder
svs []interface{}
havings []Bb
Expand Down Expand Up @@ -58,6 +58,23 @@ func (x *BuilderX) SourceBuilder() *SourceBuilder {
return &sb
}

func Source() *SourceBuilder {
var sb = SourceBuilder{}
return &sb
}

func (x *BuilderX) SourceX(source func(*SourceBuilder)) *BuilderX {
var b = Source()
x.sbs = append(x.sbs, b)
source(b)
return x
}

func (x *BuilderX) SourceScript(sqlScript string) *BuilderX {
x.orSourceSql = sqlScript
return x
}

func (x *BuilderX) ResultKey(resultKey string) *BuilderX {
if resultKey != "" {
x.resultKeys = append(x.resultKeys, resultKey)
Expand Down Expand Up @@ -124,14 +141,15 @@ func (builder *BuilderX) Build() *Built {
}
builder.optimizeSourceBuilder()
built := Built{
ResultKeys: builder.resultKeys,
ConditionX: builder.bbs,
Sorts: builder.sorts,
Aggs: builder.aggs,
Havings: builder.havings,
GroupBys: builder.groupBys,
Sbs: builder.sbs,
Svs: builder.svs,
ResultKeys: builder.resultKeys,
ConditionX: builder.bbs,
Sorts: builder.sorts,
Aggs: builder.aggs,
Havings: builder.havings,
GroupBys: builder.groupBys,
OrSourceSql: builder.orSourceSql,
Sbs: builder.sbs,
Svs: builder.svs,

Po: builder.po,
}
Expand Down
62 changes: 31 additions & 31 deletions condition_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// (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
// 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,
Expand All @@ -18,19 +18,19 @@ package sqlxb

import "time"

type ConditionBuilder struct {
type CondBuilder struct {
bbs []Bb
}

type BoolFunc func() bool

func SubCondition() *ConditionBuilder {
c := new(ConditionBuilder)
func SubCond() *CondBuilder {
c := new(CondBuilder)
c.bbs = []Bb{}
return c
}

func (builder *ConditionBuilder) X(k string, vs ...interface{}) *ConditionBuilder {
func (builder *CondBuilder) X(k string, vs ...interface{}) *CondBuilder {
bb := Bb{
op: X,
key: k,
Expand All @@ -40,7 +40,7 @@ func (builder *ConditionBuilder) X(k string, vs ...interface{}) *ConditionBuilde
return builder
}

func (builder *ConditionBuilder) doIn(p string, k string, vs ...interface{}) *ConditionBuilder {
func (builder *CondBuilder) doIn(p string, k string, vs ...interface{}) *CondBuilder {
if vs == nil || len(vs) == 0 {
return builder
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func (builder *ConditionBuilder) doIn(p string, k string, vs ...interface{}) *Co
return builder
}

func (builder *ConditionBuilder) doLike(p string, k string, v string) *ConditionBuilder {
func (builder *CondBuilder) doLike(p string, k string, v string) *CondBuilder {

bb := Bb{
op: p,
Expand All @@ -102,7 +102,7 @@ func (builder *ConditionBuilder) doLike(p string, k string, v string) *Condition
return builder
}

func (builder *ConditionBuilder) doGLE(p string, k string, v interface{}) *ConditionBuilder {
func (builder *CondBuilder) doGLE(p string, k string, v interface{}) *CondBuilder {

switch v.(type) {
case string:
Expand Down Expand Up @@ -132,7 +132,7 @@ func (builder *ConditionBuilder) doGLE(p string, k string, v interface{}) *Condi
return builder.addBb(p, k, v)
}

func (builder *ConditionBuilder) addBb(op string, key string, v interface{}) *ConditionBuilder {
func (builder *CondBuilder) addBb(op string, key string, v interface{}) *CondBuilder {
bb := Bb{
op: op,
key: key,
Expand All @@ -143,7 +143,7 @@ func (builder *ConditionBuilder) addBb(op string, key string, v interface{}) *Co
return builder
}

func (builder *ConditionBuilder) null(op string, k string) *ConditionBuilder {
func (builder *CondBuilder) null(op string, k string) *CondBuilder {
bb := Bb{
op: op,
key: k,
Expand All @@ -152,7 +152,7 @@ func (builder *ConditionBuilder) null(op string, k string) *ConditionBuilder {
return builder
}

func (builder *ConditionBuilder) orAndSub(orAnd string, sub *ConditionBuilder) *ConditionBuilder {
func (builder *CondBuilder) orAndSub(orAnd string, sub *CondBuilder) *CondBuilder {
if sub.bbs == nil || len(sub.bbs) == 0 {
return builder
}
Expand All @@ -166,7 +166,7 @@ func (builder *ConditionBuilder) orAndSub(orAnd string, sub *ConditionBuilder) *
return builder
}

func (builder *ConditionBuilder) orAnd(orAnd string) *ConditionBuilder {
func (builder *CondBuilder) orAnd(orAnd string) *CondBuilder {
length := len(builder.bbs)
if length == 0 {
return builder
Expand All @@ -182,77 +182,77 @@ func (builder *ConditionBuilder) orAnd(orAnd string) *ConditionBuilder {
return builder
}

func (builder *ConditionBuilder) And(subCondition *ConditionBuilder) *ConditionBuilder {
func (builder *CondBuilder) And(subCondition *CondBuilder) *CondBuilder {
return builder.orAndSub(AND_SUB, subCondition)
}

func (builder *ConditionBuilder) Or(sub *ConditionBuilder) *ConditionBuilder {
func (builder *CondBuilder) Or(sub *CondBuilder) *CondBuilder {
return builder.orAndSub(OR_SUB, sub)
}

func (builder *ConditionBuilder) OR() *ConditionBuilder {
func (builder *CondBuilder) OR() *CondBuilder {
return builder.orAnd(OR)
}

func (builder *ConditionBuilder) Bool(preCondition BoolFunc, then func(cb *ConditionBuilder)) *ConditionBuilder {
func (builder *CondBuilder) Bool(preCondition BoolFunc, then func(cb *CondBuilder)) *CondBuilder {
if preCondition == nil {
panic("ConditionBuilder.Bool para of BoolFunc can not nil")
panic("CondBuilder.Bool para of BoolFunc can not nil")
}
if !preCondition() {
return builder
}
if then == nil {
panic("ConditionBuilder.Bool para of func(k string, vs... interface{}) can not nil")
panic("CondBuilder.Bool para of func(k string, vs... interface{}) can not nil")
}
then(builder)
return builder
}

func (builder *ConditionBuilder) Eq(k string, v interface{}) *ConditionBuilder {
func (builder *CondBuilder) Eq(k string, v interface{}) *CondBuilder {
return builder.doGLE(EQ, k, v)
}
func (builder *ConditionBuilder) Ne(k string, v interface{}) *ConditionBuilder {
func (builder *CondBuilder) Ne(k string, v interface{}) *CondBuilder {
return builder.doGLE(NE, k, v)
}
func (builder *ConditionBuilder) Gt(k string, v interface{}) *ConditionBuilder {
func (builder *CondBuilder) Gt(k string, v interface{}) *CondBuilder {
return builder.doGLE(GT, k, v)
}
func (builder *ConditionBuilder) Lt(k string, v interface{}) *ConditionBuilder {
func (builder *CondBuilder) Lt(k string, v interface{}) *CondBuilder {
return builder.doGLE(LT, k, v)
}
func (builder *ConditionBuilder) Gte(k string, v interface{}) *ConditionBuilder {
func (builder *CondBuilder) Gte(k string, v interface{}) *CondBuilder {
return builder.doGLE(GTE, k, v)
}
func (builder *ConditionBuilder) Lte(k string, v interface{}) *ConditionBuilder {
func (builder *CondBuilder) Lte(k string, v interface{}) *CondBuilder {
return builder.doGLE(LTE, k, v)
}
func (builder *ConditionBuilder) Like(k string, v string) *ConditionBuilder {
func (builder *CondBuilder) Like(k string, v string) *CondBuilder {
if v == "" {
return builder
}
return builder.doLike(LIKE, k, "%"+v+"%")
}
func (builder *ConditionBuilder) NotLike(k string, v string) *ConditionBuilder {
func (builder *CondBuilder) NotLike(k string, v string) *CondBuilder {
if v == "" {
return builder
}
return builder.doLike(NOT_LIKE, k, "%"+v+"%")
}
func (builder *ConditionBuilder) LikeRight(k string, v string) *ConditionBuilder {
func (builder *CondBuilder) LikeRight(k string, v string) *CondBuilder {
if v == "" {
return builder
}
return builder.doLike(LIKE, k, v+"%")
}
func (builder *ConditionBuilder) In(k string, vs ...interface{}) *ConditionBuilder {
func (builder *CondBuilder) In(k string, vs ...interface{}) *CondBuilder {
return builder.doIn(IN, k, vs...)
}
func (builder *ConditionBuilder) Nin(k string, vs ...interface{}) *ConditionBuilder {
func (builder *CondBuilder) Nin(k string, vs ...interface{}) *CondBuilder {
return builder.doIn(NIN, k, vs...)
}
func (builder *ConditionBuilder) IsNull(key string) *ConditionBuilder {
func (builder *CondBuilder) IsNull(key string) *CondBuilder {
return builder.null(IS_NULL, key)
}
func (builder *ConditionBuilder) NonNull(key string) *ConditionBuilder {
func (builder *CondBuilder) NonNull(key string) *CondBuilder {
return builder.null(NON_NULL, key)
}
6 changes: 5 additions & 1 deletion filter_last.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// (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
// 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,
Expand All @@ -20,7 +20,11 @@ func (built *Built) filterLast() *Bb {
if built.PageCondition == nil {
return nil
}
if built.PageCondition.rows == 0 {
panic("page.rows must be greater than 0")
}
if built.PageCondition.last > 0 {

if built.Sorts == nil || len(built.Sorts) == 0 {
panic("last > 0, Numeric sorts[0] required")
}
Expand Down
6 changes: 1 addition & 5 deletions page_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// (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
// 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,
Expand All @@ -27,10 +27,6 @@ type PageBuilder struct {
condition PageCondition
}

func (pb *PageBuilder) Paged() *PageBuilder {
return new(PageBuilder)
}

func (pb *PageBuilder) Page(page uint) *PageBuilder {
pb.condition.page = page
return pb
Expand Down
Loading

0 comments on commit 1cb9757

Please sign in to comment.