Skip to content

Commit 07b90f1

Browse files
committed
add SetCustomTableName method to the generated model
1 parent ed73397 commit 07b90f1

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-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

+25-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,21 @@ 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 {{.LQ}}%s{{.RQ}} ({{.LQ}}%s{{.RQ}}) %%sVALUES (%s)%%s", tableName, 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 {{.LQ}}" + tableName + "{{.RQ}} %sDEFAULT VALUES%s"
8198
{{else -}}
82-
cache.query = "INSERT INTO {{$schemaTable}} () VALUES ()%s%s"
99+
cache.query = "INSERT INTO {{.LQ}}" + tableName + "{{.RQ}} () 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 {{.LQ}}%s{{.RQ}} WHERE %s", strings.Join(returnColumns, "{{.RQ}},{{.LQ}}"), tableName,
108+
strmangle.WhereClause("{{.LQ}}", "{{.RQ}}", {{if .Dialect.UseIndexPlaceholders}}1{{else}}0{{end}}, {{$alias.DownSingular}}PrimaryKeyColumns))
91109
{{else -}}
92110
{{if .Dialect.UseOutputClause -}}
93111
queryOutput = fmt.Sprintf("OUTPUT INSERTED.{{.LQ}}%s{{.RQ}} ", strings.Join(returnColumns, "{{.RQ}},INSERTED.{{.LQ}}"))
@@ -132,7 +150,7 @@ func (o *{{$alias.UpSingular}}) Insert({{if .NoContext}}exec boil.Executor{{else
132150
{{end -}}
133151
{{- end}}
134152
if err != nil {
135-
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
153+
return errors.Wrap(err, "{{.PkgName}}: unable to insert into " + tableName)
136154
}
137155

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

203221
if err != nil {
204-
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
222+
return errors.Wrap(err, "{{.PkgName}}: unable to insert into " + tableName)
205223
}
206224
{{end}}
207225

0 commit comments

Comments
 (0)