Skip to content

Commit 7ece3ea

Browse files
authored
Merge pull request #16 from knocknote/feature/add_show_query_executor
add show query executor
2 parents b2331f5 + dd57b20 commit 7ece3ea

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

exec/exec.go

+2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ func NewQueryExecutor(ctx context.Context, conn *connection.DBConnection, tx *co
117117
return NewDeleteQueryExecutor(base)
118118
case sqlparser.Drop:
119119
return NewDropQueryExecutor(base)
120+
case sqlparser.Show:
121+
return NewShowQueryExecutor(base)
120122
default:
121123
}
122124
return nil

exec/show.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package exec
2+
3+
import (
4+
"database/sql"
5+
6+
"github.com/pkg/errors"
7+
"go.knocknote.io/octillery/sqlparser"
8+
)
9+
10+
// ShowQueryExecutor inherits QueryExecutorBase structure
11+
type ShowQueryExecutor struct {
12+
*QueryExecutorBase
13+
}
14+
15+
// NewShowQueryExecutor creates instance of ShowQueryExecutor
16+
func NewShowQueryExecutor(base *QueryExecutorBase) *ShowQueryExecutor {
17+
return &ShowQueryExecutor{base}
18+
}
19+
20+
// Query show multiple rows from any one of shards.
21+
func (e *ShowQueryExecutor) Query() ([]*sql.Rows, error) {
22+
query, ok := e.query.(*sqlparser.QueryBase)
23+
if !ok {
24+
return nil, errors.New("cannot convert to sqlparser.Query to *sqlparser.QueryBase")
25+
}
26+
27+
for _, shardConn := range e.conn.ShardConnections.AllShard() {
28+
rows, err := e.execQuery(shardConn, query.Text, query.Args...)
29+
if err != nil {
30+
return nil, errors.WithStack(err)
31+
}
32+
return []*sql.Rows{rows}, nil
33+
}
34+
35+
return nil, nil
36+
}
37+
38+
// QueryRow show row from any one of shards.
39+
func (e *ShowQueryExecutor) QueryRow() (*sql.Row, error) {
40+
query, ok := e.query.(*sqlparser.QueryBase)
41+
if !ok {
42+
return nil, errors.New("cannot convert to sqlparser.Query to *sqlparser.QueryBase")
43+
}
44+
45+
for _, shardConn := range e.conn.ShardConnections.AllShard() {
46+
row, err := e.execQueryRow(shardConn, query.Text, query.Args...)
47+
if err != nil {
48+
return nil, errors.WithStack(err)
49+
}
50+
return row, nil
51+
}
52+
53+
return nil, nil
54+
}
55+
56+
// Exec doesn't support in ShowQueryExecutor, returns always error.
57+
func (e *ShowQueryExecutor) Exec() (sql.Result, error) {
58+
return nil, errors.New("ShowQueryExecutor cannot invoke Exec()")
59+
}

0 commit comments

Comments
 (0)