-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from knocknote/feature/add_show_query_executor
add show query executor
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package exec | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/pkg/errors" | ||
"go.knocknote.io/octillery/sqlparser" | ||
) | ||
|
||
// ShowQueryExecutor inherits QueryExecutorBase structure | ||
type ShowQueryExecutor struct { | ||
*QueryExecutorBase | ||
} | ||
|
||
// NewShowQueryExecutor creates instance of ShowQueryExecutor | ||
func NewShowQueryExecutor(base *QueryExecutorBase) *ShowQueryExecutor { | ||
return &ShowQueryExecutor{base} | ||
} | ||
|
||
// Query show multiple rows from any one of shards. | ||
func (e *ShowQueryExecutor) Query() ([]*sql.Rows, error) { | ||
query, ok := e.query.(*sqlparser.QueryBase) | ||
if !ok { | ||
return nil, errors.New("cannot convert to sqlparser.Query to *sqlparser.QueryBase") | ||
} | ||
|
||
for _, shardConn := range e.conn.ShardConnections.AllShard() { | ||
rows, err := e.execQuery(shardConn, query.Text, query.Args...) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
return []*sql.Rows{rows}, nil | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
// QueryRow show row from any one of shards. | ||
func (e *ShowQueryExecutor) QueryRow() (*sql.Row, error) { | ||
query, ok := e.query.(*sqlparser.QueryBase) | ||
if !ok { | ||
return nil, errors.New("cannot convert to sqlparser.Query to *sqlparser.QueryBase") | ||
} | ||
|
||
for _, shardConn := range e.conn.ShardConnections.AllShard() { | ||
row, err := e.execQueryRow(shardConn, query.Text, query.Args...) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
return row, nil | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
// Exec doesn't support in ShowQueryExecutor, returns always error. | ||
func (e *ShowQueryExecutor) Exec() (sql.Result, error) { | ||
return nil, errors.New("ShowQueryExecutor cannot invoke Exec()") | ||
} |