FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

refactor(pg): extract input values template by janisz · Pull Request #7108 · stackrox/stackrox · GitHub

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .tpl  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
35 changes: 15 additions & 20 deletions tools/generate-helpers/pg-table-bindings/store.go.tpl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@ func isUpsertAllowed(ctx context.Context, objs ...*storeType) error {
{{- define "insertFunctionName"}}{{- $schema := . }}insertInto{{$schema.Table|upperCamelCase}}
{{- end}}

{{- define "insertValues"}}{{- $schema := . -}}
{{- range $field := $schema.DBColumnFields -}}
{{- if eq $field.DataType "datetime" }}
pgutils.NilOrTime({{$field.Getter "obj"}}),
{{- else if eq $field.SQLType "uuid" }}
pgutils.NilOrUUID({{$field.Getter "obj"}}),
{{- else if eq $field.DataType "map" }}
pgutils.EmptyOrMap({{$field.Getter "obj"}}),
{{- else }}
{{$field.Getter "obj"}},{{end}}
{{- end}}
{{- end}}

{{- define "insertObject"}}
{{- $schema := .schema }}
func {{ template "insertFunctionName" $schema }}({{ if eq (len $schema.Children) 0 }}_{{ else }}ctx{{ end }} context.Context, batch *pgx.Batch, obj {{$schema.Type}}{{ range $field := $schema.FieldsDeterminedByParent }}, {{$field.Name}} {{$field.Type}}{{end}}) error {
Expand All @@ -192,16 +205,7 @@ func {{ template "insertFunctionName" $schema }}({{ if eq (len $schema.Children)

values := []interface{} {
// parent primary keys start
{{- range $field := $schema.DBColumnFields -}}
{{- if eq $field.DataType "datetime" }}
pgutils.NilOrTime({{$field.Getter "obj"}}),
{{- else if eq $field.SQLType "uuid" }}
pgutils.NilOrUUID({{$field.Getter "obj"}}),
{{- else if eq $field.DataType "map" }}
pgutils.EmptyOrMap({{$field.Getter "obj"}}),
{{- else }}
{{$field.Getter "obj"}},{{end}}
{{- end}}
{{- template "insertValues" $schema }}
}

finalStr := "INSERT INTO {{$schema.Table}} ({{template "commaSeparatedColumns" $schema.DBColumnFields }}) VALUES({{ valueExpansion (len $schema.DBColumnFields) }}) ON CONFLICT({{template "commaSeparatedColumns" $schema.PrimaryKeys}}) DO UPDATE SET {{template "updateExclusions" $schema.DBColumnFields}}"
Expand Down Expand Up @@ -264,16 +268,7 @@ func {{ template "copyFunctionName" $schema }}(ctx context.Context, s pgSearch.D
{{end}}

inputRows = append(inputRows, []interface{}{
{{- range $index, $field := $schema.DBColumnFields }}
{{- if eq $field.DataType "datetime"}}
pgutils.NilOrTime({{$field.Getter "obj"}}),
{{- else if eq $field.SQLType "uuid" }}
pgutils.NilOrUUID({{$field.Getter "obj"}}),
{{- else if eq $field.DataType "map" }}
pgutils.EmptyOrMap({{$field.Getter "obj"}}),
{{- else }}
{{$field.Getter "obj"}},{{end}}
{{- end}}
{{- template "insertValues" $schema }}
})

{{ if not $schema.Parent }}
Expand Down

Back | FazBrowse Home | New Git URL