refactor: code review

This commit is contained in:
meilin.huang
2024-01-26 17:17:26 +08:00
parent 7e9a381641
commit 923e183a67
14 changed files with 48 additions and 40 deletions

View File

@@ -1,5 +1,5 @@
<template>
<el-input v-model="cron" placeholder="可点击左边按钮进行可视化配置">
<el-input v-model="cron" placeholder="可点击左边按钮配置">
<template #prepend>
<el-button @click="showCron = true" icon="Pointer"></el-button>
</template>

View File

@@ -363,7 +363,7 @@ export class DbInst {
return value;
}
if (!dbDialect) {
return `${value}`;
return `'${value}'`;
}
return dbDialect.wrapStrValue(columnType, value);
}

View File

@@ -31,6 +31,7 @@ require (
go.mongodb.org/mongo-driver v1.13.1 // mongo
golang.org/x/crypto v0.18.0 // ssh
golang.org/x/oauth2 v0.15.0
golang.org/x/sync v0.6.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v3 v3.0.1
// gorm
@@ -84,7 +85,6 @@ require (
golang.org/x/exp v0.0.0-20230519143937-03e91628a987 // indirect
golang.org/x/image v0.13.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.7 // indirect

View File

@@ -355,7 +355,7 @@ func (d *Db) dumpDb(writer *gzipWriter, dbId uint64, dbName string, tables []str
writer.WriteString("BEGIN;\n")
}
insertSql := "INSERT INTO %s VALUES (%s);\n"
dbMeta.WalkTableRecord(table, func(record map[string]any, columns []*dbi.QueryColumn) error {
dbConn.WalkTableRows(context.TODO(), table, func(record map[string]any, columns []*dbi.QueryColumn) error {
var values []string
writer.TryFlush()
for _, column := range columns {

View File

@@ -77,6 +77,11 @@ func (d *DbConn) WalkQueryRows(ctx context.Context, querySql string, walkFn Walk
return walkQueryRows(ctx, d.db, querySql, walkFn, args...)
}
// 游标方式遍历指定表的结果集, walkFn返回error不为nil, 则跳出遍历
func (d *DbConn) WalkTableRows(ctx context.Context, tableName string, walkFn WalkQueryRowsFunc) error {
return d.WalkQueryRows(ctx, fmt.Sprintf("SELECT * FROM %s", tableName), walkFn)
}
// 执行 update, insert, delete建表等sql
// 返回影响条数和错误
func (d *DbConn) Exec(sql string, args ...any) (int64, error) {

View File

@@ -104,9 +104,6 @@ type Dialect interface {
// 获取建表ddl
GetTableDDL(tableName string) (string, error)
// WalkTableRecord 遍历指定表的数据
WalkTableRecord(tableName string, walkFn WalkQueryRowsFunc) error
GetSchemas() ([]string, error)
// GetDbProgram 获取数据库程序模块,用于数据库备份与恢复

View File

@@ -1,7 +1,6 @@
package dm
import (
"context"
"database/sql"
"fmt"
"mayfly-go/internal/db/dbm/dbi"
@@ -233,10 +232,6 @@ func (dd *DMDialect) GetTableDDL(tableName string) (string, error) {
return builder.String(), nil
}
func (dd *DMDialect) WalkTableRecord(tableName string, walkFn dbi.WalkQueryRowsFunc) error {
return dd.dc.WalkQueryRows(context.Background(), fmt.Sprintf("SELECT * FROM %s", tableName), walkFn)
}
// 获取DM当前连接的库可访问的schemaNames
func (dd *DMDialect) GetSchemas() ([]string, error) {
sql := dbi.GetLocalSql(DM_META_FILE, DM_DB_SCHEMAS)

View File

@@ -1,7 +1,6 @@
package mysql
import (
"context"
"database/sql"
"fmt"
"mayfly-go/internal/db/dbm/dbi"
@@ -164,10 +163,6 @@ func (md *MysqlDialect) GetTableDDL(tableName string) (string, error) {
return anyx.ConvString(res[0]["Create Table"]) + ";", nil
}
func (md *MysqlDialect) WalkTableRecord(tableName string, walkFn dbi.WalkQueryRowsFunc) error {
return md.dc.WalkQueryRows(context.Background(), fmt.Sprintf("SELECT * FROM %s", tableName), walkFn)
}
func (md *MysqlDialect) GetSchemas() ([]string, error) {
return nil, nil
}

View File

@@ -15,11 +15,12 @@ import (
"strings"
"time"
"github.com/pkg/errors"
"mayfly-go/internal/db/config"
"mayfly-go/internal/db/dbm/dbi"
"mayfly-go/internal/db/domain/entity"
"mayfly-go/pkg/logx"
"github.com/pkg/errors"
)
var _ dbi.DbProgram = (*DbProgramMysql)(nil)
@@ -584,7 +585,7 @@ func (svc *DbProgramMysql) ReplayBinlog(ctx context.Context, originalDatabase, t
_ = mysqlbinlogCmd.Cancel()
if err := mysqlbinlogCmd.Wait(); err != nil {
if mysqlbinlogErr.Len() > 0 {
logx.Errorf("运行 mysqlbinlog 程序失败", mysqlbinlogErr.String())
logx.Errorf("运行 mysqlbinlog 程序失败: %s", mysqlbinlogErr.String())
if replayErr != nil {
replayErr = errors.Wrap(replayErr, "运行 mysqlbinlog 程序失败: "+mysqlbinlogErr.String())
} else {

View File

@@ -1,7 +1,6 @@
package oracle
import (
"context"
"database/sql"
"fmt"
"mayfly-go/internal/db/dbm/dbi"
@@ -233,10 +232,6 @@ func (od *OracleDialect) GetTableDDL(tableName string) (string, error) {
return builder.String(), nil
}
func (od *OracleDialect) WalkTableRecord(tableName string, walkFn dbi.WalkQueryRowsFunc) error {
return od.dc.WalkQueryRows(context.Background(), fmt.Sprintf("SELECT * FROM %s", tableName), walkFn)
}
// 获取DM当前连接的库可访问的schemaNames
func (od *OracleDialect) GetSchemas() ([]string, error) {
sql := dbi.GetLocalSql(ORACLE_META_FILE, ORACLE_DB_SCHEMAS)

View File

@@ -1,7 +1,6 @@
package postgres
import (
"context"
"database/sql"
"fmt"
"mayfly-go/internal/db/dbm/dbi"
@@ -173,10 +172,6 @@ func (md *PgsqlDialect) GetTableDDL(tableName string) (string, error) {
return res[0]["sql"].(string), nil
}
func (md *PgsqlDialect) WalkTableRecord(tableName string, walkFn dbi.WalkQueryRowsFunc) error {
return md.dc.WalkQueryRows(context.Background(), fmt.Sprintf("SELECT * FROM %s", tableName), walkFn)
}
// 获取pgsql当前连接的库可访问的schemaNames
func (md *PgsqlDialect) GetSchemas() ([]string, error) {
sql := dbi.GetLocalSql(PGSQL_META_FILE, PGSQL_DB_SCHEMAS)

View File

@@ -1,7 +1,6 @@
package sqlite
import (
"context"
"database/sql"
"errors"
"fmt"
@@ -183,10 +182,6 @@ func (sd *SqliteDialect) GetTableDDL(tableName string) (string, error) {
return builder.String(), nil
}
func (sd *SqliteDialect) WalkTableRecord(tableName string, walkFn dbi.WalkQueryRowsFunc) error {
return sd.dc.WalkQueryRows(context.Background(), fmt.Sprintf("SELECT * FROM %s", tableName), walkFn)
}
func (sd *SqliteDialect) GetSchemas() ([]string, error) {
return nil, nil
}

View File

@@ -1,16 +1,22 @@
package ioc
import (
"context"
"errors"
"fmt"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/structx"
"reflect"
"strings"
"sync"
"golang.org/x/sync/errgroup"
)
// 容器
type Container struct {
mu sync.RWMutex
components map[string]*Component
}
@@ -22,6 +28,9 @@ func NewContainer() *Container {
// 注册实例至实例容器
func (c *Container) Register(bean any, opts ...ComponentOption) {
c.mu.Lock()
defer c.mu.Unlock()
component := NewComponent(bean, opts...)
componentName := component.Name
@@ -32,7 +41,7 @@ func (c *Container) Register(bean any, opts ...ComponentOption) {
component.Name = componentName
}
if _, err := c.Get(componentName); err == nil {
if _, ok := c.components[componentName]; ok {
logx.Warnf("组件名[%s]已经注册至容器, 重复注册...", componentName)
}
@@ -58,16 +67,37 @@ func (c *Container) Inject(obj any) error {
// 对所有组件实例执行Inject。即为实例字段注入依赖的组件实例
func (c *Container) InjectComponents() error {
for _, v := range c.components {
if err := c.Inject(v.Value); err != nil {
return err
}
componentsGroups := collx.ArraySplit[*Component](collx.MapValues(c.components), 10)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errGroup, _ := errgroup.WithContext(ctx)
for _, components := range componentsGroups {
components := components // 创建局部变量以在闭包中使用
errGroup.Go(func() error {
for _, v := range components {
if err := c.Inject(v.Value); err != nil {
cancel() // 取消所有协程的执行
return err
}
}
return nil
})
}
if err := errGroup.Wait(); err != nil {
return err
}
return nil
}
// 根据组件实例名,获取对应实例信息
func (c *Container) Get(name string) (any, error) {
c.mu.RLock()
defer c.mu.RUnlock()
component, ok := c.components[name]
if !ok {
return nil, errors.New("component not found: " + name)

View File

@@ -116,7 +116,7 @@ func ErrorTrace(msg string, err any) {
default:
errMsg = fmt.Sprintf("%v", t)
}
Log(context.Background(), slog.LevelError, fmt.Sprintf(msg+"\n%s\n%s", errMsg, runtimex.StatckStr(2, 10)))
Log(context.Background(), slog.LevelError, fmt.Sprintf(msg+"\n%s\n%s", errMsg, runtimex.StatckStr(2, 20)))
}
func ErrorWithFields(ctx context.Context, msg string, mapFields map[string]any) {