Skip to content

Commit 1aa6d83

Browse files
Merge branch 'kyleconroy:main' into allow-golang-slice-overrides
2 parents a007565 + 3b21fdf commit 1aa6d83

File tree

21 files changed

+375
-97
lines changed

21 files changed

+375
-97
lines changed

docs/reference/changelog.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4-
## [1.18.0](https://github.com/kyleconroy/sqlc/releases/tag/1.18.0)
4+
## [1.18.0](https://github.com/kyleconroy/sqlc/releases/tag/v1.18.0)
55
Released 2023-04-27
66

77
### Release notes
@@ -260,14 +260,14 @@ genreated method will use a argument struct.
260260
261261
- Upgrade to wasmtime v8.0.0 (#2222)
262262
263-
## [1.17.2](https://github.com/kyleconroy/sqlc/releases/tag/1.17.2)
263+
## [1.17.2](https://github.com/kyleconroy/sqlc/releases/tag/v1.17.2)
264264
Released 2023-02-22
265265
266266
### Bug Fixes
267267
268268
- Fix build on Windows (#2102)
269269
270-
## [1.17.1](https://github.com/kyleconroy/sqlc/releases/tag/1.17.1)
270+
## [1.17.1](https://github.com/kyleconroy/sqlc/releases/tag/v1.17.1)
271271
Released 2023-02-22
272272
273273
### Bug Fixes
@@ -284,7 +284,7 @@ Released 2023-02-22
284284
285285
- (deps) Bump golang from 1.20.0 to 1.20.1 (#2082)
286286
287-
## [1.17.0](https://github.com/kyleconroy/sqlc/releases/tag/1.17.0)
287+
## [1.17.0](https://github.com/kyleconroy/sqlc/releases/tag/v1.17.0)
288288
Released 2023-02-13
289289
290290
### Bug Fixes
@@ -360,7 +360,7 @@ Released 2023-02-13
360360
361361
- Upgrade to wasmtime 5.0.0 (#2065)
362362
363-
## [1.16.0](https://github.com/kyleconroy/sqlc/releases/tag/1.16.0)
363+
## [1.16.0](https://github.com/kyleconroy/sqlc/releases/tag/v1.16.0)
364364
Released 2022-11-09
365365
366366
@@ -432,7 +432,7 @@ Released 2022-11-09
432432
- Port all Python tests to sqlc-gen-python (#1907)
433433
- Upgrade to sqlc-gen-python v1.0.0 (#1932)
434434
435-
## [1.15.0](https://github.com/kyleconroy/sqlc/releases/tag/1.15.0)
435+
## [1.15.0](https://github.com/kyleconroy/sqlc/releases/tag/v1.15.0)
436436
Released 2022-08-07
437437
438438
### Bug Fixes
@@ -481,7 +481,7 @@ Released 2022-08-07
481481
- (wasm) Change default cache location (#1709)
482482
- (wasm) Change the SHA-256 config key (#1710)
483483
484-
## [1.14.0](https://github.com/kyleconroy/sqlc/releases/tag/1.14.0)
484+
## [1.14.0](https://github.com/kyleconroy/sqlc/releases/tag/v1.14.0)
485485
Released 2022-06-09
486486
487487
### Bug Fixes

examples/booktest/sqlite/query.sql.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/cmd/cmd.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import (
2626

2727
func init() {
2828
uploadCmd.Flags().BoolP("dry-run", "", false, "dump upload request (default: false)")
29+
initCmd.Flags().BoolP("v1", "", false, "generate v1 config yaml file")
30+
initCmd.Flags().BoolP("v2", "", true, "generate v2 config yaml file")
31+
initCmd.MarkFlagsMutuallyExclusive("v1", "v2")
2932
}
3033

3134
// Do runs the command logic.
@@ -88,6 +91,17 @@ var initCmd = &cobra.Command{
8891
Use: "init",
8992
Short: "Create an empty sqlc.yaml settings file",
9093
RunE: func(cmd *cobra.Command, args []string) error {
94+
useV1, err := cmd.Flags().GetBool("v1")
95+
if err != nil {
96+
return err
97+
}
98+
var yamlConfig interface{}
99+
if useV1 {
100+
yamlConfig = config.V1GenerateSettings{Version: "1"}
101+
} else {
102+
yamlConfig = config.Config{Version: "2"}
103+
}
104+
91105
defer trace.StartRegion(cmd.Context(), "init").End()
92106
file := "sqlc.yaml"
93107
if f := cmd.Flag("file"); f != nil && f.Changed {
@@ -97,13 +111,24 @@ var initCmd = &cobra.Command{
97111
}
98112
}
99113
if _, err := os.Stat(file); !os.IsNotExist(err) {
114+
fmt.Printf("%s is already created\n", file)
100115
return nil
101116
}
102-
blob, err := yaml.Marshal(config.V1GenerateSettings{Version: "1"})
117+
blob, err := yaml.Marshal(yamlConfig)
103118
if err != nil {
104119
return err
105120
}
106-
return os.WriteFile(file, blob, 0644)
121+
err = os.WriteFile(file, blob, 0644)
122+
if err != nil {
123+
return err
124+
}
125+
configDoc := "https://docs.sqlc.dev/en/stable/reference/config.html"
126+
fmt.Printf(
127+
"%s is added. Please visit %s to learn more about configuration\n",
128+
file,
129+
configDoc,
130+
)
131+
return nil
107132
},
108133
}
109134

internal/codegen/golang/templates/stdlib/queryCode.tmpl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
120120

121121
{{define "queryCodeStdExec"}}
122122
{{- if .Arg.HasSqlcSlices }}
123-
sql := {{.ConstantName}}
123+
query := {{.ConstantName}}
124124
var queryParams []interface{}
125125
{{- if .Arg.Struct }}
126126
{{- $arg := .Arg }}
@@ -130,9 +130,9 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
130130
for _, v := range {{$arg.Name}}.{{.Name}} {
131131
queryParams = append(queryParams, v)
132132
}
133-
sql = strings.Replace(sql, "/*SLICE:{{.Column.Name}}*/?", strings.Repeat(",?", len({{$arg.Name}}.{{.Name}}))[1:], 1)
133+
query = strings.Replace(query, "/*SLICE:{{.Column.Name}}*/?", strings.Repeat(",?", len({{$arg.Name}}.{{.Name}}))[1:], 1)
134134
} else {
135-
sql = strings.Replace(sql, "/*SLICE:{{.Column.Name}}*/?", "NULL", 1)
135+
query = strings.Replace(query, "/*SLICE:{{.Column.Name}}*/?", "NULL", 1)
136136
}
137137
{{- else }}
138138
queryParams = append(queryParams, {{$arg.Name}}.{{.Name}})
@@ -147,12 +147,12 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
147147
for _, v := range {{.Arg.Name}} {
148148
queryParams = append(queryParams, v)
149149
}
150-
sql = strings.Replace(sql, "/*SLICE:{{.Arg.Column.Name}}*/?", strings.Repeat(",?", len({{.Arg.Name}}))[1:], 1)
150+
query = strings.Replace(query, "/*SLICE:{{.Arg.Column.Name}}*/?", strings.Repeat(",?", len({{.Arg.Name}}))[1:], 1)
151151
} else {
152-
sql = strings.Replace(sql, "/*SLICE:{{.Arg.Column.Name}}*/?", "NULL", 1)
152+
query = strings.Replace(query, "/*SLICE:{{.Arg.Column.Name}}*/?", "NULL", 1)
153153
}
154154
{{- end }}
155-
{{ queryRetval . }} {{ queryMethod . }}(ctx, sql, queryParams...)
155+
{{ queryRetval . }} {{ queryMethod . }}(ctx, query, queryParams...)
156156
{{- else if emitPreparedQueries }}
157157
{{- queryRetval . }} {{ queryMethod . }}(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
158158
{{- else}}

internal/endtoend/testdata/select_exists/sqlite/go/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/select_exists/sqlite/go/models.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/select_exists/sqlite/go/query.sql.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE TABLE bar (id int not null primary key autoincrement);
2+
3+
-- name: BarExists :one
4+
SELECT
5+
EXISTS (
6+
SELECT
7+
1
8+
FROM
9+
bar
10+
where
11+
id = ?
12+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"name": "querytest",
7+
"engine": "sqlite",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/endtoend/testdata/select_not_exists/sqlite/go/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/select_not_exists/sqlite/go/models.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/select_not_exists/sqlite/go/query.sql.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CREATE TABLE bar (id integer not null primary key autoincrement);
2+
3+
-- name: BarNotExists :one
4+
SELECT
5+
NOT EXISTS (
6+
SELECT
7+
1
8+
FROM
9+
bar
10+
WHERE
11+
id = ?
12+
);
13+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"name": "querytest",
7+
"engine": "sqlite",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/endtoend/testdata/sqlc_slice/mysql/go/models.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)