Skip to content

Commit

Permalink
add PrepareBuilder/PreparexBuilder interface and optimize generate co…
Browse files Browse the repository at this point in the history
…de logic
  • Loading branch information
alimy committed Mar 30, 2023
1 parent 2d8f0db commit 4fc309d
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 91 deletions.
12 changes: 12 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ type PreparexContext interface {
Rebind(query string) string
}

// PrepareBuilder prepare builder interface sql
type PrepareBuilder interface {
PrepareContext
QueryHook(query string) string
}

// PreparexBuilder preparex builder interface for sqlx
type PreparexBuilder interface {
PreparexContext
QueryHook(query string) string
}

// Generator generate struct code automatic base SQLQuery
type Generator interface {
Generate(dstPath string, pkgName string, query SQLQuery, opts ...option) error
Expand Down
132 changes: 66 additions & 66 deletions examples/auto/yesql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

//go:generate go run $GOFILE
func main() {
log.Println("Yesql generate code start")
log.Println("[Yesql] generate code start")
if err := yesql.Generate("yesql.sql", "auto", "yesql"); err != nil {
log.Fatalf("generate code occurs error: %s", err)
}
log.Println("Yesql generate code finish")
log.Println("[Yesql] generate code finish")
}
7 changes: 3 additions & 4 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ module github.com/alimy/yesql/examples

go 1.18

require (
github.com/alimy/yesql v1.0.0
github.com/jmoiron/sqlx v1.3.5
)
require github.com/alimy/yesql v1.1.1

require github.com/jmoiron/sqlx v1.3.5 // indirect

replace github.com/alimy/yesql => ../
16 changes: 8 additions & 8 deletions template/sql.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,35 @@ type {{ naming $scope }} struct {
{{end -}}

{{if .DefaultQueryMapNotEmpty}}
func Build{{ .DefaultStructName }}(p yesql.PrepareContext, ctx ...context.Context) (obj *{{ .DefaultStructName }}, err error) {
func Build{{ .DefaultStructName }}(p yesql.PrepareBuilder, ctx ...context.Context) (obj *{{ .DefaultStructName }}, err error) {
var c context.Context
if len(ctx) > 0 && ctx[0] == nil {
if len(ctx) > 0 && ctx[0] != nil {
c = ctx[0]
} else {
c = context.Background()
}
obj = &{{ .DefaultStructName }}{{if .DefaultQueryMap.IsRawQueryNotEmpty }}{
{{range $name, $_ := .DefaultQueryMap.FilterByStyle "raw" }}{{ naming $name }}: _{{ naming $name}},
{{range $name, $_ := .DefaultQueryMap.FilterByStyle "raw" }}{{ naming $name }}: p.QueryHook(_{{ naming $name}}),
{{end}}}{{else}}{}{{end}}
{{- range $name, $_ := .DefaultQueryMap.FilterByStyle "stmt" }}
if obj.{{ naming $name }}, err = p.PrepareContext(c, _{{ naming $name }}); err != nil {
if obj.{{ naming $name }}, err = p.PrepareContext(c, p.QueryHook(_{{ naming $name }})); err != nil {
return
}{{end}}
return
}{{end}}
{{range $scope, $queryMap := .ScopeQuery }}
func Build{{ naming $scope }}(p yesql.PrepareContext, ctx ...context.Context) (obj *{{ naming $scope }}, err error) {
func Build{{ naming $scope }}(p yesql.PrepareBuilder, ctx ...context.Context) (obj *{{ naming $scope }}, err error) {
var c context.Context
if len(ctx) > 0 && ctx[0] == nil {
if len(ctx) > 0 && ctx[0] != nil {
c = ctx[0]
} else {
c = context.Background()
}
obj = &{{naming $scope }}{{if $queryMap.IsRawQueryNotEmpty }}{
{{range $name, $_ := $queryMap.FilterByStyle "raw" }}{{ naming $name }}: _{{ naming $name}}_{{ naming $scope }},
{{range $name, $_ := $queryMap.FilterByStyle "raw" }}{{ naming $name }}: p.QueryHook(_{{ naming $name}}_{{ naming $scope }}),
{{end}}}{{else}}{}{{end}}
{{- range $name, $_ := $queryMap.FilterByStyle "stmt" }}
if obj.{{ naming $name }}, err = p.PrepareContext(c, _{{ naming $name }}_{{ naming $scope }}); err != nil {
if obj.{{ naming $name }}, err = p.PrepareContext(c, p.QueryHook(_{{ naming $name }}_{{ naming $scope }})); err != nil {
return
}{{end}}
return
Expand Down
Loading

0 comments on commit 4fc309d

Please sign in to comment.