New
This commit is contained in:
14
internal/db/dialect/dialect.go
Normal file
14
internal/db/dialect/dialect.go
Normal file
@@ -0,0 +1,14 @@
|
||||
// Filename: internal/db/dialect/dialect.go
|
||||
package dialect
|
||||
|
||||
import (
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// “通用语言”接口。
|
||||
type DialectAdapter interface {
|
||||
// OnConflictUpdateAll 生成一个完整的、适用于当前数据库的 "ON CONFLICT DO UPDATE" 子句。
|
||||
// conflictColumns: 唯一的约束列,例如 ["time", "group_id", "model_name"]
|
||||
// updateColumns: 需要累加更新的列,例如 ["request_count", "success_count", ...]
|
||||
OnConflictUpdateAll(conflictColumns []string, updateColumns []string) clause.Expression
|
||||
}
|
||||
30
internal/db/dialect/mysql_adapter.go
Normal file
30
internal/db/dialect/mysql_adapter.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Filename: internal/db/mysql_adapter.go
|
||||
package dialect
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type mysqlAdapter struct{}
|
||||
|
||||
func NewMySQLAdapter() DialectAdapter {
|
||||
return &mysqlAdapter{}
|
||||
}
|
||||
|
||||
func (a *mysqlAdapter) 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 + " + VALUES(" + col + ")")
|
||||
}
|
||||
|
||||
return clause.OnConflict{
|
||||
Columns: conflictCols,
|
||||
DoUpdates: clause.Assignments(assignments),
|
||||
}
|
||||
}
|
||||
30
internal/db/dialect/postgres_adapter.go
Normal file
30
internal/db/dialect/postgres_adapter.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// 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),
|
||||
}
|
||||
}
|
||||
30
internal/db/dialect/sqlite_adapter.go
Normal file
30
internal/db/dialect/sqlite_adapter.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Filename: internal/db/sqlite_adapter.go (全新文件 - 最终版)
|
||||
package dialect
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type sqliteAdapter struct{}
|
||||
|
||||
func NewSQLiteAdapter() DialectAdapter {
|
||||
return &sqliteAdapter{}
|
||||
}
|
||||
|
||||
func (a *sqliteAdapter) 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),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user