diff --git a/conf.go b/conf.go new file mode 100644 index 0000000..e4cd099 --- /dev/null +++ b/conf.go @@ -0,0 +1,58 @@ +package yesql + +import ( + "os" + + "gopkg.in/yaml.v3" +) + +type yesqlConf struct { + Version string `yaml:"version"` + Generator struct { + Engine string `yaml:"engine"` + SqlxPkgName string `yaml:"sqlx_package"` + DefaultStructName string `yaml:"default_struct_name"` + GoFileName string `yaml:"go_file_name"` + } + Sql []struct { + Queries string `yaml:"queries"` + Gen struct { + Package string `yaml:"package"` + Out string `yaml:"out"` + Engine string `yaml:"engine"` + SqlxPkgName string `yaml:"sqlx_package"` + DefaultStructName string `yaml:"default_struct_name"` + GoFileName string `yaml:"go_file_name"` + } + } +} + +func (s *yesqlConf) prepare() { + for i := range s.Sql { + if s.Sql[i].Gen.Engine == "" { + s.Sql[i].Gen.Engine = s.Generator.Engine + } + if s.Sql[i].Gen.SqlxPkgName == "" { + s.Sql[i].Gen.SqlxPkgName = s.Generator.SqlxPkgName + } + if s.Sql[i].Gen.DefaultStructName == "" { + s.Sql[i].Gen.DefaultStructName = s.Generator.DefaultStructName + } + if s.Sql[i].Gen.GoFileName == "" { + s.Sql[i].Gen.GoFileName = s.Generator.GoFileName + } + } +} + +func yesqlConfFrom(path string) (*yesqlConf, error) { + file, err := os.Open(path) + if err != nil { + return nil, err + } + defer file.Close() + res := &yesqlConf{} + decoder := yaml.NewDecoder(file) + err = decoder.Decode(res) + res.prepare() + return res, err +} diff --git a/core.go b/core.go index 8b90916..60044c5 100644 --- a/core.go +++ b/core.go @@ -179,25 +179,29 @@ type Generator interface { Generate(dstPath string, pkgName string, query SQLQuery, opts ...option) error } +// OptFn option function type OptFn func(opt *generateOption) func (f OptFn) apply(opt *generateOption) { f(opt) } +// DefaultStructNameOpt set custom default global sql query struct name func DefaultStructNameOpt(name string) OptFn { return func(opt *generateOption) { opt.defaultStructName = name } } +// GoFileNameOpt set custom go file name to generate func GoFileNameOpt(name string) OptFn { return func(opt *generateOption) { opt.goFileName = name } } -func SqlxPkgName(name string) OptFn { +// SqlxPkgNameOpt set sqlx pakcage name +func SqlxPkgNameOpt(name string) OptFn { return func(opt *generateOption) { opt.sqlxPkgName = name } diff --git a/examples/auto/ac/yesql.go b/examples/auto/ac/ac.go similarity index 99% rename from examples/auto/ac/yesql.go rename to examples/auto/ac/ac.go index 25e921f..9deb843 100644 --- a/examples/auto/ac/yesql.go +++ b/examples/auto/ac/ac.go @@ -1,6 +1,6 @@ // Code generated by Yesql. DO NOT EDIT. // versions: -// - Yesql v1.8.5 +// - Yesql v1.9.0 package ac diff --git a/examples/auto/bc/yesql.go b/examples/auto/bc/bc.go similarity index 99% rename from examples/auto/bc/yesql.go rename to examples/auto/bc/bc.go index 8a46c94..e5f5514 100644 --- a/examples/auto/bc/yesql.go +++ b/examples/auto/bc/bc.go @@ -1,6 +1,6 @@ // Code generated by Yesql. DO NOT EDIT. // versions: -// - Yesql v1.8.5 +// - Yesql v1.9.0 package bc diff --git a/examples/auto/cc/yesql.go b/examples/auto/cc/cc.go similarity index 99% rename from examples/auto/cc/yesql.go rename to examples/auto/cc/cc.go index 303b959..c33f686 100644 --- a/examples/auto/cc/yesql.go +++ b/examples/auto/cc/cc.go @@ -1,6 +1,6 @@ // Code generated by Yesql. DO NOT EDIT. // versions: -// - Yesql v1.8.5 +// - Yesql v1.9.0 package cc diff --git a/examples/auto/yesql.go b/examples/auto/yesql.go index 7686943..63470fa 100644 --- a/examples/auto/yesql.go +++ b/examples/auto/yesql.go @@ -1,6 +1,6 @@ // Code generated by Yesql. DO NOT EDIT. // versions: -// - Yesql v1.8.5 +// - Yesql v1.9.0 package yesql diff --git a/examples/gen.go b/examples/gen.go index 2a31a5e..238a7c8 100644 --- a/examples/gen.go +++ b/examples/gen.go @@ -14,14 +14,7 @@ func main() { query.Query = strings.TrimRight(query.Query, ";") return query, nil }) - opt := yesql.SqlxPkgName("github.com/bitbus/sqlx") - sqlInfos := []yesql.SqlInfo{ - yesql.NewSqlInfo("yesql.sql", "auto", "yesql"), - yesql.NewSqlInfo("yesql_ac.sql", "auto/ac", "ac"), - yesql.NewSqlInfo("yesql_bc.sql", "auto/bc", "bc"), - yesql.NewSqlInfo("yesql_cc.sql", "auto/cc", "cc"), - } - if err := yesql.GenerateFrom(sqlInfos, opt); err != nil { + if err := yesql.Generate(); err != nil { log.Fatalf("generate code occurs error: %s", err) } log.Println("[Yesql] generate code finish") diff --git a/examples/go.mod b/examples/go.mod index 21d298b..f86dbff 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -3,8 +3,10 @@ module github.com/alimy/yesql/examples go 1.18 require ( - github.com/alimy/yesql v1.8.5 + github.com/alimy/yesql v1.9.0 github.com/bitbus/sqlx v1.6.0 ) +require gopkg.in/yaml.v3 v3.0.1 // indirect + replace github.com/alimy/yesql => ../ diff --git a/examples/go.sum b/examples/go.sum index a65e17c..a71a336 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -3,3 +3,7 @@ github.com/bitbus/sqlx v1.6.0/go.mod h1:MemKLfQ600g6PxUVsIDe48PlY3wOquyW2ApeiXoy github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/yesql.yaml b/examples/yesql.yaml new file mode 100644 index 0000000..e386e9e --- /dev/null +++ b/examples/yesql.yaml @@ -0,0 +1,28 @@ +version: '1' + +generator: + engine: sqlx + sqlx_package: 'github.com/bitbus/sqlx' + default_struct_name: yesql + go_file_name: yesql + +sql: + - queries: yesql.sql + gen: + package: yesql + out: auto + - queries: yesql_ac.sql + gen: + package: ac + out: auto/ac + go_file_name: ac + - queries: yesql_bc.sql + gen: + package: bc + out: auto/bc + go_file_name: bc + - queries: yesql_cc.sql + gen: + package: cc + out: auto/cc + go_file_name: cc diff --git a/go.mod b/go.mod index 09eb9f3..140653f 100644 --- a/go.mod +++ b/go.mod @@ -4,4 +4,5 @@ go 1.18 require ( github.com/bitbus/sqlx v1.6.0 + gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index 4ae7591..a71a336 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,9 @@ github.com/bitbus/sqlx v1.6.0 h1:ewrBydRkyHZqfOqvHVYpiBlqQtLn93B/loa2EwjpZ74= github.com/bitbus/sqlx v1.6.0/go.mod h1:MemKLfQ600g6PxUVsIDe48PlY3wOquyW2ApeiXoynFo= -github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= -github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= -github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= -github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= -github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= -github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= -github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/version.go b/version.go index 2d3842d..0ae4eb6 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package yesql -var Version = "v1.8.5" +var Version = "v1.9.0" diff --git a/yesql.go b/yesql.go index ddfc089..ac05dc2 100644 --- a/yesql.go +++ b/yesql.go @@ -147,8 +147,8 @@ func NewSqlInfo(sqlFilePath string, dstPath string, pkgName string) SqlInfo { } } -// Generate generate struct type autumatic by sql file with default generator -func Generate(sqlFilePath string, dstPath string, pkgName string, opts ...option) error { +// GenerateBy generate struct type autumatic by sql file with default generator +func GenerateBy(sqlFilePath string, dstPath string, pkgName string, opts ...option) error { query, err := ParseFile(sqlFilePath) if err != nil { return err @@ -159,7 +159,35 @@ func Generate(sqlFilePath string, dstPath string, pkgName string, opts ...option // GenerateFrom generate struct type autumatic from SqlInfo's inforamation with default generator func GenerateFrom(infos []SqlInfo, opts ...option) (err error) { for _, s := range infos { - if err = Generate(s.FilePath, s.DestPath, s.PkgName, opts...); err != nil { + if err = GenerateBy(s.FilePath, s.DestPath, s.PkgName, opts...); err != nil { + return + } + } + return +} + +// Generate generate struct type autumatic by configFile with default generator +func Generate(conf ...string) (err error) { + cfgFileName := "yesql.yaml" + if len(conf) > 0 && conf[0] != "" { + cfgFileName = conf[0] + } + cfg, xerr := yesqlConfFrom(cfgFileName) + if xerr != nil { + return err + } + for _, q := range cfg.Sql { + opts := []option{} + if q.Gen.DefaultStructName != "" { + opts = append(opts, DefaultStructNameOpt(q.Gen.DefaultStructName)) + } + if q.Gen.GoFileName != "" { + opts = append(opts, GoFileNameOpt(q.Gen.GoFileName)) + } + if q.Gen.SqlxPkgName != "" { + opts = append(opts, SqlxPkgNameOpt(q.Gen.SqlxPkgName)) + } + if err = GenerateBy(q.Queries, q.Gen.Out, q.Gen.Package, opts...); err != nil { return } }