Skip to content

Commit

Permalink
add QuerierPackage
Browse files Browse the repository at this point in the history
  • Loading branch information
kfly8 committed Aug 7, 2024
1 parent 77961e0 commit 2adcacd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
25 changes: 20 additions & 5 deletions internal/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (


type tmplCtx struct {
ModelPackage string
ModelsPackage string
QuerierPackage string
Structs []Struct
SqlcVersion string
}
Expand Down Expand Up @@ -60,13 +61,19 @@ func Generate(ctx context.Context, req *plugin.GenerateRequest) (*plugin.Generat

func generate(req *plugin.GenerateRequest, options *opts.Options, structs []Struct) (*plugin.GenerateResponse, error) {

modelPackage := "Models"
if options.ModelPackage != "" {
modelPackage = options.ModelPackage
modelsPackage := "Models"
if options.ModelsPackage != "" {
modelsPackage = options.ModelsPackage
}

querierPackage := "Querier"
if options.ModelsPackage != "" {
querierPackage = options.QuerierPackage
}

tctx := tmplCtx{
ModelPackage: modelPackage,
ModelsPackage: modelsPackage,
QuerierPackage: querierPackage,
SqlcVersion: req.SqlcVersion,
Structs: structs,
}
Expand Down Expand Up @@ -114,6 +121,14 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, structs []Stru
return nil, err
}

querierFileName := "Querier.pm"
if options.OutputQuerierFileName != "" {
querierFileName = options.OutputQuerierFileName
}
if err := execute(querierFileName, "querierFile"); err != nil {
return nil, err
}

resp := plugin.GenerateResponse{}

for filename, code := range output {
Expand Down
4 changes: 3 additions & 1 deletion internal/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
)

type Options struct {
ModelPackage string `json:"model_package" yaml:"model_package"`
ModelsPackage string `json:"models_package" yaml:"models_package"`
QuerierPackage string `json:"querier_package" yaml:"querier_package"`
Rename map[string]string `json:"rename,omitempty" yaml:"rename"`
OutputModelsFileName string `json:"output_models_file_name,omitempty" yaml:"output_models_file_name"`
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
}

func Parse(req *plugin.GenerateRequest) (*Options, error) {
Expand Down
13 changes: 12 additions & 1 deletion internal/templates/template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Code generated by sqlc. DO NOT EDIT.
# versions:
# sqlc: {{.SqlcVersion}}
package {{.ModelPackage}};
package {{.ModelsPackage}};
use strict;
use warnings;

Expand All @@ -13,6 +13,17 @@ use Types::Standard -types;
1;
{{end}}

{{- define "querierFile" -}}
# Code generated by sqlc. DO NOT EDIT.
# versions:
# sqlc: {{.SqlcVersion}}
package {{.QuerierPackage}};
use strict;
use warnings;

1;
{{end}}

{{- define "modelsCode" -}}
{{- range .Structs -}}
{{- if .Comment}}{comment .Comment}{{end -}}
Expand Down

0 comments on commit 2adcacd

Please sign in to comment.