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

New "editor" helper to avoid column lists for simple inserts/updates #629

Closed
wants to merge 4 commits into from
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
23 changes: 23 additions & 0 deletions templatebin/bindata.go

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

41 changes: 41 additions & 0 deletions templates/22_editor.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{{- $alias := .Aliases.Table .Table.Name}}

func (o *{{$alias.UpSingular}}) E() *{{$alias.UpSingular}}E {
return &{{$alias.UpSingular}}E{S:o}
}

func {{$alias.UpSingular}}Editor() *{{$alias.UpSingular}}E {
return &{{$alias.UpSingular}}E{S:&{{$alias.UpSingular}}{}}
}

type {{$alias.UpSingular}}E struct {
S *{{$alias.UpSingular}}
columns []string
}

func (e *{{$alias.UpSingular}}E) Insert({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) error {
return e.S.Insert(ctx, exec, boil.Whitelist(e.columns...))
}

func (e *{{$alias.UpSingular}}E) Update({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) {{if .NoRowsAffected}}error{{else}}(int64, error){{end -}} {
return e.S.Update(ctx, exec, boil.Whitelist(e.columns...))
}

{{range $column := .Table.Columns -}}
{{- $colAlias := $alias.Column $column.Name -}}
func (e *{{$alias.UpSingular}}E) {{$colAlias}}({{$column.Name | camelCase}} {{$column.Type}}) *{{$alias.UpSingular}}E {
e.S.{{$colAlias}} = {{$column.Name | camelCase}}
e.addColumn({{$alias.UpSingular}}Columns.{{$colAlias}})
return e
}

{{end -}}

func (e *{{$alias.UpSingular}}E) addColumn(column string) {
for _, col := range e.columns {
if col == column {
return
}
}
e.columns = append(e.columns, column)
}