本文整理匯總了Golang中github.com/cockroachdb/cockroach/pkg/sql/parser.Datum.String方法的典型用法代碼示例。如果您正苦於以下問題:Golang Datum.String方法的具體用法?Golang Datum.String怎麽用?Golang Datum.String使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/cockroachdb/cockroach/pkg/sql/parser.Datum
的用法示例。
在下文中一共展示了Datum.String方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: writeTextDatum
func (b *writeBuffer) writeTextDatum(d parser.Datum, sessionLoc *time.Location) {
if log.V(2) {
log.Infof(context.TODO(), "pgwire writing TEXT datum of type: %T, %#v", d, d)
}
if d == parser.DNull {
// NULL is encoded as -1; all other values have a length prefix.
b.putInt32(-1)
return
}
switch v := d.(type) {
case *parser.DBool:
b.putInt32(1)
if *v {
b.writeByte('t')
} else {
b.writeByte('f')
}
case *parser.DInt:
// Start at offset 4 because `putInt32` clobbers the first 4 bytes.
s := strconv.AppendInt(b.putbuf[4:4], int64(*v), 10)
b.putInt32(int32(len(s)))
b.write(s)
case *parser.DFloat:
// Start at offset 4 because `putInt32` clobbers the first 4 bytes.
s := strconv.AppendFloat(b.putbuf[4:4], float64(*v), 'f', -1, 64)
b.putInt32(int32(len(s)))
b.write(s)
case *parser.DDecimal:
b.writeLengthPrefixedString(v.Dec.String())
case *parser.DBytes:
// http://www.postgresql.org/docs/current/static/datatype-binary.html#AEN5667
// Code cribbed from github.com/lib/pq.
result := make([]byte, 2+hex.EncodedLen(len(*v)))
result[0] = '\\'
result[1] = 'x'
hex.Encode(result[2:], []byte(*v))
b.putInt32(int32(len(result)))
b.write(result)
case *parser.DString:
b.writeLengthPrefixedString(string(*v))
case *parser.DDate:
t := time.Unix(int64(*v)*secondsInDay, 0)
s := formatTs(t, nil)
b.putInt32(int32(len(s)))
b.write(s)
case *parser.DTimestamp:
s := formatTs(v.Time, nil)
b.putInt32(int32(len(s)))
b.write(s)
case *parser.DTimestampTZ:
s := formatTs(v.Time, sessionLoc)
b.putInt32(int32(len(s)))
b.write(s)
case *parser.DInterval:
b.writeLengthPrefixedString(v.String())
case *parser.DArray:
var tb bytes.Buffer
tb.WriteString("{")
for i, d := range *v {
if i > 0 {
tb.WriteString(",")
}
tb.WriteString(d.String())
}
tb.WriteString("}")
b.writeLengthPrefixedString(tb.String())
default:
b.setError(errors.Errorf("unsupported type %T", d))
}
}