Allow insertion of only-defaulted tables
- Tables that are only comprised of default values and have nothing passed into them on insertion will no longer generate syntax errors.
This commit is contained in:
parent
a5cb765403
commit
8f96ba35d6
2 changed files with 25 additions and 6 deletions
|
@ -190,12 +190,17 @@ func BuildUpsertQueryMySQL(dia Dialect, tableName string, update, whitelist []st
|
||||||
buf := strmangle.GetBuffer()
|
buf := strmangle.GetBuffer()
|
||||||
defer strmangle.PutBuffer(buf)
|
defer strmangle.PutBuffer(buf)
|
||||||
|
|
||||||
|
var columns string
|
||||||
|
if len(whitelist) != 0 {
|
||||||
|
columns = strings.Join(whitelist, ", ")
|
||||||
|
}
|
||||||
|
|
||||||
if len(update) == 0 {
|
if len(update) == 0 {
|
||||||
fmt.Fprintf(
|
fmt.Fprintf(
|
||||||
buf,
|
buf,
|
||||||
"INSERT IGNORE INTO %s (%s) VALUES (%s)",
|
"INSERT IGNORE INTO %s (%s) VALUES (%s)",
|
||||||
tableName,
|
tableName,
|
||||||
strings.Join(whitelist, ", "),
|
columns,
|
||||||
strmangle.Placeholders(dia.IndexPlaceholders, len(whitelist), 1, 1),
|
strmangle.Placeholders(dia.IndexPlaceholders, len(whitelist), 1, 1),
|
||||||
)
|
)
|
||||||
return buf.String()
|
return buf.String()
|
||||||
|
@ -205,7 +210,7 @@ func BuildUpsertQueryMySQL(dia Dialect, tableName string, update, whitelist []st
|
||||||
buf,
|
buf,
|
||||||
"INSERT INTO %s (%s) VALUES (%s) ON DUPLICATE KEY UPDATE ",
|
"INSERT INTO %s (%s) VALUES (%s) ON DUPLICATE KEY UPDATE ",
|
||||||
tableName,
|
tableName,
|
||||||
strings.Join(whitelist, ", "),
|
columns,
|
||||||
strmangle.Placeholders(dia.IndexPlaceholders, len(whitelist), 1, 1),
|
strmangle.Placeholders(dia.IndexPlaceholders, len(whitelist), 1, 1),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -232,12 +237,18 @@ func BuildUpsertQueryPostgres(dia Dialect, tableName string, updateOnConflict bo
|
||||||
buf := strmangle.GetBuffer()
|
buf := strmangle.GetBuffer()
|
||||||
defer strmangle.PutBuffer(buf)
|
defer strmangle.PutBuffer(buf)
|
||||||
|
|
||||||
|
columns := "DEFAULT VALUES"
|
||||||
|
if len(whitelist) != 0 {
|
||||||
|
columns = fmt.Sprintf("(%s) VALUES (%s)",
|
||||||
|
strings.Join(whitelist, ", "),
|
||||||
|
strmangle.Placeholders(dia.IndexPlaceholders, len(whitelist), 1, 1))
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Fprintf(
|
fmt.Fprintf(
|
||||||
buf,
|
buf,
|
||||||
"INSERT INTO %s (%s) VALUES (%s) ON CONFLICT ",
|
"INSERT INTO %s %s ON CONFLICT ",
|
||||||
tableName,
|
tableName,
|
||||||
strings.Join(whitelist, ", "),
|
columns,
|
||||||
strmangle.Placeholders(dia.IndexPlaceholders, len(whitelist), 1, 1),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if !updateOnConflict || len(update) == 0 {
|
if !updateOnConflict || len(update) == 0 {
|
||||||
|
|
|
@ -65,7 +65,15 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cache.query = fmt.Sprintf("INSERT INTO {{$schemaTable}} ({{.LQ}}%s{{.RQ}}) VALUES (%s)", strings.Join(wl, "{{.LQ}},{{.RQ}}"), strmangle.Placeholders(dialect.IndexPlaceholders, len(wl), 1, 1))
|
if len(wl) != 0 {
|
||||||
|
cache.query = fmt.Sprintf("INSERT INTO {{$schemaTable}} ({{.LQ}}%s{{.RQ}}) VALUES (%s)", strings.Join(wl, "{{.LQ}},{{.RQ}}"), strmangle.Placeholders(dialect.IndexPlaceholders, len(wl), 1, 1))
|
||||||
|
} else {
|
||||||
|
{{if eq .DriverName "mysql" -}}
|
||||||
|
cache.query = "INSERT INTO {{$schemaTable}} () VALUES ()"
|
||||||
|
{{else -}}
|
||||||
|
cache.query = "INSERT INTO {{$schemaTable}} DEFAULT VALUES"
|
||||||
|
{{end -}}
|
||||||
|
}
|
||||||
|
|
||||||
if len(cache.retMapping) != 0 {
|
if len(cache.retMapping) != 0 {
|
||||||
{{if .UseLastInsertID -}}
|
{{if .UseLastInsertID -}}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue