當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Datum.ConvertTo方法代碼示例

本文整理匯總了Golang中github.com/pingcap/tidb/util/types.Datum.ConvertTo方法的典型用法代碼示例。如果您正苦於以下問題:Golang Datum.ConvertTo方法的具體用法?Golang Datum.ConvertTo怎麽用?Golang Datum.ConvertTo使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/pingcap/tidb/util/types.Datum的用法示例。


在下文中一共展示了Datum.ConvertTo方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: CastValue

// CastValue casts a value based on column type.
func CastValue(ctx context.Context, val types.Datum, col *Column) (casted types.Datum, err error) {
	casted, err = val.ConvertTo(&col.FieldType)
	if err != nil {
		if variable.GetSessionVars(ctx).StrictSQLMode {
			return casted, errors.Trace(err)
		}
		// TODO: add warnings.
		log.Warnf("cast value error %v", err)
	}
	return casted, nil
}
開發者ID:tangfeixiong,項目名稱:tidb,代碼行數:12,代碼來源:column.go

示例2: convertToDuration

func convertToDuration(sc *variable.StatementContext, arg types.Datum, fsp int) (d types.Datum, err error) {
	f := types.NewFieldType(mysql.TypeDuration)
	f.Decimal = fsp

	d, err = arg.ConvertTo(sc, f)
	if err != nil {
		d.SetNull()
		return d, errors.Trace(err)
	}

	if d.IsNull() {
		return d, nil
	}

	if d.Kind() != types.KindMysqlDuration {
		d.SetNull()
		return d, errors.Errorf("need duration type, but got %T", d.GetValue())
	}
	return d, nil
}
開發者ID:pingcap,項目名稱:tidb,代碼行數:20,代碼來源:builtin_time.go

示例3: convertToTime

func convertToTime(sc *variable.StatementContext, arg types.Datum, tp byte) (d types.Datum, err error) {
	f := types.NewFieldType(tp)
	f.Decimal = types.MaxFsp

	d, err = arg.ConvertTo(sc, f)
	if err != nil {
		d.SetNull()
		return d, errors.Trace(err)
	}

	if d.IsNull() {
		return d, nil
	}

	if d.Kind() != types.KindMysqlTime {
		d.SetNull()
		return d, errors.Errorf("need time type, but got %T", d.GetValue())
	}
	return d, nil
}
開發者ID:pingcap,項目名稱:tidb,代碼行數:20,代碼來源:builtin_time.go

示例4: convertToTime

func convertToTime(arg types.Datum, tp byte) (d types.Datum, err error) {
	f := types.NewFieldType(tp)
	f.Decimal = mysql.MaxFsp

	d, err = arg.ConvertTo(f)
	if err != nil {
		d.SetNull()
		return d, errors.Trace(err)
	}

	if d.Kind() == types.KindNull {
		return d, nil
	}

	if d.Kind() != types.KindMysqlTime {
		err = errors.Errorf("need time type, but got %T", d.GetValue())
		d.SetNull()
		return d, err
	}
	return d, nil
}
開發者ID:astaxie,項目名稱:tidb,代碼行數:21,代碼來源:builtin_time.go

示例5: convertToDuration

func convertToDuration(arg types.Datum, fsp int) (d types.Datum, err error) {
	f := types.NewFieldType(mysql.TypeDuration)
	f.Decimal = fsp

	d, err = arg.ConvertTo(f)
	if err != nil {
		d.SetNull()
		return d, errors.Trace(err)
	}

	if d.Kind() == types.KindNull {
		d.SetNull()
		return d, nil
	}

	if d.Kind() != types.KindMysqlDuration {
		err = errors.Errorf("need duration type, but got %T", d.GetValue())
		d.SetNull()
		return d, err
	}
	return d, nil
}
開發者ID:astaxie,項目名稱:tidb,代碼行數:22,代碼來源:builtin_time.go

示例6: AddRecord

// AddRecord implements table.Table AddRecord interface.
func (t *Table) AddRecord(ctx context.Context, r []types.Datum) (recordID int64, err error) {
	var hasRecordID bool
	for _, col := range t.Cols() {
		if col.IsPKHandleColumn(t.meta) {
			recordID = r[col.Offset].GetInt64()
			hasRecordID = true
			break
		}
	}
	if !hasRecordID {
		recordID, err = t.alloc.Alloc(t.ID)
		if err != nil {
			return 0, errors.Trace(err)
		}
	}
	txn, err := ctx.GetTxn(false)
	if err != nil {
		return 0, errors.Trace(err)
	}
	bs := kv.NewBufferStore(txn)
	defer bs.Release()
	// Insert new entries into indices.
	h, err := t.addIndices(ctx, recordID, r, bs)
	if err != nil {
		return h, errors.Trace(err)
	}

	if err = t.LockRow(ctx, recordID, false); err != nil {
		return 0, errors.Trace(err)
	}
	// Set public and write only column value.
	for _, col := range t.writableCols() {
		if col.IsPKHandleColumn(t.meta) {
			continue
		}
		var value types.Datum
		if col.State == model.StateWriteOnly || col.State == model.StateWriteReorganization {
			// if col is in write only or write reorganization state, we must add it with its default value.
			value, _, err = table.GetColDefaultValue(ctx, &col.ColumnInfo)
			if err != nil {
				return 0, errors.Trace(err)
			}
			value, err = value.ConvertTo(&col.FieldType)
			if err != nil {
				return 0, errors.Trace(err)
			}
		} else {
			value = r[col.Offset]
		}

		key := t.RecordKey(recordID, col)
		err = SetColValue(txn, key, value)
		if err != nil {
			return 0, errors.Trace(err)
		}
	}
	if err = bs.SaveTo(txn); err != nil {
		return 0, errors.Trace(err)
	}

	variable.GetSessionVars(ctx).AddAffectedRows(1)
	return recordID, nil
}
開發者ID:astaxie,項目名稱:tidb,代碼行數:64,代碼來源:tables.go


注:本文中的github.com/pingcap/tidb/util/types.Datum.ConvertTo方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。