-
Notifications
You must be signed in to change notification settings - Fork 962
-
I am trying to play around with plugins but I can't get it to work with "go" (referring to golang.Options below in SQLGen).
type SQL struct {
Name string `json:"name" yaml:"name"`
Engine Engine `json:"engine,omitempty" yaml:"engine"`
Schema Paths `json:"schema" yaml:"schema"`
Queries Paths `json:"queries" yaml:"queries"`
Database *Database `json:"database" yaml:"database"`
StrictFunctionChecks bool `json:"strict_function_checks" yaml:"strict_function_checks"`
StrictOrderBy *bool `json:"strict_order_by" yaml:"strict_order_by"`
Gen SQLGen `json:"gen" yaml:"gen"`
Codegen []Codegen `json:"codegen" yaml:"codegen"`
Rules []string `json:"rules" yaml:"rules"`
Analyzer Analyzer `json:"analyzer" yaml:"analyzer"`
}
type Codegen struct {
Out string `json:"out" yaml:"out"`
Plugin string `json:"plugin" yaml:"plugin"`
Options yaml.Node `json:"options" yaml:"options"`
}
type SQLGen struct {
Go *golang.Options `json:"go,omitempty" yaml:"go"`
JSON *SQLJSON `json:"json,omitempty" yaml:"json"`
}
type SQLJSON struct {
Out string `json:"out" yaml:"out"`
Indent string `json:"indent,omitempty" yaml:"indent"`
Filename string `json:"filename,omitempty" yaml:"filename"`
}
This is how my sqlc.yaml looks like
version: "2"
plugins:
- name: "custom-plugin"
process:
cmd: "./internal/db/sqlc/plugin-test"
sql:
- engine: "postgresql"
queries:
- "./queries"
schema: "../schema.sql"
gen:
go:
package: "sqlcgen"
out: "sqlcgen"
emit_prepared_queries: true
emit_interface: true
emit_exact_table_names: false
emit_empty_slices: true
emit_json_tags: true
emit_db_tags: true
json_tags_case_style: "camel"
sql_package: "pgx/v5"
codegen:
- out: "sqlcgen"
plugin: "custom-plugin"
I can get the plugin to execute. I extended the sample "sqlc-gen-json" plugin to modify the "query.Name" to prefix it with
something. I can see the modifications in the generated codegen_request.json file.
My use case is, that I have many .sql files with many queries already existing, now I need to add a specific WHERE clause to all the existing SELECT queries (say "WHERE created_by=@created_by"), so I am trying to write a plugin inside which I can modify the query.Text (query string) before the rest of the sqlc codegen happens.
Regarding this I have a few questions,
- Plugin seems to be only available under "codegen", how can I enable plugin under "gen" ?
- Or is there a way to invoke "Generate" method in "internal/codegen/golang/gen.go" with all the config options under "gen:go" mentioned in the sqlc.yaml (shared above)
- With my given use case, is there a better way to achieve the same. I dont want to manually edit all the existing queries, might have more use cases like this. Also want to this to be a framework of sorts so that everyone else in team writing queries doesn't have to worry about writing these common WHERE queries.
Thanks
Beta Was this translation helpful? Give feedback.