|
1 | 1 | > 🚨 |
2 | 2 | > |
3 | | -> sqlc is **very new** and under rapid development. |
| 3 | +> sqlc is **new** and under rapid development. |
4 | 4 | > |
5 | | -> The code it generates is correct and safe for production use, but |
6 | | -> there is currently no guarantee of stability or backwards-compatibility of |
7 | | -> the command line interface, configuration file format or generated code. |
| 5 | +> The code it generates is correct and safe for production use, but there is |
| 6 | +> currently no guarantee of stability or backwards-compatibility of the command |
| 7 | +> line interface, configuration file format or generated code. |
8 | 8 | > |
9 | 9 | > 🚨 |
10 | 10 |
|
|
14 | 14 | > |
15 | 15 | > "SQL is actually pretty great" |
16 | 16 |
|
17 | | -sqlc generates **fully-type safe idiomatic Go code** from SQL. Here's how it works: |
| 17 | +sqlc generates **fully-type safe idiomatic Go code** from SQL. Here's how it |
| 18 | +works: |
18 | 19 |
|
19 | 20 | 1. You write SQL queries |
20 | | -1. You run sqlc to generate Go code that presents type-safe interfaces to those queries |
| 21 | +1. You run sqlc to generate Go code that presents type-safe interfaces to those |
| 22 | + queries |
21 | 23 | 1. You write application code that calls the methods sqlc generated. |
22 | 24 |
|
23 | | -Seriously, it's that easy. You don't have to write any boilerplate SQL querying code ever again. |
| 25 | +Seriously, it's that easy. You don't have to write any boilerplate SQL querying |
| 26 | +code ever again. |
24 | 27 |
|
25 | 28 | ## Preventing Errors |
26 | | -But sqlc doesn't just make you more productive by generating boilerplate for you. |
27 | | -sqlc **also prevents entire classes of common errors in SQL code**. Have you ever: |
28 | 29 |
|
29 | | -- Mixed up the order of the arguments when invoking the query so they didn't match up with the SQL text |
| 30 | +But sqlc doesn't just make you more productive by generating boilerplate for |
| 31 | +you. sqlc **also prevents entire classes of common errors in SQL code**. Have |
| 32 | +you ever: |
| 33 | + |
| 34 | +- Mixed up the order of the arguments when invoking the query so they didn't |
| 35 | + match up with the SQL text |
30 | 36 | - Updated the name of a column in one query both not another |
31 | 37 | - Mistyped the name of a column in a query |
32 | | -- Changed the number of arguments in a query but forgot to pass the additional values |
| 38 | +- Changed the number of arguments in a query but forgot to pass the additional |
| 39 | + values |
33 | 40 | - Changed the type of a column but forgot to change the type in your code? |
34 | 41 |
|
35 | 42 | All of these errors are *impossible* with sqlc. Wait, what? How? |
36 | 43 |
|
37 | | -sqlc parses your all of your queries and the DDL (e.g. `CREATE TABLE`) statements during the code generation processes |
38 | | -so that it knows the names and types of every column in your tables and every expression in your queries. |
39 | | -If any of them do not match, sqlc *will fail to compile your queries*, preventing entire classes of runtime problems |
40 | | -at compile time. |
| 44 | +sqlc parses your all of your queries and the DDL (e.g. `CREATE TABLE`) |
| 45 | +statements during the code generation processes so that it knows the names and |
| 46 | +types of every column in your tables and every expression in your queries. If |
| 47 | +any of them do not match, sqlc *will fail to compile your queries*, preventing |
| 48 | +entire classes of runtime problems at compile time. |
41 | 49 |
|
42 | | -Likewise, the methods that sqlc generates for you have a stricty arity and correct Go type definitions that match your columns. So if you |
43 | | -change a query's arguments or a column's type but don't update your code, it will fail to compile. |
| 50 | +Likewise, the methods that sqlc generates for you have a strict arity and |
| 51 | +correct Go type definitions that match your columns. So if you change a query's |
| 52 | +arguments or a column's type but don't update your code, it will fail to |
| 53 | +compile. |
44 | 54 |
|
45 | 55 | ## Getting Started |
46 | 56 | Okay, enough hype, let's see it in action. |
@@ -105,7 +115,8 @@ if err != nil { |
105 | 115 | fmt.Println(reflect.DeepEqual(insertedAuthor, fetchedAuthor)) |
106 | 116 | ``` |
107 | 117 |
|
108 | | -To make that possible, sqlc generates readable, **idiomatic** Go code that you otherwise would have had to write yourself. Take a look: |
| 118 | +To make that possible, sqlc generates readable, **idiomatic** Go code that you |
| 119 | +otherwise would have had to write yourself. Take a look: |
109 | 120 |
|
110 | 121 | ```go |
111 | 122 | package db |
@@ -297,6 +308,66 @@ Each package document has the following keys: |
297 | 308 | - `schema`: |
298 | 309 | - Directory of SQL migrations or path to single SQL file |
299 | 310 |
|
| 311 | +### Type Overrides |
| 312 | + |
| 313 | +The default mapping of PostgreSQL types to Go types only uses packages outside |
| 314 | +the standard library when it must. |
| 315 | + |
| 316 | +For example, the `uuid` PostgreSQL type is mapped to `github.com/google/uuid`. |
| 317 | +If a different Go package for UUIDs is required, specify the package in the |
| 318 | +`overrides` array. In this case, I'm going to use the `github.com/gofrs/uuid` |
| 319 | +instead. |
| 320 | + |
| 321 | +``` |
| 322 | +{ |
| 323 | + "version": "1", |
| 324 | + "packages": [...], |
| 325 | + "overrides": [ |
| 326 | + { |
| 327 | + "go_type": "uuid.UUID", |
| 328 | + "package": "github.com/gofrs/uuid", |
| 329 | + "postgres_type": "uuid" |
| 330 | + } |
| 331 | + ] |
| 332 | +} |
| 333 | +``` |
| 334 | + |
| 335 | +Each override document has the following keys: |
| 336 | +- `postgres_type`: |
| 337 | + - The PostgreSQL type to override. Find the full list of supported types in [gen.go](https://github.com/kyleconroy/sqlc/blob/master/internal/dinosql/gen.go#L438). |
| 338 | +- `go_type`: |
| 339 | + - The Go type, with package name, to use in the generated code. |
| 340 | +- `package`: |
| 341 | + - The full import path for the package. |
| 342 | +- `null`: |
| 343 | + - If true, use this type when a column in nullable. Defaults to `false`. |
| 344 | + |
| 345 | +### Renaming Struct Fields |
| 346 | + |
| 347 | +Struct field names are generated from column names using a simple algorithm: |
| 348 | +split the column name on underscores and capitalize the first letter of each |
| 349 | +part. |
| 350 | + |
| 351 | +``` |
| 352 | +account -> Account |
| 353 | +spotify_url -> SpotifyUrl |
| 354 | +app_id -> AppID |
| 355 | +``` |
| 356 | + |
| 357 | +If you're not happy with a field's generated name, use the `rename` dictionary |
| 358 | +to pick a new name. The keys are column names and the values are the struct |
| 359 | +field name to use. |
| 360 | + |
| 361 | +```json |
| 362 | +{ |
| 363 | + "version": "1", |
| 364 | + "packages": [...], |
| 365 | + "rename": { |
| 366 | + "spotify_url": "SpotifyURL" |
| 367 | + } |
| 368 | +} |
| 369 | +``` |
| 370 | + |
300 | 371 | ## Downloads |
301 | 372 |
|
302 | 373 | Each commit is deployed to the [`devel` channel on Equinox](https://dl.equinox.io/sqlc/sqlc/devel): |
|
0 commit comments