|
| 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