From 8c3d0e220b19594a34e727adeb9578d3e3b4e7e2 Mon Sep 17 00:00:00 2001 From: askuy Date: Tue, 24 Oct 2023 16:49:31 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=84=E4=BB=B6=E8=A7=A3=E6=9E=90=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=90=8D=E5=AD=97=E6=9C=8D=E5=8A=A1=EF=BC=8C=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E8=BE=BE=E6=A2=A6=E6=95=B0=E6=8D=AE=E5=BA=93=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E7=9A=84=E5=A4=A7=E5=B0=8F=E5=86=99=E6=94=B9=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- component.go | 12 +++++++++--- config.go | 3 +++ internal/dsn/clickhouse.go | 5 +++++ internal/dsn/mysql.go | 5 +++++ internal/dsn/postgres.go | 5 +++++ internal/dsn/sqlserver.go | 5 +++++ manager/dsn.go | 6 ++++++ option.go | 7 +++++++ 8 files changed, 45 insertions(+), 3 deletions(-) diff --git a/component.go b/component.go index aa5f601..04c385d 100644 --- a/component.go +++ b/component.go @@ -3,13 +3,12 @@ package egorm import ( "context" + "github.com/ego-component/egorm/manager" "github.com/go-sql-driver/mysql" "github.com/gotomicro/ego/core/elog" "gorm.io/gorm" "gorm.io/gorm/logger" "gorm.io/gorm/schema" - - "github.com/ego-component/egorm/manager" ) // PackageName ... @@ -51,12 +50,19 @@ func WithContext(ctx context.Context, db *Component) *Component { // newComponent ... func newComponent(compName string, dsnParser manager.DSNParser, config *config, elogger *elog.Component) (*Component, error) { // gorm的配置 - gormConfig := &gorm.Config{} + gormConfig := &gorm.Config{ + NamingStrategy: dsnParser.NamingStrategy(), // 使用组件的名字默认策略 + } // 如果没有开启gorm的原生日志,那么就丢弃掉,避免过多的日志信息 if !config.RawDebug { gormConfig.Logger = logger.Discard } + // 使用用户自定义的名字策略 + if config.namingStrategy != nil { + gormConfig.NamingStrategy = config.namingStrategy + } + db, err := gorm.Open(dsnParser.GetDialector(config.DSN), gormConfig) if err != nil { return nil, err diff --git a/config.go b/config.go index 4cd517a..74a72d2 100644 --- a/config.go +++ b/config.go @@ -4,6 +4,7 @@ import ( "time" "github.com/gotomicro/ego/core/util/xtime" + "gorm.io/gorm/schema" "github.com/ego-component/egorm/manager" ) @@ -30,6 +31,8 @@ type config struct { dsnCfg *manager.DSN // TLS 参数支持 Authentication Authentication + // NamingStrategy tables, columns naming strategy + namingStrategy schema.Namer // gorm naming strategy } // DefaultConfig 返回默认配置 diff --git a/internal/dsn/clickhouse.go b/internal/dsn/clickhouse.go index 3394b4f..54838a4 100644 --- a/internal/dsn/clickhouse.go +++ b/internal/dsn/clickhouse.go @@ -6,6 +6,7 @@ import ( "gorm.io/driver/clickhouse" "gorm.io/gorm" + "gorm.io/gorm/schema" "github.com/ego-component/egorm/manager" ) @@ -26,6 +27,10 @@ func (p *ClickHouseDSNParser) Scheme() string { return "clickhouse" } +func (p *ClickHouseDSNParser) NamingStrategy() schema.Namer { + return nil +} + func (p *ClickHouseDSNParser) GetDialector(dsn string) gorm.Dialector { return clickhouse.Open(dsn) } diff --git a/internal/dsn/mysql.go b/internal/dsn/mysql.go index 4c00057..267191e 100644 --- a/internal/dsn/mysql.go +++ b/internal/dsn/mysql.go @@ -8,6 +8,7 @@ import ( "github.com/ego-component/egorm/manager" "gorm.io/driver/mysql" "gorm.io/gorm" + "gorm.io/gorm/schema" ) var ( @@ -28,6 +29,10 @@ func (p *MysqlDSNParser) Scheme() string { return "mysql" } +func (p *MysqlDSNParser) NamingStrategy() schema.Namer { + return nil +} + func (m *MysqlDSNParser) GetDialector(dsn string) gorm.Dialector { return mysql.Open(dsn) } diff --git a/internal/dsn/postgres.go b/internal/dsn/postgres.go index 7376738..14e7902 100644 --- a/internal/dsn/postgres.go +++ b/internal/dsn/postgres.go @@ -8,6 +8,7 @@ import ( "github.com/ego-component/egorm/manager" "gorm.io/driver/postgres" "gorm.io/gorm" + "gorm.io/gorm/schema" ) var ( @@ -26,6 +27,10 @@ func (p *PostgresDSNParser) Scheme() string { return "postgres" } +func (p *PostgresDSNParser) NamingStrategy() schema.Namer { + return nil +} + func (p *PostgresDSNParser) GetDialector(dsn string) gorm.Dialector { return postgres.Open(dsn) } diff --git a/internal/dsn/sqlserver.go b/internal/dsn/sqlserver.go index 6158ab7..bc38b44 100644 --- a/internal/dsn/sqlserver.go +++ b/internal/dsn/sqlserver.go @@ -7,6 +7,7 @@ import ( "github.com/ego-component/egorm/manager" "gorm.io/driver/sqlserver" "gorm.io/gorm" + "gorm.io/gorm/schema" ) var ( @@ -24,6 +25,10 @@ func (p *SqlServerDSNParser) Scheme() string { return "mssql" } +func (p *SqlServerDSNParser) NamingStrategy() schema.Namer { + return nil +} + func (p *SqlServerDSNParser) GetDialector(dsn string) gorm.Dialector { return sqlserver.Open(dsn) } diff --git a/manager/dsn.go b/manager/dsn.go index 46b1049..47ecc72 100644 --- a/manager/dsn.go +++ b/manager/dsn.go @@ -2,6 +2,7 @@ package manager import ( "gorm.io/gorm" + "gorm.io/gorm/schema" ) // DSN ... @@ -18,4 +19,9 @@ type DSNParser interface { GetDialector(dsn string) gorm.Dialector ParseDSN(dsn string) (cfg *DSN, err error) Scheme() string + // NamingStrategy gorm naming strategy + // 该方法主要用于达梦数据库 + // 达梦数据库的表名和字段名都是大写的,gorm默认的策略是小写 + // 所以需要该方法来设置gorm的达梦命名策略 + NamingStrategy() schema.Namer } diff --git a/option.go b/option.go index df8c88f..affbae4 100644 --- a/option.go +++ b/option.go @@ -2,6 +2,7 @@ package egorm import ( "github.com/ego-component/egorm/manager" + "gorm.io/gorm/schema" ) // Option 可选项 @@ -21,6 +22,12 @@ func WithDSNParser(parser manager.DSNParser) Option { } } +func WithNamingStrategy(namingStrategy schema.Namer) Option { + return func(c *Container) { + c.config.namingStrategy = namingStrategy + } +} + // WithInterceptor 设置自定义拦截器 func WithInterceptor(is ...Interceptor) Option { return func(c *Container) {