Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: 优化列的字符集和排序规则审核逻辑 #173

Merged
merged 1 commit into from
Mar 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions session/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ var ErrorsDefault = map[ErrorCode]string{
ER_WRONG_ARGUMENTS: "Incorrect arguments to %s.",
ER_SET_DATA_TYPE_INT_BIGINT: "Set auto-increment data type to int or bigint.",
ER_TIMESTAMP_DEFAULT: "Set default value for timestamp column '%s'.",
ER_CHARSET_ON_COLUMN: "Not Allowed set charset for column '%s.%s'.",
ER_CHARSET_ON_COLUMN: "Not Allowed set charset or collation for column '%s.%s'.",
ER_AUTO_INCR_ID_WARNING: "Auto increment column '%s' is meaningful? it's dangerous!",
ER_ALTER_TABLE_ONCE: "Merge the alter statement for table '%s' to ONE.",
ER_BLOB_CANT_HAVE_DEFAULT: "BLOB, TEXT, GEOMETRY or JSON column '%s' can't have a default value.",
Expand Down Expand Up @@ -477,7 +477,7 @@ var ErrorsChinese = map[ErrorCode]string{
ER_WRONG_ARGUMENTS: "Incorrect arguments to %s.",
ER_SET_DATA_TYPE_INT_BIGINT: "自增列需要设置为int或bigint类型.",
ER_TIMESTAMP_DEFAULT: "请设置timestamp列 '%s' 的默认值.",
ER_CHARSET_ON_COLUMN: "表 '%s' 列 '%s' 禁止设置字符集!",
ER_CHARSET_ON_COLUMN: "表 '%s' 列 '%s' 禁止设置字符集或排序规则!",
ER_AUTO_INCR_ID_WARNING: "自增列('%s')建议命名为'ID'.",
ER_ALTER_TABLE_ONCE: "表 '%s' 的多个alter操作请合并成一个.",
ER_BLOB_CANT_HAVE_DEFAULT: "BLOB,TEXT,GEOMETRY或JSON列 '%s' 禁止设置默认值.",
Expand Down
22 changes: 19 additions & 3 deletions session/session_inception.go
Original file line number Diff line number Diff line change
Expand Up @@ -3895,6 +3895,7 @@ func (s *session) checkAlterTable(node *ast.AlterTableStmt, sql string) {
}

for i, alter := range node.Specs {

switch alter.Tp {
case ast.AlterTableOption:
if len(alter.Options) == 0 {
Expand Down Expand Up @@ -4578,7 +4579,11 @@ func (s *session) mysqlCheckField(t *TableInfo, field *ast.ColumnDef) {
case ast.ColumnOptionGenerated:
hasGenerated = true
case ast.ColumnOptionCollate:
s.AppendErrorNo(ER_CHARSET_ON_COLUMN, tableName, field.Name.Name)
if s.Inc.EnableColumnCharset {
s.checkCollation(op.StrValue)
} else {
s.AppendErrorNo(ER_CHARSET_ON_COLUMN, tableName, field.Name.Name)
}
}
}
}
Expand Down Expand Up @@ -4641,9 +4646,20 @@ func (s *session) mysqlCheckField(t *TableInfo, field *ast.ColumnDef) {
if !notNullFlag && !hasGenerated {
s.AppendErrorNo(ER_NOT_ALLOWED_NULLABLE, field.Name.Name, tableName)
}
}

if field.Tp.Charset != "" || field.Tp.Collate != "" {
if field.Tp.Charset != "binary" {
// 审核所有指定了charset或collate的字段
if field.Tp.Charset != "" || field.Tp.Collate != "" {
if field.Tp.Charset != "" && field.Tp.Charset != "binary" {
if s.Inc.EnableColumnCharset {
s.checkCharset(field.Tp.Charset)
} else {
s.AppendErrorNo(ER_CHARSET_ON_COLUMN, tableName, field.Name.Name)
}
} else if field.Tp.Collate != "" && field.Tp.Collate != "binary" {
if s.Inc.EnableColumnCharset {
s.checkCollation(field.Tp.Collate)
} else {
s.AppendErrorNo(ER_CHARSET_ON_COLUMN, tableName, field.Name.Name)
}
}
Expand Down
76 changes: 54 additions & 22 deletions session/session_inception_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ func (s *testSessionIncSuite) testSQLError(c *C, sql string, errors ...*session.
allErrors := []string{}
for _, row := range res.Rows()[1:] {
if v, ok := row[4].(string); ok {
allErrors = append(allErrors, strings.TrimSpace(v))
v = strings.TrimSpace(v)
if v != "<nil>" {
allErrors = append(allErrors, v)
}
}
}

Expand All @@ -134,7 +137,7 @@ func (s *testSessionIncSuite) testSQLError(c *C, sql string, errors ...*session.
errMsgs = append(errMsgs, e.Error())
}

c.Assert(len(errMsgs), Equals, len(allErrors), Commentf("%v", res.Rows()))
c.Assert(len(errMsgs), Equals, len(allErrors), Commentf("%v", allErrors))

for index, err := range allErrors {
c.Assert(err, Equals, errMsgs[index], Commentf("%v", res.Rows()))
Expand Down Expand Up @@ -927,16 +930,12 @@ func (s *testSessionIncSuite) TestAlterTableAddColumn(c *C) {
session.NewErr(session.ER_CHAR_TO_VARCHAR_LEN, "c1"))

// 字符集
res = s.runCheck(`drop table if exists t1;create table t1(id int);
alter table t1 add column c1 varchar(20) character set utf8;
alter table t1 add column c2 varchar(20) COLLATE utf8_bin;`)
row = res.Rows()[int(s.tk.Se.AffectedRows())-2]
c.Assert(row[2], Equals, "1")
c.Assert(row[4], Equals, "Not Allowed set charset for column 't1.c1'.")

row = res.Rows()[int(s.tk.Se.AffectedRows())-1]
c.Assert(row[2], Equals, "1")
c.Assert(row[4], Equals, "Not Allowed set charset for column 't1.c2'.")
sql = `drop table if exists t1;create table t1(id int);
alter table t1 add column c1 varchar(20) character set utf8;
alter table t1 add column c2 varchar(20) COLLATE utf8_bin;`
s.testSQLError(c, sql,
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c1"),
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c2"))

// 关键字
config.GetGlobalConfig().Inc.EnableIdentiferKeyword = false
Expand Down Expand Up @@ -1128,21 +1127,17 @@ func (s *testSessionIncSuite) TestAlterTableModifyColumn(c *C) {
session.NewErr(session.ER_CHAR_TO_VARCHAR_LEN, "c1"))

// 字符集
res = s.runCheck(`create table t1(id int,c1 varchar(20));
sql = `create table t1(id int,c1 varchar(20));
alter table t1 modify column c1 varchar(20) character set utf8;
alter table t1 modify column c1 varchar(20) COLLATE utf8_bin;`)
row := res.Rows()[int(s.tk.Se.AffectedRows())-2]
c.Assert(row[2], Equals, "1")
c.Assert(row[4], Equals, "Not Allowed set charset for column 't1.c1'.")

row = res.Rows()[int(s.tk.Se.AffectedRows())-1]
c.Assert(row[2], Equals, "1")
c.Assert(row[4], Equals, "Not Allowed set charset for column 't1.c1'.")
alter table t1 modify column c1 varchar(20) COLLATE utf8_bin;`
s.testSQLError(c, sql,
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c1"),
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c1"))

// 列注释
config.GetGlobalConfig().Inc.CheckColumnComment = true
res = s.runCheck("create table t1(id int,c1 varchar(10));alter table t1 modify column c1 varchar(20);")
row = res.Rows()[int(s.tk.Se.AffectedRows())-1]
row := res.Rows()[int(s.tk.Se.AffectedRows())-1]
c.Assert(row[2], Equals, "1")
c.Assert(row[4], Equals, "Column 'c1' in table 't1' have no comments.")

Expand Down Expand Up @@ -2291,6 +2286,43 @@ func (s *testSessionIncSuite) TestAlterTable(c *C) {
alter table t1 drop column c4; `
s.testErrorCode(c, sql)

// 列字符集&排序规则
config.GetGlobalConfig().Inc.EnableColumnCharset = false
sql = `drop table if exists t1;
create table t1(id int primary key);
alter table t1 add column c4 varchar(22) charset utf8;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c4"))

sql = `drop table if exists t1;
create table t1(id int primary key);
alter table t1 add column c4 varchar(22) collate utf8_bin;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c4"))

sql = `drop table if exists t1;
create table t1(id int primary key);
alter table t1 add column c4 varchar(22) charset utf8 collate utf8_bin;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c4"),
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c4"))

config.GetGlobalConfig().Inc.EnableColumnCharset = true
config.GetGlobalConfig().Inc.SupportCharset = "utf8mb4"
sql = `drop table if exists t1;
create table t1(id int primary key);
alter table t1 add column c4 varchar(22) charset utf8 collate utf8_bin;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrCharsetNotSupport, "utf8mb4"))

config.GetGlobalConfig().Inc.SupportCharset = "utf8"
config.GetGlobalConfig().Inc.SupportCollation = "utf8_bin"
sql = `drop table if exists t1;
create table t1(id int primary key);
alter table t1 add column c4 varchar(22) charset utf8 collate utf8mb4_bin;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrCollationNotSupport, "utf8_bin"))

}

func (s *testSessionIncSuite) TestCreateTablePrimaryKey(c *C) {
Expand Down