Skip to content

MySQL Support #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ require (
github.com/jinzhu/inflection v1.0.0
github.com/lfittl/pg_query_go v1.0.0
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect
golang.org/x/sys v0.0.0-20191220220014-0732a990476f // indirect
google.golang.org/genproto v0.0.0-20191223191004-3caeed10a8bf // indirect
google.golang.org/grpc v1.26.0 // indirect
vitess.io/vitess v0.0.0-20191113025808-0629f0da20ab
)
289 changes: 289 additions & 0 deletions go.sum

Large diffs are not rendered by default.

45 changes: 39 additions & 6 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path/filepath"

"github.com/kyleconroy/sqlc/internal/dinosql"
"github.com/kyleconroy/sqlc/internal/mysql"

"github.com/davecgh/go-spew/spew"
pg "github.com/lfittl/pg_query_go"
Expand All @@ -21,6 +22,7 @@ import (
func Do(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int {
rootCmd := &cobra.Command{Use: "sqlc", SilenceUsage: true}
rootCmd.AddCommand(checkCmd)
rootCmd.AddCommand(unstable__mysql)
rootCmd.AddCommand(genCmd)
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(parseCmd)
Expand Down Expand Up @@ -153,7 +155,7 @@ var genCmd = &cobra.Command{
continue
}

q, err := dinosql.ParseQueries(c, settings, pkg)
q, err := dinosql.ParseQueries(c, pkg)
if err != nil {
fmt.Fprintf(os.Stderr, "# package %s\n", name)
if parserErr, ok := err.(*dinosql.ParserErr); ok {
Expand All @@ -167,7 +169,7 @@ var genCmd = &cobra.Command{
continue
}

files, err := dinosql.Generate(q, settings, pkg)
files, err := dinosql.Generate(q, settings)
if err != nil {
fmt.Fprintf(os.Stderr, "# package %s\n", name)
fmt.Fprintf(os.Stderr, "error generating code: %s\n", err)
Expand Down Expand Up @@ -199,13 +201,13 @@ var checkCmd = &cobra.Command{
Use: "compile",
Short: "Statically check SQL for syntax and type errors",
RunE: func(cmd *cobra.Command, args []string) error {
blob, err := ioutil.ReadFile("sqlc.json")
file, err := os.Open("sqlc.json")
if err != nil {
return err
}

var settings dinosql.GenerateSettings
if err := json.Unmarshal(blob, &settings); err != nil {
settings, err := dinosql.ParseConfig(file)
if err != nil {
return err
}

Expand All @@ -214,9 +216,40 @@ var checkCmd = &cobra.Command{
if err != nil {
return err
}
if _, err := dinosql.ParseQueries(c, settings, pkg); err != nil {
if _, err := dinosql.ParseQueries(c, pkg); err != nil {
return err
}
}
return nil
},
}

var unstable__mysql = &cobra.Command{
Use: "unstable__mysql generate",
Short: "Generate MySQL Queries into typesafe Go code",
RunE: func(cmd *cobra.Command, args []string) error {
file, err := os.Open("sqlc.json")
if err != nil {
return err
}

settings, err := dinosql.ParseConfig(file)
if err != nil {
return err
}

for _, pkg := range settings.Packages {
res, err := mysql.GeneratePkg(pkg.Name, pkg.Queries, settings)
if err != nil {
return err
}
for filename, source := range res {
os.MkdirAll(filepath.Dir(filename), 0755)
if err := ioutil.WriteFile(filename, []byte(source), 0644); err != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", filename, err)
os.Exit(1)
}
}
}
return nil
},
Expand Down
1 change: 0 additions & 1 deletion internal/dinosql/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
func TestFuncs(t *testing.T) {
_, err := ParseQueries(
pg.NewCatalog(),
GenerateSettings{},
PackageSettings{
Queries: filepath.Join("testdata", "funcs"),
},
Expand Down
42 changes: 37 additions & 5 deletions internal/dinosql/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,27 @@ import (
"github.com/kyleconroy/sqlc/internal/pg"
)

const errMessageNoVersion = `The configuration file must have a version number.
Set the version to 1 at the top of sqlc.json:

{
"version": "1"
...
}
`

const errMessageUnknownVersion = `The configuration file has an invalid version number.
The only supported version is "1".
`

const errMessageNoPackages = `No packages are configured`

type GenerateSettings struct {
Version string `json:"version"`
Packages []PackageSettings `json:"packages"`
Overrides []Override `json:"overrides,omitempty"`
Rename map[string]string `json:"rename,omitempty"`
Version string `json:"version"`
Packages []PackageSettings `json:"packages"`
Overrides []Override `json:"overrides,omitempty"`
Rename map[string]string `json:"rename,omitempty"`
PackageMap map[string]PackageSettings
}

type PackageSettings struct {
Expand Down Expand Up @@ -125,5 +141,21 @@ func ParseConfig(rd io.Reader) (GenerateSettings, error) {
}
}
}
return config, nil
err := config.PopulatePkgMap()

return config, err
}

func (s *GenerateSettings) PopulatePkgMap() error {
packageMap := make(map[string]PackageSettings)

for _, c := range s.Packages {
if c.Name == "" {
panic("Package name must be specified in sqlc.json")
}
packageMap[c.Name] = c
}
s.PackageMap = packageMap

return nil
}
Loading