Skip to content

Commit

Permalink
Merge pull request #16 from knocknote/feature/add_show_query_executor
Browse files Browse the repository at this point in the history
add show query executor
  • Loading branch information
goccy authored Jul 17, 2019
2 parents b2331f5 + dd57b20 commit 7ece3ea
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func NewQueryExecutor(ctx context.Context, conn *connection.DBConnection, tx *co
return NewDeleteQueryExecutor(base)
case sqlparser.Drop:
return NewDropQueryExecutor(base)
case sqlparser.Show:
return NewShowQueryExecutor(base)
default:
}
return nil
Expand Down
59 changes: 59 additions & 0 deletions exec/show.go
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()")
}

0 comments on commit 7ece3ea

Please sign in to comment.