Skip to content

Commit

Permalink
parser: Use same function to load SQL files (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy authored May 12, 2020
1 parent cca78ef commit 40931af
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 56 deletions.
98 changes: 42 additions & 56 deletions internal/dinosql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,32 @@ func ReadSQLFiles(paths []string) ([]string, error) {
if err != nil {
return nil, fmt.Errorf("path %s does not exist", path)
}

if f.IsDir() {
listing, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
for _, f := range listing {
if migrations.IsDown(f.Name()) {
continue
}
files = append(files, filepath.Join(path, f.Name()))
}
} else {
if !migrations.IsDown(path) {
files = append(files, path)
}
files = append(files, path)
}
}
var sqlFiles []string
for _, file := range files {
if !strings.HasSuffix(file, ".sql") {
continue
}
if strings.HasPrefix(filepath.Base(file), ".") {
continue
}
if migrations.IsDown(filepath.Base(file)) {
continue
}
sqlFiles = append(sqlFiles, file)
}
return files, nil
return sqlFiles, nil
}

func ParseCatalog(schemas []string) (core.Catalog, error) {
Expand Down Expand Up @@ -193,64 +200,43 @@ type ParserOpts struct {
func ParseQueries(c core.Catalog, queriesPaths []string, opts ParserOpts) (*Result, error) {
merr := NewParserErr()
var q []*Query
for _, queries := range queriesPaths {
f, err := os.Stat(queries)

set := map[string]struct{}{}
files, err := ReadSQLFiles(queriesPaths)
if err != nil {
return nil, err
}
for _, filename := range files {
blob, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("path %s does not exist", queries)
merr.Add(filename, "", 0, err)
continue
}

var files []string
if f.IsDir() {
listing, err := ioutil.ReadDir(queries)
if err != nil {
return nil, err
}
for _, f := range listing {
files = append(files, filepath.Join(queries, f.Name()))
}
} else {
files = append(files, queries)
source := string(blob)
tree, err := pg.Parse(source)
if err != nil {
merr.Add(filename, source, 0, err)
continue
}

set := map[string]struct{}{}
for _, filename := range files {
if !strings.HasSuffix(filename, ".sql") {
continue
}
if strings.HasPrefix(filepath.Base(filename), ".") {
continue
}
blob, err := ioutil.ReadFile(filename)
if err != nil {
merr.Add(filename, "", 0, err)
for _, stmt := range tree.Statements {
query, err := parseQuery(c, stmt, source, opts.UsePositionalParameters)
if err == errUnsupportedStatementType {
continue
}
source := string(blob)
tree, err := pg.Parse(source)
if err != nil {
merr.Add(filename, source, 0, err)
merr.Add(filename, source, location(stmt), err)
continue
}
for _, stmt := range tree.Statements {
query, err := parseQuery(c, stmt, source, opts.UsePositionalParameters)
if err == errUnsupportedStatementType {
continue
}
if err != nil {
merr.Add(filename, source, location(stmt), err)
if query.Name != "" {
if _, exists := set[query.Name]; exists {
merr.Add(filename, source, location(stmt), fmt.Errorf("duplicate query name: %s", query.Name))
continue
}
if query.Name != "" {
if _, exists := set[query.Name]; exists {
merr.Add(filename, source, location(stmt), fmt.Errorf("duplicate query name: %s", query.Name))
continue
}
set[query.Name] = struct{}{}
}
query.Filename = filepath.Base(filename)
if query != nil {
q = append(q, query)
}
set[query.Name] = struct{}{}
}
query.Filename = filepath.Base(filename)
if query != nil {
q = append(q, query)
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/multischema/go/db.go

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

16 changes: 16 additions & 0 deletions internal/endtoend/testdata/multischema/go/models.go

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

62 changes: 62 additions & 0 deletions internal/endtoend/testdata/multischema/go/query.sql.go

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

5 changes: 5 additions & 0 deletions internal/endtoend/testdata/multischema/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE bar (id serial not null);
CREATE TABLE foo (id serial not null, bar serial references bar(id));

-- name: ListBar :many
SELECT * FROM bar;
3 changes: 3 additions & 0 deletions internal/endtoend/testdata/multischema/sql/ignore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hello!

This file should be ignored
2 changes: 2 additions & 0 deletions internal/endtoend/testdata/multischema/sql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: ListFoo :many
SELECT * FROM foo;
9 changes: 9 additions & 0 deletions internal/endtoend/testdata/multischema/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": "1",
"packages": [{
"path": "go",
"name": "querytest",
"schema": ["query.sql", "sql"],
"queries": ["query.sql", "sql"]
}]
}

0 comments on commit 40931af

Please sign in to comment.