Skip to content

Commit 3f2c8c8

Browse files
committed
A list of paths can be past in for schemas and queries
1 parent 575ddd4 commit 3f2c8c8

8 files changed

Lines changed: 144 additions & 90 deletions

File tree

internal/cmd/generate.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,17 @@ func Generate(dir string, stderr io.Writer) (map[string]string, error) {
113113
var result dinosql.Generateable
114114

115115
// TODO: This feels like a hack that will bite us later
116-
sql.Schema = filepath.Join(dir, sql.Schema)
117-
sql.Queries = filepath.Join(dir, sql.Queries)
116+
joined := make([]string, 0, len(sql.Schema))
117+
for _, s := range sql.Schema {
118+
joined = append(joined, filepath.Join(dir, s))
119+
}
120+
sql.Schema = joined
121+
122+
joined = make([]string, 0, len(sql.Queries))
123+
for _, q := range sql.Queries {
124+
joined = append(joined, filepath.Join(dir, q))
125+
}
126+
sql.Queries = joined
118127

119128
var name string
120129
parseOpts := dinosql.ParserOpts{}

internal/compiler/compile.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,15 @@ func Run(conf config.SQL, combo config.CombinedSettings) (*Result, error) {
7171
return nil, fmt.Errorf("unknown engine: %s", conf.Engine)
7272
}
7373

74-
rd, err := os.Open(conf.Schema)
75-
if err != nil {
76-
return nil, err
74+
blobs := make([]io.Reader, 0, len(conf.Schema))
75+
for _, s := range conf.Schema {
76+
b, err := os.Open(s)
77+
if err != nil {
78+
return nil, err
79+
}
80+
blobs = append(blobs, b)
7781
}
82+
rd := io.MultiReader(blobs...)
7883

7984
stmts, err := p.Parse(rd)
8085
if err != nil {

internal/config/config.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"go/types"
@@ -35,6 +36,41 @@ type versionSetting struct {
3536

3637
type Engine string
3738

39+
type Paths []string
40+
41+
func (p *Paths) UnmarshalJSON(data []byte) error {
42+
if string(data[0]) == `[` {
43+
var out []string
44+
if err := json.Unmarshal(data, &out); err != nil {
45+
return nil
46+
}
47+
*p = Paths(out)
48+
return nil
49+
}
50+
var out string
51+
if err := json.Unmarshal(data, &out); err != nil {
52+
return nil
53+
}
54+
*p = Paths([]string{out})
55+
return nil
56+
}
57+
58+
func (p *Paths) UnmarshalYAML(unmarshal func(interface{}) error) error {
59+
out := []string{}
60+
println("hey gang")
61+
if sliceErr := unmarshal(&out); sliceErr != nil {
62+
println("hey gang 2")
63+
var ele string
64+
if strErr := unmarshal(&ele); strErr != nil {
65+
return strErr
66+
}
67+
out = []string{ele}
68+
}
69+
70+
*p = Paths(out)
71+
return nil
72+
}
73+
3874
const (
3975
EngineMySQL Engine = "mysql"
4076
EnginePostgreSQL Engine = "postgresql"
@@ -67,8 +103,8 @@ type GenKotlin struct {
67103

68104
type SQL struct {
69105
Engine Engine `json:"engine,omitempty" yaml:"engine"`
70-
Schema string `json:"schema" yaml:"schema"`
71-
Queries string `json:"queries" yaml:"queries"`
106+
Schema Paths `json:"schema" yaml:"schema"`
107+
Queries Paths `json:"queries" yaml:"queries"`
72108
Gen SQLGen `json:"gen" yaml:"gen"`
73109
}
74110

internal/config/v_one.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ type v1PackageSettings struct {
1919
Name string `json:"name" yaml:"name"`
2020
Engine Engine `json:"engine,omitempty" yaml:"engine"`
2121
Path string `json:"path" yaml:"path"`
22-
Schema string `json:"schema" yaml:"schema"`
23-
Queries string `json:"queries" yaml:"queries"`
22+
Schema Paths `json:"schema" yaml:"schema"`
23+
Queries Paths `json:"queries" yaml:"queries"`
2424
EmitInterface bool `json:"emit_interface" yaml:"emit_interface"`
2525
EmitJSONTags bool `json:"emit_json_tags" yaml:"emit_json_tags"`
2626
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`

internal/dinosql/migrations_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func TestRemoveGolangMigrateRollback(t *testing.T) {
6868
"testdata/migrations/foo.sql",
6969
}
7070

71-
got, err := ReadSQLFiles("./testdata/migrations")
71+
got, err := ReadSQLFiles([]string{"./testdata/migrations"})
7272
if err != nil {
7373
t.Fatal(err)
7474
}

internal/dinosql/parser.go

Lines changed: 81 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -59,44 +59,46 @@ func (e *ParserErr) Error() string {
5959
return fmt.Sprintf("multiple errors: %d errors", len(e.Errs))
6060
}
6161

62-
func ReadSQLFiles(path string) ([]string, error) {
63-
f, err := os.Stat(path)
64-
if err != nil {
65-
return nil, fmt.Errorf("path %s does not exist", path)
66-
}
67-
68-
var files []string
69-
if f.IsDir() {
70-
listing, err := ioutil.ReadDir(path)
62+
func ReadSQLFiles(paths []string) ([]string, error) {
63+
var sql []string
64+
for _, path := range paths {
65+
f, err := os.Stat(path)
7166
if err != nil {
72-
return nil, err
67+
return nil, fmt.Errorf("path %s does not exist", path)
7368
}
74-
for _, f := range listing {
75-
files = append(files, filepath.Join(path, f.Name()))
76-
}
77-
} else {
78-
files = append(files, path)
79-
}
8069

81-
var sql []string
82-
for _, filename := range files {
83-
if !strings.HasSuffix(filename, ".sql") {
84-
continue
85-
}
86-
if strings.HasPrefix(filepath.Base(filename), ".") {
87-
continue
70+
var files []string
71+
if f.IsDir() {
72+
listing, err := ioutil.ReadDir(path)
73+
if err != nil {
74+
return nil, err
75+
}
76+
for _, f := range listing {
77+
files = append(files, filepath.Join(path, f.Name()))
78+
}
79+
} else {
80+
files = append(files, path)
8881
}
89-
// Remove golang-migrate rollback files.
90-
if strings.HasSuffix(filename, ".down.sql") {
91-
continue
82+
83+
for _, filename := range files {
84+
if !strings.HasSuffix(filename, ".sql") {
85+
continue
86+
}
87+
if strings.HasPrefix(filepath.Base(filename), ".") {
88+
continue
89+
}
90+
// Remove golang-migrate rollback files.
91+
if strings.HasSuffix(filename, ".down.sql") {
92+
continue
93+
}
94+
sql = append(sql, filename)
9295
}
93-
sql = append(sql, filename)
9496
}
9597
return sql, nil
9698
}
9799

98-
func ParseCatalog(schema string) (core.Catalog, error) {
99-
files, err := ReadSQLFiles(schema)
100+
func ParseCatalog(schemas []string) (core.Catalog, error) {
101+
files, err := ReadSQLFiles(schemas)
100102
if err != nil {
101103
return core.Catalog{}, err
102104
}
@@ -197,73 +199,75 @@ type ParserOpts struct {
197199
UsePositionalParameters bool
198200
}
199201

200-
func ParseQueries(c core.Catalog, queries string, opts ParserOpts) (*Result, error) {
201-
f, err := os.Stat(queries)
202-
if err != nil {
203-
return nil, fmt.Errorf("path %s does not exist", queries)
204-
}
205-
206-
var files []string
207-
if f.IsDir() {
208-
listing, err := ioutil.ReadDir(queries)
209-
if err != nil {
210-
return nil, err
211-
}
212-
for _, f := range listing {
213-
files = append(files, filepath.Join(queries, f.Name()))
214-
}
215-
} else {
216-
files = append(files, queries)
217-
}
218-
202+
func ParseQueries(c core.Catalog, queriesPaths []string, opts ParserOpts) (*Result, error) {
219203
merr := NewParserErr()
220204
var q []*Query
221-
set := map[string]struct{}{}
222-
for _, filename := range files {
223-
if !strings.HasSuffix(filename, ".sql") {
224-
continue
225-
}
226-
if strings.HasPrefix(filepath.Base(filename), ".") {
227-
continue
228-
}
229-
blob, err := ioutil.ReadFile(filename)
205+
for _, queries := range queriesPaths {
206+
f, err := os.Stat(queries)
230207
if err != nil {
231-
merr.Add(filename, "", 0, err)
232-
continue
208+
return nil, fmt.Errorf("path %s does not exist", queries)
233209
}
234-
source := string(blob)
235-
tree, err := pg.Parse(source)
236-
if err != nil {
237-
merr.Add(filename, source, 0, err)
238-
continue
210+
211+
var files []string
212+
if f.IsDir() {
213+
listing, err := ioutil.ReadDir(queries)
214+
if err != nil {
215+
return nil, err
216+
}
217+
for _, f := range listing {
218+
files = append(files, filepath.Join(queries, f.Name()))
219+
}
220+
} else {
221+
files = append(files, queries)
239222
}
240-
for _, stmt := range tree.Statements {
241-
query, err := parseQuery(c, stmt, source, opts.UsePositionalParameters)
242-
if err == errUnsupportedStatementType {
223+
224+
set := map[string]struct{}{}
225+
for _, filename := range files {
226+
if !strings.HasSuffix(filename, ".sql") {
227+
continue
228+
}
229+
if strings.HasPrefix(filepath.Base(filename), ".") {
230+
continue
231+
}
232+
blob, err := ioutil.ReadFile(filename)
233+
if err != nil {
234+
merr.Add(filename, "", 0, err)
243235
continue
244236
}
237+
source := string(blob)
238+
tree, err := pg.Parse(source)
245239
if err != nil {
246-
merr.Add(filename, source, location(stmt), err)
240+
merr.Add(filename, source, 0, err)
247241
continue
248242
}
249-
if query.Name != "" {
250-
if _, exists := set[query.Name]; exists {
251-
merr.Add(filename, source, location(stmt), fmt.Errorf("duplicate query name: %s", query.Name))
243+
for _, stmt := range tree.Statements {
244+
query, err := parseQuery(c, stmt, source, opts.UsePositionalParameters)
245+
if err == errUnsupportedStatementType {
252246
continue
253247
}
254-
set[query.Name] = struct{}{}
255-
}
256-
query.Filename = filepath.Base(filename)
257-
if query != nil {
258-
q = append(q, query)
248+
if err != nil {
249+
merr.Add(filename, source, location(stmt), err)
250+
continue
251+
}
252+
if query.Name != "" {
253+
if _, exists := set[query.Name]; exists {
254+
merr.Add(filename, source, location(stmt), fmt.Errorf("duplicate query name: %s", query.Name))
255+
continue
256+
}
257+
set[query.Name] = struct{}{}
258+
}
259+
query.Filename = filepath.Base(filename)
260+
if query != nil {
261+
q = append(q, query)
262+
}
259263
}
260264
}
261265
}
262266
if len(merr.Errs) > 0 {
263267
return nil, merr
264268
}
265269
if len(q) == 0 {
266-
return nil, fmt.Errorf("path %s contains no queries", queries)
270+
return nil, fmt.Errorf("no queries contained in paths %s", strings.Join(queriesPaths, ","))
267271
}
268272
return &Result{
269273
Catalog: c,

internal/mysql/parse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type Column struct {
3030
Table string
3131
}
3232

33-
func parsePath(sqlPath string, generator PackageGenerator) (*Result, error) {
33+
func parsePath(sqlPath []string, generator PackageGenerator) (*Result, error) {
3434
files, err := dinosql.ReadSQLFiles(sqlPath)
3535
if err != nil {
3636
return nil, err
@@ -463,7 +463,7 @@ func (pGen PackageGenerator) parseSelectAliasExpr(exprs sqlparser.SelectExprs, t
463463
}
464464

465465
// GeneratePkg is the main entry to mysql generator package
466-
func GeneratePkg(pkgName, schemaPath, querysPath string, settings config.CombinedSettings) (*Result, error) {
466+
func GeneratePkg(pkgName string, schemaPath, querysPath []string, settings config.CombinedSettings) (*Result, error) {
467467
s := NewSchema()
468468
generator := PackageGenerator{
469469
Schema: s,

internal/sqltest/postgres.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func id() string {
2929
return string(b)
3030
}
3131

32-
func PostgreSQL(t *testing.T, migrations string) (*sql.DB, func()) {
32+
func PostgreSQL(t *testing.T, migrations []string) (*sql.DB, func()) {
3333
t.Helper()
3434

3535
pgUser := os.Getenv("PG_USER")

0 commit comments

Comments
 (0)