Skip to content

Second version of the configuration format #318

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 6 commits into from
Feb 10, 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
16 changes: 16 additions & 0 deletions examples/authors/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": "2",
"sql": [
{
"schema": "schema.sql",
"queries": "query.sql",
"engine": "postgresql",
"gen": {
"go": {
"package": "authors",
"out": "."
}
}
}
]
}
19 changes: 19 additions & 0 deletions examples/booktest/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "1",
"packages": [
{
"name": "booktest",
"path": "postgresql",
"schema": "postgresql/schema.sql",
"queries": "postgresql/query.sql",
"engine": "postgresql"
},
{
"name": "booktest",
"path": "mysql",
"schema": "mysql/schema.sql",
"queries": "mysql/query.sql",
"engine": "mysql"
}
]
}
12 changes: 12 additions & 0 deletions examples/jets/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": ".",
"name": "jets",
"schema": "schema.sql",
"queries": "query-building.sql",
"engine": "postgresql"
}
]
}
15 changes: 15 additions & 0 deletions examples/ondeck/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "1",
"packages": [
{
"path": ".",
"name": "ondeck",
"schema": "schema",
"queries": "query",
"engine": "postgresql",
"emit_json_tags": true,
"emit_prepared_queries": true,
"emit_interface": true
}
]
}
40 changes: 0 additions & 40 deletions examples/sqlc.json

This file was deleted.

25 changes: 7 additions & 18 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/spf13/cobra"

"github.com/kyleconroy/sqlc/internal/config"
"github.com/kyleconroy/sqlc/internal/dinosql"
)

// Do runs the command logic.
Expand Down Expand Up @@ -53,7 +52,7 @@ var initCmd = &cobra.Command{
if _, err := os.Stat("sqlc.json"); !os.IsNotExist(err) {
return nil
}
blob, err := json.MarshalIndent(config.GenerateSettings{Version: "1"}, "", " ")
blob, err := json.MarshalIndent(config.Config{Version: "1"}, "", " ")
if err != nil {
return err
}
Expand Down Expand Up @@ -91,24 +90,14 @@ var checkCmd = &cobra.Command{
Use: "compile",
Short: "Statically check SQL for syntax and type errors",
RunE: func(cmd *cobra.Command, args []string) error {
file, err := os.Open("sqlc.json")
if err != nil {
return err
}

settings, err := config.ParseConfig(file)
stderr := cmd.ErrOrStderr()
dir, err := os.Getwd()
if err != nil {
return err
fmt.Fprintln(stderr, "error parsing sqlc.json: file does not exist")
os.Exit(1)
}

for _, pkg := range settings.Packages {
c, err := dinosql.ParseCatalog(pkg.Schema)
if err != nil {
return err
}
if _, err := dinosql.ParseQueries(c, pkg); err != nil {
return err
}
if _, err := Generate(dir, stderr); err != nil {
os.Exit(1)
}
return nil
},
Expand Down
23 changes: 11 additions & 12 deletions internal/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Generate(dir string, stderr io.Writer) (map[string]string, error) {
return nil, err
}

settings, err := config.ParseConfig(bytes.NewReader(blob))
conf, err := config.ParseConfig(bytes.NewReader(blob))
if err != nil {
switch err {
case config.ErrMissingVersion:
Expand All @@ -57,20 +57,19 @@ func Generate(dir string, stderr io.Writer) (map[string]string, error) {
output := map[string]string{}
errored := false

for _, pkg := range settings.Packages {
name := pkg.Name
combo := config.Combine(settings, pkg)
for _, sql := range conf.SQL {
combo := config.Combine(conf, sql)
name := combo.Go.Package
var result dinosql.Generateable

// TODO: This feels like a hack that will bite us later
pkg.Schema = filepath.Join(dir, pkg.Schema)
pkg.Queries = filepath.Join(dir, pkg.Queries)

switch pkg.Engine {
sql.Schema = filepath.Join(dir, sql.Schema)
sql.Queries = filepath.Join(dir, sql.Queries)

switch sql.Engine {
case config.EngineMySQL:
// Experimental MySQL support
q, err := mysql.GeneratePkg(name, pkg.Schema, pkg.Queries, combo)
q, err := mysql.GeneratePkg(name, sql.Schema, sql.Queries, combo)
if err != nil {
fmt.Fprintf(stderr, "# package %s\n", name)
if parserErr, ok := err.(*dinosql.ParserErr); ok {
Expand All @@ -86,7 +85,7 @@ func Generate(dir string, stderr io.Writer) (map[string]string, error) {
result = q

case config.EnginePostgreSQL:
c, err := dinosql.ParseCatalog(pkg.Schema)
c, err := dinosql.ParseCatalog(sql.Schema)
if err != nil {
fmt.Fprintf(stderr, "# package %s\n", name)
if parserErr, ok := err.(*dinosql.ParserErr); ok {
Expand All @@ -100,7 +99,7 @@ func Generate(dir string, stderr io.Writer) (map[string]string, error) {
continue
}

q, err := dinosql.ParseQueries(c, pkg)
q, err := dinosql.ParseQueries(c, sql)
if err != nil {
fmt.Fprintf(stderr, "# package %s\n", name)
if parserErr, ok := err.(*dinosql.ParserErr); ok {
Expand All @@ -126,7 +125,7 @@ func Generate(dir string, stderr io.Writer) (map[string]string, error) {
}

for n, source := range files {
filename := filepath.Join(dir, pkg.Path, n)
filename := filepath.Join(dir, combo.Go.Out, n)
output[filename] = source
}
}
Expand Down
Loading