fix postgres

Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
Andrew Thornton 2023-03-13 12:59:19 +00:00
parent 1e50bd8705
commit 5b92ebc141
No known key found for this signature in database
GPG Key ID: 3CDE74631F13A748
1 changed files with 19 additions and 1 deletions

View File

@ -58,7 +58,7 @@ func (statement *Statement) GenUpsertSQL(doUpdate bool, columns []string, args [
}
switch statement.dialect.URI().DBType {
case schemas.SQLITE, schemas.POSTGRES:
case schemas.SQLITE:
write(" ON CONFLICT DO ")
if doUpdate {
write("UPDATE SET ", updateColumns[0], " = excluded.", updateColumns[0])
@ -68,6 +68,24 @@ func (statement *Statement) GenUpsertSQL(doUpdate bool, columns []string, args [
} else {
write("NOTHING")
}
case schemas.POSTGRES:
if doUpdate {
for _, index := range table.Indexes {
if index.Type != schemas.UniqueType {
continue
}
write(" ON CONFLICT (", quote(index.Cols[0]))
for _, col := range index.Cols[1:] {
write(", ", quote(col))
}
write(") DO UPDATE SET ", updateColumns[0], " = excluded.", updateColumns[0])
for _, column := range updateColumns[1:] {
write(", ", column, " = excluded.", column)
}
}
} else {
write(" ON CONFLICT DO NOTHING")
}
case schemas.MYSQL:
if doUpdate {
// FIXME: mysql >= 8.0.19 should use table alias