package dialector

import (
	logger "eta/eta_mini_ht_api/common/component/log"
	"gorm.io/gorm"
)

type DBDial interface {
	GetDBName() string
	GetDial(dns string) gorm.Dialector
}
type Instance func() DBDial

var dbs = make(map[string]Instance)

func GetGormDial(driver string) (dial DBDial) {
	instanceFunc, ok := dbs[driver]
	if !ok {
		logger.Error("cache: unknown adapter name %q (forgot to import?)")
		return nil
	}
	return instanceFunc()
}
func Register(name string, adapter Instance) {
	if adapter == nil {
		panic("当前数据库类型不支持")
	}
	if _, ok := dbs[name]; ok {
		panic("请勿重复注册数据源" + name)
	}
	dbs[name] = adapter
}
