Skip to content

Commit

Permalink
add box results to generated table queries (#194)
Browse files Browse the repository at this point in the history
This adds support for the `box_results` flag to tables as well as queries.
  • Loading branch information
miniscruff authored May 15, 2022
1 parent 1c0755e commit de44900
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 23 deletions.
12 changes: 11 additions & 1 deletion examples/boxed_values/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {

pgClient := models.NewPGClient(conn)

_, err = pgClient.BulkInsertUser(ctx, []models.User{
ids, err := pgClient.BulkInsertUser(ctx, []models.User{
{ Nickname: "Jim", Email: "[email protected]" },
{ Nickname: "Bill", Email: "[email protected]" },
{ Nickname: "Stacy", Email: "[email protected]" },
Expand All @@ -30,11 +30,21 @@ func main() {
log.Fatal(err)
}

users, err := pgClient.ListUser(ctx, ids)
if err != nil {
log.Fatal(err)
}

fmt.Printf("type of user result: %T\n", users[0])
fmt.Println("name 1:", users[0].Nickname)

res, err := pgClient.GetUsersFromGmail(ctx)
if err != nil {
log.Fatal(err)
}

fmt.Printf("type of user result: %T\n", res[0])

fmt.Println("users from gmail:", len(res))
fmt.Println("name 1:", res[0].Nickname)
fmt.Println("name 2:", res[1].Nickname)
Expand Down
18 changes: 9 additions & 9 deletions examples/boxed_values/models/models.gen.go

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

1 change: 1 addition & 0 deletions examples/boxed_values/models/pggen.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[[table]]
name = "users"
box_results = true

[[query]]
name = "GetUsersFromGmail"
Expand Down
3 changes: 3 additions & 0 deletions examples/boxed_values/output.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
type of user result: *models.User
name 1: Jim
type of user result: *models.User
users from gmail: 2
name 1: Jim
name 2: Bill
12 changes: 7 additions & 5 deletions gen/gen_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ func (g *Generator) genInterfaces(into io.Writer, conf *config.DbConfig) error {
}

genCtx.Tables = append(genCtx.Tables, tableIfaceGenCtx{
GoName: tableInfo.Info.GoName,
PkeyType: tableInfo.Info.PkeyCol.TypeInfo.Name,
GoName: tableInfo.Info.GoName,
PkeyType: tableInfo.Info.PkeyCol.TypeInfo.Name,
BoxResults: tableInfo.Config.BoxResults,
})
}

Expand Down Expand Up @@ -54,8 +55,9 @@ func (g *Generator) genInterfaces(into io.Writer, conf *config.DbConfig) error {
}

type tableIfaceGenCtx struct {
GoName string
PkeyType string
GoName string
PkeyType string
BoxResults bool
}

type ifaceGenCtx struct {
Expand All @@ -75,7 +77,7 @@ type DBQueries interface {
{{ range .Tables }}
// {{ .GoName }} methods
Get{{ .GoName }}(ctx context.Context, id {{ .PkeyType }}, opts ...pggen.GetOpt) (*{{ .GoName }}, error)
List{{ .GoName }}(ctx context.Context, ids []{{ .PkeyType }}, opts ...pggen.ListOpt) ([]{{ .GoName }}, error)
List{{ .GoName }}(ctx context.Context, ids []{{ .PkeyType }}, opts ...pggen.ListOpt) ([]{{- if .BoxResults }}*{{- end }}{{ .GoName }}, error)
Insert{{ .GoName }}(ctx context.Context, value *{{ .GoName }}, opts ...pggen.InsertOpt) ({{ .PkeyType }}, error)
BulkInsert{{ .GoName }}(ctx context.Context, values []{{ .GoName }}, opts ...pggen.InsertOpt) ([]{{ .PkeyType }}, error)
Update{{ .GoName }}(ctx context.Context, value *{{ .GoName }}, fieldMask pggen.FieldSet, opts ...pggen.UpdateOpt) (ret {{ .PkeyType }}, err error)
Expand Down
16 changes: 8 additions & 8 deletions gen/gen_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,42 +118,42 @@ func (p *pgClientImpl) get{{ .GoName }}(
// List{{ .GoName }} always returns the same number of records as were
// requested, so this is safe.
return &values[0], err
return {{ if (not .Meta.Config.BoxResults) }}&{{- end }}values[0], err
}
func (p *PGClient) List{{ .GoName }}(
ctx context.Context,
ids []{{ .PkeyCol.TypeInfo.Name }},
opts ...pggen.ListOpt,
) (ret []{{ .GoName }}, err error) {
) (ret []{{- if .Meta.Config.BoxResults }}*{{- end }}{{ .GoName }}, err error) {
return p.impl.list{{ .GoName }}(ctx, ids, false /* isGet */, opts...)
}
func (tx *TxPGClient) List{{ .GoName }}(
ctx context.Context,
ids []{{ .PkeyCol.TypeInfo.Name }},
opts ...pggen.ListOpt,
) (ret []{{ .GoName }}, err error) {
) (ret []{{- if .Meta.Config.BoxResults }}*{{- end }}{{ .GoName }}, err error) {
return tx.impl.list{{ .GoName }}(ctx, ids, false /* isGet */, opts...)
}
func (conn *ConnPGClient) List{{ .GoName }}(
ctx context.Context,
ids []{{ .PkeyCol.TypeInfo.Name }},
opts ...pggen.ListOpt,
) (ret []{{ .GoName }}, err error) {
) (ret []{{- if .Meta.Config.BoxResults }}*{{- end }}{{ .GoName }}, err error) {
return conn.impl.list{{ .GoName }}(ctx, ids, false /* isGet */, opts...)
}
func (p *pgClientImpl) list{{ .GoName }}(
ctx context.Context,
ids []{{ .PkeyCol.TypeInfo.Name }},
isGet bool,
opts ...pggen.ListOpt,
) (ret []{{ .GoName }}, err error) {
) (ret []{{- if .Meta.Config.BoxResults }}*{{- end }}{{ .GoName }}, err error) {
opt := pggen.ListOptions{}
for _, o := range opts {
o(&opt)
}
if len(ids) == 0 {
return []{{ .GoName }}{}, nil
return []{{- if .Meta.Config.BoxResults }}*{{- end }}{{ .GoName }}{}, nil
}
rows, err := p.queryContext(
Expand All @@ -180,14 +180,14 @@ func (p *pgClientImpl) list{{ .GoName }}(
}
}()
ret = make([]{{ .GoName }}, 0, len(ids))
ret = make([]{{- if .Meta.Config.BoxResults }}*{{- end }}{{ .GoName }}, 0, len(ids))
for rows.Next() {
var value {{ .GoName }}
err = value.Scan(ctx, p.client, rows)
if err != nil {
return nil, p.client.errorConverter(err)
}
ret = append(ret, value)
ret = append(ret, {{- if .Meta.Config.BoxResults }}&{{- end }}value)
}
if len(ret) != len(ids) {
Expand Down
3 changes: 3 additions & 0 deletions gen/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ type TableConfig struct {
// be (de)serialized into. By default, all `json` and `jsonb` columns will
// become byte arrays.
JsonTypes []JsonType `toml:"json_type"`
// If true, queries that return sliced results will return a slice of pointers.
// Otherwise, it will be a slice of struct values.
BoxResults bool `toml:"box_results"`
}

// An explicitly configured foreign key relationship which can be attached
Expand Down

0 comments on commit de44900

Please sign in to comment.