-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathengine.go
More file actions
188 lines (169 loc) · 6.04 KB
/
engine.go
File metadata and controls
188 lines (169 loc) · 6.04 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package compiler
import (
"context"
"fmt"
"github.com/sqlc-dev/sqlc/internal/analyzer"
"github.com/sqlc-dev/sqlc/internal/config"
"github.com/sqlc-dev/sqlc/internal/dbmanager"
"github.com/sqlc-dev/sqlc/internal/engine"
"github.com/sqlc-dev/sqlc/internal/engine/dolphin"
"github.com/sqlc-dev/sqlc/internal/engine/plugin"
"github.com/sqlc-dev/sqlc/internal/engine/postgresql"
pganalyze "github.com/sqlc-dev/sqlc/internal/engine/postgresql/analyzer"
"github.com/sqlc-dev/sqlc/internal/engine/sqlite"
sqliteanalyze "github.com/sqlc-dev/sqlc/internal/engine/sqlite/analyzer"
"github.com/sqlc-dev/sqlc/internal/opts"
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
"github.com/sqlc-dev/sqlc/internal/x/expander"
)
type Compiler struct {
conf config.SQL
combo config.CombinedSettings
catalog *catalog.Catalog
parser Parser
result *Result
analyzer analyzer.Analyzer
client dbmanager.Client
selector selector
schema []string
// databaseOnlyMode indicates that the compiler should use database-only analysis
// and skip building the internal catalog from schema files (analyzer.database: only)
databaseOnlyMode bool
// expander is used to expand SELECT * and RETURNING * in database-only mode
expander *expander.Expander
}
func NewCompiler(conf config.SQL, combo config.CombinedSettings, parserOpts opts.Parser) (*Compiler, error) {
c := &Compiler{conf: conf, combo: combo}
if conf.Database != nil && conf.Database.Managed {
client := dbmanager.NewClient(combo.Global.Servers)
c.client = client
}
// Check for database-only mode (analyzer.database: only)
// This feature requires the analyzerv2 experiment to be enabled
databaseOnlyMode := conf.Analyzer.Database.IsOnly() && parserOpts.Experiment.AnalyzerV2
switch conf.Engine {
case config.EngineSQLite:
parser := sqlite.NewParser()
c.parser = parser
c.catalog = sqlite.NewCatalog()
c.selector = newSQLiteSelector()
if databaseOnlyMode {
// Database-only mode requires a database connection
if conf.Database == nil {
return nil, fmt.Errorf("analyzer.database: only requires database configuration")
}
if conf.Database.URI == "" && !conf.Database.Managed {
return nil, fmt.Errorf("analyzer.database: only requires database.uri or database.managed")
}
c.databaseOnlyMode = true
// Create the SQLite analyzer (implements Analyzer interface)
sqliteAnalyzer := sqliteanalyze.New(*conf.Database)
c.analyzer = analyzer.Cached(sqliteAnalyzer, combo.Global, *conf.Database)
// Create the expander using the analyzer as the column getter
c.expander = expander.New(c.analyzer, parser, parser)
} else if conf.Database != nil {
if conf.Analyzer.Database.IsEnabled() {
c.analyzer = analyzer.Cached(
sqliteanalyze.New(*conf.Database),
combo.Global,
*conf.Database,
)
}
}
case config.EngineMySQL:
c.parser = dolphin.NewParser()
c.catalog = dolphin.NewCatalog()
c.selector = newDefaultSelector()
case config.EnginePostgreSQL:
parser := postgresql.NewParser()
c.parser = parser
c.catalog = postgresql.NewCatalog()
c.selector = newDefaultSelector()
if databaseOnlyMode {
// Database-only mode requires a database connection
if conf.Database == nil {
return nil, fmt.Errorf("analyzer.database: only requires database configuration")
}
if conf.Database.URI == "" && !conf.Database.Managed {
return nil, fmt.Errorf("analyzer.database: only requires database.uri or database.managed")
}
c.databaseOnlyMode = true
// Create the PostgreSQL analyzer (implements Analyzer interface)
pgAnalyzer := pganalyze.New(c.client, *conf.Database)
c.analyzer = analyzer.Cached(pgAnalyzer, combo.Global, *conf.Database)
// Create the expander using the analyzer as the column getter
c.expander = expander.New(c.analyzer, parser, parser)
} else if conf.Database != nil {
if conf.Analyzer.Database.IsEnabled() {
c.analyzer = analyzer.Cached(
pganalyze.New(c.client, *conf.Database),
combo.Global,
*conf.Database,
)
}
}
default:
// Check if this is a plugin engine
if enginePlugin, found := config.FindEnginePlugin(&combo.Global, string(conf.Engine)); found {
eng, err := createPluginEngine(enginePlugin, combo.Dir)
if err != nil {
return nil, err
}
c.parser = eng.Parser()
c.catalog = eng.Catalog()
sel := eng.Selector()
if sel != nil {
c.selector = &engineSelectorAdapter{sel}
} else {
c.selector = newDefaultSelector()
}
} else {
return nil, fmt.Errorf("unknown engine: %s\n\nTo use a custom database engine, add it to the 'engines' section of sqlc.yaml:\n\n engines:\n - name: %s\n process:\n cmd: sqlc-engine-%s\n\nThen install the plugin: go install github.com/example/sqlc-engine-%s@latest",
conf.Engine, conf.Engine, conf.Engine, conf.Engine)
}
}
return c, nil
}
// createPluginEngine creates an engine from an engine plugin configuration.
func createPluginEngine(ep *config.EnginePlugin, dir string) (engine.Engine, error) {
switch {
case ep.Process != nil:
return plugin.NewPluginEngine(ep.Name, ep.Process.Cmd, dir, ep.Env), nil
case ep.WASM != nil:
return plugin.NewWASMPluginEngine(ep.Name, ep.WASM.URL, ep.WASM.SHA256, ep.Env), nil
default:
return nil, fmt.Errorf("engine plugin %s has no process or wasm configuration", ep.Name)
}
}
// engineSelectorAdapter adapts engine.Selector to the compiler's selector interface.
type engineSelectorAdapter struct {
sel engine.Selector
}
func (a *engineSelectorAdapter) ColumnExpr(name string, column *Column) string {
return a.sel.ColumnExpr(name, column.DataType)
}
func (c *Compiler) Catalog() *catalog.Catalog {
return c.catalog
}
func (c *Compiler) ParseCatalog(schema []string) error {
return c.parseCatalog(schema)
}
func (c *Compiler) ParseQueries(queries []string, o opts.Parser) error {
r, err := c.parseQueries(o)
if err != nil {
return err
}
c.result = r
return nil
}
func (c *Compiler) Result() *Result {
return c.result
}
func (c *Compiler) Close(ctx context.Context) {
if c.analyzer != nil {
c.analyzer.Close(ctx)
}
if c.client != nil {
c.client.Close(ctx)
}
}