Skip to content

Commit a762b00

Browse files
committed
Clean up some code
1 parent 5cfa3fc commit a762b00

File tree

8 files changed

+31
-45
lines changed

8 files changed

+31
-45
lines changed

internal/analyzer/analyzer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ type Analysis struct {
4242

4343
type Analyzer interface {
4444
Analyze(context.Context, ast.Node, string, []string, *named.ParamSet) (*Analysis, error)
45+
Close(context.Context) error
4546
}

internal/cmd/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ func remoteGenerate(ctx context.Context, configPath string, conf *config.Config,
336336
func parse(ctx context.Context, name, dir string, sql config.SQL, combo config.CombinedSettings, parserOpts opts.Parser, stderr io.Writer) (*compiler.Result, bool) {
337337
defer trace.StartRegion(ctx, "parse").End()
338338
c, err := compiler.NewCompiler(sql, combo)
339+
defer c.Close(ctx)
339340
if err != nil {
340341
fmt.Fprintf(stderr, "error creating compiler: %s\n", err)
341342
return nil, true

internal/compiler/analyze.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,6 @@ func combineAnalysis(prev *analysis, a *analyzer.Analysis) *analysis {
5555
Column: convertColumn(*p.Column),
5656
})
5757
}
58-
if prev == nil {
59-
// TODO: NO QUERY
60-
return &analysis{
61-
Columns: cols,
62-
Parameters: params,
63-
}
64-
}
65-
6658
if len(prev.Columns) == len(cols) {
6759
for i := range prev.Columns {
6860
prev.Columns[i].DataType = cols[i].DataType

internal/compiler/engine.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package compiler
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/sqlc-dev/sqlc/internal/analyzer"
@@ -79,3 +80,9 @@ func (c *Compiler) ParseQueries(queries []string, o opts.Parser) error {
7980
func (c *Compiler) Result() *Result {
8081
return c.result
8182
}
83+
84+
func (c *Compiler) Close(ctx context.Context) {
85+
if c.analyzer != nil {
86+
c.analyzer.Close(ctx)
87+
}
88+
}

internal/compiler/expand.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,6 @@ func (c *Compiler) expandStmt(qc *QueryCatalog, raw *ast.RawStmt, node ast.Node)
8484
return nil, err
8585
}
8686

87-
// If we can't find any tables and the parent of the current node is the
88-
// root statement, use the generated output columns as the expand output.
89-
// FIXME: This doesn't work! We'd have to split the asterisks, etc.
90-
// if len(tables) == 0 && node == raw.Stmt {
91-
// tables = append(tables, &Table{
92-
// Rel: &ast.TableName{
93-
// Name: "?table?",
94-
// },
95-
// Columns: a.Columns,
96-
// })
97-
// }
98-
9987
var targets *ast.List
10088
switch n := node.(type) {
10189
case *ast.DeleteStmt:

internal/compiler/parse.go

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/sqlc-dev/sqlc/internal/source"
1313
"github.com/sqlc-dev/sqlc/internal/sql/ast"
1414
"github.com/sqlc-dev/sqlc/internal/sql/astutils"
15-
"github.com/sqlc-dev/sqlc/internal/sql/named"
1615
"github.com/sqlc-dev/sqlc/internal/sql/validate"
1716
)
1817

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

55-
var analysis *analysis
54+
var anlys *analysis
5655
if c.analyzer != nil {
57-
// We expect inference to fail sometimes
5856
// TODO: Handle panics
5957
inference, _ := c.inferQuery(raw, rawSQL)
60-
61-
query := rawSQL
62-
var ps *named.ParamSet
63-
if inference != nil {
64-
if inference.Query != "" {
65-
query = inference.Query
66-
}
67-
if inference.Named != nil {
68-
ps = inference.Named
69-
}
58+
if inference == nil {
59+
inference = &analysis{}
60+
}
61+
if inference.Query == "" {
62+
inference.Query = rawSQL
7063
}
7164

72-
result, err := c.analyzer.Analyze(ctx, raw, query, c.schema, ps)
65+
result, err := c.analyzer.Analyze(ctx, raw, inference.Query, c.schema, inference.Named)
7366
if err != nil {
7467
return nil, err
7568
}
7669

77-
// FOOTGUN: Careful, combine mutates inference
78-
analysis = combineAnalysis(inference, result)
70+
// FOOTGUN: combineAnalysis mutates inference
71+
anlys = combineAnalysis(inference, result)
7972
} else {
80-
analysis, err = c.analyzeQuery(raw, rawSQL)
73+
anlys, err = c.analyzeQuery(raw, rawSQL)
8174
if err != nil {
8275
return nil, err
8376
}
8477
}
8578

86-
expanded := analysis.Query
79+
expanded := anlys.Query
8780

8881
// If the query string was edited, make sure the syntax is valid
8982
if expanded != rawSQL {
9083
if _, err := c.parser.Parse(strings.NewReader(expanded)); err != nil {
91-
fmt.Println(expanded)
9284
return nil, fmt.Errorf("edited query syntax is invalid: %w", err)
9385
}
9486
}
@@ -109,10 +101,10 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
109101
Comments: comments,
110102
Name: name,
111103
Flags: flags,
112-
Params: analysis.Parameters,
113-
Columns: analysis.Columns,
104+
Params: anlys.Parameters,
105+
Columns: anlys.Columns,
114106
SQL: trimmed,
115-
InsertIntoTable: analysis.Table,
107+
InsertIntoTable: anlys.Table,
116108
}, nil
117109
}
118110

internal/endtoend/endtoend_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func TestReplay(t *testing.T) {
145145
}
146146

147147
t.Run(filepath.Join(name, tc), func(t *testing.T) {
148-
// t.Parallel()
148+
t.Parallel()
149149
var stderr bytes.Buffer
150150
var output map[string]string
151151
var err error

internal/engine/postgresql/analyzer/analyze.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,6 @@ func (a *Analyzer) Analyze(ctx context.Context, n ast.Node, query string, migrat
229229
}
230230
defer c.Release()
231231

232-
fmt.Println(query)
233-
234232
// TODO: Pick a random name
235233
desc, err := c.Conn().Prepare(ctx, "foo", query)
236234
if err != nil {
@@ -314,3 +312,10 @@ func (a *Analyzer) Analyze(ctx context.Context, n ast.Node, query string, migrat
314312

315313
return &result, nil
316314
}
315+
316+
func (a *Analyzer) Close(_ context.Context) error {
317+
if a.pool != nil {
318+
a.pool.Close()
319+
}
320+
return nil
321+
}

0 commit comments

Comments
 (0)