Skip to content

Commit

Permalink
♻️ refactor: updated codebase and upgraded deps #5
Browse files Browse the repository at this point in the history
  • Loading branch information
arisnguyen215 committed Dec 9, 2023
1 parent dde9923 commit e4f3d56
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions mysqlconn/mysqlconn.go → mysqlconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
)

var (
instance *MySql
_logger = logger.NewLogger()
_logger = logger.NewLogger()
)

func NewMySql() *MySql {
Expand Down Expand Up @@ -54,22 +53,19 @@ func (m *MySql) Close() error {
}

func NewClient(config mysql.MysqlConfig) (*MySql, dbx.Dbx) {
instance := NewMySql()
s := dbx.NewDbx().SetDatabase(config.Database)
if !config.IsEnabled {
s.SetConnected(false).
SetMessage("Mysql unavailable").
SetError(fmt.Errorf(s.Message))
instance = NewMySql().SetState(*s)
return instance, *s
}
if instance != nil {
s.SetConnected(true)
instance.SetState(*s)
return instance, *s
}
client, err := sql.Open(common.EntryKeyMysql, Dsn(config))
if err != nil {
s.SetConnected(false).SetError(err).SetMessage(err.Error())
instance = NewMySql().SetState(*s)
instance.SetState(*s)
return instance, *s
}
if config.MaxOpenConn <= 0 {
Expand All @@ -89,16 +85,16 @@ func NewClient(config mysql.MysqlConfig) (*MySql, dbx.Dbx) {
err = client.PingContext(ctx)
if err != nil {
s.SetConnected(false).SetError(err).SetMessage(err.Error())
instance = NewMySql().SetState(*s)
instance.SetState(*s)
return instance, *s
}
if config.DebugMode {
_logger.Info(fmt.Sprintf("Mysql client connection:: %s", config.Json()))
_logger.Info(fmt.Sprintf("Connected successfully to mysql:: %s (database: %s)", Dsn(config), config.Database))
}
pid := os.Getpid()
s.SetConnected(true).SetMessage("Connection successfully").SetPid(pid).SetNewInstance(true)
instance = NewMySql().SetConn(client).SetConfig(config).SetState(*s)
s.SetConnected(true).SetMessage("Connected successfully").SetPid(pid).SetNewInstance(true)
instance.SetConn(client).SetConfig(config).SetState(*s)
return instance, *s
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e4f3d56

Please sign in to comment.