Example #1
0
func (c *Col) getTypeStr() string {
	ans := []string{types.FieldTypeToStr(c.Tp, c.Charset)}
	if c.Flen != -1 {
		if c.Decimal == -1 {
			ans = append(ans, fmt.Sprintf("(%d)", c.Flen))
		} else {
			ans = append(ans, fmt.Sprintf("(%d, %d)", c.Flen, c.Decimal))
		}
	}
	if mysql.HasUnsignedFlag(c.Flag) {
		ans = append(ans, "UNSIGNED")
	}
	if mysql.HasZerofillFlag(c.Flag) {
		ans = append(ans, "ZEROFILL")
	}
	if mysql.HasBinaryFlag(c.Flag) {
		ans = append(ans, "BINARY")
	}
	if c.Charset != "" && c.Charset != charset.CharsetBin {
		ans = append(ans, fmt.Sprintf("CHARACTER SET %s", c.Charset))
	}
	if c.Collate != "" {
		ans = append(ans, fmt.Sprintf("COLLATE %s", c.Collate))
	}
	return strings.Join(ans, " ")
}
Example #2
0
// GetTypeDesc gets the description for column type.
func (c *Col) GetTypeDesc() string {
	var buf bytes.Buffer

	buf.WriteString(types.FieldTypeToStr(c.Tp, c.Charset))
	switch c.Tp {
	case mysql.TypeSet, mysql.TypeEnum:
		// Format is ENUM ('e1', 'e2') or SET ('e1', 'e2')
		// If elem contain ', we will convert ' -> ''
		elems := make([]string, len(c.Elems))
		for i := range elems {
			elems[i] = strings.Replace(c.Elems[i], "'", "''", -1)
		}
		buf.WriteString(fmt.Sprintf("('%s')", strings.Join(elems, "','")))
	default:
		if c.Flen != -1 {
			if c.Decimal == -1 {
				buf.WriteString(fmt.Sprintf("(%d)", c.Flen))
			} else {
				buf.WriteString(fmt.Sprintf("(%d,%d)", c.Flen, c.Decimal))
			}
		}
	}

	if mysql.HasUnsignedFlag(c.Flag) {
		buf.WriteString(" UNSIGNED")
	}
	return buf.String()
}
Example #3
0
// String implements fmt.Stringer interface.
func (c *Col) String() string {
	ans := []string{c.Name.O, types.FieldTypeToStr(c.Tp, c.Charset)}
	if mysql.HasAutoIncrementFlag(c.Flag) {
		ans = append(ans, "AUTO_INCREMENT")
	}
	if mysql.HasNotNullFlag(c.Flag) {
		ans = append(ans, "NOT NULL")
	}
	return strings.Join(ans, " ")
}
Example #4
0
func (c *Col) getTypeDesc() string {
	ans := []string{types.FieldTypeToStr(c.Tp, c.Charset)}
	if c.Flen != -1 {
		if c.Decimal == -1 {
			ans = append(ans, fmt.Sprintf("(%d)", c.Flen))
		} else {
			ans = append(ans, fmt.Sprintf("(%d, %d)", c.Flen, c.Decimal))
		}
	}
	if mysql.HasUnsignedFlag(c.Flag) {
		ans = append(ans, "UNSIGNED")
	}
	return strings.Join(ans, " ")
}
Example #5
0
func (c *Col) getTypeDesc() string {
	ans := []string{types.FieldTypeToStr(c.Tp, c.Charset)}
	switch c.Tp {
	case mysql.TypeSet, mysql.TypeEnum:
		// Format is ENUM ('e1', 'e2') or SET ('e1', 'e2')
		ans = append(ans, fmt.Sprintf("('%s')", strings.Join(c.Elems, "','")))
	default:
		if c.Flen != -1 {
			if c.Decimal == -1 {
				ans = append(ans, fmt.Sprintf("(%d)", c.Flen))
			} else {
				ans = append(ans, fmt.Sprintf("(%d, %d)", c.Flen, c.Decimal))
			}
		}
	}

	if mysql.HasUnsignedFlag(c.Flag) {
		ans = append(ans, "UNSIGNED")
	}
	return strings.Join(ans, " ")
}
Example #6
0
// TypeError returns error for invalid value type.
func (c *Col) TypeError(v interface{}) error {
	return errors.Errorf("cannot use %v (type %T) in assignment to, or comparison with, column %s (type %s)",
		v, v, c.Name, types.FieldTypeToStr(c.Tp, c.Charset))
}
Example #7
0
func newParseColError(err error, c *Col) error {
	return errors.Errorf("parse err %v at column %s (type %s)", err, c.Name, types.FieldTypeToStr(c.Tp, c.Charset))
}