Skip to content

Commit 5ef0fbf

Browse files
committed
feat: support show stmt
1 parent d4a162b commit 5ef0fbf

2 files changed

Lines changed: 49 additions & 11 deletions

File tree

pkg/executor/sharding.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ func (executor *ShardingExecutor) ExecutorComQuery(ctx context.Context, sql stri
211211
if queryStmt == nil {
212212
return nil, 0, errors.New("query stmt should not be nil")
213213
}
214-
if _, ok := queryStmt.(*ast.SetStmt); ok {
214+
215+
switch stmt := queryStmt.(type) {
216+
case *ast.SetStmt:
215217
for _, db := range executor.all {
216218
go func(db *DataSourceBrief) {
217219
if _, _, err := db.DB.Query(spanCtx, sql); err != nil {
@@ -224,20 +226,28 @@ func (executor *ShardingExecutor) ExecutorComQuery(ctx context.Context, sql stri
224226
AffectedRows: 0,
225227
InsertId: 0,
226228
}, 0, nil
227-
}
228-
if selectStmt, ok := queryStmt.(*ast.SelectStmt); ok {
229-
if selectStmt.Fields != nil && len(selectStmt.Fields.Fields) > 0 {
230-
if _, ok := selectStmt.Fields.Fields[0].Expr.(*ast.VariableExpr); ok {
229+
case *ast.ShowStmt:
230+
return executor.all[0].DB.Query(spanCtx, sql)
231+
case *ast.SelectStmt:
232+
if stmt.Fields != nil && len(stmt.Fields.Fields) > 0 {
233+
if _, ok := stmt.Fields.Fields[0].Expr.(*ast.VariableExpr); ok {
231234
return executor.all[0].DB.Query(spanCtx, sql)
232235
}
233236
}
237+
plan, err = executor.optimizer.Optimize(spanCtx, queryStmt)
238+
if err != nil {
239+
return nil, 0, err
240+
}
241+
proto.WithVariable(spanCtx, constant.TransactionTimeout, executor.config.TransactionTimeout)
242+
return plan.Execute(spanCtx)
243+
default:
244+
plan, err = executor.optimizer.Optimize(spanCtx, queryStmt)
245+
if err != nil {
246+
return nil, 0, err
247+
}
248+
proto.WithVariable(spanCtx, constant.TransactionTimeout, executor.config.TransactionTimeout)
249+
return plan.Execute(spanCtx)
234250
}
235-
plan, err = executor.optimizer.Optimize(spanCtx, queryStmt)
236-
if err != nil {
237-
return nil, 0, err
238-
}
239-
proto.WithVariable(spanCtx, constant.TransactionTimeout, executor.config.TransactionTimeout)
240-
return plan.Execute(spanCtx)
241251
}
242252

243253
func (executor *ShardingExecutor) ExecutorComStmtExecute(ctx context.Context, stmt *proto.Stmt) (proto.Result, uint16, error) {

test/shd/sharding_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,34 @@ func (suite *_ShardingSuite) TestSelectCount() {
181181
}
182182
}
183183

184+
func (suite *_ShardingSuite) TestShowDatabases() {
185+
rows, err := suite.db.Query("SHOW DATABASES")
186+
if suite.NoErrorf(err, "show databases error: %v", err) {
187+
var (
188+
database string
189+
)
190+
for rows.Next() {
191+
err := rows.Scan(&database)
192+
suite.NoError(err)
193+
suite.T().Logf("database: %s", database)
194+
}
195+
}
196+
}
197+
198+
func (suite *_ShardingSuite) TestShowEngines() {
199+
rows, err := suite.db.Query("SHOW ENGINES")
200+
if suite.NoErrorf(err, "show engines error: %v", err) {
201+
var (
202+
engine, support, comment, transactions, xa, savepoints string
203+
)
204+
for rows.Next() {
205+
err := rows.Scan(&engine, &support, &comment, &transactions, &xa, &savepoints)
206+
suite.NoError(err)
207+
suite.T().Logf("%s %s %s %s %s %s", engine, support, comment, transactions, xa, savepoints)
208+
}
209+
}
210+
}
211+
184212
func (suite *_ShardingSuite) TestDeleteDrugResource() {
185213
result, err := suite.db.Exec(deleteDrugResource, 10, 20)
186214
suite.Assert().Nil(err)

0 commit comments

Comments
 (0)