Пример #1
0
func getDefaultValue(ctx context.Context, c *column.Col) (interface{}, bool, error) {
	// Check no default value flag.
	if mysql.HasNoDefaultValueFlag(c.Flag) && c.Tp != mysql.TypeEnum {
		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 := expression.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
	} else if c.Tp == mysql.TypeEnum {
		// For enum type, if no default value and not null is set,
		// the default value is the first element of the enum list
		if c.DefaultValue == nil && mysql.HasNotNullFlag(c.Flag) {
			return c.FieldType.Elems[0], true, nil
		}
	}

	return c.DefaultValue, true, nil
}
Пример #2
0
// NewColDesc returns a new ColDesc for a column.
func NewColDesc(col *Col) *ColDesc {
	// TODO: if we have no primary key and a unique index which's columns are all not null
	// we will set these columns' flag as PriKeyFlag
	// see https://dev.mysql.com/doc/refman/5.7/en/show-columns.html
	// create table
	name := col.Name
	nullFlag := "YES"
	if mysql.HasNotNullFlag(col.Flag) {
		nullFlag = "NO"
	}
	keyFlag := ""
	if mysql.HasPriKeyFlag(col.Flag) {
		keyFlag = "PRI"
	} else if mysql.HasUniKeyFlag(col.Flag) {
		keyFlag = "UNI"
	} else if mysql.HasMultipleKeyFlag(col.Flag) {
		keyFlag = "MUL"
	}
	var defaultValue interface{}
	if !mysql.HasNoDefaultValueFlag(col.Flag) {
		defaultValue = col.DefaultValue
	}

	extra := ""
	if mysql.HasAutoIncrementFlag(col.Flag) {
		extra = "auto_increment"
	} else if mysql.HasOnUpdateNowFlag(col.Flag) {
		extra = "on update CURRENT_TIMESTAMP"
	}

	return &ColDesc{
		Field:        name.O,
		Type:         col.GetTypeDesc(),
		Collation:    col.Collate,
		Null:         nullFlag,
		Key:          keyFlag,
		DefaultValue: defaultValue,
		Extra:        extra,
		Privileges:   defaultPrivileges,
		Comment:      "",
	}
}
Пример #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
}