Skip to content

Commit

Permalink
Add Delete builder (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
sim-wangyan committed Jul 10, 2024
1 parent 4539e9e commit fe05fd5
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 34 deletions.
4 changes: 2 additions & 2 deletions builder_x.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ type BuilderX struct {
CondBuilderX
pageBuilder *PageBuilder

inserts *[]Bb
updates *[]Bb
sorts []Sort
resultKeys []string
orFromSql string
inserts *[]Bb
updates *[]Bb
sxs []*FromX
svs []interface{}
havings []Bb
Expand Down
1 change: 1 addition & 0 deletions internal/sql_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
SELECT = "SELECT "
INSERT = "INSERT INTO" + " "
VALUES = " VALUES "
DELETE = "DELETE "
UPDATE = "UPDATE "
SET = " SET "
WHERE = " WHERE "
Expand Down
24 changes: 24 additions & 0 deletions sqlxb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,27 @@ func TestUpdate(t *testing.T) {
})

}

func TestDelete(t *testing.T) {

t.Run("delete", func(t *testing.T) {

var po Pet
sql, vs := Of(&po).Eq("id", 2).
Any(func(x *BuilderX) {
if po.Id != 4 {
x.Gt("id", 1)
}
}).
Build().
SqlOfDelete()

if !strings.Contains(sql, "t_pet") {
t.Error("sql erro")
}

fmt.Println(vs)
fmt.Println(sql)
})

}
53 changes: 53 additions & 0 deletions to_insert_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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) sqlInsert(vs *[]interface{}) string {

bp := strings.Builder{}
bp.WriteString(INSERT)
bp.WriteString(built.OrFromSql)
bp.WriteString(SPACE)
bp.WriteString(BEGIN_SUB)
length := len(*built.Inserts)
for i := 0; i < length; i++ {
v := (*built.Inserts)[i]
bp.WriteString(v.key)
if i < length-1 {
bp.WriteString(COMMA)
}
*vs = append(*vs, v.value)
}

bp.WriteString(END_SUB)
bp.WriteString(VALUES)
bp.WriteString(BEGIN_SUB)
for i := 0; i < length; i++ {
bp.WriteString(PLACE_HOLDER)
if i < length-1 {
bp.WriteString(COMMA)
}
}
bp.WriteString(END_SUB)

return bp.String()
}
62 changes: 30 additions & 32 deletions to_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import (
)

type Built struct {
ResultKeys []string
Delete bool
Inserts *[]Bb
Updates *[]Bb
ResultKeys []string
Conds []Bb
Sorts []Sort
Havings []Bb
Expand Down Expand Up @@ -292,6 +293,12 @@ func (built *Built) SqlOfUpdate() (string, []interface{}) {
return dataSql, vs
}

func (built *Built) SqlOfDelete() (string, []interface{}) {
vs := []interface{}{}
sql := built.sqlDelete(&vs)
return sql, vs
}

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

Expand Down Expand Up @@ -339,6 +346,28 @@ func (built *Built) sqlWhere(bp *strings.Builder) {
bp.WriteString(WHERE)
}

func (built *Built) toDelete(bp *strings.Builder) {
bp.WriteString(DELETE)
}

func (built *Built) sqlDelete(vs *[]interface{}) string {
sb := strings.Builder{}
built.toDelete(&sb)
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)
built.toGroupBySql(&sb)
built.toHavingSql(vs, &sb)
built.toSortSql(&sb)
built.toPageSql(&sb)
built.toLastSql(&sb)
deleteSql := sb.String()
return deleteSql
}

func (built *Built) sqlData(vs *[]interface{}, km map[string]string) (string, map[string]string) {
sb := strings.Builder{}
built.toResultKeySql(&sb, km)
Expand Down Expand Up @@ -373,34 +402,3 @@ func (built *Built) sqlCount() string {
countSql := built.toSqlCount(sbCount)
return countSql
}

func (built *Built) sqlInsert(vs *[]interface{}) string {

bp := strings.Builder{}
bp.WriteString(INSERT)
bp.WriteString(built.OrFromSql)
bp.WriteString(SPACE)
bp.WriteString(BEGIN_SUB)
length := len(*built.Inserts)
for i := 0; i < length; i++ {
v := (*built.Inserts)[i]
bp.WriteString(v.key)
if i < length-1 {
bp.WriteString(COMMA)
}
*vs = append(*vs, v.value)
}

bp.WriteString(END_SUB)
bp.WriteString(VALUES)
bp.WriteString(BEGIN_SUB)
for i := 0; i < length; i++ {
bp.WriteString(PLACE_HOLDER)
if i < length-1 {
bp.WriteString(COMMA)
}
}
bp.WriteString(END_SUB)

return bp.String()
}

0 comments on commit fe05fd5

Please sign in to comment.