Skip to content

Commit

Permalink
fixed generate error code when no stmt query in generate mode
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Jul 13, 2023
1 parent c5abde4 commit 0735e39
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
12 changes: 12 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ func (q QueryMap) IsRawQueryNotEmpty() bool {
return false
}

func (q QueryMap) IsStmtQueryNotEmpty() bool {
for _, query := range q {
switch query.PrepareStyle() {
case PrepareStyleStmt, PrepareStyleNamedStmt:
return true
default:
// nothing
}
}
return false
}

// ScopeQuery is a namespace QueryMap
type ScopeQuery map[string]QueryMap

Expand Down
10 changes: 7 additions & 3 deletions template/sql.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ type {{ naming $scope }} struct {
{{end -}}

{{if .DefaultQueryMapNotEmpty}}
func Build{{ .DefaultStructName }}(p yesql.PrepareBuilder, ctx ...context.Context) (obj *{{ .DefaultStructName }}, err error) {
var c context.Context
func Build{{ .DefaultStructName }}(p yesql.PrepareBuilder{{if .DefaultQueryMap.IsStmtQueryNotEmpty }}, ctx ...context.Context{{end}}) (obj *{{ .DefaultStructName }}, err error) {
{{- if .DefaultQueryMap.IsStmtQueryNotEmpty -}}
var c context.Context
if len(ctx) > 0 && ctx[0] != nil {
c = ctx[0]
} else {
c = context.Background()
}
{{- end}}
obj = &{{ .DefaultStructName }}{{if .DefaultQueryMap.IsRawQueryNotEmpty }}{
{{range $name, $_ := .DefaultQueryMap.FilterByStyle "raw" }}{{ naming $name }}: p.QueryHook(_{{ naming $name}}),
{{end}}}{{else}}{}{{end}}
Expand All @@ -50,13 +52,15 @@ func Build{{ .DefaultStructName }}(p yesql.PrepareBuilder, ctx ...context.Contex
return
}{{end}}
{{range $scope, $queryMap := .ScopeQuery }}
func Build{{ naming $scope }}(p yesql.PrepareBuilder, ctx ...context.Context) (obj *{{ naming $scope }}, err error) {
func Build{{ naming $scope }}(p yesql.PrepareBuilder{{if $queryMap.IsStmtQueryNotEmpty }}, ctx ...context.Context{{end}}) (obj *{{ naming $scope }}, err error) {
{{- if $queryMap.IsStmtQueryNotEmpty -}}
var c context.Context
if len(ctx) > 0 && ctx[0] != nil {
c = ctx[0]
} else {
c = context.Background()
}
{{- end}}
obj = &{{naming $scope }}{{if $queryMap.IsRawQueryNotEmpty }}{
{{range $name, $_ := $queryMap.FilterByStyle "raw" }}{{ naming $name }}: p.QueryHook(_{{ naming $name}}_{{ naming $scope }}),
{{end}}}{{else}}{}{{end}}
Expand Down
10 changes: 7 additions & 3 deletions template/sqlx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ type {{ naming $scope }} struct {
{{end -}}

{{if .DefaultQueryMapNotEmpty}}
func Build{{ .DefaultStructName }}(p yesql.PreparexBuilder, ctx ...context.Context) (obj *{{ .DefaultStructName }}, err error) {
var c context.Context
func Build{{ .DefaultStructName }}(p yesql.PreparexBuilder{{if .DefaultQueryMap.IsStmtQueryNotEmpty }}, ctx ...context.Context{{end}}) (obj *{{ .DefaultStructName }}, err error) {
{{- if .DefaultQueryMap.IsStmtQueryNotEmpty -}}
var c context.Context
if len(ctx) > 0 && ctx[0] != nil {
c = ctx[0]
} else {
c = context.Background()
}
{{- end}}
obj = &{{ .DefaultStructName }}{{if .DefaultQueryMap.IsRawQueryNotEmpty }}{
{{range $name, $_ := .DefaultQueryMap.FilterByStyle "raw" }}{{ naming $name }}: p.QueryHook(_{{ naming $name}}),
{{end}}}{{else}}{}{{end}}
Expand All @@ -58,13 +60,15 @@ func Build{{ .DefaultStructName }}(p yesql.PreparexBuilder, ctx ...context.Conte
return
}{{end}}
{{range $scope, $queryMap := .ScopeQuery }}
func Build{{ naming $scope }}(p yesql.PreparexBuilder, ctx ...context.Context) (obj *{{ naming $scope }}, err error) {
func Build{{ naming $scope }}(p yesql.PreparexBuilder{{if $queryMap.IsStmtQueryNotEmpty }}, ctx ...context.Context{{end}}) (obj *{{ naming $scope }}, err error) {
{{- if $queryMap.IsStmtQueryNotEmpty -}}
var c context.Context
if len(ctx) > 0 && ctx[0] != nil {
c = ctx[0]
} else {
c = context.Background()
}
{{- end}}
obj = &{{naming $scope }}{{if $queryMap.IsRawQueryNotEmpty }}{
{{range $name, $_ := $queryMap.FilterByStyle "raw" }}{{ naming $name }}: p.QueryHook(_{{ naming $name}}_{{ naming $scope }}),
{{end}}}{{else}}{}{{end}}
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package yesql

var Version = "v1.2.0"
var Version = "v1.3.0"

0 comments on commit 0735e39

Please sign in to comment.