Skip to content

Commit

Permalink
Revert "🐞 fix(gen): pg gen of insert (#1591)" (#1598)
Browse files Browse the repository at this point in the history
This reverts commit cc4c492.
  • Loading branch information
kevwan authored Mar 1, 2022
1 parent 168740b commit e311286
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 70 deletions.
2 changes: 1 addition & 1 deletion tools/goctl/model/sql/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
table.UniqueCacheKey = uniqueKey
table.ContainsUniqueCacheKey = len(uniqueKey) > 0

importsCode, err := genImports(withCache, in.ContainsTime(), table, g.isPostgreSql)
importsCode, err := genImports(withCache, in.ContainsTime(), table)
if err != nil {
return "", err
}
Expand Down
18 changes: 5 additions & 13 deletions tools/goctl/model/sql/gen/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)

func genImports(withCache, timeImport bool, table Table, postgreSql bool) (string, error) {
func genImports(withCache, timeImport bool, table Table) (string, error) {
if withCache {
text, err := pathx.LoadTemplate(category, importsTemplateFile, template.Imports)
if err != nil {
Expand All @@ -23,18 +23,10 @@ func genImports(withCache, timeImport bool, table Table, postgreSql bool) (strin

return buffer.String(), nil
}
var text string
var err error
if postgreSql {
text, err = pathx.LoadTemplate(category, importsWithNoCacheTemplateFile, template.ImportsNoCachePg)
if err != nil {
return "", err
}
} else {
text, err = pathx.LoadTemplate(category, importsWithNoCacheTemplateFile, template.ImportsNoCache)
if err != nil {
return "", err
}

text, err := pathx.LoadTemplate(category, importsWithNoCacheTemplateFile, template.ImportsNoCache)
if err != nil {
return "", err
}

buffer, err := util.With("import").Parse(text).Execute(map[string]interface{}{
Expand Down
30 changes: 6 additions & 24 deletions tools/goctl/model/sql/gen/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,9 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)
}

camel := table.Name.ToCamel()
var text string
var err error
// if database is postgresql
if postgreSql {
text, err = pathx.LoadTemplate(category, insertTemplateFile, template.InsertPg)
if err != nil {
return "", "", err
}
} else {
text, err = pathx.LoadTemplate(category, insertTemplateFile, template.Insert)
if err != nil {
return "", "", err
}
text, err := pathx.LoadTemplate(category, insertTemplateFile, template.Insert)
if err != nil {
return "", "", err
}

output, err := util.With("insert").
Expand All @@ -79,17 +69,9 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)
}

// interface method
// if database is postgresql
if postgreSql {
text, err = pathx.LoadTemplate(category, insertTemplateMethodFile, template.InsertMethodPg)
if err != nil {
return "", "", err
}
} else {
text, err = pathx.LoadTemplate(category, insertTemplateMethodFile, template.InsertMethod)
if err != nil {
return "", "", err
}
text, err = pathx.LoadTemplate(category, insertTemplateMethodFile, template.InsertMethod)
if err != nil {
return "", "", err
}

insertMethodOutput, err := util.With("insertMethod").Parse(text).Execute(map[string]interface{}{
Expand Down
13 changes: 0 additions & 13 deletions tools/goctl/model/sql/template/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,4 @@ var (
"github.com/zeromicro/go-zero/core/stringx"
)
`
// pg
ImportsNoCachePg = `import (
"context"
"fmt"
"strings"
{{if .time}}"time"{{end}}
"github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx"
)
`
)
19 changes: 0 additions & 19 deletions tools/goctl/model/sql/template/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,5 @@ func (m *default{{.upperStartCamelObject}}Model) Insert(ctx context.Context, dat
}
`

// pg
var InsertPg = `
func (m *default{{.upperStartCamelObject}}Model) Insert(data *{{.upperStartCamelObject}}) (int64,error) {
{{if .withCache}}{{if .containsIndexCache}}{{.keys}}
ret, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet)
return conn.Exec(query, {{.expressionValues}})
}, {{.keyValues}}){{else}}query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet)
ret,err:=m.ExecNoCache(query, {{.expressionValues}})
{{end}}{{else}}query := fmt.Sprintf("insert into %s (%s) values ({{.expression}}) RETURNING id", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet)
var id int64
err:=m.conn.QueryRow(&id, query, {{.expressionValues}}){{end}}
return id,err
}
`

// InsertMethod defines an interface method template for insert code in model
var InsertMethod = `Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error)`

// pg
var InsertMethodPg = `Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (int64,error)`

0 comments on commit e311286

Please sign in to comment.