diff --git a/.template/go-zero/model/customized.tpl b/.template/go-zero/model/customized.tpl index c05662b..0655128 100644 --- a/.template/go-zero/model/customized.tpl +++ b/.template/go-zero/model/customized.tpl @@ -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) @@ -51,16 +70,26 @@ 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 } @@ -68,7 +97,7 @@ func (m *custom{{.upperStartCamelObject}}Model) PageByCondition(ctx context.Cont 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 } @@ -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 } \ No newline at end of file diff --git a/.template/go-zero/model/delete.tpl b/.template/go-zero/model/delete.tpl index 77bac5d..99b9325 100644 --- a/.template/go-zero/model/delete.tpl +++ b/.template/go-zero/model/delete.tpl @@ -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 @@ -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 } diff --git a/.template/go-zero/model/find-one-by-field.tpl b/.template/go-zero/model/find-one-by-field.tpl index 340c6e2..9a523d7 100644 --- a/.template/go-zero/model/find-one-by-field.tpl +++ b/.template/go-zero/model/find-one-by-field.tpl @@ -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 @@ -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: diff --git a/.template/go-zero/model/find-one.tpl b/.template/go-zero/model/find-one.tpl index e5af014..8f8e817 100644 --- a/.template/go-zero/model/find-one.tpl +++ b/.template/go-zero/model/find-one.tpl @@ -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 { @@ -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 diff --git a/.template/go-zero/model/import.tpl b/.template/go-zero/model/import.tpl index 17440c9..646731c 100644 --- a/.template/go-zero/model/import.tpl +++ b/.template/go-zero/model/import.tpl @@ -1,6 +1,7 @@ import ( "context" "database/sql" + "fmt" "strings" {{if .time}}"time"{{end}} diff --git a/.template/go-zero/model/insert.tpl b/.template/go-zero/model/insert.tpl index a355a11..3f1ce08 100644 --- a/.template/go-zero/model/insert.tpl +++ b/.template/go-zero/model/insert.tpl @@ -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}} } diff --git a/.template/go-zero/model/interface-delete.tpl b/.template/go-zero/model/interface-delete.tpl index d10978b..c75a6aa 100644 --- a/.template/go-zero/model/interface-delete.tpl +++ b/.template/go-zero/model/interface-delete.tpl @@ -1 +1 @@ -Delete(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error \ No newline at end of file +Delete(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error \ No newline at end of file diff --git a/.template/go-zero/model/interface-find-one-by-field.tpl b/.template/go-zero/model/interface-find-one-by-field.tpl index e18ded7..a689a91 100644 --- a/.template/go-zero/model/interface-find-one-by-field.tpl +++ b/.template/go-zero/model/interface-find-one-by-field.tpl @@ -1 +1 @@ -FindOneBy{{.upperField}}(ctx context.Context, {{.in}}) (*{{.upperStartCamelObject}}, error) \ No newline at end of file +FindOneBy{{.upperField}}(ctx context.Context, session sqlx.Session, {{.in}}) (*{{.upperStartCamelObject}}, error) \ No newline at end of file diff --git a/.template/go-zero/model/interface-find-one.tpl b/.template/go-zero/model/interface-find-one.tpl index a7a5440..7d6dfb0 100644 --- a/.template/go-zero/model/interface-find-one.tpl +++ b/.template/go-zero/model/interface-find-one.tpl @@ -1 +1 @@ -FindOne(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) \ No newline at end of file +FindOne(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) \ No newline at end of file diff --git a/.template/go-zero/model/interface-insert.tpl b/.template/go-zero/model/interface-insert.tpl index 2872403..5146747 100644 --- a/.template/go-zero/model/interface-insert.tpl +++ b/.template/go-zero/model/interface-insert.tpl @@ -1 +1 @@ -Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error) \ No newline at end of file +Insert(ctx context.Context, session sqlx.Session, data *{{.upperStartCamelObject}}) (sql.Result,error) \ No newline at end of file diff --git a/.template/go-zero/model/interface-update.tpl b/.template/go-zero/model/interface-update.tpl index 1920425..33d6845 100644 --- a/.template/go-zero/model/interface-update.tpl +++ b/.template/go-zero/model/interface-update.tpl @@ -1 +1 @@ -Update(ctx context.Context, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error \ No newline at end of file +Update(ctx context.Context, session sqlx.Session, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error \ No newline at end of file diff --git a/.template/go-zero/model/types.tpl b/.template/go-zero/model/types.tpl index 98bef21..41fd862 100644 --- a/.template/go-zero/model/types.tpl +++ b/.template/go-zero/model/types.tpl @@ -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 { diff --git a/.template/go-zero/model/update.tpl b/.template/go-zero/model/update.tpl index 7e89f12..af82986 100644 --- a/.template/go-zero/model/update.tpl +++ b/.template/go-zero/model/update.tpl @@ -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 @@ -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 @@ -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 }