Skip to content

Commit

Permalink
feat: add bcs-task mysql store add ShowDebug option
Browse files Browse the repository at this point in the history
  • Loading branch information
ifooth committed Oct 24, 2024
1 parent b2a0fc9 commit 86c8306
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions bcs-common/common/task/stores/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ package mysql

import (
"context"
"net/url"
"strconv"

"gorm.io/driver/mysql"
"gorm.io/gorm"
Expand All @@ -32,10 +30,21 @@ type mysqlStore struct {
db *gorm.DB
}

type option func(*mysqlStore)

// ShowDebug 是否显示sql语句
func ShowDebug(showDebug bool) option {
return func(s *mysqlStore) {
s.showDebug = showDebug
}
}

// New init mysql iface.Store
func New(dsn string) (iface.Store, error) {
func New(dsn string, opts ...option) (iface.Store, error) {
store := &mysqlStore{dsn: dsn, showDebug: false}
store.initDsn(dsn)
for _, opt := range opts {
opt(store)
}

// 是否显示sql语句
level := logger.Warn
Expand All @@ -54,29 +63,6 @@ func New(dsn string) (iface.Store, error) {
return store, nil
}

// initDsn 解析debug参数是否开启sql显示, 任意异常都原样不动
func (s *mysqlStore) initDsn(raw string) {
u, err := url.Parse(raw)
if err != nil {
return
}
query := u.Query()

// 是否开启debug
debugStr := query.Get("debug")
if debugStr != "" {
debug, err := strconv.ParseBool(debugStr)
if err != nil {
return
}
s.showDebug = debug
query.Del("debug")
u.RawQuery = query.Encode()
}

s.dsn = u.String()
}

// EnsureTable implement istore EnsureTable interface
func (s *mysqlStore) EnsureTable(ctx context.Context, dst ...any) error {
// 没有自定义数据, 使用默认表结构
Expand Down

0 comments on commit 86c8306

Please sign in to comment.