31 lines
755 B
Go
31 lines
755 B
Go
// Filename: internal/db/dialect/postgres_adapter.go (全新文件)
|
|
package dialect
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
type postgresAdapter struct{}
|
|
|
|
func NewPostgresAdapter() DialectAdapter {
|
|
return &postgresAdapter{}
|
|
}
|
|
|
|
func (a *postgresAdapter) OnConflictUpdateAll(conflictColumns []string, updateColumns []string) clause.Expression {
|
|
conflictCols := make([]clause.Column, len(conflictColumns))
|
|
for i, col := range conflictColumns {
|
|
conflictCols[i] = clause.Column{Name: col}
|
|
}
|
|
|
|
assignments := make(map[string]interface{})
|
|
for _, col := range updateColumns {
|
|
assignments[col] = gorm.Expr(col + " + excluded." + col)
|
|
}
|
|
|
|
return clause.OnConflict{
|
|
Columns: conflictCols,
|
|
DoUpdates: clause.Assignments(assignments),
|
|
}
|
|
}
|