Skip to content

Commit

Permalink
Add refresh builder api (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
sim-wangyan committed Feb 15, 2024
1 parent 9acd07d commit 75d65bd
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 37 deletions.
86 changes: 86 additions & 0 deletions builder_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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

import "time"

type UpdateBuilder struct {
bbs []Bb
}

func (ub *UpdateBuilder) Set(k string, v interface{}) *UpdateBuilder {

switch v.(type) {
case string:
if v.(string) == "" {
return ub
}
case uint64, uint, int64, int, int32, int16, int8, bool, byte, float64, float32:
if v == 0 {
return ub
}
case *uint64, *uint, *int64, *int, *int32, *int16, *int8, *bool, *byte, *float64, *float32:
isNil, n := NilOrNumber(v)
if isNil {
return ub
}
v = n
case time.Time:
ts := v.(time.Time).Format("2006-01-02 15:04:05")
v = ts
case []interface{}:
panic("Builder.doGLE(ke, []arr), [] ?")
default:
if v == nil {
return ub
}
}

ub.bbs = append(ub.bbs, Bb{
op: "SET",
key: k,
value: v,
})
return ub
}

func (ub *UpdateBuilder) X(s string) *UpdateBuilder {
ub.bbs = append(ub.bbs, Bb{
op: "SET",
key: s,
})
return ub
}

func (ub *UpdateBuilder) Any(f func(*UpdateBuilder)) *UpdateBuilder {
f(ub)
return ub
}

func (ub *UpdateBuilder) Bool(preCond BoolFunc, f func(cb *UpdateBuilder)) *UpdateBuilder {
if preCond == nil {
panic("UpdateBuilder.Bool para of BoolFunc can not nil")
}
if !preCond() {
return ub
}
if f == nil {
panic("UpdateBuilder.Bool para of func(k string, vs... interface{}) can not nil")
}
f(ub)
return ub
}
37 changes: 23 additions & 14 deletions builder_x.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type BuilderX struct {
sorts []Sort
resultKeys []string
orFromSql string
updates *[]Bb
sxs []*FromX
svs []interface{}
havings []Bb
Expand Down Expand Up @@ -59,7 +60,7 @@ func X() *BuilderX {
return x
}

func (x *BuilderX) FromX(fromX func(fb *FromBuilder)) *BuilderX {
func (x *BuilderX) FromX(f func(fb *FromBuilder)) *BuilderX {

if len(x.sxs) == 0 {
sb := FromX{
Expand All @@ -77,7 +78,7 @@ func (x *BuilderX) FromX(fromX func(fb *FromBuilder)) *BuilderX {
xs: &x.sxs,
x: x.sxs[0],
}
fromX(&b)
f(&b)
return x
}

Expand All @@ -100,9 +101,9 @@ func (x *BuilderX) Select(resultKeys ...string) *BuilderX {
return x
}

func (x *BuilderX) Having(cond func(cb *CondBuilderX)) *BuilderX {
func (x *BuilderX) Having(f func(cb *CondBuilderX)) *BuilderX {
var cb = new(CondBuilderX)
cond(cb)
f(cb)
x.havings = cb.bbs
return x
}
Expand All @@ -128,23 +129,23 @@ func (x *BuilderX) Agg(fn string, vs ...interface{}) *BuilderX {
return x
}

func (x *BuilderX) Sub(s string, sub func(sb *BuilderX)) *BuilderX {
x.CondBuilderX.Sub(s, sub)
func (x *BuilderX) Sub(s string, f func(sb *BuilderX)) *BuilderX {
x.CondBuilderX.Sub(s, f)
return x
}

func (x *BuilderX) Any(any func(x *BuilderX)) *BuilderX {
any(x)
func (x *BuilderX) Any(f func(x *BuilderX)) *BuilderX {
f(x)
return x
}

func (x *BuilderX) And(sub func(cb *CondBuilder)) *BuilderX {
x.CondBuilder.And(sub)
func (x *BuilderX) And(f func(cb *CondBuilder)) *BuilderX {
x.CondBuilder.And(f)
return x
}

func (x *BuilderX) Or(sub func(cb *CondBuilder)) *BuilderX {
x.CondBuilder.Or(sub)
func (x *BuilderX) Or(f func(cb *CondBuilder)) *BuilderX {
x.CondBuilder.Or(f)
return x
}

Expand Down Expand Up @@ -240,10 +241,10 @@ func (x *BuilderX) Sort(orderBy string, direction Direction) *BuilderX {
return x
}

func (x *BuilderX) Paged(page func(pb *PageBuilder)) *BuilderX {
func (x *BuilderX) Paged(f func(pb *PageBuilder)) *BuilderX {
pageBuilder := new(PageBuilder)
x.pageBuilder = pageBuilder
page(pageBuilder)
f(pageBuilder)
return x
}

Expand All @@ -252,13 +253,21 @@ func (x *BuilderX) Last(last string) *BuilderX {
return x
}

func (x *BuilderX) Update(f func(ub *UpdateBuilder)) *BuilderX {
updateBuilder := new(UpdateBuilder)
x.updates = &updateBuilder.bbs
f(updateBuilder)
return x
}

func (x *BuilderX) Build() *Built {
if x == nil {
panic("sqlxb.Builder is nil")
}
x.optimizeFromBuilder()
built := Built{
ResultKeys: x.resultKeys,
Updates: x.updates,
Conds: x.bbs,
Sorts: x.sorts,
Aggs: x.aggs,
Expand Down
18 changes: 9 additions & 9 deletions cond_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ func (cb *CondBuilder) null(op string, k string) *CondBuilder {
return cb
}

func (cb *CondBuilder) orAndSub(orAnd string, sub func(cb *CondBuilder)) *CondBuilder {
func (cb *CondBuilder) orAndSub(orAnd string, f func(cb *CondBuilder)) *CondBuilder {
c := subCondBuilder()
sub(c)
f(c)
if c.bbs == nil || len(c.bbs) == 0 {
return cb
}
Expand Down Expand Up @@ -174,29 +174,29 @@ func (cb *CondBuilder) orAnd(orAnd string) *CondBuilder {
return cb
}

func (cb *CondBuilder) And(sub func(cb *CondBuilder)) *CondBuilder {
return cb.orAndSub(AND_SUB, sub)
func (cb *CondBuilder) And(f func(cb *CondBuilder)) *CondBuilder {
return cb.orAndSub(AND_SUB, f)
}

func (cb *CondBuilder) Or(sub func(cb *CondBuilder)) *CondBuilder {
return cb.orAndSub(OR_SUB, sub)
func (cb *CondBuilder) Or(f func(cb *CondBuilder)) *CondBuilder {
return cb.orAndSub(OR_SUB, f)
}

func (cb *CondBuilder) OR() *CondBuilder {
return cb.orAnd(OR)
}

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

Expand Down
4 changes: 2 additions & 2 deletions cond_builder_x.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ type CondBuilderX struct {
CondBuilder
}

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

b := new(BuilderX)
sub(b)
f(b)
bb := Bb{
op: SUB,
key: s,
Expand Down
2 changes: 2 additions & 0 deletions internal/sql_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const (
GROUP_BY = " GROUP BY "
HAVING = " HAVING "
SELECT = "SELECT "
UPDATE = "UPDATE "
SET = " SET "
WHERE = " WHERE "
FROM = "FROM "
ON_SCRIPT = " ON "
Expand Down
2 changes: 0 additions & 2 deletions to_from_sql_by_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
package sqlxb

import (
"fmt"
. "github.com/x-ream/sqlxb/internal"
"strings"
)

func (built *Built) toFromSqlBySql(bp *strings.Builder) bool {
fmt.Println(built.Fxs)
if (len(built.Fxs) == 0) && (built.OrFromSql != "") {
var sql = strings.Trim(built.OrFromSql, SPACE)
if strings.HasPrefix(sql, "FROM") {
Expand Down
4 changes: 4 additions & 0 deletions to_result_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func buildResultKey(key string, km map[string]string) string {
}

func (built *Built) toResultKeySql(bp *strings.Builder, km map[string]string) {
if built.Updates != nil {
bp.WriteString(UPDATE)
return
}
bp.WriteString(SELECT)
if built.ResultKeys == nil {
bp.WriteString(STAR)
Expand Down
22 changes: 12 additions & 10 deletions to_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

type Built struct {
ResultKeys []string
Updates *[]Bb
Conds []Bb
Sorts []Sort
Havings []Bb
Expand All @@ -37,15 +38,6 @@ type Built struct {
PageCondition *PageCondition
}

type Qr struct {
Rs []Bb
Cs []Bb

Svs []interface{}

Po Po
}

func (built *Built) toFromSqlOfCount(bpCount *strings.Builder) {
built.toFromSql(nil, bpCount)
}
Expand Down Expand Up @@ -270,6 +262,13 @@ func (built *Built) countBuilder() *strings.Builder {
return sbCount
}

func (built *Built) SqlOfUpdate() (string, []interface{}) {
vs := []interface{}{}
km := make(map[string]string) //nil for sub From builder,
dataSql, _ := built.sqlData(&vs, km)
return dataSql, vs
}

func (built *Built) SqlOfCond() (string, string, []interface{}) {
vs := []interface{}{}

Expand Down Expand Up @@ -305,7 +304,9 @@ func (built *Built) countSqlWhere(sbCount *strings.Builder) {
}

func (built *Built) sqlFrom(bp *strings.Builder) {
bp.WriteString(FROM)
if built.Updates == nil {
bp.WriteString(FROM)
}
}

func (built *Built) sqlWhere(bp *strings.Builder) {
Expand All @@ -329,6 +330,7 @@ func (built *Built) sqlData(vs *[]interface{}, km map[string]string) (string, ma
built.toResultKeySql(&sb, km)
built.sqlFrom(&sb)
built.toFromSql(vs, &sb)
built.toUpdateSql(&sb, vs)
built.sqlWhere(&sb)
built.toCondSql(built.Conds, &sb, vs, built.filterLast)
built.toAggSql(vs, &sb)
Expand Down
49 changes: 49 additions & 0 deletions to_update_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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

import (
. "github.com/x-ream/sqlxb/internal"
"strings"
)

func (built *Built) toUpdateSql(bp *strings.Builder, vs *[]interface{}) {
if built.Updates == nil {
return
}

bp.WriteString(SET)
length := len(*built.Updates)

for i := 0; i < length; i++ {
u := (*built.Updates)[i]
bp.WriteString(u.key)
if !strings.Contains(u.key, EQ) {
bp.WriteString(SPACE)
bp.WriteString(EQ)
}
if u.value != nil {
bp.WriteString(PLACE_HOLDER)
*vs = append(*vs, u.value)
}
if i < length-1 {
bp.WriteString(COMMA)
} else {
bp.WriteString(SPACE)
}
}
}

0 comments on commit 75d65bd

Please sign in to comment.