Skip to content

Commit

Permalink
bugfix[jdbc 'use db']
Browse files Browse the repository at this point in the history
  • Loading branch information
flike committed Jul 31, 2015
1 parent 270f4c9 commit 179ebaa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mysql/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const (
MinProtocolVersion byte = 10
MaxPayloadLen int = 1<<24 - 1
TimeFormat string = "2006-01-02 15:04:05"
ServerVersion string = "kingshard-1.0"
ServerVersion string = "5.6.20-kingshard-1.0"
)

const (
Expand Down Expand Up @@ -154,6 +154,7 @@ var (
KS_TK_COMMIT = 1
KS_TK_ROLLBACK = 1
KS_TK_ADMIN = 1
KS_TK_USE = 1

KS_TK_SELECT = 2

Expand All @@ -168,5 +169,6 @@ var (
"rollback": KS_TK_ROLLBACK,
"admin": KS_TK_ADMIN,
"select": KS_TK_SELECT,
"use": KS_TK_USE,
}
)
2 changes: 2 additions & 0 deletions proxy/server/conn_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func (c *ClientConn) handleQuery(sql string) (err error) {
return c.handleShow(sql, v)
case *sqlparser.Admin:
return c.handleAdmin(v)
case *sqlparser.UseDB:
return c.handleUseDB(v)
default:
return fmt.Errorf("statement %T not support now", stmt)
}
Expand Down
14 changes: 14 additions & 0 deletions proxy/server/conn_use.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package server

import (
"fmt"
"github.com/flike/kingshard/sqlparser"
)

func (c *ClientConn) handleUseDB(stmt *sqlparser.UseDB) error {
if len(stmt.DB) == 0 {
return fmt.Errorf("must have database, not %s", sqlparser.String(stmt))
}
c.db = string(stmt.DB)
return c.writeOK(nil)
}
10 changes: 10 additions & 0 deletions sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,3 +936,13 @@ func (*Show) IStatement() {}
func (node *Show) Format(buf *TrackedBuffer) {
buf.Fprintf("show %s %s %v %v", node.Section, node.Key, node.From, node.LikeOrWhere)
}

type UseDB struct {
DB []byte
}

func (*UseDB) IStatement() {}

func (node *UseDB) Format(buf *TrackedBuffer) {
buf.Fprintf("use %s", node.DB)
}
8 changes: 8 additions & 0 deletions sqlparser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ var (
%type <statement> begin_statement commit_statement rollback_statement
%type <statement> replace_statement
%type <statement> admin_statement
%type <statement> use_statement

%%

Expand Down Expand Up @@ -181,6 +182,7 @@ command:
| rollback_statement
| replace_statement
| admin_statement
| use_statement

select_statement:
SELECT comment_opt distinct_opt select_expression_list
Expand Down Expand Up @@ -276,6 +278,12 @@ admin_statement:
$$ = &Admin{Name : $2, Values : $4}
}

use_statement:
USE sql_id
{
$$=$2
}

create_statement:
CREATE TABLE not_exists_opt ID force_eof
{
Expand Down

0 comments on commit 179ebaa

Please sign in to comment.