-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support show table status (#263)
- Loading branch information
1 parent
6611c17
commit f896fc3
Showing
6 changed files
with
169 additions
and
5 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,58 @@ | ||
/* | ||
* Copyright 2022 CECTC, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package optimize | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/cectc/dbpack/pkg/plan" | ||
"github.com/cectc/dbpack/pkg/proto" | ||
"github.com/cectc/dbpack/pkg/topo" | ||
"github.com/cectc/dbpack/third_party/parser/ast" | ||
driver "github.com/cectc/dbpack/third_party/types/parser_driver" | ||
) | ||
|
||
func (o Optimizer) optimizeShowTableStatus(ctx context.Context, stmt *ast.ShowStmt, args []interface{}) (proto.Plan, error) { | ||
var ( | ||
topology *topo.Topology | ||
exists bool | ||
) | ||
|
||
if stmt.Tp != ast.ShowTableStatus { | ||
return nil, errors.New("statement must be show table status stmt") | ||
} | ||
|
||
pattern := stmt.Pattern.Pattern.(*driver.ValueExpr) | ||
tableName := pattern.GetDatumString() | ||
if topology, exists = o.topologies[tableName]; !exists { | ||
return &plan.ShowTableStatusPlan{ | ||
Stmt: stmt, | ||
Args: args, | ||
Executor: o.executors[0], | ||
}, nil | ||
} else { | ||
table := topology.DBs[o.executors[0].GroupName()][0] | ||
pattern.SetValue(table) | ||
return &plan.ShowTableStatusPlan{ | ||
Stmt: stmt, | ||
Args: args, | ||
Executor: o.executors[0], | ||
}, nil | ||
} | ||
} |
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
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,57 @@ | ||
/* | ||
* Copyright 2022 CECTC, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package plan | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/cectc/dbpack/pkg/constant" | ||
"github.com/cectc/dbpack/pkg/proto" | ||
"github.com/cectc/dbpack/third_party/parser/ast" | ||
"github.com/cectc/dbpack/third_party/parser/format" | ||
) | ||
|
||
type ShowTableStatusPlan struct { | ||
Stmt *ast.ShowStmt | ||
Args []interface{} | ||
Executor proto.DBGroupExecutor | ||
} | ||
|
||
func (p *ShowTableStatusPlan) Execute(ctx context.Context, _ ...*ast.TableOptimizerHint) (proto.Result, uint16, error) { | ||
var ( | ||
sb strings.Builder | ||
sql string | ||
err error | ||
) | ||
restoreCtx := format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb) | ||
if err = p.Stmt.Restore(restoreCtx); err != nil { | ||
return nil, 0, errors.WithStack(err) | ||
} | ||
sql = sb.String() | ||
commandType := proto.CommandType(ctx) | ||
switch commandType { | ||
case constant.ComQuery: | ||
return p.Executor.Query(ctx, sql) | ||
case constant.ComStmtExecute: | ||
return p.Executor.PrepareQuery(ctx, sql, p.Args...) | ||
default: | ||
return nil, 0, nil | ||
} | ||
} |
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