Skip to content

Commit

Permalink
Merge branch 'master' into coocood/fix-index-plan
Browse files Browse the repository at this point in the history
Conflicts:
	plan/plans/join.go
  • Loading branch information
coocood committed Oct 7, 2015
2 parents b28cf14 + 4172bff commit 90983de
Show file tree
Hide file tree
Showing 64 changed files with 1,956 additions and 788 deletions.
67 changes: 65 additions & 2 deletions bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package tidb

import (
"fmt"
"runtime/debug"

"github.com/ngaut/log"
mysql "github.com/pingcap/tidb/mysqldef"
Expand All @@ -28,7 +29,58 @@ import (

const (
// CreateUserTable is the SQL statement creates User table in system db.
CreateUserTable = "CREATE TABLE if not exists mysql.user (Host CHAR(64), User CHAR(16), Password CHAR(41), PRIMARY KEY (Host, User));"
CreateUserTable = `CREATE TABLE if not exists mysql.user (
Host CHAR(64),
User CHAR(16),
Password CHAR(41),
Select_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Insert_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Update_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Delete_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Create_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Drop_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Grant_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Alter_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Show_db_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Execute_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Create_user_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
PRIMARY KEY (Host, User));`
// CreateDBPrivTable is the SQL statement creates DB scope privilege table in system db.
CreateDBPrivTable = `CREATE TABLE if not exists mysql.db (
Host CHAR(60),
DB CHAR(64),
User CHAR(16),
Select_priv ENUM('N','Y') Not Null DEFAULT 'N',
Insert_priv ENUM('N','Y') Not Null DEFAULT 'N',
Update_priv ENUM('N','Y') Not Null DEFAULT 'N',
Delete_priv ENUM('N','Y') Not Null DEFAULT 'N',
Create_priv ENUM('N','Y') Not Null DEFAULT 'N',
Drop_priv ENUM('N','Y') Not Null DEFAULT 'N',
Grant_priv ENUM('N','Y') Not Null DEFAULT 'N',
Alter_priv ENUM('N','Y') Not Null DEFAULT 'N',
Execute_priv ENUM('N','Y') Not Null DEFAULT 'N',
PRIMARY KEY (Host, DB, User));`
// CreateTablePrivTable is the SQL statement creates table scope privilege table in system db.
CreateTablePrivTable = `CREATE TABLE if not exists mysql.tables_priv (
Host CHAR(60),
DB CHAR(64),
User CHAR(16),
Table_name CHAR(64),
Grantor CHAR(77),
Timestamp Timestamp DEFAULT CURRENT_TIMESTAMP,
Table_priv SET('Select','Insert','Update','Delete','Create','Drop','Grant', 'Index','Alter'),
Column_priv SET('Select','Insert','Update'),
PRIMARY KEY (Host, DB, User, Table_name));`
// CreateColumnPrivTable is the SQL statement creates column scope privilege table in system db.
CreateColumnPrivTable = `CREATE TABLE if not exists mysql.columns_priv(
Host CHAR(60),
DB CHAR(64),
User CHAR(16),
Table_name CHAR(64),
Column_name CHAR(64),
Timestamp Timestamp DEFAULT CURRENT_TIMESTAMP,
Column_priv SET('Select','Insert','Update'),
PRIMARY KEY (Host, DB, User, Table_name, Column_name));`
)

// Bootstrap initiates system DB for a store.
Expand All @@ -46,17 +98,28 @@ func bootstrap(s Session) {
}
mustExecute(s, fmt.Sprintf("CREATE DATABASE %s;", mysql.SystemDB))
initUserTable(s)
initPrivTables(s)
}

func initUserTable(s Session) {
mustExecute(s, CreateUserTable)
// Insert a default user with empty password.
mustExecute(s, `INSERT INTO mysql.user VALUES ("localhost", "root", ""), ("127.0.0.1", "root", ""), ("::1", "root", "");`)
mustExecute(s, `INSERT INTO mysql.user VALUES ("localhost", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"),
("127.0.0.1", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"),
("::1", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y");`)
}

// Initiates privilege tables including mysql.db, mysql.tables_priv and mysql.column_priv.
func initPrivTables(s Session) {
mustExecute(s, CreateDBPrivTable)
mustExecute(s, CreateTablePrivTable)
mustExecute(s, CreateColumnPrivTable)
}

func mustExecute(s Session, sql string) {
_, err := s.Execute(sql)
if err != nil {
debug.PrintStack()
log.Fatal(err)
}
}
17 changes: 12 additions & 5 deletions column/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,20 @@ const defaultPrivileges string = "select,insert,update,references"

func (c *Col) getTypeDesc() string {
ans := []string{types.FieldTypeToStr(c.Tp, c.Charset)}
if c.Flen != -1 {
if c.Decimal == -1 {
ans = append(ans, fmt.Sprintf("(%d)", c.Flen))
} else {
ans = append(ans, fmt.Sprintf("(%d, %d)", c.Flen, c.Decimal))
switch c.Tp {
case mysql.TypeSet, mysql.TypeEnum:
// Format is ENUM ('e1', 'e2') or SET ('e1', 'e2')
ans = append(ans, fmt.Sprintf("('%s')", strings.Join(c.Elems, "','")))
default:
if c.Flen != -1 {
if c.Decimal == -1 {
ans = append(ans, fmt.Sprintf("(%d)", c.Flen))
} else {
ans = append(ans, fmt.Sprintf("(%d, %d)", c.Flen, c.Decimal))
}
}
}

if mysql.HasUnsignedFlag(c.Flag) {
ans = append(ans, "UNSIGNED")
}
Expand Down
5 changes: 5 additions & 0 deletions column/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func (s *testColumnSuite) TestString(c *C) {

cs := col.String()
c.Assert(len(cs), Greater, 0)

col.Tp = mysql.TypeEnum
col.Flag = 0
col.Elems = []string{"a", "b"}
c.Assert(col.getTypeDesc(), Equals, "enum ('a','b')")
}

func (s *testColumnSuite) TestFind(c *C) {
Expand Down
Loading

0 comments on commit 90983de

Please sign in to comment.