diff --git a/core.go b/core.go index fc16b67..3b27651 100644 --- a/core.go +++ b/core.go @@ -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 diff --git a/template/sql.tmpl b/template/sql.tmpl index bd30919..5b68bea 100644 --- a/template/sql.tmpl +++ b/template/sql.tmpl @@ -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}} @@ -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}} diff --git a/template/sqlx.tmpl b/template/sqlx.tmpl index 4f400dd..eeb3baa 100644 --- a/template/sqlx.tmpl +++ b/template/sqlx.tmpl @@ -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}} @@ -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}} diff --git a/version.go b/version.go index d513310..3276b13 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package yesql -var Version = "v1.2.0" +var Version = "v1.3.0"