Skip to content

Commit

Permalink
explain for one query, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
室岡 豊人 authored and 室岡 豊人 committed Jun 27, 2019
1 parent ba6d2d5 commit 4dbca3b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 90 deletions.
4 changes: 2 additions & 2 deletions cmd/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ var explainCmd = &cobra.Command{
return err
}

list, err := service.Explains(ctx, []string{sql}, expOpt, fiOpt)
exp, err := service.Explain(ctx, sql, expOpt, fiOpt)
if err == nil {
view.RenderExplains(list, false)
view.RenderExplain(exp, false)
}
return err
}()
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
versionFlag, err := cmd.Flags().GetBool("version")
if err == nil && versionFlag {
fmt.Println("1.0.1")
fmt.Println("1.0.2")
}
},
}
Expand All @@ -61,5 +61,5 @@ func init() {

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("version", "", true, "version")
rootCmd.Flags().BoolP("version", "v", true, "version")
}
121 changes: 43 additions & 78 deletions service/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func ExplainChannels(
exCh := make(chan *model.ExplainInfo)
errCh := make(chan error)

option.TableMap = GetTableDBMap(ctx) // TODO: ここでやるべき?
option.TableMap = GetTableDBMap(ctx)

go func() {
defer func() {
Expand Down Expand Up @@ -51,27 +51,31 @@ func ExplainChannels(
return exCh, errCh
}

// Explains execute explain queries
func Explains(
// Explain execute explain query
func Explain(
ctx context.Context,
queries []string,
query string,
option *model.ExplainOption,
fi *model.ExplainFilter,
) ([]*model.ExplainInfo, error) {
infos := make([]*model.ExplainInfo, 0)
) (*model.ExplainInfo, error) {
expIno := new(model.ExplainInfo)

option.TableMap = GetTableDBMap(ctx) // TODO: ここでやるべき?
option.TableMap = GetTableDBMap(ctx)

if err := openAdditonal(ctx, GetDBInfo(ctx)); err != nil {
return infos, err
return expIno, err
}

infos, err := exeExplains(ctx, queries, option)
exp, err := exeExplainOne(ctx, query, option)
if err != nil {
return infos, err
return expIno, err
}

return filterResults(infos, fi), nil
if !getAdditionalFlgInFilterResult(exp, fi) {
return expIno, nil
}

return exp, nil
}

func openAdditonal(ctx context.Context, dbi *model.DBInfo) error {
Expand All @@ -86,69 +90,45 @@ func openAdditonal(ctx context.Context, dbi *model.DBInfo) error {
return nil
}

func exeExplains(
ctx context.Context, queries []string, option *model.ExplainOption,
) ([]*model.ExplainInfo, error) {

list := []*model.ExplainInfo{}
func exeExplainOne(
ctx context.Context, query string, option *model.ExplainOption,
) (*model.ExplainInfo, error) {

queryMap := map[string]*model.SQLInfo{}
expInfo := new(model.ExplainInfo)

for _, q := range queries {
// SQL Parse
info, err := getSQLInfo(ctx, q)
if err != nil {
if option.NoError {
if ErrCode(err) == int(SQLParseError) {
continue
}
}
// SQL Parse
info, err := getSQLInfo(ctx, query)
if err != nil {
if !option.NoError || ErrCode(err) != int(SQLParseError) {
return nil, err
}
if info.Table == "" {
continue
}

// uniqフラグ指定の場合、重複SQLの除外
if _, ok := queryMap[info.PrepareSQL]; ok && option.Uniq {
continue
}
queryMap[info.PrepareSQL] = info

if option.UseTableMap {
for _, db := range option.TableMap[info.Table] {
// Explain実行
expInfo, err := exeExplain(ctx, db, q, info.PrepareSQL)
if err != nil {
if option.NoError {
if ErrCode(err) == int(ExeExplainError) {
continue
}
}
return nil, err
}
}
if info.Table == "" {
return expInfo, nil
}

list = append(list, expInfo)
}
} else {
if option.UseTableMap {
for _, db := range option.TableMap[info.Table] {
// Explain実行
db := option.DB
expInfo, err := exeExplain(ctx, db, q, info.PrepareSQL)
expInfo, err = exeExplain(ctx, db, query, info.PrepareSQL)
if err != nil {
if option.NoError {
if ErrCode(err) == int(ExeExplainError) {
continue
}
if !option.NoError || ErrCode(err) != int(ExeExplainError) {
return nil, err
}
}
}
} else {
// Explain実行
db := option.DB
expInfo, err = exeExplain(ctx, db, query, info.PrepareSQL)
if err != nil {
if !option.NoError || ErrCode(err) != int(ExeExplainError) {
return nil, err
}

list = append(list, expInfo)
}

}

return list, nil
return expInfo, nil
}

func exeExplainChannels(
Expand Down Expand Up @@ -241,26 +221,11 @@ func exeExplain(ctx context.Context, db, sql, prepareSQL string) (*model.Explain
}, nil
}

func filterResults(infos []*model.ExplainInfo, fi *model.ExplainFilter) []*model.ExplainInfo {

func getAdditionalFlgInFilterResult(info *model.ExplainInfo, fi *model.ExplainFilter) bool {
if fi == (&model.ExplainFilter{}) {
return infos
return true
}

list := make([]*model.ExplainInfo, 0, len(infos))

for _, info := range infos {

if !getAdditionalFlgInFilterResult(info, fi) {
continue
}
list = append(list, info)
}

return list
}

func getAdditionalFlgInFilterResult(info *model.ExplainInfo, fi *model.ExplainFilter) bool {
add := true
for i, exp := range info.Values {

Expand Down
12 changes: 4 additions & 8 deletions view/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,13 @@ var optionHeader []string = []string{
"value",
}

// RenderExplains render expl results
func RenderExplains(infos []*model.ExplainInfo, isParseEnable bool) {

for _, info := range infos {
RenderExplain(info, isParseEnable)
}
}

// RenderExplain render expl result
func RenderExplain(info *model.ExplainInfo, isParseEnable bool) {

if info == nil || len(info.Values) == 0 {
return
}

headerDatas := make([][]string, 0, 3)
headerDatas = append(headerDatas, []string{"DataBase:", info.DataBase})
if isParseEnable {
Expand Down

0 comments on commit 4dbca3b

Please sign in to comment.