diff --git a/internal/cmd/shim.go b/internal/cmd/shim.go index e879b33c80..6fee3737fe 100644 --- a/internal/cmd/shim.go +++ b/internal/cmd/shim.go @@ -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" ) @@ -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, } } diff --git a/internal/codegen/golang/gen.go b/internal/codegen/golang/gen.go index d1e07c10fa..f18c331615 100644 --- a/internal/codegen/golang/gen.go +++ b/internal/codegen/golang/gen.go @@ -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" ) @@ -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 { diff --git a/internal/codegen/kotlin/gen.go b/internal/codegen/kotlin/gen.go index d6cb0671a7..275b76c2b2 100644 --- a/internal/codegen/kotlin/gen.go +++ b/internal/codegen/kotlin/gen.go @@ -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" ) @@ -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{} diff --git a/internal/codegen/python/gen.go b/internal/codegen/python/gen.go index eea575b9ac..0d79a0ff2e 100644 --- a/internal/codegen/python/gen.go +++ b/internal/codegen/python/gen.go @@ -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" @@ -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( @@ -492,7 +491,7 @@ func moduleNode(source string) *pyast.Module { "versions:", ), poet.Comment( - " sqlc " + info.Version, + " sqlc " + version, ), }, } @@ -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)) @@ -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{ @@ -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 { @@ -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{} diff --git a/internal/plugin/codegen.pb.go b/internal/plugin/codegen.pb.go index 218ce916f4..78b9da7067 100644 --- a/internal/plugin/codegen.pb.go +++ b/internal/plugin/codegen.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.27.1 -// protoc v3.19.1 +// protoc v3.19.4 // source: plugin/codegen.proto package plugin @@ -1430,9 +1430,10 @@ type CodeGenRequest struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Settings *Settings `protobuf:"bytes,1,opt,name=settings,proto3" json:"settings,omitempty"` - Catalog *Catalog `protobuf:"bytes,2,opt,name=catalog,proto3" json:"catalog,omitempty"` - Queries []*Query `protobuf:"bytes,3,rep,name=queries,proto3" json:"queries,omitempty"` + Settings *Settings `protobuf:"bytes,1,opt,name=settings,proto3" json:"settings,omitempty"` + Catalog *Catalog `protobuf:"bytes,2,opt,name=catalog,proto3" json:"catalog,omitempty"` + Queries []*Query `protobuf:"bytes,3,rep,name=queries,proto3" json:"queries,omitempty"` + SqlcVersion string `protobuf:"bytes,4,opt,name=sqlc_version,proto3" json:"sqlc_version,omitempty"` } func (x *CodeGenRequest) Reset() { @@ -1488,6 +1489,13 @@ func (x *CodeGenRequest) GetQueries() []*Query { return nil } +func (x *CodeGenRequest) GetSqlcVersion() string { + if x != nil { + return x.SqlcVersion + } + return "" +} + type CodeGenResponse struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -1757,7 +1765,7 @@ var file_plugin_codegen_proto_rawDesc = []byte{ 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x12, 0x26, 0x0a, 0x06, 0x63, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x43, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x52, 0x06, 0x63, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x22, - 0x92, 0x01, 0x0a, 0x0e, 0x43, 0x6f, 0x64, 0x65, 0x47, 0x65, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, + 0xb6, 0x01, 0x0a, 0x0e, 0x43, 0x6f, 0x64, 0x65, 0x47, 0x65, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x2c, 0x0a, 0x08, 0x73, 0x65, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x53, 0x65, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x73, 0x52, 0x08, 0x73, 0x65, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x73, @@ -1766,14 +1774,16 @@ var file_plugin_codegen_proto_rawDesc = []byte{ 0x6f, 0x67, 0x52, 0x07, 0x63, 0x61, 0x74, 0x61, 0x6c, 0x6f, 0x67, 0x12, 0x27, 0x0a, 0x07, 0x71, 0x75, 0x65, 0x72, 0x69, 0x65, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, 0x07, 0x71, 0x75, 0x65, - 0x72, 0x69, 0x65, 0x73, 0x22, 0x35, 0x0a, 0x0f, 0x43, 0x6f, 0x64, 0x65, 0x47, 0x65, 0x6e, 0x52, - 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x22, 0x0a, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, - 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, - 0x46, 0x69, 0x6c, 0x65, 0x52, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x42, 0x2c, 0x5a, 0x2a, 0x67, - 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6b, 0x79, 0x6c, 0x65, 0x63, 0x6f, - 0x6e, 0x72, 0x6f, 0x79, 0x2f, 0x73, 0x71, 0x6c, 0x63, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, - 0x61, 0x6c, 0x2f, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x33, + 0x72, 0x69, 0x65, 0x73, 0x12, 0x22, 0x0a, 0x0c, 0x73, 0x71, 0x6c, 0x63, 0x5f, 0x76, 0x65, 0x72, + 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x73, 0x71, 0x6c, 0x63, + 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x22, 0x35, 0x0a, 0x0f, 0x43, 0x6f, 0x64, 0x65, + 0x47, 0x65, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x22, 0x0a, 0x05, 0x66, + 0x69, 0x6c, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x70, 0x6c, 0x75, + 0x67, 0x69, 0x6e, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x52, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x42, + 0x2c, 0x5a, 0x2a, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6b, 0x79, + 0x6c, 0x65, 0x63, 0x6f, 0x6e, 0x72, 0x6f, 0x79, 0x2f, 0x73, 0x71, 0x6c, 0x63, 0x2f, 0x69, 0x6e, + 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/internal/plugin/codegen_vtproto.pb.go b/internal/plugin/codegen_vtproto.pb.go index 7a5e1da987..299a2cbb27 100644 --- a/internal/plugin/codegen_vtproto.pb.go +++ b/internal/plugin/codegen_vtproto.pb.go @@ -1408,6 +1408,13 @@ func (m *CodeGenRequest) MarshalToSizedBufferVT(dAtA []byte) (int, error) { i -= len(m.unknownFields) copy(dAtA[i:], m.unknownFields) } + if len(m.SqlcVersion) > 0 { + i -= len(m.SqlcVersion) + copy(dAtA[i:], m.SqlcVersion) + i = encodeVarint(dAtA, i, uint64(len(m.SqlcVersion))) + i-- + dAtA[i] = 0x22 + } if len(m.Queries) > 0 { for iNdEx := len(m.Queries) - 1; iNdEx >= 0; iNdEx-- { size, err := m.Queries[iNdEx].MarshalToSizedBufferVT(dAtA[:i]) @@ -2097,6 +2104,10 @@ func (m *CodeGenRequest) SizeVT() (n int) { n += 1 + l + sov(uint64(l)) } } + l = len(m.SqlcVersion) + if l > 0 { + n += 1 + l + sov(uint64(l)) + } if m.unknownFields != nil { n += len(m.unknownFields) } @@ -5964,6 +5975,38 @@ func (m *CodeGenRequest) UnmarshalVT(dAtA []byte) error { return err } iNdEx = postIndex + case 4: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field SqlcVersion", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflow + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLength + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLength + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.SqlcVersion = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skip(dAtA[iNdEx:]) diff --git a/protos/plugin/codegen.proto b/protos/plugin/codegen.proto index 55d203f6c5..89ca60a8f3 100644 --- a/protos/plugin/codegen.proto +++ b/protos/plugin/codegen.proto @@ -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