Skip to content

Commit

Permalink
feat: test goner/gorm and update
Browse files Browse the repository at this point in the history
  • Loading branch information
dapeng committed Nov 23, 2024
1 parent d3b6393 commit e8bff7e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 65 deletions.
32 changes: 15 additions & 17 deletions goner/gorm/clickhouse/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,22 @@ type dial struct {
defaultTableEngineOpts string `gone:"gorm.clickhouse.default-table-engine-opts,default="`
}

func (d *dial) AfterRevive() (err error) {
if d.Dialector != nil {
return gone.NewInnerError("gorm.mysql.dialer has been initialized", gone.StartError)
func (d *dial) Apply(*gorm.Config) error {
if d.Dialector == nil {
d.Dialector = clickhouse.New(clickhouse.Config{
DriverName: d.driverName,
DSN: d.dsn,
DisableDatetimePrecision: d.disableDatetimePrecision,
DontSupportRenameColumn: d.dontSupportRenameColumn,
DontSupportColumnPrecision: d.dontSupportColumnPrecision,
DontSupportEmptyDefaultValue: d.dontSupportEmptyDefaultValue,
SkipInitializeWithVersion: d.skipInitializeWithVersion,
DefaultGranularity: d.defaultGranularity,
DefaultCompression: d.defaultCompression,
DefaultIndexType: d.defaultIndexType,
DefaultTableEngineOpts: d.defaultTableEngineOpts,
})
}

d.Dialector = clickhouse.New(clickhouse.Config{
DriverName: d.driverName,
DSN: d.dsn,
DisableDatetimePrecision: d.disableDatetimePrecision,
DontSupportRenameColumn: d.dontSupportRenameColumn,
DontSupportColumnPrecision: d.dontSupportColumnPrecision,
DontSupportEmptyDefaultValue: d.dontSupportEmptyDefaultValue,
SkipInitializeWithVersion: d.skipInitializeWithVersion,
DefaultGranularity: d.defaultGranularity,
DefaultCompression: d.defaultCompression,
DefaultIndexType: d.defaultIndexType,
DefaultTableEngineOpts: d.defaultTableEngineOpts,
})
return nil
}

Expand Down
39 changes: 19 additions & 20 deletions goner/gorm/mysql/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,25 @@ type dial struct {
DontSupportDropConstraint bool `gone:"config,gorm.mysql.dont-support-drop-constraint"`
}

func (d *dial) AfterRevive() error {
if d.Dialector != nil {
return gone.NewInnerError("gorm.mysql.dialer has been initialized", gone.StartError)
func (d *dial) Apply(*gorm.Config) error {
if d.Dialector == nil {
d.Dialector = mysql.New(mysql.Config{
DriverName: d.DriverName,
ServerVersion: d.ServerVersion,
DSN: d.DSN,
SkipInitializeWithVersion: d.SkipInitializeWithVersion,
DefaultStringSize: d.DefaultStringSize,
DefaultDatetimePrecision: d.DefaultDatetimePrecision,
DisableWithReturning: d.DisableWithReturning,
DisableDatetimePrecision: d.DisableDatetimePrecision,
DontSupportRenameIndex: d.DontSupportRenameIndex,
DontSupportRenameColumn: d.DontSupportRenameColumn,
DontSupportForShareClause: d.DontSupportForShareClause,
DontSupportNullAsDefaultValue: d.DontSupportNullAsDefaultValue,
DontSupportRenameColumnUnique: d.DontSupportRenameColumnUnique,
DontSupportDropConstraint: d.DontSupportDropConstraint,
})
}

d.Dialector = mysql.New(mysql.Config{
DriverName: d.DriverName,
ServerVersion: d.ServerVersion,
DSN: d.DSN,
SkipInitializeWithVersion: d.SkipInitializeWithVersion,
DefaultStringSize: d.DefaultStringSize,
DefaultDatetimePrecision: d.DefaultDatetimePrecision,
DisableWithReturning: d.DisableWithReturning,
DisableDatetimePrecision: d.DisableDatetimePrecision,
DontSupportRenameIndex: d.DontSupportRenameIndex,
DontSupportRenameColumn: d.DontSupportRenameColumn,
DontSupportForShareClause: d.DontSupportForShareClause,
DontSupportNullAsDefaultValue: d.DontSupportNullAsDefaultValue,
DontSupportRenameColumnUnique: d.DontSupportRenameColumnUnique,
DontSupportDropConstraint: d.DontSupportDropConstraint,
})
return nil
}

Expand All @@ -58,6 +56,7 @@ func Priest(cemetery gone.Cemetery) error {
cemetery.Bury(
&dial{},
gone.IsDefault(new(gorm.Dialector)),
gone.Order1,
)
return nil
}
20 changes: 9 additions & 11 deletions goner/gorm/postgres/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ type dial struct {
withoutReturning bool `gone:"config,gorm.postgres.without-returning=default"`
}

func (d *dial) AfterRevive() (err error) {
if d.Dialector != nil {
return gone.NewInnerError("gorm.mysql.dialer has been initialized", gone.StartError)
func (d *dial) Apply(*gorm.Config) error {
if d.Dialector == nil {
d.Dialector = postgres.New(postgres.Config{
DriverName: d.driverName,
DSN: d.dsn,
WithoutReturning: d.withoutReturning,
PreferSimpleProtocol: d.preferSimpleProtocol,
WithoutQuotingCheck: d.withoutQuotingCheck,
})
}

d.Dialector = postgres.New(postgres.Config{
DriverName: d.driverName,
DSN: d.dsn,
WithoutReturning: d.withoutReturning,
PreferSimpleProtocol: d.preferSimpleProtocol,
WithoutQuotingCheck: d.withoutQuotingCheck,
})
return nil
}

Expand Down
6 changes: 6 additions & 0 deletions goner/gorm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

func ProviderPriest(cemetery gone.Cemetery) error {
var gInstance *gorm.DB

return gone.NewProviderPriest(func(tagConf string, s struct {
dial gorm.Dialector `gone:"*"`
logger logger.Interface `gone:"*"`
Expand Down Expand Up @@ -56,6 +58,9 @@ func ProviderPriest(cemetery gone.Cemetery) error {
MaxOpen int `gone:"config,gorm.pool.max-open"`
ConnMaxLifetime *time.Duration `gone:"config,gorm.pool.conn-max-lifetime"`
}) (*gorm.DB, error) {
if gInstance != nil {
return gInstance, nil
}
g, err := gorm.Open(s.dial, &gorm.Config{
SkipDefaultTransaction: s.SkipDefaultTransaction,
FullSaveAssociations: s.FullSaveAssociations,
Expand Down Expand Up @@ -92,6 +97,7 @@ func ProviderPriest(cemetery gone.Cemetery) error {
if s.ConnMaxLifetime != nil {
db.SetConnMaxLifetime(*s.ConnMaxLifetime)
}
gInstance = g
return g, nil
})(cemetery)
}
14 changes: 6 additions & 8 deletions goner/gorm/sqlite/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ type dial struct {
DSN string `gone:"config,gorm.sqlite.dsn"`
}

func (d *dial) AfterRevive() error {
if d.Dialector != nil {
return gone.NewInnerError("gorm.mysql.dialer has been initialized", gone.StartError)
func (d *dial) Apply(*gorm.Config) error {
if d.Dialector == nil {
d.Dialector = sqlite.New(sqlite.Config{
DriverName: d.DriverName,
DSN: d.DSN,
})
}

d.Dialector = sqlite.New(sqlite.Config{
DriverName: d.DriverName,
DSN: d.DSN,
})
return nil
}

Expand Down
16 changes: 7 additions & 9 deletions goner/gorm/sqlserver/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ type dial struct {
DefaultStringSize int `gone:"config,gorm.sqlserver.default-string-size"`
}

func (d *dial) AfterRevive() error {
if d.Dialector != nil {
return gone.NewInnerError("gorm.mysql.dialer has been initialized", gone.StartError)
func (d *dial) Apply(*gorm.Config) error {
if d.Dialector == nil {
d.Dialector = sqlserver.New(sqlserver.Config{
DriverName: d.DriverName,
DSN: d.DSN,
DefaultStringSize: d.DefaultStringSize,
})
}

d.Dialector = sqlserver.New(sqlserver.Config{
DriverName: d.DriverName,
DSN: d.DSN,
DefaultStringSize: d.DefaultStringSize,
})
return nil
}

Expand Down

0 comments on commit e8bff7e

Please sign in to comment.