Skip to content

Commit 4064e81

Browse files
committed
add SetCustomTableName method to the generated model
1 parent ed73397 commit 4064e81

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

templates/main/00_struct.go.tpl

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ type {{$alias.UpSingular}} struct {
4545
R *{{$alias.DownSingular}}R `{{generateTags $.Tags $.RelationTag}}boil:"{{$.RelationTag}}" json:"{{$.RelationTag}}" toml:"{{$.RelationTag}}" yaml:"{{$.RelationTag}}"`
4646
L {{$alias.DownSingular}}L `{{generateIgnoreTags $.Tags}}boil:"-" json:"-" toml:"-" yaml:"-"`
4747
{{end -}}
48+
49+
// customTableName is for custom table name insertion
50+
customTableName string
4851
}
4952
5053
var {{$alias.UpSingular}}Columns = struct {

templates/main/15_insert.go.tpl

+24-7
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,28 @@ func (o *{{$alias.UpSingular}}) InsertGP({{if not .NoContext}}ctx context.Contex
3131

3232
{{end -}}
3333

34+
35+
// SetCustomTableName sets the custom table name for insertion
36+
func (o *{{$alias.UpSingular}}) SetCustomTableName(tableName string) {
37+
o.customTableName = tableName
38+
}
39+
40+
func (o *{{$alias.UpSingular}}) getTableName() string {
41+
if len(o.customTableName) > 0 {
42+
return o.customTableName
43+
}
44+
45+
return {{$alias.UpSingular}}TableName
46+
}
47+
48+
3449
// Insert a single record using an executor.
3550
// See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts.
3651
func (o *{{$alias.UpSingular}}) Insert({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, columns boil.Columns) error {
52+
var tableName = o.getTableName()
53+
3754
if o == nil {
38-
return errors.New("{{.PkgName}}: no {{.Table.Name}} provided for insertion")
55+
return errors.New("{{.PkgName}}: no " + tableName + " provided for insertion")
3956
}
4057

4158
var err error
@@ -74,20 +91,20 @@ func (o *{{$alias.UpSingular}}) Insert({{if .NoContext}}exec boil.Executor{{else
7491
return err
7592
}
7693
if len(wl) != 0 {
77-
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))
94+
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))
7895
} else {
7996
{{if .Dialect.UseDefaultKeyword -}}
80-
cache.query = "INSERT INTO {{$schemaTable}} %sDEFAULT VALUES%s"
97+
cache.query = "INSERT INTO " + tableName + " %sDEFAULT VALUES%s"
8198
{{else -}}
82-
cache.query = "INSERT INTO {{$schemaTable}} () VALUES ()%s%s"
99+
cache.query = "INSERT INTO " + tableName + " () VALUES ()%s%s"
83100
{{end -}}
84101
}
85102

86103
var queryOutput, queryReturning string
87104

88105
if len(cache.retMapping) != 0 {
89106
{{if .Dialect.UseLastInsertID -}}
90-
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))
107+
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))
91108
{{else -}}
92109
{{if .Dialect.UseOutputClause -}}
93110
queryOutput = fmt.Sprintf("OUTPUT INSERTED.{{.LQ}}%s{{.RQ}} ", strings.Join(returnColumns, "{{.RQ}},INSERTED.{{.LQ}}"))
@@ -132,7 +149,7 @@ func (o *{{$alias.UpSingular}}) Insert({{if .NoContext}}exec boil.Executor{{else
132149
{{end -}}
133150
{{- end}}
134151
if err != nil {
135-
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
152+
return errors.Wrap(err, "{{.PkgName}}: unable to insert into " + tableName)
136153
}
137154

138155
{{if $canLastInsertID -}}
@@ -201,7 +218,7 @@ func (o *{{$alias.UpSingular}}) Insert({{if .NoContext}}exec boil.Executor{{else
201218
}
202219

203220
if err != nil {
204-
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
221+
return errors.Wrap(err, "{{.PkgName}}: unable to insert into " + tableName)
205222
}
206223
{{end}}
207224

0 commit comments

Comments
 (0)