Database: Make dialects independent of xorm Engine (#69955)

* make dialects independent of xorm Engine

* goimports
This commit is contained in:
Dan Cech
2023-06-14 16:13:36 -04:00
committed by GitHub
parent e7d8d48407
commit a6279b2d62
9 changed files with 76 additions and 92 deletions
+16 -11
View File
@@ -61,8 +61,8 @@ type Dialect interface {
PreInsertId(table string, sess *xorm.Session) error
PostInsertId(table string, sess *xorm.Session) error
CleanDB() error
TruncateDBTables() error
CleanDB(engine *xorm.Engine) error
TruncateDBTables(engine *xorm.Engine) error
NoOpSQL() string
IsUniqueConstraintViolation(err error) bool
@@ -70,14 +70,17 @@ type Dialect interface {
IsDeadlock(err error) bool
Lock(LockCfg) error
Unlock(LockCfg) error
GetDBName(string) (string, error)
}
type LockCfg struct {
Session *xorm.Session
Key string
Timeout int
}
type dialectFunc func(*xorm.Engine) Dialect
type dialectFunc func() Dialect
var supportedDialects = map[string]dialectFunc{
MySQL: NewMysqlDialect,
@@ -88,18 +91,16 @@ var supportedDialects = map[string]dialectFunc{
Postgres + "WithHooks": NewPostgresDialect,
}
func NewDialect(engine *xorm.Engine) Dialect {
name := engine.DriverName()
if fn, exist := supportedDialects[name]; exist {
return fn(engine)
func NewDialect(driverName string) Dialect {
if fn, exist := supportedDialects[driverName]; exist {
return fn()
}
panic("Unsupported database type: " + name)
panic("Unsupported database type: " + driverName)
}
type BaseDialect struct {
dialect Dialect
engine *xorm.Engine
driverName string
}
@@ -302,7 +303,7 @@ func (b *BaseDialect) PostInsertId(table string, sess *xorm.Session) error {
return nil
}
func (b *BaseDialect) CleanDB() error {
func (b *BaseDialect) CleanDB(engine *xorm.Engine) error {
return nil
}
@@ -310,7 +311,7 @@ func (b *BaseDialect) NoOpSQL() string {
return "SELECT 0;"
}
func (b *BaseDialect) TruncateDBTables() error {
func (b *BaseDialect) TruncateDBTables(engine *xorm.Engine) error {
return nil
}
@@ -330,3 +331,7 @@ func (b *BaseDialect) Unlock(_ LockCfg) error {
func (b *BaseDialect) OrderBy(order string) string {
return order
}
func (b *BaseDialect) GetDBName(_ string) (string, error) {
return "", nil
}