Esempio n. 1
0
func getZeroValue(col *model.ColumnInfo) types.Datum {
	var d types.Datum
	switch col.Tp {
	case mysql.TypeTiny, mysql.TypeInt24, mysql.TypeShort, mysql.TypeLong, mysql.TypeLonglong, mysql.TypeYear:
		if mysql.HasUnsignedFlag(col.Flag) {
			d.SetUint64(0)
		} else {
			d.SetInt64(0)
		}
	case mysql.TypeFloat:
		d.SetFloat32(0)
	case mysql.TypeDouble:
		d.SetFloat64(0)
	case mysql.TypeNewDecimal:
		d.SetMysqlDecimal(mysql.NewDecimalFromInt(0, 0))
	case mysql.TypeString, mysql.TypeVarString, mysql.TypeVarchar:
		d.SetString("")
	case mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
		d.SetBytes([]byte{})
	case mysql.TypeDuration:
		d.SetMysqlDuration(mysql.ZeroDuration)
	case mysql.TypeDate, mysql.TypeNewDate:
		d.SetMysqlTime(mysql.ZeroDate)
	case mysql.TypeTimestamp:
		d.SetMysqlTime(mysql.ZeroTimestamp)
	case mysql.TypeDatetime:
		d.SetMysqlTime(mysql.ZeroDatetime)
	case mysql.TypeBit:
		d.SetMysqlBit(mysql.Bit{Value: 0, Width: mysql.MinBitWidth})
	case mysql.TypeSet:
		d.SetMysqlSet(mysql.Set{})
	}
	return d
}
Esempio n. 2
0
// See http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format
func builtinDateFormat(args []types.Datum, ctx context.Context) (types.Datum, error) {
	var (
		isPercent bool
		ret       []byte
		d         types.Datum
	)

	// TODO: Some invalid format like 2000-00-01(the month is 0) will return null.
	for _, b := range []byte(args[1].GetString()) {
		if isPercent {
			if b == '%' {
				ret = append(ret, b)
			} else {
				str, err := convertDateFormat(ctx, args[0], b)
				if err != nil {
					return types.Datum{}, errors.Trace(err)
				}
				if str.IsNull() {
					return types.Datum{}, nil
				}
				ret = append(ret, str.GetString()...)
			}
			isPercent = false
			continue
		}
		if b == '%' {
			isPercent = true
		} else {
			ret = append(ret, b)
		}
	}
	d.SetString(string(ret))
	return d, nil
}
Esempio n. 3
0
func flatten(data types.Datum) (types.Datum, error) {
	switch data.Kind() {
	case types.KindMysqlTime:
		// for mysql datetime, timestamp and date type
		b, err := data.GetMysqlTime().Marshal()
		if err != nil {
			return types.NewDatum(nil), errors.Trace(err)
		}
		return types.NewDatum(b), nil
	case types.KindMysqlDuration:
		// for mysql time type
		data.SetInt64(int64(data.GetMysqlDuration().Duration))
		return data, nil
	case types.KindMysqlDecimal:
		data.SetString(data.GetMysqlDecimal().String())
		return data, nil
	case types.KindMysqlEnum:
		data.SetUint64(data.GetMysqlEnum().Value)
		return data, nil
	case types.KindMysqlSet:
		data.SetUint64(data.GetMysqlSet().Value)
		return data, nil
	case types.KindMysqlBit:
		data.SetUint64(data.GetMysqlBit().Value)
		return data, nil
	case types.KindMysqlHex:
		data.SetInt64(data.GetMysqlHex().Value)
		return data, nil
	default:
		return data, nil
	}
}
Esempio n. 4
0
func flatten(data types.Datum) (types.Datum, error) {
	switch data.Kind() {
	case types.KindMysqlTime:
		// for mysql datetime, timestamp and date type
		return types.NewUintDatum(data.GetMysqlTime().ToPackedUint()), nil
	case types.KindMysqlDuration:
		// for mysql time type
		data.SetInt64(int64(data.GetMysqlDuration().Duration))
		return data, nil
	case types.KindMysqlDecimal:
		data.SetString(data.GetMysqlDecimal().String())
		return data, nil
	case types.KindMysqlEnum:
		data.SetUint64(data.GetMysqlEnum().Value)
		return data, nil
	case types.KindMysqlSet:
		data.SetUint64(data.GetMysqlSet().Value)
		return data, nil
	case types.KindMysqlBit:
		data.SetUint64(data.GetMysqlBit().Value)
		return data, nil
	case types.KindMysqlHex:
		data.SetInt64(data.GetMysqlHex().Value)
		return data, nil
	default:
		return data, nil
	}
}
Esempio n. 5
0
// GetSystemVar gets a system variable.
func (s *SessionVars) GetSystemVar(key string) types.Datum {
	var d types.Datum
	key = strings.ToLower(key)
	sVal, ok := s.systems[key]
	if ok {
		d.SetString(sVal)
	}
	return d
}
Esempio n. 6
0
func parseDayInterval(value types.Datum) (int64, error) {
	switch value.Kind() {
	case types.KindString:
		vs := value.GetString()
		s := strings.ToLower(vs)
		if s == "false" {
			return 0, nil
		} else if s == "true" {
			return 1, nil
		}
		value.SetString(reg.FindString(vs))
	}
	return value.ToInt64()
}
Esempio n. 7
0
// GetSystemVar gets a system variable.
func GetSystemVar(s *variable.SessionVars, key string) types.Datum {
	var d types.Datum
	key = strings.ToLower(key)
	sVal, ok := s.Systems[key]
	if ok {
		d.SetString(sVal)
	} else {
		// TiDBSkipConstraintCheck is a session scope vars. We do not store it in the global table.
		if key == variable.TiDBSkipConstraintCheck {
			d.SetString(variable.SysVars[variable.TiDBSkipConstraintCheck].Value)
		}
	}
	return d
}
Esempio n. 8
0
func convertDateFormat(ctx context.Context, arg types.Datum, b byte) (types.Datum, error) {
	var d types.Datum
	var err error

	switch b {
	case 'b':
		d, err = builtinMonthName([]types.Datum{arg}, ctx)
		if err == nil && !d.IsNull() {
			d.SetString(d.GetString()[:3])
		}
	case 'M':
		d, err = builtinMonthName([]types.Datum{arg}, ctx)
	case 'm':
		d, err = builtinMonth([]types.Datum{arg}, ctx)
		if err == nil && !d.IsNull() {
			d.SetString(fmt.Sprintf("%02d", d.GetInt64()))
		}
	case 'c':
		d, err = builtinMonth([]types.Datum{arg}, ctx)
	case 'D':
		d, err = abbrDayOfMonth(arg, ctx)
	case 'd':
		d, err = builtinDayOfMonth([]types.Datum{arg}, ctx)
		if err == nil && !d.IsNull() {
			d.SetString(fmt.Sprintf("%02d", d.GetInt64()))
		}
	case 'e':
		d, err = builtinDayOfMonth([]types.Datum{arg}, ctx)
	case 'j':
		d, err = builtinDayOfYear([]types.Datum{arg}, ctx)
		if err == nil {
			d.SetString(fmt.Sprintf("%03d", d.GetInt64()))
		}
	case 'H', 'k':
		d, err = builtinHour([]types.Datum{arg}, ctx)
		if err == nil && b == 'H' && !d.IsNull() {
			d.SetString(fmt.Sprintf("%02d", d.GetInt64()))
		}
	case 'h', 'I', 'l':
		d, err = builtinHour([]types.Datum{arg}, ctx)
		if err == nil && !d.IsNull() {
			if d.GetInt64() > 12 {
				d.SetInt64(d.GetInt64() - 12)
			} else if d.GetInt64() == 0 {
				d.SetInt64(12)
			}
			d.SetString(fmt.Sprintf("%02d", d.GetInt64()))
		}
	case 'i':
		d, err = builtinMinute([]types.Datum{arg}, ctx)
		if err == nil && !d.IsNull() {
			d.SetString(fmt.Sprintf("%02d", d.GetInt64()))
		}
	case 'p':
		d, err = builtinHour([]types.Datum{arg}, ctx)
		if err == nil && !d.IsNull() {
			if d.GetInt64() < 12 {
				d.SetString("AM")
				break
			}
			d.SetString("PM")
		}
	case 'r':
		d, err = to12Hour(arg, ctx)
	case 'T':
		d, err = builtinTime([]types.Datum{arg}, ctx)
		if err == nil && !d.IsNull() {
			duration := types.Duration{
				Duration: d.GetMysqlDuration().Duration,
				Fsp:      0}
			d.SetMysqlDuration(duration)
		}
	case 'S', 's':
		d, err = builtinSecond([]types.Datum{arg}, ctx)
		if err == nil && !d.IsNull() {
			d.SetString(fmt.Sprintf("%02d", d.GetInt64()))
		}
	case 'f':
		d, err = builtinMicroSecond([]types.Datum{arg}, ctx)
		if err == nil && !d.IsNull() {
			d.SetString(fmt.Sprintf("%06d", d.GetInt64()))
		}
	case 'U':
		d, err = builtinWeek([]types.Datum{arg, types.NewIntDatum(0)}, ctx)
		if err == nil && !d.IsNull() {
			d.SetString(fmt.Sprintf("%02d", d.GetInt64()))
		}
	case 'u':
		d, err = builtinWeek([]types.Datum{arg, types.NewIntDatum(1)}, ctx)
		if err == nil && !d.IsNull() {
			d.SetString(fmt.Sprintf("%02d", d.GetInt64()))
		}
	case 'V':
		d, err = builtinWeek([]types.Datum{arg, types.NewIntDatum(2)}, ctx)
		if err == nil && !d.IsNull() {
			d.SetString(fmt.Sprintf("%02d", d.GetInt64()))
		}
	case 'v':
		d, err = builtinWeek([]types.Datum{arg, types.NewIntDatum(3)}, ctx)
		if err == nil && !d.IsNull() {
			d.SetString(fmt.Sprintf("%02d", d.GetInt64()))
		}
	case 'a':
		d, err = builtinDayName([]types.Datum{arg}, ctx)
		if err == nil && !d.IsNull() {
			d.SetString(d.GetString()[:3])
		}
	case 'W':
		d, err = builtinDayName([]types.Datum{arg}, ctx)
	case 'w':
		d, err = builtinDayOfWeek([]types.Datum{arg}, ctx)
		if err == nil && !d.IsNull() {
			d.SetInt64(d.GetInt64() - 1)
		}
	case 'X':
		d, err = builtinYearWeek([]types.Datum{arg, types.NewIntDatum(2)}, ctx)
		if err == nil && !d.IsNull() {
			if d.GetInt64() == math.MaxUint32 {
				break
			}
			str := fmt.Sprintf("%04d", d.GetInt64())
			d.SetString(fmt.Sprintf("%04s", str[:4]))
		}
	case 'x':
		d, err = builtinYearWeek([]types.Datum{arg, types.NewIntDatum(3)}, ctx)
		if err == nil && !d.IsNull() {
			if d.GetInt64() == math.MaxUint32 {
				break
			}
			str := fmt.Sprintf("%04d", d.GetInt64())
			d.SetString(fmt.Sprintf("%04s", str[:4]))
		}
	case 'Y':
		d, err = builtinYear([]types.Datum{arg}, ctx)
		if err == nil && !d.IsNull() {
			d.SetString(fmt.Sprintf("%04d", d.GetInt64()))
		}
	case 'y':
		d, err = builtinYear([]types.Datum{arg}, ctx)
		if err == nil && !d.IsNull() {
			str := fmt.Sprintf("%04d", d.GetInt64())
			d.SetString(fmt.Sprintf("%02s", str[2:]))
		}
	default:
		d.SetString(string(b))
	}

	if err == nil && !d.IsNull() {
		d.SetString(fmt.Sprintf("%v", d.GetValue()))
	}
	return d, errors.Trace(err)
}