Skip to content

Commit

Permalink
feat: Pass sqlc version in codegen request (#1514)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy committed Mar 28, 2022
1 parent 031cd20 commit 3d20d04
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 36 deletions.
8 changes: 5 additions & 3 deletions internal/cmd/shim.go
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/kyleconroy/sqlc/internal/compiler"
"github.com/kyleconroy/sqlc/internal/config"
"github.com/kyleconroy/sqlc/internal/info"
"github.com/kyleconroy/sqlc/internal/plugin"
"github.com/kyleconroy/sqlc/internal/sql/catalog"
)
Expand Down Expand Up @@ -274,8 +275,9 @@ func pluginQueryParam(p compiler.Parameter) *plugin.Parameter {

func codeGenRequest(r *compiler.Result, settings config.CombinedSettings) *plugin.CodeGenRequest {
return &plugin.CodeGenRequest{
Settings: pluginSettings(settings),
Catalog: pluginCatalog(r.Catalog),
Queries: pluginQueries(r),
Settings: pluginSettings(settings),
Catalog: pluginCatalog(r.Catalog),
Queries: pluginQueries(r),
SqlcVersion: info.Version,
}
}
3 changes: 1 addition & 2 deletions internal/codegen/golang/gen.go
Expand Up @@ -10,7 +10,6 @@ import (
"text/template"

"github.com/kyleconroy/sqlc/internal/codegen/sdk"
"github.com/kyleconroy/sqlc/internal/info"
"github.com/kyleconroy/sqlc/internal/metadata"
"github.com/kyleconroy/sqlc/internal/plugin"
)
Expand Down Expand Up @@ -93,7 +92,7 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
GoQueries: queries,
Enums: enums,
Structs: structs,
SqlcVersion: info.Version,
SqlcVersion: req.SqlcVersion,
}

if tctx.UsesCopyFrom && tctx.SQLPackage != SQLPackagePGX {
Expand Down
3 changes: 1 addition & 2 deletions internal/codegen/kotlin/gen.go
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/kyleconroy/sqlc/internal/codegen/sdk"
"github.com/kyleconroy/sqlc/internal/inflection"
"github.com/kyleconroy/sqlc/internal/info"
"github.com/kyleconroy/sqlc/internal/metadata"
"github.com/kyleconroy/sqlc/internal/plugin"
)
Expand Down Expand Up @@ -793,7 +792,7 @@ func Generate(req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
Queries: queries,
Enums: enums,
DataClasses: structs,
SqlcVersion: info.Version,
SqlcVersion: req.SqlcVersion,
}

output := map[string]string{}
Expand Down
33 changes: 17 additions & 16 deletions internal/codegen/python/gen.go
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/kyleconroy/sqlc/internal/codegen/sdk"
"github.com/kyleconroy/sqlc/internal/inflection"
"github.com/kyleconroy/sqlc/internal/info"
"github.com/kyleconroy/sqlc/internal/metadata"
"github.com/kyleconroy/sqlc/internal/plugin"
pyast "github.com/kyleconroy/sqlc/internal/python/ast"
Expand Down Expand Up @@ -482,7 +481,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
return qs, nil
}

func moduleNode(source string) *pyast.Module {
func moduleNode(version, source string) *pyast.Module {
mod := &pyast.Module{
Body: []*pyast.Node{
poet.Comment(
Expand All @@ -492,7 +491,7 @@ func moduleNode(source string) *pyast.Module {
"versions:",
),
poet.Comment(
" sqlc " + info.Version,
" sqlc " + version,
),
},
}
Expand Down Expand Up @@ -661,7 +660,7 @@ func buildImportGroup(specs map[string]importSpec) *pyast.Node {
}

func buildModelsTree(ctx *pyTmplCtx, i *importer) *pyast.Node {
mod := moduleNode("")
mod := moduleNode(ctx.SqlcVersion, "")
std, pkg := i.modelImportSpecs()
mod.Body = append(mod.Body, buildImportGroup(std), buildImportGroup(pkg))

Expand Down Expand Up @@ -793,7 +792,7 @@ func asyncQuerierClassDef() *pyast.ClassDef {
}

func buildQueryTree(ctx *pyTmplCtx, i *importer, source string) *pyast.Node {
mod := moduleNode(source)
mod := moduleNode(ctx.SqlcVersion, source)
std, pkg := i.queryImportSpecs(source)
mod.Body = append(mod.Body, buildImportGroup(std), buildImportGroup(pkg))
mod.Body = append(mod.Body, &pyast.Node{
Expand Down Expand Up @@ -1028,12 +1027,13 @@ func buildQueryTree(ctx *pyTmplCtx, i *importer, source string) *pyast.Node {
}

type pyTmplCtx struct {
Models []Struct
Queries []Query
Enums []Enum
EmitSync bool
EmitAsync bool
SourceName string
Models []Struct
Queries []Query
Enums []Enum
EmitSync bool
EmitAsync bool
SourceName string
SqlcVersion string
}

func (t *pyTmplCtx) OutputQuery(sourceName string) bool {
Expand All @@ -1060,11 +1060,12 @@ func Generate(req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
}

tctx := pyTmplCtx{
Models: models,
Queries: queries,
Enums: enums,
EmitSync: req.Settings.Python.EmitSyncQuerier,
EmitAsync: req.Settings.Python.EmitAsyncQuerier,
Models: models,
Queries: queries,
Enums: enums,
EmitSync: req.Settings.Python.EmitSyncQuerier,
EmitAsync: req.Settings.Python.EmitAsyncQuerier,
SqlcVersion: req.SqlcVersion,
}

output := map[string]string{}
Expand Down
36 changes: 23 additions & 13 deletions internal/plugin/codegen.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions internal/plugin/codegen_vtproto.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions protos/plugin/codegen.proto
Expand Up @@ -183,6 +183,7 @@ message CodeGenRequest
Settings settings = 1 [json_name="settings"];
Catalog catalog = 2 [json_name="catalog"];
repeated Query queries = 3 [json_name="queries"];
string sqlc_version = 4 [json_name="sqlc_version"];
}

message CodeGenResponse
Expand Down

0 comments on commit 3d20d04

Please sign in to comment.