3
3
## Arrays
4
4
5
5
PostgreSQL [ arrays] ( https://www.postgresql.org/docs/current/arrays.html ) are
6
- materialized as Go slices. Currently, only one-dimensional arrays are
7
- supported.
6
+ materialized as Go slices. Currently, the ` pgx/v5 ` sql package only supports multidimensional arrays.
8
7
9
8
``` sql
10
9
CREATE TABLE places (
@@ -26,6 +25,7 @@ type Place struct {
26
25
27
26
All PostgreSQL time and date types are returned as ` time.Time ` structs. For
28
27
null time or date values, the ` NullTime ` type from ` database/sql ` is used.
28
+ The ` pgx/v5 ` sql package uses the appropriate pgx types.
29
29
30
30
``` sql
31
31
CREATE TABLE authors (
@@ -86,7 +86,7 @@ type Store struct {
86
86
## Null
87
87
88
88
For structs, null values are represented using the appropriate type from the
89
- ` database/sql ` package.
89
+ ` database/sql ` or ` pgx ` package.
90
90
91
91
``` sql
92
92
CREATE TABLE authors (
@@ -132,3 +132,48 @@ type Author struct {
132
132
ID uuid.UUID
133
133
}
134
134
```
135
+
136
+ ## JSON
137
+
138
+ By default, sqlc will generate the ` []byte ` , ` pgtype.JSON ` or ` json.RawMessage ` for JSON column type.
139
+ But if you use the ` pgx/v5 ` sql package then you can specify a some struct instead of default type.
140
+ The ` pgx ` implementation will marshall/unmarshall the struct automatically.
141
+
142
+ ``` go
143
+ package dto
144
+
145
+ type BookData struct {
146
+ Genres []string ` json:"genres"`
147
+ Title string ` json:"title"`
148
+ Published bool ` json:"published"`
149
+ }
150
+ ```
151
+
152
+ ``` sql
153
+ CREATE TABLE books (
154
+ data jsonb
155
+ );
156
+ ```
157
+
158
+ ``` json
159
+ {
160
+ "overrides" : [
161
+ {
162
+ "column" : " books.data" ,
163
+ "go_type" : " *example.com/db/dto.BookData"
164
+ }
165
+ ]
166
+ }
167
+ ```
168
+
169
+ ``` go
170
+ package db
171
+
172
+ import (
173
+ " example.com/db/dto"
174
+ )
175
+
176
+ type Book struct {
177
+ Data *dto.BookData
178
+ }
179
+ ```
0 commit comments