Skip to content

Commit

Permalink
Merge pull request #16 from askuy/feature/options_20231024
Browse files Browse the repository at this point in the history
组件解析增加名字服务,用于达梦数据库字段的大小写改写
  • Loading branch information
askuy authored Oct 24, 2023
2 parents 0b4eb07 + 8c3d0e2 commit 50d6e90
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 3 deletions.
12 changes: 9 additions & 3 deletions component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/gotomicro/ego/core/util/xtime"
"gorm.io/gorm/schema"

"github.com/ego-component/egorm/manager"
)
Expand All @@ -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 返回默认配置
Expand Down
5 changes: 5 additions & 0 deletions internal/dsn/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"gorm.io/driver/clickhouse"
"gorm.io/gorm"
"gorm.io/gorm/schema"

"github.com/ego-component/egorm/manager"
)
Expand All @@ -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)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/dsn/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ego-component/egorm/manager"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)

var (
Expand All @@ -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)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/dsn/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ego-component/egorm/manager"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)

var (
Expand All @@ -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)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/dsn/sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ego-component/egorm/manager"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)

var (
Expand All @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions manager/dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package manager

import (
"gorm.io/gorm"
"gorm.io/gorm/schema"
)

// DSN ...
Expand All @@ -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
}
7 changes: 7 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package egorm

import (
"github.com/ego-component/egorm/manager"
"gorm.io/gorm/schema"
)

// Option 可选项
Expand All @@ -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) {
Expand Down

0 comments on commit 50d6e90

Please sign in to comment.