forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.go
More file actions
286 lines (257 loc) · 7.17 KB
/
generate.go
File metadata and controls
286 lines (257 loc) · 7.17 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package cmd
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime/trace"
"strings"
"github.com/kyleconroy/sqlc/internal/codegen/golang"
"github.com/kyleconroy/sqlc/internal/codegen/kotlin"
"github.com/kyleconroy/sqlc/internal/codegen/python"
"github.com/kyleconroy/sqlc/internal/compiler"
"github.com/kyleconroy/sqlc/internal/config"
"github.com/kyleconroy/sqlc/internal/debug"
"github.com/kyleconroy/sqlc/internal/multierr"
"github.com/kyleconroy/sqlc/internal/opts"
"github.com/kyleconroy/sqlc/internal/plugin"
)
const errMessageNoVersion = `The configuration file must have a version number.
Set the version to 1 at the top of sqlc.json:
{
"version": "1"
...
}
`
const errMessageUnknownVersion = `The configuration file has an invalid version number.
The only supported version is "1".
`
const errMessageNoPackages = `No packages are configured`
func printFileErr(stderr io.Writer, dir string, fileErr *multierr.FileError) {
filename := strings.TrimPrefix(fileErr.Filename, dir+"/")
fmt.Fprintf(stderr, "%s:%d:%d: %s\n", filename, fileErr.Line, fileErr.Column, fileErr.Err)
}
type outPair struct {
Gen config.SQLGen
config.SQL
}
func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer) (map[string]string, error) {
configPath := ""
if filename != "" {
configPath = filepath.Join(dir, filename)
} else {
var yamlMissing, jsonMissing bool
yamlPath := filepath.Join(dir, "sqlc.yaml")
jsonPath := filepath.Join(dir, "sqlc.json")
if _, err := os.Stat(yamlPath); os.IsNotExist(err) {
yamlMissing = true
}
if _, err := os.Stat(jsonPath); os.IsNotExist(err) {
jsonMissing = true
}
if yamlMissing && jsonMissing {
fmt.Fprintln(stderr, "error parsing configuration files. sqlc.yaml or sqlc.json: file does not exist")
return nil, errors.New("config file missing")
}
if !yamlMissing && !jsonMissing {
fmt.Fprintln(stderr, "error: both sqlc.json and sqlc.yaml files present")
return nil, errors.New("sqlc.json and sqlc.yaml present")
}
configPath = yamlPath
if yamlMissing {
configPath = jsonPath
}
}
base := filepath.Base(configPath)
blob, err := os.ReadFile(configPath)
if err != nil {
fmt.Fprintf(stderr, "error parsing %s: file does not exist\n", base)
return nil, err
}
conf, err := config.ParseConfig(bytes.NewReader(blob))
if err != nil {
switch err {
case config.ErrMissingVersion:
fmt.Fprintf(stderr, errMessageNoVersion)
case config.ErrUnknownVersion:
fmt.Fprintf(stderr, errMessageUnknownVersion)
case config.ErrNoPackages:
fmt.Fprintf(stderr, errMessageNoPackages)
}
fmt.Fprintf(stderr, "error parsing %s: %s\n", base, err)
return nil, err
}
if err := config.Validate(conf); err != nil {
fmt.Fprintf(stderr, "error validating %s: %s\n", base, err)
return nil, err
}
output := map[string]string{}
errored := false
var pairs []outPair
for _, sql := range conf.SQL {
if sql.Gen.Go != nil {
pairs = append(pairs, outPair{
SQL: sql,
Gen: config.SQLGen{Go: sql.Gen.Go},
})
}
if sql.Gen.Kotlin != nil {
pairs = append(pairs, outPair{
SQL: sql,
Gen: config.SQLGen{Kotlin: sql.Gen.Kotlin},
})
}
if sql.Gen.Python != nil {
if !e.ExperimentalFeatures {
fmt.Fprintf(stderr, "error parsing %s: unknown target langauge \"python\"\n", base)
return nil, fmt.Errorf("unknown target language \"python\"")
}
pairs = append(pairs, outPair{
SQL: sql,
Gen: config.SQLGen{Python: sql.Gen.Python},
})
}
}
for _, sql := range pairs {
combo := config.Combine(conf, sql.SQL)
// TODO: This feels like a hack that will bite us later
joined := make([]string, 0, len(sql.Schema))
for _, s := range sql.Schema {
joined = append(joined, filepath.Join(dir, s))
}
sql.Schema = joined
joined = make([]string, 0, len(sql.Queries))
for _, q := range sql.Queries {
joined = append(joined, filepath.Join(dir, q))
}
sql.Queries = joined
var name, lang string
parseOpts := opts.Parser{
Debug: debug.Debug,
}
if sql.Gen.Go != nil {
name = combo.Go.Package
lang = "golang"
} else if sql.Gen.Kotlin != nil {
if sql.Engine == config.EnginePostgreSQL {
parseOpts.UsePositionalParameters = true
}
lang = "kotlin"
name = combo.Kotlin.Package
} else if sql.Gen.Python != nil {
lang = "python"
name = combo.Python.Package
}
var packageRegion *trace.Region
if debug.Traced {
packageRegion = trace.StartRegion(ctx, "package")
trace.Logf(ctx, "", "name=%s dir=%s language=%s", name, dir, lang)
}
result, failed := parse(ctx, e, name, dir, sql.SQL, combo, parseOpts, stderr)
if failed {
if packageRegion != nil {
packageRegion.End()
}
errored = true
break
}
var region *trace.Region
if debug.Traced {
region = trace.StartRegion(ctx, "codegen")
}
var files map[string]string
var resp *plugin.CodeGenResponse
var out string
switch {
case sql.Gen.Go != nil:
out = combo.Go.Out
files, err = golang.Generate(result, combo)
case sql.Gen.Kotlin != nil:
out = combo.Kotlin.Out
files, err = kotlin.Generate(result, combo)
case sql.Gen.Python != nil:
out = combo.Python.Out
req := codeGenRequest(result, combo)
if os.Getenv("WASM") != "" {
resp, err = pythonGenerate(ctx, req)
} else {
blob, err := req.MarshalVT()
if err != nil {
log.Fatal(err)
}
var codeReq plugin.CodeGenRequest
if err := codeReq.UnmarshalVT(blob); err != nil {
log.Fatal(err)
}
resp, err = python.Generate(&codeReq)
}
default:
panic("missing language backend")
}
if region != nil {
region.End()
}
if resp != nil {
files = map[string]string{}
for _, file := range resp.Files {
files[file.Name] = string(file.Contents)
}
}
if err != nil {
fmt.Fprintf(stderr, "# package %s\n", name)
fmt.Fprintf(stderr, "error generating code: %s\n", err)
errored = true
if packageRegion != nil {
packageRegion.End()
}
continue
}
for n, source := range files {
filename := filepath.Join(dir, out, n)
output[filename] = source
}
if packageRegion != nil {
packageRegion.End()
}
}
if errored {
return nil, fmt.Errorf("errored")
}
return output, nil
}
func parse(ctx context.Context, e Env, name, dir string, sql config.SQL, combo config.CombinedSettings, parserOpts opts.Parser, stderr io.Writer) (*compiler.Result, bool) {
if debug.Traced {
defer trace.StartRegion(ctx, "parse").End()
}
c := compiler.NewCompiler(sql, combo)
if err := c.ParseCatalog(sql.Schema); err != nil {
fmt.Fprintf(stderr, "# package %s\n", name)
if parserErr, ok := err.(*multierr.Error); ok {
for _, fileErr := range parserErr.Errs() {
printFileErr(stderr, dir, fileErr)
}
} else {
fmt.Fprintf(stderr, "error parsing schema: %s\n", err)
}
return nil, true
}
if parserOpts.Debug.DumpCatalog {
debug.Dump(c.Catalog())
}
if err := c.ParseQueries(sql.Queries, parserOpts); err != nil {
fmt.Fprintf(stderr, "# package %s\n", name)
if parserErr, ok := err.(*multierr.Error); ok {
for _, fileErr := range parserErr.Errs() {
printFileErr(stderr, dir, fileErr)
}
} else {
fmt.Fprintf(stderr, "error parsing queries: %s\n", err)
}
return nil, true
}
return c.Result(), false
}