Example #1
0
func (t *Table) setOnUpdateData(ctx context.Context, touched []bool, data []interface{}) error {
	ucols := column.FindOnUpdateCols(t.Cols())
	for _, c := range ucols {
		if !touched[c.Offset] {
			v, err := expressions.GetTimeValue(ctx, expressions.CurrentTimestamp, c.Tp, c.Decimal)
			if err != nil {
				return errors.Trace(err)
			}
			data[c.Offset] = v
			touched[c.Offset] = true
		}
	}
	return nil
}
Example #2
0
func getDefaultValue(c *ConstraintOpt, tp byte, fsp int) (interface{}, error) {
	if tp == mysql.TypeTimestamp || tp == mysql.TypeDatetime {
		value, err := expressions.GetTimeValue(nil, c.Evalue, tp, fsp)
		if err != nil {
			return nil, errors.Trace(err)
		}

		// Value is nil means `default null`.
		if value == nil {
			return nil, nil
		}

		// If value is mysql.Time, convert it to string.
		if vv, ok := value.(mysql.Time); ok {
			return vv.String(), nil
		}

		return value, nil
	}

	return expressions.FastEval(c.Evalue), nil
}
Example #3
0
func getDefaultValue(ctx context.Context, c *column.Col) (interface{}, bool, error) {
	// Check no default value flag.
	if mysql.HasNoDefaultValueFlag(c.Flag) {
		return nil, false, errors.Errorf("Field '%s' doesn't have a default value", c.Name)
	}

	// Check and get timestamp/datetime default value.
	if c.Tp == mysql.TypeTimestamp || c.Tp == mysql.TypeDatetime {
		if c.DefaultValue == nil {
			return nil, true, nil
		}

		value, err := expressions.GetTimeValue(ctx, c.DefaultValue, c.Tp, c.Decimal)
		if err != nil {
			return nil, true, errors.Errorf("Field '%s' get default value fail - %s", c.Name, errors.Trace(err))
		}

		return value, true, nil
	}

	return c.DefaultValue, true, nil
}