From 00db97fcc1570c27bad7dcaf0170b29d91ba5e56 Mon Sep 17 00:00:00 2001 From: MarkJoyMa <64180138+MarkJoyMa@users.noreply.github.com> Date: Sun, 22 Oct 2023 23:43:05 -0500 Subject: [PATCH] feat: model add withSession (#3658) --- tools/goctl/model/sql/template/tpl/model-new.tpl | 6 ------ tools/goctl/model/sql/template/tpl/model.tpl | 8 ++++++++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/tools/goctl/model/sql/template/tpl/model-new.tpl b/tools/goctl/model/sql/template/tpl/model-new.tpl index 1743e578..f6271cca 100644 --- a/tools/goctl/model/sql/template/tpl/model-new.tpl +++ b/tools/goctl/model/sql/template/tpl/model-new.tpl @@ -5,9 +5,3 @@ func new{{.upperStartCamelObject}}Model(conn sqlx.SqlConn{{if .withCache}}, c ca } } -func (m *default{{.upperStartCamelObject}}Model) withSession(session sqlx.Session) *default{{.upperStartCamelObject}}Model { - return &default{{.upperStartCamelObject}}Model{ - {{if .withCache}}CachedConn:m.CachedConn.WithSession(session){{else}}conn:sqlx.NewSqlConnFromSession(session){{end}}, - table: {{.table}}, - } -} diff --git a/tools/goctl/model/sql/template/tpl/model.tpl b/tools/goctl/model/sql/template/tpl/model.tpl index 3b1b1013..d861da79 100644 --- a/tools/goctl/model/sql/template/tpl/model.tpl +++ b/tools/goctl/model/sql/template/tpl/model.tpl @@ -15,6 +15,7 @@ type ( // and implement the added methods in custom{{.upperStartCamelObject}}Model. {{.upperStartCamelObject}}Model interface { {{.lowerStartCamelObject}}Model + {{if not .withCache}}withSession(session sqlx.Session) {{.upperStartCamelObject}}Model{{end}} } custom{{.upperStartCamelObject}}Model struct { @@ -28,3 +29,10 @@ func New{{.upperStartCamelObject}}Model(conn sqlx.SqlConn{{if .withCache}}, c ca default{{.upperStartCamelObject}}Model: new{{.upperStartCamelObject}}Model(conn{{if .withCache}}, c, opts...{{end}}), } } + +{{if not .withCache}} +func (m *custom{{.upperStartCamelObject}}Model) withSession(session sqlx.Session) {{.upperStartCamelObject}}Model { + return New{{.upperStartCamelObject}}Model(sqlx.NewSqlConnFromSession(session)) +} +{{end}} +