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

feature: set custom table name for insertion #1375

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions templates/main/00_struct.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type {{$alias.UpSingular}} struct {
R *{{$alias.DownSingular}}R `{{generateTags $.Tags $.RelationTag}}boil:"{{$.RelationTag}}" json:"{{$.RelationTag}}" toml:"{{$.RelationTag}}" yaml:"{{$.RelationTag}}"`
L {{$alias.DownSingular}}L `{{generateIgnoreTags $.Tags}}boil:"-" json:"-" toml:"-" yaml:"-"`
{{end -}}

// customTableName is for custom table name insertion
customTableName string
}

var {{$alias.UpSingular}}Columns = struct {
Expand Down
32 changes: 25 additions & 7 deletions templates/main/15_insert.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,28 @@ func (o *{{$alias.UpSingular}}) InsertGP({{if not .NoContext}}ctx context.Contex

{{end -}}


// SetCustomTableName sets the custom table name for insertion
func (o *{{$alias.UpSingular}}) SetCustomTableName(tableName string) {
o.customTableName = tableName
}

func (o *{{$alias.UpSingular}}) getTableName() string {
if len(o.customTableName) > 0 {
return o.customTableName
}

return "{{.Table.Name}}"
}


// Insert a single record using an executor.
// See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts.
func (o *{{$alias.UpSingular}}) Insert({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, columns boil.Columns) error {
var tableName = o.getTableName()

if o == nil {
return errors.New("{{.PkgName}}: no {{.Table.Name}} provided for insertion")
return errors.New("{{.PkgName}}: no " + tableName + " provided for insertion")
}

var err error
Expand Down Expand Up @@ -74,20 +91,21 @@ func (o *{{$alias.UpSingular}}) Insert({{if .NoContext}}exec boil.Executor{{else
return err
}
if len(wl) != 0 {
cache.query = fmt.Sprintf("INSERT INTO {{$schemaTable}} ({{.LQ}}%s{{.RQ}}) %%sVALUES (%s)%%s", strings.Join(wl, "{{.RQ}},{{.LQ}}"), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1))
cache.query = fmt.Sprintf("INSERT INTO {{.LQ}}%s{{.RQ}} ({{.LQ}}%s{{.RQ}}) %%sVALUES (%s)%%s", tableName, strings.Join(wl, "{{.RQ}},{{.LQ}}"), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1))
} else {
{{if .Dialect.UseDefaultKeyword -}}
cache.query = "INSERT INTO {{$schemaTable}} %sDEFAULT VALUES%s"
cache.query = "INSERT INTO {{.LQ}}" + tableName + "{{.RQ}} %sDEFAULT VALUES%s"
{{else -}}
cache.query = "INSERT INTO {{$schemaTable}} () VALUES ()%s%s"
cache.query = "INSERT INTO {{.LQ}}" + tableName + "{{.RQ}} () VALUES ()%s%s"
{{end -}}
}

var queryOutput, queryReturning string

if len(cache.retMapping) != 0 {
{{if .Dialect.UseLastInsertID -}}
cache.retQuery = fmt.Sprintf("SELECT {{.LQ}}%s{{.RQ}} FROM {{$schemaTable}} WHERE %s", strings.Join(returnColumns, "{{.RQ}},{{.LQ}}"), strmangle.WhereClause("{{.LQ}}", "{{.RQ}}", {{if .Dialect.UseIndexPlaceholders}}1{{else}}0{{end}}, {{$alias.DownSingular}}PrimaryKeyColumns))
cache.retQuery = fmt.Sprintf("SELECT {{.LQ}}%s{{.RQ}} FROM {{.LQ}}%s{{.RQ}} WHERE %s", strings.Join(returnColumns, "{{.RQ}},{{.LQ}}"), tableName,
strmangle.WhereClause("{{.LQ}}", "{{.RQ}}", {{if .Dialect.UseIndexPlaceholders}}1{{else}}0{{end}}, {{$alias.DownSingular}}PrimaryKeyColumns))
{{else -}}
{{if .Dialect.UseOutputClause -}}
queryOutput = fmt.Sprintf("OUTPUT INSERTED.{{.LQ}}%s{{.RQ}} ", strings.Join(returnColumns, "{{.RQ}},INSERTED.{{.LQ}}"))
Expand Down Expand Up @@ -132,7 +150,7 @@ func (o *{{$alias.UpSingular}}) Insert({{if .NoContext}}exec boil.Executor{{else
{{end -}}
{{- end}}
if err != nil {
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
return errors.Wrap(err, "{{.PkgName}}: unable to insert into " + tableName)
}

{{if $canLastInsertID -}}
Expand Down Expand Up @@ -201,7 +219,7 @@ func (o *{{$alias.UpSingular}}) Insert({{if .NoContext}}exec boil.Executor{{else
}

if err != nil {
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
return errors.Wrap(err, "{{.PkgName}}: unable to insert into " + tableName)
}
{{end}}

Expand Down
Loading