Skip to content

Commit

Permalink
fix: solve GetTableMeta return type && constant define
Browse files Browse the repository at this point in the history
  • Loading branch information
王瑞 committed Oct 2, 2022
1 parent b7fc488 commit 59c9b79
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 27 deletions.
8 changes: 4 additions & 4 deletions pkg/datasource/sql/datasource/base/meta_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
type (
// trigger
trigger interface {
LoadOne(ctx context.Context, dbName string, table string, conn *sql.Conn) (types.TableMeta, error)
LoadOne(ctx context.Context, dbName string, table string, conn *sql.Conn) (*types.TableMeta, error)

LoadAll() ([]types.TableMeta, error)
}
Expand Down Expand Up @@ -148,13 +148,13 @@ func (c *BaseTableMetaCache) GetTableMeta(ctx context.Context, tableName string,
return types.TableMeta{}, err
}

if !meta.IsEmpty() {
if meta != nil && !meta.IsEmpty() {
c.cache[tableName] = &entry{
value: meta,
value: *meta,
lastAccess: time.Now(),
}

return meta, nil
return *meta, nil
}

return types.TableMeta{}, errors.New("not found table metadata")
Expand Down
2 changes: 1 addition & 1 deletion pkg/datasource/sql/datasource/datasource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ type TableMetaCache interface {
// Init
Init(ctx context.Context, conn *sql.DB) error
// GetTableMeta
GetTableMeta(ctx context.Context, table string, conn *sql.Conn) (types.TableMeta, error)
GetTableMeta(ctx context.Context, table string, conn *sql.Conn) (*types.TableMeta, error)
// Destroy
Destroy() error
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/datasource/sql/datasource/mysql/meta_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ func (c *tableMetaCache) Init(ctx context.Context, conn *sql.DB) error {
}

// GetTableMeta get table info from cache or information schema
func (c *tableMetaCache) GetTableMeta(ctx context.Context, tableName string, conn *sql.Conn) (types.TableMeta, error) {
func (c *tableMetaCache) GetTableMeta(ctx context.Context, tableName string, conn *sql.Conn) (*types.TableMeta, error) {
if tableName == "" {
return types.TableMeta{}, errors.New("TableMeta cannot be fetched without tableName")
return nil, errors.New("TableMeta cannot be fetched without tableName")
}

tableMeta, err := c.tableMetaCache.GetTableMeta(ctx, tableName, conn)
if err != nil {
return types.TableMeta{}, err
return nil, err
}

return tableMeta, nil
return &tableMeta, nil
}

// Destroy
Expand Down
33 changes: 15 additions & 18 deletions pkg/datasource/sql/datasource/mysql/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ import (
"github.com/seata/seata-go/pkg/datasource/sql/types"
)

// Table schema
const (
IndexSchemaSql = "SELECT `INDEX_NAME`, `COLUMN_NAME`, `NON_UNIQUE`, `INDEX_TYPE`, `COLLATION`, `CARDINALITY` " +
"FROM `INFORMATION_SCHEMA`.`STATISTICS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"

ColumnSchemaSql = "select TABLE_CATALOG, TABLE_NAME, TABLE_SCHEMA, COLUMN_NAME, DATA_TYPE, COLUMN_TYPE, COLUMN_KEY, " +
" IS_NULLABLE, EXTRA from INFORMATION_SCHEMA.COLUMNS where `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"
)

type mysqlTrigger struct {
}

Expand All @@ -45,7 +36,7 @@ func NewMysqlTrigger() *mysqlTrigger {
}

// LoadOne get table meta column and index
func (m *mysqlTrigger) LoadOne(ctx context.Context, dbName string, tableName string, conn *sql.Conn) (types.TableMeta, error) {
func (m *mysqlTrigger) LoadOne(ctx context.Context, dbName string, tableName string, conn *sql.Conn) (*types.TableMeta, error) {
tableMeta := types.TableMeta{
Name: tableName,
Columns: make(map[string]types.ColumnMeta),
Expand All @@ -54,10 +45,10 @@ func (m *mysqlTrigger) LoadOne(ctx context.Context, dbName string, tableName str

colMetas, err := m.getColumns(ctx, dbName, tableName, conn)
if err != nil {
return types.TableMeta{}, errors.Wrapf(err, "Could not found any column in the table: %s", tableName)
return nil, errors.Wrapf(err, "Could not found any column in the table: %s", tableName)
}

columns := make([]string, 0)
var columns []string
for _, column := range colMetas {
tableMeta.Columns[column.ColumnName] = column
columns = append(columns, column.ColumnName)
Expand All @@ -66,7 +57,7 @@ func (m *mysqlTrigger) LoadOne(ctx context.Context, dbName string, tableName str

indexes, err := m.getIndexes(ctx, dbName, tableName, conn)
if err != nil {
return types.TableMeta{}, errors.Wrapf(err, "Could not found any index in the table: %s", tableName)
return nil, errors.Wrapf(err, "Could not found any index in the table: %s", tableName)
}
for _, index := range indexes {
col := tableMeta.Columns[index.ColumnName]
Expand All @@ -79,10 +70,10 @@ func (m *mysqlTrigger) LoadOne(ctx context.Context, dbName string, tableName str
}
}
if len(tableMeta.Indexs) == 0 {
return types.TableMeta{}, errors.Errorf("Could not found any index in the table: %s", tableName)
return nil, errors.Errorf("Could not found any index in the table: %s", tableName)
}

return tableMeta, nil
return &tableMeta, nil
}

// LoadAll
Expand All @@ -94,9 +85,12 @@ func (m *mysqlTrigger) LoadAll() ([]types.TableMeta, error) {
func (m *mysqlTrigger) getColumns(ctx context.Context, dbName string, table string, conn *sql.Conn) ([]types.ColumnMeta, error) {
table = executor.DelEscape(table, types.DBTypeMySQL)

result := make([]types.ColumnMeta, 0)
var result []types.ColumnMeta

stmt, err := conn.PrepareContext(ctx, ColumnSchemaSql)
columnSchemaSql := "select TABLE_CATALOG, TABLE_NAME, TABLE_SCHEMA, COLUMN_NAME, DATA_TYPE, COLUMN_TYPE, COLUMN_KEY, " +
" IS_NULLABLE, EXTRA from INFORMATION_SCHEMA.COLUMNS where `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"

stmt, err := conn.PrepareContext(ctx, columnSchemaSql)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -172,7 +166,10 @@ func (m *mysqlTrigger) getIndexes(ctx context.Context, dbName string, tableName

result := make([]types.IndexMeta, 0)

stmt, err := conn.PrepareContext(ctx, IndexSchemaSql)
indexSchemaSql := "SELECT `INDEX_NAME`, `COLUMN_NAME`, `NON_UNIQUE`, `INDEX_TYPE`, `COLLATION`, `CARDINALITY` " +
"FROM `INFORMATION_SCHEMA`.`STATISTICS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"

stmt, err := conn.PrepareContext(ctx, indexSchemaSql)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 59c9b79

Please sign in to comment.