func removeOnUpdateNowFlag(c *column.Col) { // For timestamp Col, if it is set null or default value, // OnUpdateNowFlag should be removed. if mysql.HasTimestampFlag(c.Flag) { c.Flag &= ^uint(mysql.OnUpdateNowFlag) } }
func setNoDefaultValueFlag(c *column.Col, hasDefaultValue bool) { if hasDefaultValue { return } if !mysql.HasNotNullFlag(c.Flag) { return } // Check if it is an `AUTO_INCREMENT` field or `TIMESTAMP` field. if !mysql.HasAutoIncrementFlag(c.Flag) && !mysql.HasTimestampFlag(c.Flag) { c.Flag |= mysql.NoDefaultValueFlag } }
func setTimestampDefaultValue(c *column.Col, hasDefaultValue bool, setOnUpdateNow bool) { if hasDefaultValue { return } // For timestamp Col, if is not set default value or not set null, use current timestamp. if mysql.HasTimestampFlag(c.Flag) && mysql.HasNotNullFlag(c.Flag) { if setOnUpdateNow { c.DefaultValue = expressions.ZeroTimestamp } else { c.DefaultValue = expressions.CurrentTimestamp } } }
func (s *InsertIntoStmt) initDefaultValues(ctx context.Context, t table.Table, cols []*column.Col, row []interface{}, marked map[int]struct{}) error { var err error var defaultValueCols []*column.Col for i, c := range cols { if row[i] != nil { // Column value is not nil, continue. continue } // If the nil value is evaluated in insert list, we will use nil except auto increment column. if _, ok := marked[i]; ok && !mysql.HasAutoIncrementFlag(c.Flag) && !mysql.HasTimestampFlag(c.Flag) { continue } if mysql.HasAutoIncrementFlag(c.Flag) { var id int64 if id, err = t.AllocAutoID(); err != nil { return errors.Trace(err) } row[i] = id variable.GetSessionVars(ctx).SetLastInsertID(uint64(id)) } else { var value interface{} value, _, err = getDefaultValue(ctx, c) if err != nil { return errors.Trace(err) } row[i] = value } defaultValueCols = append(defaultValueCols, c) } if err = column.CastValues(ctx, row, defaultValueCols); err != nil { return errors.Trace(err) } return nil }