本文整理汇总了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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}