Skip to content

Commit 5f854dd

Browse files
authored
Merge branch 'master' into support_ipv6
2 parents e18a09b + b690562 commit 5f854dd

File tree

5 files changed

+156
-12
lines changed

5 files changed

+156
-12
lines changed

pkg/datasource/sql/datasource/base/meta_cache.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"database/sql"
2323
"fmt"
24+
"strings"
2425
"sync"
2526
"time"
2627

@@ -110,8 +111,9 @@ func (c *BaseTableMetaCache) refresh(ctx context.Context) {
110111

111112
for i := range v {
112113
tm := v[i]
113-
if _, ok := c.cache[tm.TableName]; ok {
114-
c.cache[tm.TableName] = &entry{
114+
upperTableName := strings.ToUpper(tm.TableName)
115+
if _, ok := c.cache[upperTableName]; ok {
116+
c.cache[upperTableName] = &entry{
115117
value: tm,
116118
}
117119
}
@@ -157,16 +159,16 @@ func (c *BaseTableMetaCache) GetTableMeta(ctx context.Context, dbName, tableName
157159
defer c.lock.Unlock()
158160

159161
defer conn.Close()
160-
161-
v, ok := c.cache[tableName]
162+
upperTableName := strings.ToUpper(tableName)
163+
v, ok := c.cache[upperTableName]
162164
if !ok {
163-
meta, err := c.trigger.LoadOne(ctx, dbName, tableName, conn)
165+
meta, err := c.trigger.LoadOne(ctx, dbName, upperTableName, conn)
164166
if err != nil {
165167
return types.TableMeta{}, err
166168
}
167169

168170
if meta != nil && !meta.IsEmpty() {
169-
c.cache[tableName] = &entry{
171+
c.cache[upperTableName] = &entry{
170172
value: *meta,
171173
lastAccess: time.Now(),
172174
}
@@ -178,7 +180,7 @@ func (c *BaseTableMetaCache) GetTableMeta(ctx context.Context, dbName, tableName
178180
}
179181

180182
v.lastAccess = time.Now()
181-
c.cache[tableName] = v
183+
c.cache[upperTableName] = v
182184

183185
return v.value, nil
184186
}

pkg/datasource/sql/datasource/base/meta_cache_test.go

+135-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"testing"
2525
"time"
2626

27+
"github.com/DATA-DOG/go-sqlmock"
2728
"github.com/agiledragon/gomonkey/v2"
2829
"github.com/go-sql-driver/mysql"
2930
"github.com/stretchr/testify/assert"
@@ -40,8 +41,30 @@ var (
4041
type mockTrigger struct {
4142
}
4243

44+
// LoadOne simulates loading table metadata, including id, name, and age columns.
4345
func (m *mockTrigger) LoadOne(ctx context.Context, dbName string, table string, conn *sql.Conn) (*types.TableMeta, error) {
44-
return nil, nil
46+
47+
return &types.TableMeta{
48+
TableName: table,
49+
Columns: map[string]types.ColumnMeta{
50+
"id": {ColumnName: "id"},
51+
"name": {ColumnName: "name"},
52+
"age": {ColumnName: "age"},
53+
},
54+
Indexs: map[string]types.IndexMeta{
55+
"id": {
56+
Name: "PRIMARY",
57+
IType: types.IndexTypePrimaryKey,
58+
Columns: []types.ColumnMeta{{ColumnName: "id"}},
59+
},
60+
"id_name_age": {
61+
Name: "name_age_idx",
62+
IType: types.IndexUnique,
63+
Columns: []types.ColumnMeta{{ColumnName: "name"}, {ColumnName: "age"}},
64+
},
65+
},
66+
ColumnNames: []string{"id", "name", "age"},
67+
}, nil
4568
}
4669

4770
func (m *mockTrigger) LoadAll(ctx context.Context, dbName string, conn *sql.Conn, tables ...string) ([]types.TableMeta, error) {
@@ -77,7 +100,7 @@ func TestBaseTableMetaCache_refresh(t *testing.T) {
77100
size: 0,
78101
expireDuration: EexpireTime,
79102
cache: map[string]*entry{
80-
"test": {
103+
"TEST": {
81104
value: types.TableMeta{},
82105
lastAccess: time.Now(),
83106
},
@@ -120,7 +143,116 @@ func TestBaseTableMetaCache_refresh(t *testing.T) {
120143
go c.refresh(tt.args.ctx)
121144
time.Sleep(time.Second * 3)
122145

123-
assert.Equal(t, c.cache["test"].value, tt.want)
146+
assert.Equal(t, c.cache["TEST"].value, tt.want)
147+
})
148+
}
149+
}
150+
151+
func TestBaseTableMetaCache_GetTableMeta(t *testing.T) {
152+
var (
153+
tableMeta1 types.TableMeta
154+
tableMeta2 types.TableMeta
155+
columns = make(map[string]types.ColumnMeta)
156+
index = make(map[string]types.IndexMeta)
157+
index2 = make(map[string]types.IndexMeta)
158+
columnMeta1 []types.ColumnMeta
159+
columnMeta2 []types.ColumnMeta
160+
ColumnNames []string
161+
)
162+
columnId := types.ColumnMeta{
163+
ColumnDef: nil,
164+
ColumnName: "id",
165+
}
166+
columnName := types.ColumnMeta{
167+
ColumnDef: nil,
168+
ColumnName: "name",
169+
}
170+
columnAge := types.ColumnMeta{
171+
ColumnDef: nil,
172+
ColumnName: "age",
173+
}
174+
columns["id"] = columnId
175+
columns["name"] = columnName
176+
columns["age"] = columnAge
177+
columnMeta1 = append(columnMeta1, columnId)
178+
columnMeta2 = append(columnMeta2, columnName, columnAge)
179+
index["id"] = types.IndexMeta{
180+
Name: "PRIMARY",
181+
IType: types.IndexTypePrimaryKey,
182+
Columns: columnMeta1,
183+
}
184+
index["id_name_age"] = types.IndexMeta{
185+
Name: "name_age_idx",
186+
IType: types.IndexUnique,
187+
Columns: columnMeta2,
188+
}
189+
190+
ColumnNames = []string{"id", "name", "age"}
191+
tableMeta1 = types.TableMeta{
192+
TableName: "T_USER1",
193+
Columns: columns,
194+
Indexs: index,
195+
ColumnNames: ColumnNames,
196+
}
197+
198+
index2["id_name_age"] = types.IndexMeta{
199+
Name: "name_age_idx",
200+
IType: types.IndexUnique,
201+
Columns: columnMeta2,
202+
}
203+
204+
tableMeta2 = types.TableMeta{
205+
TableName: "T_USER2",
206+
Columns: columns,
207+
Indexs: index2,
208+
ColumnNames: ColumnNames,
209+
}
210+
tests := []types.TableMeta{tableMeta1, tableMeta2}
211+
// Use sqlmock to simulate a database connection
212+
db, mock, err := sqlmock.New()
213+
if err != nil {
214+
t.Fatalf("Failed to create sqlmock: %v", err)
215+
}
216+
defer db.Close()
217+
for _, tt := range tests {
218+
t.Run(tt.TableName, func(t *testing.T) {
219+
mockTrigger := &mockTrigger{}
220+
// Mock a query response
221+
mock.ExpectQuery("SELECT").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "age"}))
222+
// Create a mock database connection
223+
conn, err := db.Conn(context.Background())
224+
if err != nil {
225+
t.Fatalf("Failed to get connection: %v", err)
226+
}
227+
defer conn.Close()
228+
cache := &BaseTableMetaCache{
229+
trigger: mockTrigger,
230+
cache: map[string]*entry{
231+
"T_USER": {
232+
value: tableMeta2,
233+
lastAccess: time.Now(),
234+
},
235+
"T_USER1": {
236+
value: tableMeta1,
237+
lastAccess: time.Now(),
238+
},
239+
},
240+
lock: sync.RWMutex{},
241+
}
242+
243+
meta, _ := cache.GetTableMeta(context.Background(), "db", tt.TableName, conn)
244+
245+
if meta.TableName != tt.TableName {
246+
t.Errorf("GetTableMeta() got TableName = %v, want %v", meta.TableName, tt.TableName)
247+
}
248+
// Ensure the retrieved table is cached
249+
cache.lock.RLock()
250+
_, cached := cache.cache[tt.TableName]
251+
cache.lock.RUnlock()
252+
253+
if !cached {
254+
t.Errorf("GetTableMeta() got TableName = %v, want %v", meta.TableName, tt.TableName)
255+
}
124256
})
125257
}
126258
}

pkg/datasource/sql/types/image.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func (c *ColumnImage) UnmarshalJSON(data []byte) error {
251251
if err != nil {
252252
return err
253253
}
254-
case JDBCTypeChar, JDBCTypeVarchar:
254+
case JDBCTypeChar, JDBCTypeVarchar, JDBCTypeLongVarchar:
255255
var val []byte
256256
if val, err = base64.StdEncoding.DecodeString(value.(string)); err != nil {
257257
val = []byte(value.(string))

pkg/datasource/sql/types/image_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ func TestColumnImage_UnmarshalJSON(t *testing.T) {
4242
},
4343
expectValue: "Seata-go",
4444
},
45+
{
46+
name: "test-text",
47+
image: &ColumnImage{
48+
KeyType: IndexTypePrimaryKey,
49+
ColumnName: "Text",
50+
ColumnType: JDBCTypeLongVarchar,
51+
Value: []byte("Seata-go"),
52+
},
53+
expectValue: "Seata-go",
54+
},
4555
{
4656
name: "test-int",
4757
image: &ColumnImage{

pkg/datasource/sql/undo/builder/basic_undo_log_builder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (*BasicUndoLogBuilder) GetScanSlice(columnNames []string, tableMeta *types.
4242
for _, columnNmae := range columnNames {
4343
var (
4444
scanVal interface{}
45-
// 从metData获取该列的元信息
45+
// Gets Meta information about the column from metData
4646
columnMeta = tableMeta.Columns[columnNmae]
4747
)
4848
switch strings.ToUpper(columnMeta.DatabaseTypeString) {

0 commit comments

Comments
 (0)