Skip to content

Commit 3aaa3a7

Browse files
committed
fix integration test cases
1 parent 5e77eab commit 3aaa3a7

21 files changed

Lines changed: 200 additions & 50 deletions

docker/scripts/mod_world_0.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3441,6 +3441,10 @@ COMMIT;
34413441

34423442
SET FOREIGN_KEY_CHECKS = 1;
34433443

3444+
DROP DATABASE IF EXISTS meta;
3445+
CREATE DATABASE IF NOT EXISTS meta;
3446+
USE meta;
3447+
34443448
CREATE TABLE `undo_log` (
34453449
`id` bigint NOT NULL AUTO_INCREMENT,
34463450
`branch_id` bigint NOT NULL,

docker/scripts/mod_world_1.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3432,6 +3432,10 @@ COMMIT;
34323432

34333433
SET FOREIGN_KEY_CHECKS = 1;
34343434

3435+
DROP DATABASE IF EXISTS meta;
3436+
CREATE DATABASE IF NOT EXISTS meta;
3437+
USE meta;
3438+
34353439
CREATE TABLE `undo_log` (
34363440
`id` bigint NOT NULL AUTO_INCREMENT,
34373441
`branch_id` bigint NOT NULL,

docker/scripts/range_world_0.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3901,6 +3901,10 @@ COMMIT;
39013901

39023902
SET FOREIGN_KEY_CHECKS = 1;
39033903

3904+
DROP DATABASE IF EXISTS meta;
3905+
CREATE DATABASE IF NOT EXISTS meta;
3906+
USE meta;
3907+
39043908
CREATE TABLE `undo_log` (
39053909
`id` bigint NOT NULL AUTO_INCREMENT,
39063910
`branch_id` bigint NOT NULL,

docker/scripts/range_world_1.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2966,6 +2966,10 @@ COMMIT;
29662966

29672967
SET FOREIGN_KEY_CHECKS = 1;
29682968

2969+
DROP DATABASE IF EXISTS meta;
2970+
CREATE DATABASE IF NOT EXISTS meta;
2971+
USE meta;
2972+
29692973
CREATE TABLE `undo_log` (
29702974
`id` bigint NOT NULL AUTO_INCREMENT,
29712975
`branch_id` bigint NOT NULL,

pkg/executor/sharding.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/cectc/dbpack/pkg/topo"
3939
"github.com/cectc/dbpack/pkg/tracing"
4040
"github.com/cectc/dbpack/third_party/parser/ast"
41+
"github.com/cectc/dbpack/third_party/parser/format"
4142
)
4243

4344
type ShardingExecutor struct {
@@ -207,15 +208,23 @@ func (executor *ShardingExecutor) ExecutorComQuery(ctx context.Context, sql stri
207208
}
208209
}()
209210

210-
var plan proto.Plan
211+
var (
212+
plan proto.Plan
213+
sb strings.Builder
214+
)
211215

212-
log.Debugf("query: %s", sql)
213216
connectionID := proto.ConnectionID(spanCtx)
214217
queryStmt := proto.QueryStmt(spanCtx)
215218
if queryStmt == nil {
216219
return nil, 0, errors.New("query stmt should not be nil")
217220
}
221+
if err := queryStmt.Restore(format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)); err != nil {
222+
return nil, 0, err
223+
}
224+
newSql := sb.String()
225+
spanCtx = proto.WithSqlText(spanCtx, newSql)
218226

227+
log.Debugf("connectionID: %d, query: %s", connectionID, newSql)
219228
switch stmt := queryStmt.(type) {
220229
case *ast.SetStmt:
221230
if shouldStartTransaction(stmt) {
@@ -278,7 +287,7 @@ func (executor *ShardingExecutor) ExecutorComQuery(ctx context.Context, sql stri
278287
case *ast.SelectStmt:
279288
if stmt.Fields != nil && len(stmt.Fields.Fields) > 0 {
280289
if _, ok := stmt.Fields.Fields[0].Expr.(*ast.VariableExpr); ok {
281-
return executor.executors[0].Query(spanCtx, sql)
290+
return executor.executors[0].Query(spanCtx, newSql)
282291
}
283292
}
284293
txi, ok := executor.localTransactionMap.Load(connectionID)

pkg/listener/mysql.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,6 @@ func (l *MysqlListener) ExecuteCommand(ctx context.Context, c *mysql.Conn, data
518518
connectionID := proto.ConnectionID(ctx)
519519
l.executor.ConnectionClose(proto.WithConnectionID(ctx, connectionID))
520520
log.Debugf("connection closed, id: %d", connectionID)
521-
return errors.New("ComQuit")
522521
case constant.ComInitDB:
523522
db := string(data[1:])
524523
c.RecycleReadPacket()
@@ -549,8 +548,14 @@ func (l *MysqlListener) ExecuteCommand(ctx context.Context, c *mysql.Conn, data
549548
return nil
550549
}
551550

552-
if showStmt, ok := stmt.(*ast.ShowStmt); ok && showStmt.Tp == ast.ShowTables {
553-
showStmt.DBName = c.Database()
551+
if showStmt, ok := stmt.(*ast.ShowStmt); ok {
552+
switch showStmt.Tp {
553+
case ast.ShowTables, ast.ShowTableStatus, ast.ShowColumns, ast.ShowIndex, ast.ShowTriggers:
554+
if misc.IsBlank(showStmt.DBName) {
555+
showStmt.DBName = c.Database()
556+
}
557+
default:
558+
}
554559
}
555560

556561
if !misc.IsBlank(c.Database()) {

pkg/plan/delete.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ func (p *DeletePlan) Execute(ctx context.Context, hints ...*ast.TableOptimizerHi
6666
return nil, 0, errors.WithStack(err)
6767
}
6868
}
69+
schema := proto.Schema(ctx)
6970
for _, table := range p.Tables {
7071
sb.Reset()
71-
if err = p.generate(&sb, table, hints...); err != nil {
72+
if err = p.generate(&sb, schema, table, hints...); err != nil {
7273
return nil, 0, errors.Wrap(err, "failed to generate sql for delete")
7374
}
7475
sql := sb.String()
@@ -114,7 +115,7 @@ func (p *DeletePlan) Execute(ctx context.Context, hints ...*ast.TableOptimizerHi
114115
return mysqlResult, warnings, nil
115116
}
116117

117-
func (p *DeletePlan) generate(sb *strings.Builder, table string, hints ...*ast.TableOptimizerHint) error {
118+
func (p *DeletePlan) generate(sb *strings.Builder, schema, table string, hints ...*ast.TableOptimizerHint) error {
118119
ctx := format.NewRestoreCtx(constant.DBPackRestoreFormat, sb)
119120
ctx.WriteKeyWord("DELETE ")
120121

@@ -133,7 +134,7 @@ func (p *DeletePlan) generate(sb *strings.Builder, table string, hints ...*ast.T
133134
}
134135

135136
ctx.WriteKeyWord("FROM ")
136-
ctx.WritePlain(table)
137+
ctx.WritePlainf("%s.%s", schema, table)
137138
if p.Stmt.Where != nil {
138139
ctx.WriteKeyWord(" WHERE ")
139140
if err := p.Stmt.Where.Restore(ctx); err != nil {

pkg/plan/delete_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ func TestDeleteOnSingleDBPlan(t *testing.T) {
3737
deleteSql: "delete from student where id in (?,?)",
3838
tables: []string{"student_1", "student_5"},
3939
expectedGenerateSqls: []string{
40-
"DELETE FROM student_1 WHERE `id` IN (?,?)",
41-
"DELETE FROM student_5 WHERE `id` IN (?,?)",
40+
"DELETE FROM school.student_1 WHERE `id` IN (?,?)",
41+
"DELETE FROM school.student_5 WHERE `id` IN (?,?)",
4242
},
4343
},
4444
{
4545
deleteSql: "delete from student where id = 9",
4646
tables: []string{"student_9"},
4747
expectedGenerateSqls: []string{
48-
"DELETE FROM student_9 WHERE `id`=9",
48+
"DELETE FROM school.student_9 WHERE `id`=9",
4949
},
5050
},
5151
}
@@ -68,7 +68,7 @@ func TestDeleteOnSingleDBPlan(t *testing.T) {
6868
}
6969
for i, table := range plan.Tables {
7070
var sb strings.Builder
71-
err := plan.generate(&sb, table)
71+
err := plan.generate(&sb, "school", table)
7272
assert.Nil(t, err)
7373
assert.Equal(t, c.expectedGenerateSqls[i], sb.String())
7474
}

pkg/plan/insert.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ func (p *InsertPlan) Execute(ctx context.Context, _ ...*ast.TableOptimizerHint)
4444
tx proto.Tx
4545
err error
4646
)
47-
if err = p.generate(&sb); err != nil {
47+
schema := proto.Schema(ctx)
48+
if err = p.generate(&sb, schema); err != nil {
4849
return nil, 0, errors.WithStack(err)
4950
}
5051
sql := sb.String()
@@ -77,13 +78,13 @@ func (p *InsertPlan) Execute(ctx context.Context, _ ...*ast.TableOptimizerHint)
7778
}
7879
}
7980

80-
func (p *InsertPlan) generate(sb *strings.Builder) (err error) {
81+
func (p *InsertPlan) generate(sb *strings.Builder, schema string) (err error) {
8182
ctx := format.NewRestoreCtx(constant.DBPackRestoreFormat, sb)
8283

8384
ctx.WriteKeyWord("INSERT ")
8485
ctx.WriteKeyWord("INTO ")
8586

86-
ctx.WritePlain(p.Table)
87+
ctx.WritePlainf("%s.%s", schema, p.Table)
8788

8889
ctx.WritePlain("(")
8990
columnLen := len(p.Columns)

pkg/plan/insert_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestInsertPlan(t *testing.T) {
3636
{
3737
insertSql: "insert into student(id, name, gender, age) values(?,?,?,?)",
3838
table: "student_5",
39-
expectedGenerateSql: "INSERT INTO student_5(id,name,gender,age) VALUES (?,?,?,?)",
39+
expectedGenerateSql: "INSERT INTO school.student_5(id,name,gender,age) VALUES (?,?,?,?)",
4040
},
4141
}
4242

@@ -59,7 +59,7 @@ func TestInsertPlan(t *testing.T) {
5959
Executor: nil,
6060
}
6161
var sb strings.Builder
62-
err = plan.generate(&sb)
62+
err = plan.generate(&sb, "school")
6363
assert.Nil(t, err)
6464
assert.Equal(t, c.expectedGenerateSql, sb.String())
6565
})

0 commit comments

Comments
 (0)