Skip to content

Commit

Permalink
feat(sqlbuilderzero): fix session and cache
Browse files Browse the repository at this point in the history
feat(sqlbuilderzero): fix session and cache
  • Loading branch information
jaronnie committed Nov 16, 2024
1 parent ea25651 commit ca35b5b
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 62 deletions.
83 changes: 62 additions & 21 deletions .template/go-zero/model/customized.tpl
Original file line number Diff line number Diff line change
@@ -1,42 +1,61 @@
func (m *custom{{.upperStartCamelObject}}Model) BulkInsert(ctx context.Context, datas []*{{.upperStartCamelObject}}) error {
func (m *custom{{.upperStartCamelObject}}Model) BulkInsert(ctx context.Context, session sqlx.Session, datas []*{{.upperStartCamelObject}}) error {
sb := sqlbuilder.InsertInto(m.table)
sb.Cols({{.lowerStartCamelObject}}RowsExpectAutoSet)
for _, data := range datas {
sb.Values({{.expressionValues}})
}
sql, args := sb.Build()
_, err:= m.conn.ExecCtx(ctx, sql, args...)
statement, args := sb.Build()

var err error
if session != nil {
_, err = session.ExecCtx(ctx, statement, args...)
} else {
{{if .withCache}}_, err = m.ExecNoCacheCtx(ctx, statement, args...){{else}}_, err = m.conn.ExecCtx(ctx, statement, args...){{end}}
}
return err
}

func (m *custom{{.upperStartCamelObject}}Model) FindByCondition(ctx context.Context, conds ...condition.Condition) ([]*{{.upperStartCamelObject}}, error) {
sb := sqlbuilder.Select({{.lowerStartCamelObject}}FieldNames...).From(m.table)
func (m *custom{{.upperStartCamelObject}}Model) FindByCondition(ctx context.Context, session sqlx.Session, conds ...condition.Condition) ([]*{{.upperStartCamelObject}}, error) {
sb := sqlbuilder.Select({{.lowerStartCamelObject}}FieldNames...).From(m.table)
condition.ApplySelect(sb, conds...)
sql, args := sb.Build()
statement, args := sb.Build()

var resp []*{{.upperStartCamelObject}}
err := m.conn.QueryRowsCtx(ctx, &resp, sql, args...)
var err error

if session != nil {
err = session.QueryRowsCtx(ctx, &resp, statement, args...)
} else {
{{if .withCache}}err = m.QueryRowsNoCacheCtx(ctx, &resp, statement, args...){{else}}err = m.conn.QueryRowsCtx(ctx, &resp, statement, args...){{end}}
}
if err != nil {
return nil, err
}
return resp, nil
}

func (m *custom{{.upperStartCamelObject}}Model) FindOneByCondition(ctx context.Context, conds ...condition.Condition) (*{{.upperStartCamelObject}}, error) {
func (m *custom{{.upperStartCamelObject}}Model) FindOneByCondition(ctx context.Context, session sqlx.Session, conds ...condition.Condition) (*{{.upperStartCamelObject}}, error) {
sb := sqlbuilder.Select({{.lowerStartCamelObject}}FieldNames...).From(m.table)

condition.ApplySelect(sb, conds...)
sb.Limit(1)
sql, args := sb.Build()
statement, args := sb.Build()

var resp {{.upperStartCamelObject}}
err := m.conn.QueryRowCtx(ctx, &resp, sql, args...)
var err error

if session != nil {
err = session.QueryRowCtx(ctx, &resp, statement, args...)
} else {
{{if .withCache}}err = m.QueryRowNoCacheCtx(ctx, &resp, statement, args...){{else}}err = m.conn.QueryRowCtx(ctx, &resp, statement, args...){{end}}
}
if err != nil {
return nil, err
}
return &resp, nil
}

func (m *custom{{.upperStartCamelObject}}Model) PageByCondition(ctx context.Context, conds ...condition.Condition) ([]*{{.upperStartCamelObject}}, int64 ,error) {
func (m *custom{{.upperStartCamelObject}}Model) PageByCondition(ctx context.Context, session sqlx.Session, conds ...condition.Condition) ([]*{{.upperStartCamelObject}}, int64 ,error) {
sb := sqlbuilder.Select({{.lowerStartCamelObject}}FieldNames...).From(m.table)
countsb := sqlbuilder.Select("count(*)").From(m.table)

Expand All @@ -51,24 +70,34 @@ func (m *custom{{.upperStartCamelObject}}Model) PageByCondition(ctx context.Cont
condition.ApplySelect(countsb, countConds...)

var resp []*{{.upperStartCamelObject}}
var err error

sql, args := sb.Build()
err := m.conn.QueryRowsCtx(ctx, &resp, sql, args...)
statement, args := sb.Build()

if session != nil {
err = session.QueryRowsCtx(ctx, &resp, statement, args...)
} else {
{{if .withCache}}err = m.QueryRowsNoCacheCtx(ctx, &resp, statement, args...){{else}}err = m.conn.QueryRowsCtx(ctx, &resp, statement, args...){{end}}
}
if err != nil {
return nil, 0, err
}

var total int64
sql, args = countsb.Build()
err = m.conn.QueryRowCtx(ctx, &total, sql, args...)
statement, args = countsb.Build()
if session != nil {
err = session.QueryRowCtx(ctx, &total, statement, args...)
} else {
{{if .withCache}}err = m.QueryRowNoCacheCtx(ctx, &total, statement, args...){{else}}err = m.conn.QueryRowCtx(ctx, &total, statement, args...){{end}}
}
if err != nil {
return nil, 0, err
}

return resp, total, nil
}

func (m *custom{{.upperStartCamelObject}}Model) UpdateFieldsByCondition(ctx context.Context, field map[string]any, conds ...condition.Condition) error {
func (m *custom{{.upperStartCamelObject}}Model) UpdateFieldsByCondition(ctx context.Context, session sqlx.Session, field map[string]any, conds ...condition.Condition) error {
if field == nil {
return nil
}
Expand All @@ -82,21 +111,33 @@ func (m *custom{{.upperStartCamelObject}}Model) UpdateFieldsByCondition(ctx cont
}
sb.Set(assigns...)

sql, args := sb.Build()
_, err := m.conn.ExecCtx(ctx, sql, args...)
statement, args := sb.Build()

var err error
if session != nil {
_, err = session.ExecCtx(ctx, statement, args...)
} else {
{{if .withCache}}_, err = m.ExecNoCacheCtx(ctx, statement, args...){{else}}_, err = m.conn.ExecCtx(ctx, statement, args...){{end}}
}
if err != nil {
return err
}
return nil
}

func (m *custom{{.upperStartCamelObject}}Model) DeleteByCondition(ctx context.Context, conds ...condition.Condition) error {
func (m *custom{{.upperStartCamelObject}}Model) DeleteByCondition(ctx context.Context, session sqlx.Session, conds ...condition.Condition) error {
if len(conds) == 0 {
return nil
}
sb := sqlbuilder.DeleteFrom(m.table)
condition.ApplyDelete(sb, conds...)
sql, args := sb.Build()
_, err := m.conn.ExecCtx(ctx, sql, args...)
statement, args := sb.Build()

var err error
if session != nil {
_, err = session.ExecCtx(ctx, statement, args...)
} else {
{{if .withCache}}_, err = m.ExecNoCacheCtx(ctx, statement, args...){{else}}_, err = m.conn.ExecCtx(ctx, statement, args...){{end}}
}
return err
}
13 changes: 11 additions & 2 deletions .template/go-zero/model/delete.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func (m *default{{.upperStartCamelObject}}Model) Delete(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error {
func (m *default{{.upperStartCamelObject}}Model) Delete(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error {
{{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne(ctx, {{.lowerStartCamelPrimaryKey}})
if err!=nil{
return err
Expand All @@ -9,10 +9,19 @@ func (m *default{{.upperStartCamelObject}}Model) Delete(ctx context.Context, {{.
sb := sqlbuilder.DeleteFrom(m.table)
sb.Where(sb.EQ("{{.originalPrimaryKey}}", {{.lowerStartCamelPrimaryKey}}))
sql, args := sb.Build()
if session != nil {
return session.ExecCtx(ctx, sql, args...)
}
return conn.ExecCtx(ctx, sql, args...)
}, {{.keyValues}}){{else}}sb := sqlbuilder.DeleteFrom(m.table)
sb.Where(sb.EQ("{{.originalPrimaryKey}}", {{.lowerStartCamelPrimaryKey}}))
sql, args := sb.Build()
_,err:=m.conn.ExecCtx(ctx, sql, args...){{end}}
var err error
if session != nil {
_, err = session.ExecCtx(ctx, sql, args...)
}else{
_, err = m.conn.ExecCtx(ctx, sql, args...)
}
{{end}}
return err
}
22 changes: 17 additions & 5 deletions .template/go-zero/model/find-one-by-field.tpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
func (m *default{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}(ctx context.Context, {{.in}}) (*{{.upperStartCamelObject}}, error) {
func (m *default{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}(ctx context.Context, session sqlx.Session, {{.in}}) (*{{.upperStartCamelObject}}, error) {
{{if .withCache}}{{.cacheKey}}
var resp {{.upperStartCamelObject}}
err := m.QueryRowIndexCtx(ctx, &resp, {{.cacheKeyVariable}}, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
sb := sqlbuilder.Select({{.lowerStartCamelObject}}Rows).From(m.table)
// patch
sb.Where(sb.EQ(strings.Split(strings.ReplaceAll("{{.originalField}}", " ", ""), "=")[0], {{.lowerStartCamelField}}))
sb.Limit(1)
sql, args := sb.Build()
if err := conn.QueryRowCtx(ctx, &resp, sql, args...); err != nil {
var err error

if session != nil {
err = session.QueryRowCtx(ctx, &resp, sql, args...)
} else {
err = conn.QueryRowCtx(ctx, &resp, sql, args...)
}
if err != nil {
return nil, err
}
return resp.{{.upperStartCamelPrimaryKey}}, nil
Expand All @@ -21,13 +27,19 @@ func (m *default{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}(ctx co
return nil, err
}
}{{else}}var resp {{.upperStartCamelObject}}
var err error

sb := sqlbuilder.Select({{.lowerStartCamelObject}}Rows).From(m.table)
// patch
sb.Where(sb.EQ(strings.Split(strings.ReplaceAll("{{.originalField}}", " ", ""), "=")[0], {{.lowerStartCamelField}}))
sb.Limit(1)

sql, args := sb.Build()
err := m.conn.QueryRowCtx(ctx, &resp, sql, args...)

if session != nil {
err = session.QueryRowCtx(ctx, &resp, sql, args...)
} else {
err = m.conn.QueryRowCtx(ctx, &resp, sql, args...)
}

switch err {
case nil:
Expand Down
12 changes: 10 additions & 2 deletions .template/go-zero/model/find-one.tpl
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
func (m *default{{.upperStartCamelObject}}Model) FindOne(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) {
func (m *default{{.upperStartCamelObject}}Model) FindOne(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) {
{{if .withCache}}{{.cacheKey}}
var resp {{.upperStartCamelObject}}
err := m.QueryRowCtx(ctx, &resp, {{.cacheKeyVariable}}, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
sb := sqlbuilder.Select({{.lowerStartCamelObject}}Rows).From(m.table)
sb.Where(sb.EQ("{{.originalPrimaryKey}}", {{.lowerStartCamelPrimaryKey}}))
sql, args := sb.Build()
if session != nil {
return session.QueryRowCtx(ctx, v, sql, args...)
}
return conn.QueryRowCtx(ctx, v, sql, args...)
})
switch err {
Expand All @@ -19,7 +22,12 @@ func (m *default{{.upperStartCamelObject}}Model) FindOne(ctx context.Context, {{
sb.Limit(1)
sql, args := sb.Build()
var resp {{.upperStartCamelObject}}
err := m.conn.QueryRowCtx(ctx, &resp, sql, args...)
var err error
if session != nil {
err = session.QueryRowCtx(ctx, &resp, sql, args...)
} else {
err = m.conn.QueryRowCtx(ctx, &resp, sql, args...)
}
switch err {
case nil:
return &resp, nil
Expand Down
1 change: 1 addition & 0 deletions .template/go-zero/model/import.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import (
"context"
"database/sql"
"fmt"
"strings"
{{if .time}}"time"{{end}}

Expand Down
23 changes: 12 additions & 11 deletions .template/go-zero/model/insert.tpl
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
func (m *default{{.upperStartCamelObject}}Model) Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error) {
{{if .withCache}}{{.keys}}
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
sql, args := sqlbuilder.NewInsertBuilder().
func (m *default{{.upperStartCamelObject}}Model) Insert(ctx context.Context, session sqlx.Session, data *{{.upperStartCamelObject}}) (sql.Result,error) {
statement, args := sqlbuilder.NewInsertBuilder().
InsertInto(m.table).
Cols({{.lowerStartCamelObject}}RowsExpectAutoSet).
Values({{.expressionValues}}).Build()
return conn.ExecCtx(ctx, sql, args...)
}, {{.keyValues}}){{else}}sql, args := sqlbuilder.NewInsertBuilder().
InsertInto(m.table).
Cols({{.lowerStartCamelObject}}RowsExpectAutoSet).
Values({{.expressionValues}}).Build()
ret,err:=m.conn.ExecCtx(ctx, sql, args...){{end}}
return ret,err
{{if .withCache}}{{.keys}}
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
if session != nil {
return session.ExecCtx(ctx, statement, args...)
}
return conn.ExecCtx(ctx, statement, args...)
}, {{.keyValues}}){{else}}if session != nil {
return session.ExecCtx(ctx, statement, args...)
}
return m.conn.ExecCtx(ctx, statement, args...){{end}}
}
2 changes: 1 addition & 1 deletion .template/go-zero/model/interface-delete.tpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Delete(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error
Delete(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error
2 changes: 1 addition & 1 deletion .template/go-zero/model/interface-find-one-by-field.tpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
FindOneBy{{.upperField}}(ctx context.Context, {{.in}}) (*{{.upperStartCamelObject}}, error)
FindOneBy{{.upperField}}(ctx context.Context, session sqlx.Session, {{.in}}) (*{{.upperStartCamelObject}}, error)
2 changes: 1 addition & 1 deletion .template/go-zero/model/interface-find-one.tpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
FindOne(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error)
FindOne(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error)
2 changes: 1 addition & 1 deletion .template/go-zero/model/interface-insert.tpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error)
Insert(ctx context.Context, session sqlx.Session, data *{{.upperStartCamelObject}}) (sql.Result,error)
2 changes: 1 addition & 1 deletion .template/go-zero/model/interface-update.tpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Update(ctx context.Context, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error
Update(ctx context.Context, session sqlx.Session, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error
12 changes: 6 additions & 6 deletions .template/go-zero/model/types.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ type (
{{.method}}

// custom interface generated by jzero
BulkInsert(ctx context.Context, datas []*{{.upperStartCamelObject}}) error
FindByCondition(ctx context.Context, conds ...condition.Condition) ([]*{{.upperStartCamelObject}}, error)
FindOneByCondition(ctx context.Context, conds ...condition.Condition) (*{{.upperStartCamelObject}}, error)
PageByCondition(ctx context.Context, conds ...condition.Condition) ([]*{{.upperStartCamelObject}}, int64 ,error)
UpdateFieldsByCondition(ctx context.Context, field map[string]any, conds ...condition.Condition) error
DeleteByCondition(ctx context.Context, conds ...condition.Condition) error
BulkInsert(ctx context.Context, session sqlx.Session, datas []*{{.upperStartCamelObject}}) error
FindByCondition(ctx context.Context, session sqlx.Session, conds ...condition.Condition) ([]*{{.upperStartCamelObject}}, error)
FindOneByCondition(ctx context.Context, session sqlx.Session, conds ...condition.Condition) (*{{.upperStartCamelObject}}, error)
PageByCondition(ctx context.Context, session sqlx.Session, conds ...condition.Condition) ([]*{{.upperStartCamelObject}}, int64 ,error)
UpdateFieldsByCondition(ctx context.Context, session sqlx.Session, field map[string]any, conds ...condition.Condition) error
DeleteByCondition(ctx context.Context, session sqlx.Session, conds ...condition.Condition) error
}

default{{.upperStartCamelObject}}Model struct {
Expand Down
28 changes: 18 additions & 10 deletions .template/go-zero/model/update.tpl
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
func (m *default{{.upperStartCamelObject}}Model) Update(ctx context.Context, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error {
{{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne(ctx, newData.{{.upperStartCamelPrimaryKey}})
if err!=nil{
func (m *default{{.upperStartCamelObject}}Model) Update(ctx context.Context, session sqlx.Session, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error {
{{if .withCache}}{{if .containsIndexCache}}data, err := m.FindOne(ctx, newData.{{.upperStartCamelPrimaryKey}})
if err != nil{
return err
}

{{end}} {{.keys}}
_, {{if .containsIndexCache}}err{{else}}err:{{end}}= m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
{{end}}{{.keys}}
_, {{if .containsIndexCache}}err{{else}}err :{{end}}= m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
sb := sqlbuilder.Update(m.table)
split := strings.Split({{.lowerStartCamelObject}}RowsExpectAutoSet, ",")
var assigns []string
Expand All @@ -14,8 +13,11 @@ func (m *default{{.upperStartCamelObject}}Model) Update(ctx context.Context, {{i
}
sb.Set(assigns...)
sb.Where(sb.EQ("{{.originalPrimaryKey}}", nil))
sql, _ := sb.Build()
return conn.ExecCtx(ctx, sql, {{.expressionValues}})
statement, _ := sb.Build()
if session != nil{
return session.ExecCtx(ctx, statement, {{.expressionValues}})
}
return conn.ExecCtx(ctx, statement, {{.expressionValues}})
}, {{.keyValues}}){{else}} sb := sqlbuilder.Update(m.table)
split := strings.Split({{.lowerStartCamelObject}}RowsExpectAutoSet, ",")
var assigns []string
Expand All @@ -24,7 +26,13 @@ func (m *default{{.upperStartCamelObject}}Model) Update(ctx context.Context, {{i
}
sb.Set(assigns...)
sb.Where(sb.EQ("{{.originalPrimaryKey}}", nil))
sql, _ := sb.Build()
_,err:=m.conn.ExecCtx(ctx, sql, {{.expressionValues}}){{end}}
statement, _ := sb.Build()

var err error
if session != nil{
_, err = session.ExecCtx(ctx, statement, {{.expressionValues}})
}else{
_, err = m.conn.ExecCtx(ctx, statement, {{.expressionValues}})
}{{end}}
return err
}

0 comments on commit ca35b5b

Please sign in to comment.