Skip to content

Commit

Permalink
update template for code
Browse files Browse the repository at this point in the history
  • Loading branch information
jschaf committed Jan 26, 2024
1 parent 2b41dd1 commit 7ca7c67
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 270 deletions.
124 changes: 54 additions & 70 deletions example/author/query.sql.go

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

54 changes: 18 additions & 36 deletions internal/codegen/golang/query.gotemplate
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Querier interface {
var _ Querier = &DBQuerier{}

type DBQuerier struct {
conn genericConn // underlying Postgres transport to use
conn genericConn
}

// genericConn is a connection like *pgx.Conn, pgx.Tx, or *pgxpool.Pool.
Expand All @@ -53,48 +53,30 @@ const {{ $q.SQLVarName }} = {{ $q.EmitPreparedSQL }}
// {{ $q.Name }} implements Querier.{{ $q.Name }}.
func (q *DBQuerier) {{ $q.Name }}(ctx context.Context {{- $q.EmitParams }}) ({{ $q.EmitResultType }}, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "{{ $q.Name }}")
{{- if eq $q.ResultKind ":one" }}
row, err := q.conn.Query(ctx, {{ $q.SQLVarName }} {{- $q.EmitParamNames }})
{{- if eq $q.ResultKind ":exec" }}
cmdTag, err := q.conn.Exec(ctx, {{ $q.SQLVarName }} {{- $q.EmitParamNames }})
if err != nil {
return nil, fmt.Errorf("query {{ $q.Name }}: %w", err)
return {{ $q.EmitZeroResult }}, fmt.Errorf("exec query {{ $q.Name }}: %w", err)
}
return cmdTag, err
{{- else }}
rows, err := q.conn.Query(ctx, {{ $q.SQLVarName }} {{- $q.EmitParamNames }})
if err != nil {
return {{ $q.EmitZeroResult }}, fmt.Errorf("query {{ $q.Name }}: %w", err)
}

fds := rows.FieldDescriptions()
{{- range $i, $col := $q.Outputs -}}
{{- range $i, $col := $q.Outputs }}
{{ $q.EmitPlanScan $i $col}}
{{- end -}}
{{- end }}

return pgx.CollectExactlyOneRow(rows, func(row pgx.CollectableRow) ({{ $q.EmitSingularResultType }}, error) {
return {{ $q.EmitCollectionFunc }}(rows, func(row pgx.CollectableRow) ({{ $q.EmitSingularResultType }}, error) {
vals := row.RawValues()
{{ $q.EmitResultTypeInit "item" }}
{{- range $i, $col := $q.Outputs -}}
{{- $q.EmitScanColumn $i $col -}}
{{- end -}}
})
{{- else if eq $q.ResultKind ":many" }}
row, err := q.conn.Query(ctx, {{ $q.SQLVarName }} {{- $q.EmitParamNames }})
if err != nil {
return nil, fmt.Errorf("query {{ $q.Name }}: %w", err)
}

fds := rows.FieldDescriptions()
{{- range $i, $col := $q.Outputs -}}
{{ $q.EmitPlanScan $i $col}}
{{- end -}}

return pgx.CollectRows(rows, func(row pgx.CollectableRow) ({{ $q.EmitSingularResultType }}, error) {
vals := row.RawValues()
{{ $q.EmitResultTypeInit "item" }}
{{- range $i, $col := $q.Outputs -}}
{{- $q.EmitScanColumn $i $col -}}
{{- end -}}
var item {{ $q.EmitSingularResultType }}
{{- range $i, $col := $q.Outputs }}
{{ $q.EmitScanColumn $i $col }}
{{- end }}
return item, nil
})
{{- else if eq $q.ResultKind ":exec" }}
cmdTag, err := q.conn.Exec(ctx, {{ $q.SQLVarName }} {{- $q.EmitParamNames }})
if err != nil {
return cmdTag, fmt.Errorf("exec query {{ $q.Name }}: %w", err)
}
return cmdTag, err
{{- end }}
}
{{- end -}}
Expand Down
Loading

0 comments on commit 7ca7c67

Please sign in to comment.