Skip to content

Commit

Permalink
bugfix select shard rule
Browse files Browse the repository at this point in the history
  • Loading branch information
flike committed Sep 14, 2015
1 parent 7e71658 commit cd866d6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
32 changes: 27 additions & 5 deletions proxy/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func (r *Router) GetRule(table string) *Rule {
table = arry[1]
}
}

rule := r.Rules[table]
if rule == nil {
return r.DefaultRule
Expand Down Expand Up @@ -210,8 +209,14 @@ func (r *Router) BuildPlan(statement sqlparser.Statement) (*Plan, error) {
func (r *Router) buildSelectPlan(statement sqlparser.Statement) (*Plan, error) {
plan := &Plan{}
var where *sqlparser.Where
var tableName string
stmt := statement.(*sqlparser.Select)
plan.Rule = r.GetRule(sqlparser.String(stmt.From[0])) //根据表名获得分表规则
if ate, ok := (stmt.From[0]).(*sqlparser.AliasedTableExpr); ok {
tableName = sqlparser.String(ate.Expr)
} else {
tableName = sqlparser.String(stmt.From[0])
}
plan.Rule = r.GetRule(tableName) //根据表名获得分表规则
where = stmt.Where

if where != nil {
Expand Down Expand Up @@ -388,13 +393,30 @@ func (r *Router) generateSelectSql(plan *Plan, stmt sqlparser.Statement) error {
for i := 0; i < tableCount; i++ {
buf := sqlparser.NewTrackedBuffer(nil)

buf.Fprintf("select %v%s%v from %v",
buf.Fprintf("select %v%s%v from ",
node.Comments,
node.Distinct,
node.SelectExprs,
node.From,
)
fmt.Fprintf(buf, "_%04d", plan.RouteTableIndexs[i])
if ate, ok := (node.From[0]).(*sqlparser.AliasedTableExpr); ok {
if len(ate.As) != 0 {
fmt.Fprintf(buf, "%s_%04d AS %s",
sqlparser.String(ate.Expr),
plan.RouteTableIndexs[i],
string(ate.As),
)
} else {
fmt.Fprintf(buf, "%s_%04d",
sqlparser.String(ate.Expr),
plan.RouteTableIndexs[i],
)
}
} else {
fmt.Fprintf(buf, "%s_%04d",
sqlparser.String(node.From[0]),
plan.RouteTableIndexs[i],
)
}
buf.Fprintf("%v%v%v%v%v%s",
node.Where,
node.GroupBy,
Expand Down
2 changes: 1 addition & 1 deletion proxy/server/conn_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func (c *ClientConn) GetExecNode(tokens []string,
if 0 < len(rules) {
tokensLen := len(tokens)
if 0 < tokensLen {
tokenId, ok := mysql.WHITE_TOKEN_MAP[tokens[0]]
tokenId, ok := mysql.WHITE_TOKEN_MAP[strings.ToLower(tokens[0])]
if ok == true {
switch tokenId {
case mysql.TK_ID_SELECT, mysql.TK_ID_DELETE:
Expand Down

0 comments on commit cd866d6

Please sign in to comment.