Skip to content

Commit

Permalink
fixed generate code not correct when have global sql clause only in s…
Browse files Browse the repository at this point in the history
…ql define file
  • Loading branch information
alimy committed Aug 19, 2023
1 parent 86c49a5 commit 3d3317d
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 13 deletions.
10 changes: 5 additions & 5 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,25 +179,25 @@ type Generator interface {
Generate(dstPath string, pkgName string, query SQLQuery, opts ...option) error
}

type OptionFunc func(opt *generateOption)
type OptFn func(opt *generateOption)

func (f OptionFunc) apply(opt *generateOption) {
func (f OptFn) apply(opt *generateOption) {
f(opt)
}

func DefaultStructNameOpt(name string) OptionFunc {
func DefaultStructNameOpt(name string) OptFn {
return func(opt *generateOption) {
opt.defaultStructName = name
}
}

func GoFileNameOpt(name string) OptionFunc {
func GoFileNameOpt(name string) OptFn {
return func(opt *generateOption) {
opt.goFileName = name
}
}

func SqlxPkgName(name string) OptionFunc {
func SqlxPkgName(name string) OptFn {
return func(opt *generateOption) {
opt.sqlxPkgName = name
}
Expand Down
4 changes: 2 additions & 2 deletions examples/auto/yesql.go → examples/auto/ac/yesql.go

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

86 changes: 86 additions & 0 deletions examples/auto/cc/yesql.go

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

8 changes: 6 additions & 2 deletions examples/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ func main() {
query.Query = strings.TrimRight(query.Query, ";")
return query, nil
})
if err := yesql.Generate("yesql.sql", "auto", "yesql", yesql.SqlxPkgName("github.com/bitbus/sqlx")); err != nil {
log.Fatalf("generate code occurs error: %s", err)
opt := yesql.SqlxPkgName("github.com/bitbus/sqlx")
if err := yesql.Generate("yesql_ac.sql", "auto/ac", "ac", opt); err != nil {
log.Fatalf("generate ac code occurs error: %s", err)
}
if err := yesql.Generate("yesql_cc.sql", "auto/cc", "cc", opt); err != nil {
log.Fatalf("generate cc code occurs error: %s", err)
}
log.Println("[Yesql] generate code finish")
}
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/alimy/yesql/examples
go 1.18

require (
github.com/alimy/yesql v1.8.0
github.com/alimy/yesql v1.8.1
github.com/bitbus/sqlx v1.6.0
)

Expand Down
File renamed without changes.
37 changes: 37 additions & 0 deletions examples/yesql_cc.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
----------------------
-- default sql ddl --
----------------------

-- name: user_info_a
-- prepare: stmt
SELECT `username` FROM @user WHERE username=?

-- name: user_info_b
SELECT `username`, `nickname` FROM @user WHERE username=?

-- name: user_info_c
-- prepare: raw
SELECT "username" FROM @user WHERE username=?

-- name: user_info_d
-- prepare: string
SELECT "username", "nickname" FROM @user WHERE username=?

-- name: login_info_a
-- prepare: named_stmt
SELECT * FROM @user WHERE username=?

-- name: login_info_b
SELECT * FROM @user WHERE username=?

-- name: logout_info_c
-- prepare: raw
SELECT * FROM @user WHERE username=?

-- name: login_info_d
-- prepare: string
SELECT * FROM @user WHERE username=?

-- name: login_info_e
-- prepare: named_stmt
SELECT * FROM @user WHERE username=?
4 changes: 4 additions & 0 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (t *tmplCtx) DefaultQueryMapNotEmpty() bool {
return len(t.DefaultQueryMap) != 0
}

func (t *tmplCtx) ScopeQueryNotEmpty() bool {
return len(t.ScopeQuery) != 0
}

func (s *sqlGenerator) Generate(dstPath string, pkgName string, query SQLQuery, opts ...option) (err error) {
opt := &generateOption{
goFileName: "yesql.go",
Expand Down
2 changes: 1 addition & 1 deletion template/sql.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"
"database/sql"

"github.com/alimy/yesql"
{{if .ScopeQueryNotEmpty}}"github.com/alimy/yesql"{{end}}
)

const (
Expand Down
2 changes: 1 addition & 1 deletion template/sqlx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package {{ .PkgName }}
import (
"context"

"github.com/alimy/yesql"
"{{ .SqlxPkgName }}"
{{if .ScopeQueryNotEmpty}}"github.com/alimy/yesql"{{end}}
)

const (
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.8.0"
var Version = "v1.8.1"

0 comments on commit 3d3317d

Please sign in to comment.