Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PA-29417 | Escaping singleton while creating sql client #40

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions inssql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ func Init(Driver, DBUser, DBPassword, DBHost, DBName string) (*sql.DB, error) {
return sqlClient, nil
}

var err error
sqlClient, err = New(Driver, DBUser, DBPassword, DBHost, DBName)

return sqlClient, err
}

// New creates brand new sql client
func New(Driver string, DBUser string, DBPassword string, DBHost string, DBName string) (*sql.DB, error) {
dsn := fmt.Sprintf(
"%v:%v@%v/%v?charset=utf8mb4&collation=utf8mb4_unicode_ci&parseTime=true",
DBUser,
Expand All @@ -33,8 +41,6 @@ func Init(Driver, DBUser, DBPassword, DBHost, DBName string) (*sql.DB, error) {
return nil, err
}

sqlClient = db

return db, err
}

Expand All @@ -49,12 +55,18 @@ func WrapWithGorm(sqlDB *sql.DB) (*gorm.DB, error) {
return gormClient, nil
}

var err error
gormClient, err = NewGorm(sqlDB)

return gormClient, err
}

// NewGorm wrap new sql client
func NewGorm(sqlDB *sql.DB) (*gorm.DB, error) {
gormDB, err := gorm.Open(mysql.New(mysql.Config{
Conn: sqlDB,
}), &gorm.Config{})

gormClient = gormDB

return gormDB, err
}

Expand Down
Loading