Skip to content

Commit 5f6b0eb

Browse files
feat: Update sqlc go sdk dependency (#31)
1 parent 50a4af0 commit 5f6b0eb

File tree

5 files changed

+44
-74
lines changed

5 files changed

+44
-74
lines changed

go.mod

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ module github.com/sqlc-dev/sqlc-gen-python
33
go 1.19
44

55
require (
6-
buf.build/gen/go/sqlc/sqlc/protocolbuffers/go v1.31.0-20231002190240-3f2d312ab6fd.1
76
github.com/google/go-cmp v0.6.0
87
github.com/jinzhu/inflection v1.0.0
9-
github.com/sqlc-dev/sqlc-go v1.22.0
8+
github.com/sqlc-dev/sqlc-go v1.22.1-0.20231102170509-ee94c710d92e
109
google.golang.org/protobuf v1.31.0
1110
)
11+
12+
require (
13+
github.com/golang/protobuf v1.5.3 // indirect
14+
golang.org/x/net v0.14.0 // indirect
15+
golang.org/x/sys v0.11.0 // indirect
16+
golang.org/x/text v0.12.0 // indirect
17+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
18+
google.golang.org/grpc v1.59.0 // indirect
19+
)

go.sum

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
buf.build/gen/go/sqlc/sqlc/protocolbuffers/go v1.31.0-20231002190240-3f2d312ab6fd.1 h1:94JzirpGhebc3++MqmvWY0fi9TJxlle5M52NO4pTEZY=
2-
buf.build/gen/go/sqlc/sqlc/protocolbuffers/go v1.31.0-20231002190240-3f2d312ab6fd.1/go.mod h1:x7kMRcmAiYQXko+NDqLP2agondNlbHKUNGPXqU1nrOU=
31
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
2+
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
3+
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
44
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
55
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
66
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
77
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
88
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
9-
github.com/sqlc-dev/sqlc-go v1.22.0 h1:ivUplxHRkw1WZ++rs80OfoJLYbpXMXYGtc79e7z/0HA=
10-
github.com/sqlc-dev/sqlc-go v1.22.0/go.mod h1:/4snw3ucbglJfyLRxp8X2weM4pwT8w1NlEKm4PzxAuQ=
9+
github.com/sqlc-dev/sqlc-go v1.22.1-0.20231102170509-ee94c710d92e h1:fgvOWVSJLVvl1ETExPEw2okhTj2kbxvAQ5ECLrAtWbQ=
10+
github.com/sqlc-dev/sqlc-go v1.22.1-0.20231102170509-ee94c710d92e/go.mod h1:SXs+GYtPBUfDJQWSOvcJesiAwj4HNfigK7EwzwpHNPk=
11+
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
12+
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
13+
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
14+
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15+
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
16+
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
1117
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
18+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4=
19+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
20+
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
21+
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
1222
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
23+
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
1324
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
1425
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=

internal/gen.go

+13-31
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"sort"
1111
"strings"
1212

13-
"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
1413
"github.com/sqlc-dev/sqlc-go/metadata"
14+
"github.com/sqlc-dev/sqlc-go/plugin"
1515
"github.com/sqlc-dev/sqlc-go/sdk"
1616

1717
pyast "github.com/sqlc-dev/sqlc-gen-python/internal/ast"
@@ -180,7 +180,7 @@ func (q Query) ArgDictNode() *pyast.Node {
180180
}
181181
}
182182

183-
func makePyType(req *plugin.CodeGenRequest, col *plugin.Column) pyType {
183+
func makePyType(req *plugin.GenerateRequest, col *plugin.Column) pyType {
184184
typ := pyInnerType(req, col)
185185
return pyType{
186186
InnerType: typ,
@@ -189,21 +189,7 @@ func makePyType(req *plugin.CodeGenRequest, col *plugin.Column) pyType {
189189
}
190190
}
191191

192-
func pyInnerType(req *plugin.CodeGenRequest, col *plugin.Column) string {
193-
columnType := sdk.DataType(col.Type)
194-
for _, oride := range req.Settings.Overrides {
195-
if !pyTypeIsSet(oride) {
196-
continue
197-
}
198-
sameTable := sdk.Matches(oride, col.Table, req.Catalog.DefaultSchema)
199-
if oride.Column != "" && sdk.MatchString(oride.ColumnName, col.Name) && sameTable {
200-
return oride.CodeType
201-
}
202-
if oride.DbType != "" && oride.DbType == columnType && oride.Nullable != (col.NotNull || col.IsArray) {
203-
return oride.CodeType
204-
}
205-
}
206-
192+
func pyInnerType(req *plugin.GenerateRequest, col *plugin.Column) string {
207193
switch req.Settings.Engine {
208194
case "postgresql":
209195
return postgresType(req, col)
@@ -214,9 +200,6 @@ func pyInnerType(req *plugin.CodeGenRequest, col *plugin.Column) string {
214200
}
215201

216202
func modelName(name string, settings *plugin.Settings) string {
217-
if rename := settings.Rename[name]; rename != "" {
218-
return rename
219-
}
220203
out := ""
221204
for _, p := range strings.Split(name, "_") {
222205
out += strings.Title(p)
@@ -243,7 +226,7 @@ func pyEnumValueName(value string) string {
243226
return strings.ToUpper(id)
244227
}
245228

246-
func buildEnums(req *plugin.CodeGenRequest) []Enum {
229+
func buildEnums(req *plugin.GenerateRequest) []Enum {
247230
var enums []Enum
248231
for _, schema := range req.Catalog.Schemas {
249232
if schema.Name == "pg_catalog" || schema.Name == "information_schema" {
@@ -276,7 +259,7 @@ func buildEnums(req *plugin.CodeGenRequest) []Enum {
276259
return enums
277260
}
278261

279-
func buildModels(conf Config, req *plugin.CodeGenRequest) []Struct {
262+
func buildModels(conf Config, req *plugin.GenerateRequest) []Struct {
280263
var structs []Struct
281264
for _, schema := range req.Catalog.Schemas {
282265
if schema.Name == "pg_catalog" || schema.Name == "information_schema" {
@@ -338,7 +321,7 @@ type pyColumn struct {
338321
*plugin.Column
339322
}
340323

341-
func columnsToStruct(req *plugin.CodeGenRequest, name string, columns []pyColumn) *Struct {
324+
func columnsToStruct(req *plugin.GenerateRequest, name string, columns []pyColumn) *Struct {
342325
gs := Struct{
343326
Name: name,
344327
}
@@ -380,7 +363,7 @@ func sqlalchemySQL(s, engine string) string {
380363
return s
381364
}
382365

383-
func buildQueries(conf Config, req *plugin.CodeGenRequest, structs []Struct) ([]Query, error) {
366+
func buildQueries(conf Config, req *plugin.GenerateRequest, structs []Struct) ([]Query, error) {
384367
qs := make([]Query, 0, len(req.Queries))
385368
for _, query := range req.Queries {
386369
if query.Name == "" {
@@ -1091,7 +1074,7 @@ func HashComment(s string) string {
10911074
return "# " + strings.ReplaceAll(s, "\n", "\n# ")
10921075
}
10931076

1094-
func Generate(_ context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
1077+
func Generate(_ context.Context, req *plugin.GenerateRequest) (*plugin.GenerateResponse, error) {
10951078
var conf Config
10961079
if len(req.PluginOptions) > 0 {
10971080
if err := json.Unmarshal(req.PluginOptions, &conf); err != nil {
@@ -1107,11 +1090,10 @@ func Generate(_ context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenRes
11071090
}
11081091

11091092
i := &importer{
1110-
Settings: req.Settings,
1111-
Models: models,
1112-
Queries: queries,
1113-
Enums: enums,
1114-
C: conf,
1093+
Models: models,
1094+
Queries: queries,
1095+
Enums: enums,
1096+
C: conf,
11151097
}
11161098

11171099
tctx := pyTmplCtx{
@@ -1143,7 +1125,7 @@ func Generate(_ context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenRes
11431125
output[name] = string(result.Python)
11441126
}
11451127

1146-
resp := plugin.CodeGenResponse{}
1128+
resp := plugin.GenerateResponse{}
11471129

11481130
for filename, code := range output {
11491131
resp.Files = append(resp.Files, &plugin.File{

internal/imports.go

+4-35
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"fmt"
55
"sort"
66
"strings"
7-
8-
"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
97
)
108

119
type importSpec struct {
@@ -14,10 +12,6 @@ type importSpec struct {
1412
Alias string
1513
}
1614

17-
func pyTypeIsSet(o *plugin.Override) bool {
18-
return o.CodeType != ""
19-
}
20-
2115
func (i importSpec) String() string {
2216
if i.Alias != "" {
2317
if i.Name == "" {
@@ -32,11 +26,10 @@ func (i importSpec) String() string {
3226
}
3327

3428
type importer struct {
35-
Settings *plugin.Settings
36-
Models []Struct
37-
Queries []Query
38-
Enums []Enum
39-
C Config
29+
Models []Struct
30+
Queries []Query
31+
Enums []Enum
32+
C Config
4033
}
4134

4235
func structUses(name string, s Struct) bool {
@@ -104,18 +97,6 @@ func (i *importer) modelImportSpecs() (map[string]importSpec, map[string]importS
10497

10598
pkg := make(map[string]importSpec)
10699

107-
for _, o := range i.Settings.Overrides {
108-
if pyTypeIsSet(o) {
109-
mod, _, found := strings.Cut(o.CodeType, ".")
110-
if !found {
111-
continue
112-
}
113-
if modelUses(o.CodeType) {
114-
pkg[mod] = importSpec{Module: mod}
115-
}
116-
}
117-
}
118-
119100
return std, pkg
120101
}
121102

@@ -155,18 +136,6 @@ func (i *importer) queryImportSpecs(fileName string) (map[string]importSpec, map
155136
pkg["sqlalchemy.ext.asyncio"] = importSpec{Module: "sqlalchemy.ext.asyncio"}
156137
}
157138

158-
for _, o := range i.Settings.Overrides {
159-
if pyTypeIsSet(o) {
160-
mod, _, found := strings.Cut(o.CodeType, ".")
161-
if !found {
162-
continue
163-
}
164-
if queryUses(o.CodeType) {
165-
pkg[mod] = importSpec{Module: mod}
166-
}
167-
}
168-
}
169-
170139
queryValueModelImports := func(qv QueryValue) {
171140
if qv.IsStruct() && qv.EmitStruct() {
172141
if i.C.EmitPydanticModels {

internal/postgresql_type.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package python
33
import (
44
"log"
55

6-
"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
6+
"github.com/sqlc-dev/sqlc-go/plugin"
77
"github.com/sqlc-dev/sqlc-go/sdk"
88
)
99

10-
func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
10+
func postgresType(req *plugin.GenerateRequest, col *plugin.Column) string {
1111
columnType := sdk.DataType(col.Type)
1212

1313
switch columnType {

0 commit comments

Comments
 (0)