Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
pkgs.git-cliff
pkgs.govulncheck
pkgs.gopls
pkgs.golint
pkgs.mysql-shell
pkgs.postgresql_15
pkgs.python311
Expand Down
17 changes: 14 additions & 3 deletions internal/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -16,7 +17,7 @@ import (
"google.golang.org/grpc/status"

"github.com/sqlc-dev/sqlc/internal/codegen/golang"
"github.com/sqlc-dev/sqlc/internal/codegen/json"
genjson "github.com/sqlc-dev/sqlc/internal/codegen/json"
"github.com/sqlc-dev/sqlc/internal/compiler"
"github.com/sqlc-dev/sqlc/internal/config"
"github.com/sqlc-dev/sqlc/internal/config/convert"
Expand Down Expand Up @@ -166,7 +167,7 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer
Gen: config.SQLGen{JSON: sql.Gen.JSON},
})
}
for i, _ := range sql.Codegen {
for i := range sql.Codegen {
pairs = append(pairs, outPair{
SQL: sql,
Plugin: &sql.Codegen[i],
Expand Down Expand Up @@ -399,10 +400,20 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql outPair, re
case sql.Gen.Go != nil:
out = combo.Go.Out
handler = ext.HandleFunc(golang.Generate)
opts, err := json.Marshal(sql.Gen.Go)
if err != nil {
return "", nil, fmt.Errorf("opts marshal failed: %w", err)
}
req.PluginOptions = opts

case sql.Gen.JSON != nil:
out = combo.JSON.Out
handler = ext.HandleFunc(json.Generate)
handler = ext.HandleFunc(genjson.Generate)
opts, err := json.Marshal(sql.Gen.JSON)
if err != nil {
return "", nil, fmt.Errorf("opts marshal failed: %w", err)
}
req.Settings.Codegen.Options = opts

default:
return "", nil, fmt.Errorf("missing language backend")
Expand Down
49 changes: 0 additions & 49 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ func pluginSettings(r *compiler.Result, cs config.CombinedSettings) *plugin.Sett
Overrides: over,
Rename: cs.Rename,
Codegen: pluginCodegen(cs.Codegen),
Go: pluginGoCode(cs.Go),
Json: pluginJSONCode(cs.JSON),
}
}

Expand All @@ -75,45 +73,6 @@ func pluginCodegen(s config.Codegen) *plugin.Codegen {
}
}

func pluginGoCode(s config.SQLGo) *plugin.GoCode {
if s.QueryParameterLimit == nil {
s.QueryParameterLimit = new(int32)
*s.QueryParameterLimit = 1
}

return &plugin.GoCode{
EmitInterface: s.EmitInterface,
EmitJsonTags: s.EmitJSONTags,
JsonTagsIdUppercase: s.JsonTagsIDUppercase,
EmitDbTags: s.EmitDBTags,
EmitPreparedQueries: s.EmitPreparedQueries,
EmitExactTableNames: s.EmitExactTableNames,
EmitEmptySlices: s.EmitEmptySlices,
EmitExportedQueries: s.EmitExportedQueries,
EmitResultStructPointers: s.EmitResultStructPointers,
EmitParamsStructPointers: s.EmitParamsStructPointers,
EmitMethodsWithDbArgument: s.EmitMethodsWithDBArgument,
EmitPointersForNullTypes: s.EmitPointersForNullTypes,
EmitEnumValidMethod: s.EmitEnumValidMethod,
EmitAllEnumValues: s.EmitAllEnumValues,
JsonTagsCaseStyle: s.JSONTagsCaseStyle,
Package: s.Package,
Out: s.Out,
SqlPackage: s.SQLPackage,
SqlDriver: s.SQLDriver,
OutputDbFileName: s.OutputDBFileName,
OutputBatchFileName: s.OutputBatchFileName,
OutputModelsFileName: s.OutputModelsFileName,
OutputQuerierFileName: s.OutputQuerierFileName,
OutputCopyfromFileName: s.OutputCopyFromFileName,
OutputFilesSuffix: s.OutputFilesSuffix,
InflectionExcludeTableNames: s.InflectionExcludeTableNames,
QueryParameterLimit: s.QueryParameterLimit,
OmitUnusedStructs: s.OmitUnusedStructs,
BuildTags: s.BuildTags,
}
}

func pluginGoType(o config.Override) *plugin.ParsedGoType {
// Note that there is a slight mismatch between this and the
// proto api. The GoType on the override is the unparsed type,
Expand All @@ -128,14 +87,6 @@ func pluginGoType(o config.Override) *plugin.ParsedGoType {
}
}

func pluginJSONCode(s config.SQLJSON) *plugin.JSONCode {
return &plugin.JSONCode{
Out: s.Out,
Indent: s.Indent,
Filename: s.Filename,
}
}

func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
var schemas []*plugin.Schema
for _, s := range c.Schemas {
Expand Down
6 changes: 3 additions & 3 deletions internal/codegen/golang/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func TagsToString(tags map[string]string) string {
return strings.Join(tagParts, " ")
}

func JSONTagName(name string, settings *plugin.Settings) string {
style := settings.Go.JsonTagsCaseStyle
idUppercase := settings.Go.JsonTagsIdUppercase
func JSONTagName(name string, options *opts) string {
style := options.JsonTagsCaseStyle
idUppercase := options.JsonTagsIdUppercase
if style == "" || style == "none" {
return name
} else {
Expand Down
73 changes: 39 additions & 34 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,55 +103,60 @@ func (t *tmplCtx) codegenQueryRetval(q Query) (string, error) {
}

func Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
enums := buildEnums(req)
structs := buildStructs(req)
queries, err := buildQueries(req, structs)
options, err := parseOpts(req)
if err != nil {
return nil, err
}

if req.Settings.Go.OmitUnusedStructs {
enums := buildEnums(req, options)
structs := buildStructs(req, options)
queries, err := buildQueries(req, options, structs)
Comment on lines +111 to +113
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think some of these funcs won't need to take the req as a param at all, once we figure out how to handle overrides and renames.

if err != nil {
return nil, err
}

if options.OmitUnusedStructs {
enums, structs = filterUnusedStructs(enums, structs, queries)
}

return generate(req, enums, structs, queries)
return generate(req, options, enums, structs, queries)
}

func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, queries []Query) (*plugin.CodeGenResponse, error) {
func generate(req *plugin.CodeGenRequest, options *opts, enums []Enum, structs []Struct, queries []Query) (*plugin.CodeGenResponse, error) {
i := &importer{
Settings: req.Settings,
Options: options,
Queries: queries,
Enums: enums,
Structs: structs,
}

golang := req.Settings.Go
tctx := tmplCtx{
EmitInterface: golang.EmitInterface,
EmitJSONTags: golang.EmitJsonTags,
JsonTagsIDUppercase: golang.JsonTagsIdUppercase,
EmitDBTags: golang.EmitDbTags,
EmitPreparedQueries: golang.EmitPreparedQueries,
EmitEmptySlices: golang.EmitEmptySlices,
EmitMethodsWithDBArgument: golang.EmitMethodsWithDbArgument,
EmitEnumValidMethod: golang.EmitEnumValidMethod,
EmitAllEnumValues: golang.EmitAllEnumValues,
EmitInterface: options.EmitInterface,
EmitJSONTags: options.EmitJsonTags,
JsonTagsIDUppercase: options.JsonTagsIdUppercase,
EmitDBTags: options.EmitDbTags,
EmitPreparedQueries: options.EmitPreparedQueries,
EmitEmptySlices: options.EmitEmptySlices,
EmitMethodsWithDBArgument: options.EmitMethodsWithDbArgument,
EmitEnumValidMethod: options.EmitEnumValidMethod,
EmitAllEnumValues: options.EmitAllEnumValues,
UsesCopyFrom: usesCopyFrom(queries),
UsesBatch: usesBatch(queries),
SQLDriver: parseDriver(golang.SqlPackage),
SQLDriver: parseDriver(options.SqlPackage),
Q: "`",
Package: golang.Package,
Package: options.Package,
Enums: enums,
Structs: structs,
SqlcVersion: req.SqlcVersion,
BuildTags: golang.BuildTags,
BuildTags: options.BuildTags,
}

if tctx.UsesCopyFrom && !tctx.SQLDriver.IsPGX() && golang.SqlDriver != SQLDriverGoSQLDriverMySQL {
if tctx.UsesCopyFrom && !tctx.SQLDriver.IsPGX() && options.SqlDriver != SQLDriverGoSQLDriverMySQL {
return nil, errors.New(":copyfrom is only supported by pgx and github.com/go-sql-driver/mysql")
}

if tctx.UsesCopyFrom && golang.SqlDriver == SQLDriverGoSQLDriverMySQL {
if tctx.UsesCopyFrom && options.SqlDriver == SQLDriverGoSQLDriverMySQL {
if err := checkNoTimesForMySQLCopyFrom(queries); err != nil {
return nil, err
}
Expand Down Expand Up @@ -208,8 +213,8 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
return fmt.Errorf("source error: %w", err)
}

if templateName == "queryFile" && golang.OutputFilesSuffix != "" {
name += golang.OutputFilesSuffix
if templateName == "queryFile" && options.OutputFilesSuffix != "" {
name += options.OutputFilesSuffix
}

if !strings.HasSuffix(name, ".go") {
Expand All @@ -220,25 +225,25 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
}

dbFileName := "db.go"
if golang.OutputDbFileName != "" {
dbFileName = golang.OutputDbFileName
if options.OutputDbFileName != "" {
dbFileName = options.OutputDbFileName
}
modelsFileName := "models.go"
if golang.OutputModelsFileName != "" {
modelsFileName = golang.OutputModelsFileName
if options.OutputModelsFileName != "" {
modelsFileName = options.OutputModelsFileName
}
querierFileName := "querier.go"
if golang.OutputQuerierFileName != "" {
querierFileName = golang.OutputQuerierFileName
if options.OutputQuerierFileName != "" {
querierFileName = options.OutputQuerierFileName
}
copyfromFileName := "copyfrom.go"
if golang.OutputCopyfromFileName != "" {
copyfromFileName = golang.OutputCopyfromFileName
if options.OutputCopyfromFileName != "" {
copyfromFileName = options.OutputCopyfromFileName
}

batchFileName := "batch.go"
if golang.OutputBatchFileName != "" {
batchFileName = golang.OutputBatchFileName
if options.OutputBatchFileName != "" {
batchFileName = options.OutputBatchFileName
}

if err := execute(dbFileName, "dbFile"); err != nil {
Expand All @@ -247,7 +252,7 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
if err := execute(modelsFileName, "modelsFile"); err != nil {
return nil, err
}
if golang.EmitInterface {
if options.EmitInterface {
if err := execute(querierFileName, "interfaceFile"); err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/codegen/golang/go_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func addExtraGoStructTags(tags map[string]string, req *plugin.CodeGenRequest, co
}
}

func goType(req *plugin.CodeGenRequest, col *plugin.Column) string {
func goType(req *plugin.CodeGenRequest, options *opts, col *plugin.Column) string {
// Check if the column's type has been overridden
for _, oride := range req.Settings.Overrides {
if oride.GoType.TypeName == "" {
Expand All @@ -49,7 +49,7 @@ func goType(req *plugin.CodeGenRequest, col *plugin.Column) string {
return oride.GoType.TypeName
}
}
typ := goInnerType(req, col)
typ := goInnerType(req, options, col)
if col.IsSqlcSlice {
return "[]" + typ
}
Expand All @@ -59,7 +59,7 @@ func goType(req *plugin.CodeGenRequest, col *plugin.Column) string {
return typ
}

func goInnerType(req *plugin.CodeGenRequest, col *plugin.Column) string {
func goInnerType(req *plugin.CodeGenRequest, options *opts, col *plugin.Column) string {
columnType := sdk.DataType(col.Type)
notNull := col.NotNull || col.IsArray

Expand All @@ -78,7 +78,7 @@ func goInnerType(req *plugin.CodeGenRequest, col *plugin.Column) string {
case "mysql":
return mysqlType(req, col)
case "postgresql":
return postgresType(req, col)
return postgresType(req, options, col)
case "sqlite":
return sqliteType(req, col)
default:
Expand Down
Loading