Skip to content

Commit

Permalink
添加参数enable_zero_date
Browse files Browse the repository at this point in the history
添加参数enable_zero_date,设置是否支持时间为0值,关闭时忽略数据库设置,默认值为true(基于数据库sql_mode的NO_ZERO_DATE)
  • Loading branch information
hanchuanchuan authored Jul 18, 2019
2 parents 15bec8d + bfb7cc8 commit d8b60f4
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 5 deletions.
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ type Inc struct {
CheckTimestampDefault bool `toml:"check_timestamp_default" json:"check_timestamp_default"`
CheckTimestampCount bool `toml:"check_timestamp_count" json:"check_timestamp_count"`

EnableZeroDate bool `toml:"enable_zero_date" json:"enable_zero_date"`
CheckDatetimeDefault bool `toml:"check_datetime_default" json:"check_datetime_default"`
CheckDatetimeCount bool `toml:"check_datetime_count" json:"check_datetime_count"`

EnableAutoIncrementUnsigned bool `toml:"enable_autoincrement_unsigned" json:"enable_autoincrement_unsigned"`
// 允许blob,text,json列设置为NOT NULL
EnableBlobNotNull bool `toml:"enable_blob_not_null" json:"enable_blob_not_null"`
Expand Down Expand Up @@ -600,6 +604,8 @@ type IncLevel struct {
ER_TEXT_NOT_NULLABLE_ERROR int8 `toml:"er_text_not_nullable_error"`
ER_TIMESTAMP_DEFAULT int8 `toml:"er_timestamp_default"`
ER_TOO_MUCH_AUTO_TIMESTAMP_COLS int8 `toml:"er_too_much_auto_timestamp_cols"`
ER_DATETIME_DEFAULT int8 `toml:"er_datetime_default"`
ER_TOO_MUCH_AUTO_DATETIME_COLS int8 `toml:"er_too_much_auto_datetime_cols"`
ER_USE_ENUM int8 `toml:"er_use_enum"`
ER_USE_TEXT_OR_BLOB int8 `toml:"er_use_text_or_blob"`
ER_WITH_DEFAULT_ADD_COLUMN int8 `toml:"er_with_default_add_column"`
Expand Down Expand Up @@ -690,6 +696,7 @@ var defaultConf = Config{
SkipGrantTable: true,
},
Inc: Inc{
EnableZeroDate: true,
EnableNullable: true,
EnableDropTable: false,
EnableSetEngine: true,
Expand Down
2 changes: 1 addition & 1 deletion config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ write-timeout = "15s"
ignore-error = false

[inc]

enable_zero_date = true
enable_nullable = true
enable_drop_table = false
enable_set_engine = true
Expand Down
14 changes: 11 additions & 3 deletions session/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ const (
ErrFieldNotInGroupBy
ErCantChangeColumnPosition
ER_ERROR_LAST
ER_DATATIME_DEFAULT
ER_TOO_MUCH_AUTO_DATATIME_COLS
)

var ErrorsDefault = map[ErrorCode]string{
Expand Down Expand Up @@ -365,6 +367,8 @@ var ErrorsDefault = map[ErrorCode]string{
ErCantChangeColumnPosition: "Cannot change the position of the column '%s'",
// ErrMixOfGroupFuncAndFields: "Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause",
//ER_NULL_NAME_FOR_INDEX: "Index name cannot be null in table '%s'.",
ER_DATATIME_DEFAULT: "Set default value for DATETIME column '%s'.",
ER_TOO_MUCH_AUTO_DATATIME_COLS: "Incorrect table definition; there can be only one DATETIME column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause",
}

var ErrorsChinese = map[ErrorCode]string{
Expand Down Expand Up @@ -523,10 +527,12 @@ var ErrorsChinese = map[ErrorCode]string{
ErrJsonTypeSupport: "不允许使用json类型(列'%s').",
ErCantChangeColumnPosition: "不允许改变列顺序(列'%s').",
//ER_NULL_NAME_FOR_INDEX: "在表 '%s' 中, 索引名称不能为空.",
ER_DATATIME_DEFAULT: "请设置 datetime 列 '%s' 的默认值.",
ER_TOO_MUCH_AUTO_DATATIME_COLS: "表定义不正确,只能有一个 datetime 字段,在 DEFAULT 或 ON UPDATE指定CURRENT_TIMESTAMP.",
}

func GetErrorLevel(code ErrorCode) uint8 {

switch code {
case ER_ALTER_TABLE_ONCE,
ER_AUTO_INCR_ID_WARNING,
Expand Down Expand Up @@ -575,9 +581,10 @@ func GetErrorLevel(code ErrorCode) uint8 {
ErrNotFoundTableInfo,
ErrNotFoundThreadId,
ErrTableCollationNotSupport,
ER_DATATIME_DEFAULT,
ER_WITH_INSERT_FIELD:
return 1

case ER_CONFLICTING_DECLARATIONS,
ER_NO_DB_ERROR,
ER_KEY_COLUMN_DOES_NOT_EXITS,
Expand Down Expand Up @@ -632,10 +639,11 @@ func GetErrorLevel(code ErrorCode) uint8 {
ErrCollationNotSupport,
ErrEngineNotSupport,
ER_FOREIGN_KEY,
ER_TOO_MUCH_AUTO_DATATIME_COLS,
ER_INCEPTION_EMPTY_QUERY:
//ER_NULL_NAME_FOR_INDEX:
return 2

default:
return 2
}
Expand Down
28 changes: 28 additions & 0 deletions session/session_inception.go
Original file line number Diff line number Diff line change
Expand Up @@ -2812,6 +2812,9 @@ func (s *session) checkCreateTable(node *ast.CreateTableStmt, sql string) {

currentTimestampCount := 0
onUpdateTimestampCount := 0

currentDatetimeCount := 0
onUpdateDatetimeCount := 0

for _, field := range node.Cols {
s.mysqlCheckField(table, field)
Expand Down Expand Up @@ -2846,11 +2849,32 @@ func (s *session) checkCreateTable(node *ast.CreateTableStmt, sql string) {
}
}
}

if field.Tp.Tp == mysql.TypeDatetime {
for _, op := range field.Options {
if op.Tp == ast.ColumnOptionDefaultValue {
if f, ok := op.Expr.(*ast.FuncCallExpr); ok {
if f.FnName.L == ast.CurrentTimestamp {
currentDatetimeCount += 1
}
}
} else if op.Tp == ast.ColumnOptionOnUpdate {
if f, ok := op.Expr.(*ast.FuncCallExpr); ok {
if f.FnName.L == ast.CurrentTimestamp {
onUpdateDatetimeCount += 1
}
}
}
}
}
}

if currentTimestampCount > 1 || onUpdateTimestampCount > 1 {
s.AppendErrorNo(ER_TOO_MUCH_AUTO_TIMESTAMP_COLS)
}
if currentDatetimeCount > 1 || onUpdateDatetimeCount > 1 {
s.AppendErrorNo(ER_TOO_MUCH_AUTO_DATATIME_COLS)
}

s.cacheNewTable(table)
s.myRecord.TableInfo = table
Expand Down Expand Up @@ -6397,6 +6421,10 @@ func (s *session) checkInceptionVariables(number ErrorCode) bool {
return !s.Inc.EnableBlobNotNull
/*case ER_NULL_NAME_FOR_INDEX:
return s.Inc.EnableNullIndexName*/
case ER_DATATIME_DEFAULT:
return s.Inc.CheckDatetimeDefault
case ER_TOO_MUCH_AUTO_DATATIME_COLS:
return s.Inc.CheckDatetimeCount
}

return true
Expand Down
14 changes: 14 additions & 0 deletions session/session_inception_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2173,3 +2173,17 @@ func (s *testSessionIncSuite) TestForeignKey(c *C) {
s.testErrorCode(c, sql)

}
func (s *testSessionIncSuite) TestZeroDate(c *C) {
saved := config.GetGlobalConfig().Inc
defer func() {
config.GetGlobalConfig().Inc = saved
}()

sql := ""

config.GetGlobalConfig().Inc.EnableZeroDate = false
sql = `create table t4 (id int unsigned not null auto_increment primary key comment 'primary key', a datetime not null default 0 comment 'a') comment 'test';`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "a"))
config.GetGlobalConfig().Inc.EnableZeroDate = true
}
6 changes: 5 additions & 1 deletion session/tidb_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ func (s *session) isInvalidDefaultValue(colDef *ast.ColumnDef) bool {
// log.Info(vars.StrictSQLMode, vars.SQLMode.HasNoZeroDateMode(), t.IsZero())
// log.Info(vars.StrictSQLMode, vars.SQLMode.HasNoZeroInDateMode(), t.InvalidZero())
if t.IsZero() {
return vars.StrictSQLMode && vars.SQLMode.HasNoZeroDateMode()
if s.Inc.EnableZeroDate {
return vars.StrictSQLMode && vars.SQLMode.HasNoZeroDateMode()
} else {
return true
}
} else if t.InvalidZero() {
return vars.StrictSQLMode && vars.SQLMode.HasNoZeroInDateMode()
}
Expand Down

0 comments on commit d8b60f4

Please sign in to comment.