-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathanalyzer.go
More file actions
130 lines (111 loc) · 3.4 KB
/
analyzer.go
File metadata and controls
130 lines (111 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package analyzer
import (
"context"
"encoding/json"
"fmt"
"hash/fnv"
"log/slog"
"os"
"path/filepath"
"google.golang.org/protobuf/proto"
"github.com/sqlc-dev/sqlc/internal/analysis"
"github.com/sqlc-dev/sqlc/internal/cache"
"github.com/sqlc-dev/sqlc/internal/config"
"github.com/sqlc-dev/sqlc/internal/info"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/named"
)
type CachedAnalyzer struct {
a Analyzer
config config.Config
configBytes []byte
db config.Database
}
func Cached(a Analyzer, c config.Config, db config.Database) *CachedAnalyzer {
return &CachedAnalyzer{
a: a,
config: c,
db: db,
}
}
// Create a new error here
func (c *CachedAnalyzer) Analyze(ctx context.Context, n ast.Node, q string, schema []string, np *named.ParamSet) (*analysis.Analysis, error) {
result, rerun, err := c.analyze(ctx, n, q, schema, np)
if rerun {
if err != nil {
slog.Warn("first analysis failed with error", "err", err)
}
return c.a.Analyze(ctx, n, q, schema, np)
}
return result, err
}
func (c *CachedAnalyzer) analyze(ctx context.Context, n ast.Node, q string, schema []string, np *named.ParamSet) (*analysis.Analysis, bool, error) {
// Only cache queries for managed databases. We can't be certain the
// database is in an unchanged state otherwise
if !c.db.Managed {
return nil, true, nil
}
dir, err := cache.AnalysisDir()
if err != nil {
return nil, true, err
}
if c.configBytes == nil {
c.configBytes, err = json.Marshal(c.config)
if err != nil {
return nil, true, err
}
}
// Calculate cache key
h := fnv.New64()
h.Write([]byte(info.Version))
h.Write(c.configBytes)
for _, m := range schema {
h.Write([]byte(m))
}
h.Write([]byte(q))
key := fmt.Sprintf("%x", h.Sum(nil))
path := filepath.Join(dir, key)
if _, err := os.Stat(path); err == nil {
contents, err := os.ReadFile(path)
if err != nil {
return nil, true, err
}
var a analysis.Analysis
if err := proto.Unmarshal(contents, &a); err != nil {
return nil, true, err
}
return &a, false, nil
}
result, err := c.a.Analyze(ctx, n, q, schema, np)
if err == nil {
contents, err := proto.Marshal(result)
if err != nil {
slog.Warn("unable to marshal analysis", "err", err)
return result, false, nil
}
if err := os.WriteFile(path, contents, 0644); err != nil {
slog.Warn("saving analysis to disk failed", "err", err)
return result, false, nil
}
}
return result, false, err
}
func (c *CachedAnalyzer) Close(ctx context.Context) error {
return c.a.Close(ctx)
}
func (c *CachedAnalyzer) EnsureConn(ctx context.Context, migrations []string) error {
return c.a.EnsureConn(ctx, migrations)
}
func (c *CachedAnalyzer) GetColumnNames(ctx context.Context, query string) ([]string, error) {
return c.a.GetColumnNames(ctx, query)
}
type Analyzer interface {
Analyze(context.Context, ast.Node, string, []string, *named.ParamSet) (*analysis.Analysis, error)
Close(context.Context) error
// EnsureConn initializes the database connection with the given migrations.
// This is required for database-only mode where we need to connect before analyzing queries.
EnsureConn(ctx context.Context, migrations []string) error
// GetColumnNames returns the column names for a query by preparing it against the database.
// This is used for star expansion in database-only mode.
GetColumnNames(ctx context.Context, query string) ([]string, error)
}