Esempio n. 1
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
}
Esempio n. 2
0
// Plan gets SrcPlan/SelectFieldsDefaultPlan.
// If all fields are equal to src plan fields, then gets SrcPlan.
// Default gets SelectFieldsDefaultPlan.
func (r *SelectFieldsRset) Plan(ctx context.Context) (plan.Plan, error) {
	fields := r.SelectList.Fields
	srcFields := r.Src.GetFields()
	if len(fields) == len(srcFields) {
		match := true
		for i, v := range fields {
			// TODO: is it this check enough? e.g, the ident field is t.c.
			if x, ok := v.Expr.(*expressions.Ident); ok && strings.EqualFold(x.L, srcFields[i].Name) && strings.EqualFold(v.Name, srcFields[i].Name) {
				continue
			}

			match = false
			break
		}

		if match {
			return r.Src, nil
		}
	}

	src := r.Src
	if x, ok := src.(*plans.TableDefaultPlan); ok {
		// check whether src plan will be set TableNilPlan, like `select 1, 2 from t`.
		isConst := true
		for _, v := range fields {
			if expressions.FastEval(v.Expr) == nil {
				isConst = false
				break
			}
		}
		if isConst {
			src = &plans.TableNilPlan{T: x.T}
		}
	}

	p := &plans.SelectFieldsDefaultPlan{Src: src, SelectList: r.SelectList}
	return p, nil
}