What's New
Remote code generation
Developed by @andrewmbenton
At its core, sqlc is powered by SQL engines, which include parsers, formatters,
analyzers and more. While our goal is to support each engine on each operating
system, it's not always possible. For example, the PostgreSQL engine does not
work on Windows.
To bridge that gap, we're announcing remote code generation, currently in
private alpha. To join the private alpha, sign up for the waitlist.
To configure remote generation, configure a cloud
block in sqlc.json
.
{
"version": "2",
"cloud": {
"organization": "<org-id>",
"project": "<project-id>",
},
...
}
You'll also need to the SQLC_AUTH_TOKEN
environment variable.
export SQLC_AUTH_TOKEN=<token>
When the cloud configuration exists, sqlc generate
will default to remote
generation. If you'd like to generate code locally, pass the --no-remote
option.
sqlc generate --no-remote
Remote generation is off by default and requires an opt-in to use.
sqlc.embed
Developed by @nickjackson
Embedding allows you to reuse existing model structs in more queries, resulting
in less manual serilization work. First, imagine we have the following schema
with students and test scores.
CREATE TABLE students (
id bigserial PRIMARY KEY,
name text,
age integer
)
CREATE TABLE test_scores (
student_id bigint,
score integer,
grade text
)
We want to select the student record and the highest score they got on a test.
Here's how we'd usually do that:
-- name: HighScore :many
WITH high_scores AS (
SELECT student_id, max(score) as high_score
FROM test_scores
GROUP BY 1
)
SELECT students.*, high_score::integer
FROM students
JOIN high_scores ON high_scores.student_id = students.id;
When using Go, sqlc will produce a struct like this:
type HighScoreRow struct {
ID int64
Name sql.NullString
Age sql.NullInt32
HighScore int32
}
With embedding, the struct will contain a model for the table instead of a
flattened list of columns.
-- name: HighScoreEmbed :many
WITH high_scores AS (
SELECT student_id, max(score) as high_score
FROM test_scores
GROUP BY 1
)
SELECT sqlc.embed(students), high_score::integer
FROM students
JOIN high_scores ON high_scores.student_id = students.id;
type HighScoreRow struct {
Student Student
HighScore int32
}
sqlc.slice
Developed by Paul Cameron and Jille Timmermans
The MySQL Go driver does not support passing slices to the IN operator. The
sqlc.slice
function generates a dynamic query at runtime with the correct
number of parameters.
/* name: SelectStudents :many */
SELECT * FROM students
WHERE age IN (sqlc.slice("ages"))
func (q *Queries) SelectStudents(ctx context.Context, arges []int32) ([]Student, error) {
This feature is only supported in MySQL and cannot be used with prepared
queries.
Batch operation improvements
When using batches with pgx, the error returned when a batch is closed is
exported by the generated package. This change allows for cleaner error
handling using errors.Is
.
errors.Is(err, generated_package.ErrBatchAlreadyClosed)
Previously, you would have had to check match on the error message itself.
err.Error() == "batch already closed"
The generated code for batch operations always lived in batch.go
. This file
name can now be configured via the output_batch_file_name
configuration
option.
Configurable query parameter limits for Go
By default, sqlc will limit Go functions to a single parameter. If a query
includes more than one parameter, the generated method will use an argument
struct instead of positional arguments. This behavior can now be changed via
the query_parameter_limit
configuration option. If set to 0
, every
generated method will use a argument struct.
What's Changed
- Upgrade to Go 1.20 by @kyleconroy in #2105
- test: Skip tests if required plugins are missing by @kyleconroy in #2104
- fix: Prevent variable redeclaration in single param conflict for pgx by @zaneli in #2058
- fix: Retrieve Larg/Rarg join query after inner join by @zaneli in #2051
- fix: Rename argument when conflicted to imported package by @zaneli in #2048
- fix: pgx closed batch return pointer if need #1959 by @flymedllva in #1960
- parser: Generate correct types for
SELECT NOT EXISTS
by @haines in #1972 - Implement ALTER TYPE SET SCHEMA by @linux2647 in #2027
- Fix test: Bump version to v1.17.2 in internal/endtoend/testdata/ddl_alter_type_set_schema/postgresql by @monkey-mas in #2108
- build(deps): bump github.com/antlr/antlr4/runtime/Go/antlr from 0.0.0-20220626175859-9abda183db8e to 1.4.10 by @dependabot in #2109
- Implement support for enum ordering by @mikemherron in #2111
- engine/postgres: Upgrade to pg_query_go/v4 by @kyleconroy in #2114
- build(deps): bump github.com/jackc/pgx/v4 from 4.18.0 to 4.18.1 by @dependabot in #2119
- Enum ordering minor code tidy up by @mikemherron in #2126
- build(deps): bump golang from 1.20.1 to 1.20.2 by @dependabot in #2135
- build(deps): bump google.golang.org/protobuf from 1.28.1 to 1.29.0 by @dependabot in #2137
- allow deleting composite types by @josharian in #2138
- Fix typo (incorrect domain in URL) by @starquake in #2142
- build(deps): bump google.golang.org/protobuf from 1.29.0 to 1.29.1 by @dependabot in #2143
- docs(config.md): add
sqlite
as engine option by @aaanders in #2164 - Add link to introductory blog post by @meblum in #2155
- cmd/sqlc: Remove --experimental flag by @kyleconroy in #2170
- docs: Add first pass at pgx documentation by @kyleconroy in #2174
- sqlite: Add support for CREATE TABLE ... STRICT by @kyleconroy in #2175
- cmd/sqlc: Add option to disable process-based plugins by @kyleconroy in #2180
- codegen: Correctly generate CopyFrom columns for single-column copyfroms by @Jille in #2185
- More consistent filename formatting in codegen stderr output by @andrewmbenton in #2186
- docs: Add missed configuration option by @abekoh in #2188
- bin/sqlc: Add SQLCTMPDIR environment variable by @kyleconroy in #2189
- fix: correct singularization of "waves" by @rliebz in #2194
- Add sqlc.slice() to support IN clauses in MySQL (#695) by @Jille in #1816
- feat: add
sqlc.embed
to allow model re-use by @nickjackson in #1615 - feat(Go):Add query_parameter_limit conf to codegen by @go-mez in #1558
- fix: Honor Package level renames in v2 yaml config by @Emyrk in #2001
- tests: Add tests for reanme fix in v2 by @kyleconroy in #2196
- build(deps): bump golang from 1.20.2 to 1.20.3 by @dependabot in #2192
- build(deps): bump actions/setup-go from 3 to 4 by @dependabot in #2150
- build(deps): bump google.golang.org/protobuf from 1.29.1 to 1.30.0 by @dependabot in #2151
- build(deps): bump github.com/spf13/cobra from 1.6.1 to 1.7.0 by @dependabot in #2193
- fix:
specifies parameter ":one" without containing a RETURNING clause
by @ihatov08 in #2173 - Customizable batch output file name (add OutputBatchFileName field) by @JordanP in #2178
- fix: update panic #1590 by @nusr in #2154
- Define base error in batch.go for better error handling by @Aisoipheo in #2147
- fix: mysql delete join panic by @nusr in #2197
- config: Add top-level cloud configuration by @kyleconroy in #2204
- Use os.CreateTemp() to remove ioutil dependency by @andrewmbenton in #2216
- Avoid some unnecessary buffering when reading config file by @andrewmbenton in #2215
- Add remote execution for codegen by @andrewmbenton in #2214
- fix panic caused by invalid shorthand for
--no-remote
flag by @andrewmbenton in #2218 - build(deps): bump github.com/lib/pq from 1.10.7 to 1.10.8 by @dependabot in #2211
- wasm: Upgrade to wasmtime v8.0.0 by @kyleconroy in #2222
- Allow sql_package: "go-sql-driver/mysql" by @Jille in #2219
- ext/wasm: Check exit code on returned error by @kyleconroy in #2223
- Update default remote execution hostname by @andrewmbenton in #2230
- fix: missing import with pointer overrides, solves #2168 #2125 by @ludusrusso in #2217
- build(deps): bump github.com/lib/pq from 1.10.8 to 1.10.9 by @dependabot in #2229
- Add support for SHOW WARNINGS and SELECTs without a FROM by @Jille in #2227
- build(deps): bump github.com/go-sql-driver/mysql from 1.7.0 to 1.7.1 by @dependabot in #2228
- Add changelog for v1.18.0 by @kyleconroy in #2231
- test: Remove remote test by @kyleconroy in #2232
- cmd/sqlc: Bump version to 1.18.0 by @kyleconroy in #2233
New Contributors
- @flymedllva made their first contribution in #1960
- @haines made their first contribution in #1972
- @linux2647 made their first contribution in #2027
- @monkey-mas made their first contribution in #2108
- @mikemherron made their first contribution in #2111
- @starquake made their first contribution in #2142
- @aaanders made their first contribution in #2164
- @meblum made their first contribution in #2155
- @abekoh made their first contribution in #2188
- @go-mez made their first contribution in #1558
- @Emyrk made their first contribution in #2001
- @ihatov08 made their first contribution in #2173
- @JordanP made their first contribution in #2178
- @nusr made their first contribution in #2154
- @Aisoipheo made their first contribution in #2147
Full Changelog: v1.17.2...v1.18.0