Skip to content

Commit

Permalink
add SetCustomTableName method to the generated model
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Apr 5, 2024
1 parent ed73397 commit c9e4485
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
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
31 changes: 24 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 {{$alias.UpSingular}}TableName
}


// 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,20 @@ 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 " + tableName + " ({{.LQ}}%s{{.RQ}}) %%sVALUES (%s)%%s", 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 " + tableName + " %sDEFAULT VALUES%s"
{{else -}}
cache.query = "INSERT INTO {{$schemaTable}} () VALUES ()%s%s"
cache.query = "INSERT INTO " + tableName + " () 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 " + tableName + " WHERE %s", strings.Join(returnColumns, "{{.RQ}},{{.LQ}}"), 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 +149,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 +218,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

0 comments on commit c9e4485

Please sign in to comment.