Skip to content

Commit

Permalink
feat: Update sqlc go sdk dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmbenton committed Nov 2, 2023
1 parent 50a4af0 commit af3323b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 74 deletions.
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ module github.com/sqlc-dev/sqlc-gen-python
go 1.19

require (
buf.build/gen/go/sqlc/sqlc/protocolbuffers/go v1.31.0-20231002190240-3f2d312ab6fd.1
github.com/google/go-cmp v0.6.0
github.com/jinzhu/inflection v1.0.0
github.com/sqlc-dev/sqlc-go v1.22.0
github.com/sqlc-dev/sqlc-go v1.22.1-0.20231102170509-ee94c710d92e
google.golang.org/protobuf v1.31.0
)

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

"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
"github.com/sqlc-dev/sqlc-go/metadata"
"github.com/sqlc-dev/sqlc-go/plugin"
"github.com/sqlc-dev/sqlc-go/sdk"

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

func makePyType(req *plugin.CodeGenRequest, col *plugin.Column) pyType {
func makePyType(req *plugin.GenerateRequest, col *plugin.Column) pyType {
typ := pyInnerType(req, col)
return pyType{
InnerType: typ,
Expand All @@ -189,21 +189,7 @@ func makePyType(req *plugin.CodeGenRequest, col *plugin.Column) pyType {
}
}

func pyInnerType(req *plugin.CodeGenRequest, col *plugin.Column) string {
columnType := sdk.DataType(col.Type)
for _, oride := range req.Settings.Overrides {
if !pyTypeIsSet(oride) {
continue
}
sameTable := sdk.Matches(oride, col.Table, req.Catalog.DefaultSchema)
if oride.Column != "" && sdk.MatchString(oride.ColumnName, col.Name) && sameTable {
return oride.CodeType
}
if oride.DbType != "" && oride.DbType == columnType && oride.Nullable != (col.NotNull || col.IsArray) {
return oride.CodeType
}
}

func pyInnerType(req *plugin.GenerateRequest, col *plugin.Column) string {
switch req.Settings.Engine {
case "postgresql":
return postgresType(req, col)
Expand All @@ -214,9 +200,6 @@ func pyInnerType(req *plugin.CodeGenRequest, col *plugin.Column) string {
}

func modelName(name string, settings *plugin.Settings) string {
if rename := settings.Rename[name]; rename != "" {
return rename
}
out := ""
for _, p := range strings.Split(name, "_") {
out += strings.Title(p)
Expand All @@ -243,7 +226,7 @@ func pyEnumValueName(value string) string {
return strings.ToUpper(id)
}

func buildEnums(req *plugin.CodeGenRequest) []Enum {
func buildEnums(req *plugin.GenerateRequest) []Enum {
var enums []Enum
for _, schema := range req.Catalog.Schemas {
if schema.Name == "pg_catalog" || schema.Name == "information_schema" {
Expand Down Expand Up @@ -276,7 +259,7 @@ func buildEnums(req *plugin.CodeGenRequest) []Enum {
return enums
}

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

func columnsToStruct(req *plugin.CodeGenRequest, name string, columns []pyColumn) *Struct {
func columnsToStruct(req *plugin.GenerateRequest, name string, columns []pyColumn) *Struct {
gs := Struct{
Name: name,
}
Expand Down Expand Up @@ -380,7 +363,7 @@ func sqlalchemySQL(s, engine string) string {
return s
}

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

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

i := &importer{
Settings: req.Settings,
Models: models,
Queries: queries,
Enums: enums,
C: conf,
Models: models,
Queries: queries,
Enums: enums,
C: conf,
}

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

resp := plugin.CodeGenResponse{}
resp := plugin.GenerateResponse{}

for filename, code := range output {
resp.Files = append(resp.Files, &plugin.File{
Expand Down
39 changes: 4 additions & 35 deletions internal/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"sort"
"strings"

"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
)

type importSpec struct {
Expand All @@ -14,10 +12,6 @@ type importSpec struct {
Alias string
}

func pyTypeIsSet(o *plugin.Override) bool {
return o.CodeType != ""
}

func (i importSpec) String() string {
if i.Alias != "" {
if i.Name == "" {
Expand All @@ -32,11 +26,10 @@ func (i importSpec) String() string {
}

type importer struct {
Settings *plugin.Settings
Models []Struct
Queries []Query
Enums []Enum
C Config
Models []Struct
Queries []Query
Enums []Enum
C Config
}

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

pkg := make(map[string]importSpec)

for _, o := range i.Settings.Overrides {
if pyTypeIsSet(o) {
mod, _, found := strings.Cut(o.CodeType, ".")
if !found {
continue
}
if modelUses(o.CodeType) {
pkg[mod] = importSpec{Module: mod}
}
}
}

return std, pkg
}

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

for _, o := range i.Settings.Overrides {
if pyTypeIsSet(o) {
mod, _, found := strings.Cut(o.CodeType, ".")
if !found {
continue
}
if queryUses(o.CodeType) {
pkg[mod] = importSpec{Module: mod}
}
}
}

queryValueModelImports := func(qv QueryValue) {
if qv.IsStruct() && qv.EmitStruct() {
if i.C.EmitPydanticModels {
Expand Down
4 changes: 2 additions & 2 deletions internal/postgresql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package python
import (
"log"

"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
"github.com/sqlc-dev/sqlc-go/plugin"
"github.com/sqlc-dev/sqlc-go/sdk"
)

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

switch columnType {
Expand Down

0 comments on commit af3323b

Please sign in to comment.