Skip to content

Commit

Permalink
Merge branch 'kyleconroy:main' into allow-golang-slice-overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmbenton authored Jun 20, 2023
2 parents a007565 + 3b21fdf commit 1aa6d83
Show file tree
Hide file tree
Showing 21 changed files with 375 additions and 97 deletions.
14 changes: 7 additions & 7 deletions docs/reference/changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changelog
All notable changes to this project will be documented in this file.

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

### Release notes
Expand Down Expand Up @@ -260,14 +260,14 @@ genreated method will use a argument struct.
- Upgrade to wasmtime v8.0.0 (#2222)
## [1.17.2](https://github.com/kyleconroy/sqlc/releases/tag/1.17.2)
## [1.17.2](https://github.com/kyleconroy/sqlc/releases/tag/v1.17.2)
Released 2023-02-22
### Bug Fixes
- Fix build on Windows (#2102)
## [1.17.1](https://github.com/kyleconroy/sqlc/releases/tag/1.17.1)
## [1.17.1](https://github.com/kyleconroy/sqlc/releases/tag/v1.17.1)
Released 2023-02-22
### Bug Fixes
Expand All @@ -284,7 +284,7 @@ Released 2023-02-22
- (deps) Bump golang from 1.20.0 to 1.20.1 (#2082)
## [1.17.0](https://github.com/kyleconroy/sqlc/releases/tag/1.17.0)
## [1.17.0](https://github.com/kyleconroy/sqlc/releases/tag/v1.17.0)
Released 2023-02-13
### Bug Fixes
Expand Down Expand Up @@ -360,7 +360,7 @@ Released 2023-02-13
- Upgrade to wasmtime 5.0.0 (#2065)
## [1.16.0](https://github.com/kyleconroy/sqlc/releases/tag/1.16.0)
## [1.16.0](https://github.com/kyleconroy/sqlc/releases/tag/v1.16.0)
Released 2022-11-09
Expand Down Expand Up @@ -432,7 +432,7 @@ Released 2022-11-09
- Port all Python tests to sqlc-gen-python (#1907)
- Upgrade to sqlc-gen-python v1.0.0 (#1932)
## [1.15.0](https://github.com/kyleconroy/sqlc/releases/tag/1.15.0)
## [1.15.0](https://github.com/kyleconroy/sqlc/releases/tag/v1.15.0)
Released 2022-08-07
### Bug Fixes
Expand Down Expand Up @@ -481,7 +481,7 @@ Released 2022-08-07
- (wasm) Change default cache location (#1709)
- (wasm) Change the SHA-256 config key (#1710)
## [1.14.0](https://github.com/kyleconroy/sqlc/releases/tag/1.14.0)
## [1.14.0](https://github.com/kyleconroy/sqlc/releases/tag/v1.14.0)
Released 2022-06-09
### Bug Fixes
Expand Down
8 changes: 4 additions & 4 deletions examples/booktest/sqlite/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 27 additions & 2 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import (

func init() {
uploadCmd.Flags().BoolP("dry-run", "", false, "dump upload request (default: false)")
initCmd.Flags().BoolP("v1", "", false, "generate v1 config yaml file")
initCmd.Flags().BoolP("v2", "", true, "generate v2 config yaml file")
initCmd.MarkFlagsMutuallyExclusive("v1", "v2")
}

// Do runs the command logic.
Expand Down Expand Up @@ -88,6 +91,17 @@ var initCmd = &cobra.Command{
Use: "init",
Short: "Create an empty sqlc.yaml settings file",
RunE: func(cmd *cobra.Command, args []string) error {
useV1, err := cmd.Flags().GetBool("v1")
if err != nil {
return err
}
var yamlConfig interface{}
if useV1 {
yamlConfig = config.V1GenerateSettings{Version: "1"}
} else {
yamlConfig = config.Config{Version: "2"}
}

defer trace.StartRegion(cmd.Context(), "init").End()
file := "sqlc.yaml"
if f := cmd.Flag("file"); f != nil && f.Changed {
Expand All @@ -97,13 +111,24 @@ var initCmd = &cobra.Command{
}
}
if _, err := os.Stat(file); !os.IsNotExist(err) {
fmt.Printf("%s is already created\n", file)
return nil
}
blob, err := yaml.Marshal(config.V1GenerateSettings{Version: "1"})
blob, err := yaml.Marshal(yamlConfig)
if err != nil {
return err
}
return os.WriteFile(file, blob, 0644)
err = os.WriteFile(file, blob, 0644)
if err != nil {
return err
}
configDoc := "https://docs.sqlc.dev/en/stable/reference/config.html"
fmt.Printf(
"%s is added. Please visit %s to learn more about configuration\n",
file,
configDoc,
)
return nil
},
}

Expand Down
12 changes: 6 additions & 6 deletions internal/codegen/golang/templates/stdlib/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}

{{define "queryCodeStdExec"}}
{{- if .Arg.HasSqlcSlices }}
sql := {{.ConstantName}}
query := {{.ConstantName}}
var queryParams []interface{}
{{- if .Arg.Struct }}
{{- $arg := .Arg }}
Expand All @@ -130,9 +130,9 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
for _, v := range {{$arg.Name}}.{{.Name}} {
queryParams = append(queryParams, v)
}
sql = strings.Replace(sql, "/*SLICE:{{.Column.Name}}*/?", strings.Repeat(",?", len({{$arg.Name}}.{{.Name}}))[1:], 1)
query = strings.Replace(query, "/*SLICE:{{.Column.Name}}*/?", strings.Repeat(",?", len({{$arg.Name}}.{{.Name}}))[1:], 1)
} else {
sql = strings.Replace(sql, "/*SLICE:{{.Column.Name}}*/?", "NULL", 1)
query = strings.Replace(query, "/*SLICE:{{.Column.Name}}*/?", "NULL", 1)
}
{{- else }}
queryParams = append(queryParams, {{$arg.Name}}.{{.Name}})
Expand All @@ -147,12 +147,12 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
for _, v := range {{.Arg.Name}} {
queryParams = append(queryParams, v)
}
sql = strings.Replace(sql, "/*SLICE:{{.Arg.Column.Name}}*/?", strings.Repeat(",?", len({{.Arg.Name}}))[1:], 1)
query = strings.Replace(query, "/*SLICE:{{.Arg.Column.Name}}*/?", strings.Repeat(",?", len({{.Arg.Name}}))[1:], 1)
} else {
sql = strings.Replace(sql, "/*SLICE:{{.Arg.Column.Name}}*/?", "NULL", 1)
query = strings.Replace(query, "/*SLICE:{{.Arg.Column.Name}}*/?", "NULL", 1)
}
{{- end }}
{{ queryRetval . }} {{ queryMethod . }}(ctx, sql, queryParams...)
{{ queryRetval . }} {{ queryMethod . }}(ctx, query, queryParams...)
{{- else if emitPreparedQueries }}
{{- queryRetval . }} {{ queryMethod . }}(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
{{- else}}
Expand Down
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/select_exists/sqlite/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions internal/endtoend/testdata/select_exists/sqlite/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions internal/endtoend/testdata/select_exists/sqlite/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions internal/endtoend/testdata/select_exists/sqlite/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE bar (id int not null primary key autoincrement);

-- name: BarExists :one
SELECT
EXISTS (
SELECT
1
FROM
bar
where
id = ?
);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/select_exists/sqlite/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "querytest",
"engine": "sqlite",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/select_not_exists/sqlite/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions internal/endtoend/testdata/select_not_exists/sqlite/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions internal/endtoend/testdata/select_not_exists/sqlite/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE bar (id integer not null primary key autoincrement);

-- name: BarNotExists :one
SELECT
NOT EXISTS (
SELECT
1
FROM
bar
WHERE
id = ?
);

12 changes: 12 additions & 0 deletions internal/endtoend/testdata/select_not_exists/sqlite/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "querytest",
"engine": "sqlite",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
5 changes: 4 additions & 1 deletion internal/endtoend/testdata/sqlc_slice/mysql/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1aa6d83

Please sign in to comment.