Skip to content

Commit

Permalink
Clean up some code
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy committed Oct 10, 2023
1 parent 5cfa3fc commit a762b00
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 45 deletions.
1 change: 1 addition & 0 deletions internal/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ type Analysis struct {

type Analyzer interface {
Analyze(context.Context, ast.Node, string, []string, *named.ParamSet) (*Analysis, error)
Close(context.Context) error
}
1 change: 1 addition & 0 deletions internal/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ func remoteGenerate(ctx context.Context, configPath string, conf *config.Config,
func parse(ctx context.Context, name, dir string, sql config.SQL, combo config.CombinedSettings, parserOpts opts.Parser, stderr io.Writer) (*compiler.Result, bool) {
defer trace.StartRegion(ctx, "parse").End()
c, err := compiler.NewCompiler(sql, combo)
defer c.Close(ctx)
if err != nil {
fmt.Fprintf(stderr, "error creating compiler: %s\n", err)
return nil, true
Expand Down
8 changes: 0 additions & 8 deletions internal/compiler/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ func combineAnalysis(prev *analysis, a *analyzer.Analysis) *analysis {
Column: convertColumn(*p.Column),
})
}
if prev == nil {
// TODO: NO QUERY
return &analysis{
Columns: cols,
Parameters: params,
}
}

if len(prev.Columns) == len(cols) {
for i := range prev.Columns {
prev.Columns[i].DataType = cols[i].DataType
Expand Down
7 changes: 7 additions & 0 deletions internal/compiler/engine.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package compiler

import (
"context"
"fmt"

"github.com/sqlc-dev/sqlc/internal/analyzer"
Expand Down Expand Up @@ -79,3 +80,9 @@ func (c *Compiler) ParseQueries(queries []string, o opts.Parser) error {
func (c *Compiler) Result() *Result {
return c.result
}

func (c *Compiler) Close(ctx context.Context) {
if c.analyzer != nil {
c.analyzer.Close(ctx)
}
}
12 changes: 0 additions & 12 deletions internal/compiler/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,6 @@ func (c *Compiler) expandStmt(qc *QueryCatalog, raw *ast.RawStmt, node ast.Node)
return nil, err
}

// If we can't find any tables and the parent of the current node is the
// root statement, use the generated output columns as the expand output.
// FIXME: This doesn't work! We'd have to split the asterisks, etc.
// if len(tables) == 0 && node == raw.Stmt {
// tables = append(tables, &Table{
// Rel: &ast.TableName{
// Name: "?table?",
// },
// Columns: a.Columns,
// })
// }

var targets *ast.List
switch n := node.(type) {
case *ast.DeleteStmt:
Expand Down
36 changes: 14 additions & 22 deletions internal/compiler/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/sqlc-dev/sqlc/internal/source"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/astutils"
"github.com/sqlc-dev/sqlc/internal/sql/named"
"github.com/sqlc-dev/sqlc/internal/sql/validate"
)

Expand Down Expand Up @@ -52,43 +51,36 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
return nil, err
}

var analysis *analysis
var anlys *analysis
if c.analyzer != nil {
// We expect inference to fail sometimes
// TODO: Handle panics
inference, _ := c.inferQuery(raw, rawSQL)

query := rawSQL
var ps *named.ParamSet
if inference != nil {
if inference.Query != "" {
query = inference.Query
}
if inference.Named != nil {
ps = inference.Named
}
if inference == nil {
inference = &analysis{}
}
if inference.Query == "" {
inference.Query = rawSQL
}

result, err := c.analyzer.Analyze(ctx, raw, query, c.schema, ps)
result, err := c.analyzer.Analyze(ctx, raw, inference.Query, c.schema, inference.Named)
if err != nil {
return nil, err
}

// FOOTGUN: Careful, combine mutates inference
analysis = combineAnalysis(inference, result)
// FOOTGUN: combineAnalysis mutates inference
anlys = combineAnalysis(inference, result)
} else {
analysis, err = c.analyzeQuery(raw, rawSQL)
anlys, err = c.analyzeQuery(raw, rawSQL)
if err != nil {
return nil, err
}
}

expanded := analysis.Query
expanded := anlys.Query

// If the query string was edited, make sure the syntax is valid
if expanded != rawSQL {
if _, err := c.parser.Parse(strings.NewReader(expanded)); err != nil {
fmt.Println(expanded)
return nil, fmt.Errorf("edited query syntax is invalid: %w", err)
}
}
Expand All @@ -109,10 +101,10 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
Comments: comments,
Name: name,
Flags: flags,
Params: analysis.Parameters,
Columns: analysis.Columns,
Params: anlys.Parameters,
Columns: anlys.Columns,
SQL: trimmed,
InsertIntoTable: analysis.Table,
InsertIntoTable: anlys.Table,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/endtoend/endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestReplay(t *testing.T) {
}

t.Run(filepath.Join(name, tc), func(t *testing.T) {
// t.Parallel()
t.Parallel()
var stderr bytes.Buffer
var output map[string]string
var err error
Expand Down
9 changes: 7 additions & 2 deletions internal/engine/postgresql/analyzer/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,6 @@ func (a *Analyzer) Analyze(ctx context.Context, n ast.Node, query string, migrat
}
defer c.Release()

fmt.Println(query)

// TODO: Pick a random name
desc, err := c.Conn().Prepare(ctx, "foo", query)
if err != nil {
Expand Down Expand Up @@ -314,3 +312,10 @@ func (a *Analyzer) Analyze(ctx context.Context, n ast.Node, query string, migrat

return &result, nil
}

func (a *Analyzer) Close(_ context.Context) error {
if a.pool != nil {
a.pool.Close()
}
return nil
}

0 comments on commit a762b00

Please sign in to comment.