Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stand_alone_name configuration for each queries #2627

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/stand_alone_name/ding_depts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- name: Create :exec
insert into ding_depts (id, pid, title)
values ($1, $2, $3);


-- name: CountById :one
SELECT count(*)
FROM ding_depts
where id = $1;

-- name: ListByPid :many
SELECT *
FROM ding_depts
where pid = $1;
2 changes: 2 additions & 0 deletions examples/stand_alone_name/domains.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: GetByNo :one
SELECT * FROM domains where tag = $1;
27 changes: 27 additions & 0 deletions examples/stand_alone_name/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"context"
"github.com/sqlc-dev/sqlc/internal/cmd"
"os"
"path/filepath"
)

func main() {
stderr := os.Stderr
wd, err := os.Getwd()
if err != nil {
panic(err)
}
wd = filepath.Join(wd, "examples/stand_alone_name")
output, err := cmd.Generate(context.TODO(), cmd.Env{}, wd, "sqlc.yaml", stderr)
if err != nil {
panic(err)
}
for filename, source := range output {
os.MkdirAll(filepath.Dir(filename), 0755)
if err := os.WriteFile(filename, []byte(source), 0644); err != nil {
panic(err)
}
}
}
89 changes: 89 additions & 0 deletions examples/stand_alone_name/repo/ding_depts.sql.go

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

44 changes: 44 additions & 0 deletions examples/stand_alone_name/repo/domains.sql.go

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

31 changes: 31 additions & 0 deletions examples/stand_alone_name/repo/models.go

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

39 changes: 39 additions & 0 deletions examples/stand_alone_name/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
create table ding_depts
(
id bigint not null
constraint ding_depts_pk
primary key,
pid bigint,
title varchar
);

comment on table ding_depts is '钉钉部门';

comment on column ding_depts.id is '部门id';

comment on column ding_depts.pid is '上级部门id';

comment on column ding_depts.title is '部门名称';

create index ding_depts_pid_index
on ding_depts (pid);

create table domains
(
tag varchar not null
constraint domain_pk
primary key,
leaders character varying[] default '{}'::character varying[] not null,
configs jsonb default '{}'::jsonb not null
);

comment on table domains is '领域';

comment on column domains.tag is '领域标签';

comment on column domains.leaders is '领域领导';

comment on column domains.configs is '领域配置';

create index domain_leaders_index
on domains (leaders);
27 changes: 27 additions & 0 deletions examples/stand_alone_name/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: "2"
overrides:
go:
overrides:
- db_type: "jsonb"
go_type: "encoding/json.RawMessage"
sql:
- engine: "postgresql"
queries: "ding_depts.sql"
schema: "schema.sql"
gen:
go:
package: "repo"
sql_package: "pgx/v5"
out: "./repo"
stand_alone_name: "DingDept"
emit_json_tags: true
- engine: "postgresql"
queries: "domains.sql"
schema: "schema.sql"
gen:
go:
package: "repo"
sql_package: "pgx/v5"
out: "./repo"
stand_alone_name: "Domain"
emit_json_tags: true
1 change: 1 addition & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
InflectionExcludeTableNames: s.InflectionExcludeTableNames,
QueryParameterLimit: s.QueryParameterLimit,
OmitUnusedStructs: s.OmitUnusedStructs,
StandAloneName: s.StandAloneName,
}
}

Expand Down
9 changes: 6 additions & 3 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type tmplCtx struct {
EmitAllEnumValues bool
UsesCopyFrom bool
UsesBatch bool
StandAloneName string
}

func (t *tmplCtx) OutputQuery(sourceName string) bool {
Expand Down Expand Up @@ -135,6 +136,7 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
EmitMethodsWithDBArgument: golang.EmitMethodsWithDbArgument,
EmitEnumValidMethod: golang.EmitEnumValidMethod,
EmitAllEnumValues: golang.EmitAllEnumValues,
StandAloneName: golang.StandAloneName,
UsesCopyFrom: usesCopyFrom(queries),
UsesBatch: usesBatch(queries),
SQLDriver: parseDriver(golang.SqlPackage),
Expand Down Expand Up @@ -236,9 +238,10 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
if golang.OutputBatchFileName != "" {
batchFileName = golang.OutputBatchFileName
}

if err := execute(dbFileName, "dbFile"); err != nil {
return nil, err
if golang.StandAloneName == "" {
if err := execute(dbFileName, "dbFile"); err != nil {
return nil, err
}
}
if err := execute(modelsFileName, "modelsFile"); err != nil {
return nil, err
Expand Down
6 changes: 6 additions & 0 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,18 @@ func (i *importer) Imports(filename string) [][]ImportSpec {
case modelsFileName:
return mergeImports(i.modelImports())
case querierFileName:
if i.Settings.Go.StandAloneName != "" {
return mergeImports(i.dbImports(), i.interfaceImports(), i.queryImports(filename))
}
return mergeImports(i.interfaceImports())
case copyfromFileName:
return mergeImports(i.copyfromImports())
case batchFileName:
return mergeImports(i.batchImports())
default:
if i.Settings.Go.StandAloneName != "" {
return mergeImports(i.dbImports(), i.interfaceImports(), i.queryImports(filename))
}
return mergeImports(i.queryImports(filename))
}
}
Expand Down
4 changes: 3 additions & 1 deletion internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
if query.Cmd == "" {
continue
}

if req.Settings.Go.StandAloneName != "" {
query.Name = req.Settings.Go.StandAloneName + sdk.Title(query.Name)
}
var constantName string
if req.Settings.Go.EmitExportedQueries {
constantName = sdk.Title(query.Name)
Expand Down
18 changes: 9 additions & 9 deletions internal/codegen/golang/templates/pgx/dbCode.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{define "dbCodeTemplatePgx"}}

type DBTX interface {
type {{.StandAloneName}}DBTX interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
Expand All @@ -13,23 +13,23 @@ type DBTX interface {
}

{{ if .EmitMethodsWithDBArgument}}
func New() *Queries {
return &Queries{}
func New{{.StandAloneName}}() *{{.StandAloneName}}Queries {
return &{{.StandAloneName}}Queries{}
{{- else -}}
func New(db DBTX) *Queries {
return &Queries{db: db}
func New{{.StandAloneName}}(db {{.StandAloneName}}DBTX) *{{.StandAloneName}}Queries {
return &{{.StandAloneName}}Queries{db: db}
{{- end}}
}

type Queries struct {
type {{.StandAloneName}}Queries struct {
{{if not .EmitMethodsWithDBArgument}}
db DBTX
db {{.StandAloneName}}DBTX
{{end}}
}

{{if not .EmitMethodsWithDBArgument}}
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
return &Queries{
func (q *{{.StandAloneName}}Queries) WithTx(tx pgx.Tx) *{{.StandAloneName}}Queries {
return &{{.StandAloneName}}Queries{
db: tx,
}
}
Expand Down
Loading