本文整理汇总了Golang中github.com/pingcap/tidb/util/types.NewStringDatum函数的典型用法代码示例。如果您正苦于以下问题:Golang NewStringDatum函数的具体用法?Golang NewStringDatum怎么用?Golang NewStringDatum使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewStringDatum函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestStrToDate
func (s *testEvaluatorSuite) TestStrToDate(c *C) {
tests := []struct {
Date string
Format string
Success bool
Expect time.Time
}{
{"20161122165022", "%Y%m%d%H%i%s", true, time.Date(2016, 11, 22, 16, 50, 22, 0, time.Local)},
{"2016 11 22 16 50 22", "%Y%m%d%H%i%s", true, time.Date(2016, 11, 22, 16, 50, 22, 0, time.Local)},
{"16-50-22 2016 11 22", "%H-%i-%s%Y%m%d", true, time.Date(2016, 11, 22, 16, 50, 22, 0, time.Local)},
{"16-50 2016 11 22", "%H-%i-%s%Y%m%d", false, time.Time{}},
}
for _, test := range tests {
date := types.NewStringDatum(test.Date)
format := types.NewStringDatum(test.Format)
result, err := builtinStrToDate([]types.Datum{date, format}, s.ctx)
if !test.Success {
c.Assert(err, IsNil)
c.Assert(result.IsNull(), IsTrue)
continue
}
c.Assert(result.Kind(), Equals, types.KindMysqlTime)
value := result.GetMysqlTime()
t1, _ := value.Time.GoTime()
c.Assert(t1, Equals, test.Expect)
}
}
示例2: TestEvalCoalesce
func (s *testEvalSuite) TestEvalCoalesce(c *C) {
colID := int64(1)
row := make(map[int64]types.Datum)
row[colID] = types.NewIntDatum(100)
xevaluator := &Evaluator{Row: row}
nullDatum := types.Datum{}
nullDatum.SetNull()
notNullDatum := types.NewStringDatum("not-null")
cases := []struct {
expr *tipb.Expr
result types.Datum
}{
{
expr: buildExpr(tipb.ExprType_Coalesce, nullDatum, nullDatum, nullDatum),
result: nullDatum,
},
{
expr: buildExpr(tipb.ExprType_Coalesce, nullDatum, notNullDatum, nullDatum),
result: notNullDatum,
},
{
expr: buildExpr(tipb.ExprType_Coalesce, nullDatum, notNullDatum, types.NewStringDatum("not-null-2"), nullDatum),
result: notNullDatum,
},
}
for _, ca := range cases {
result, err := xevaluator.Eval(ca.expr)
c.Assert(err, IsNil)
c.Assert(result.Kind(), Equals, ca.result.Kind())
cmp, err := result.CompareDatum(xevaluator.sc, ca.result)
c.Assert(err, IsNil)
c.Assert(cmp, Equals, 0)
}
}
示例3: TestEvalIfNull
func (s *testEvalSuite) TestEvalIfNull(c *C) {
colID := int64(1)
xevaluator := NewEvaluator(new(variable.StatementContext))
xevaluator.Row[colID] = types.NewDatum(100)
null, notNull, expr := types.Datum{}, types.NewStringDatum("left"), types.NewStringDatum("right")
cases := []struct {
expr *tipb.Expr
result types.Datum
}{
{
expr: buildExpr(tipb.ExprType_IfNull,
null, expr),
result: expr,
},
{
expr: buildExpr(tipb.ExprType_IfNull,
notNull, expr),
result: notNull,
},
{
expr: buildExpr(tipb.ExprType_IfNull,
notNull, null),
result: notNull,
},
}
for _, ca := range cases {
result, err := xevaluator.Eval(ca.expr)
c.Assert(err, IsNil)
c.Assert(result.Kind(), Equals, ca.result.Kind())
cmp, err := result.CompareDatum(xevaluator.sc, ca.result)
c.Assert(err, IsNil)
c.Assert(cmp, Equals, 0)
}
}
示例4: TestFromUnixTime
func (s *testEvaluatorSuite) TestFromUnixTime(c *C) {
defer testleak.AfterTest(c)()
tbl := []struct {
isDecimal bool
integralPart int64
fractionalPart int64
decimal float64
format string
ansLen int
}{
{false, 1451606400, 0, 0, "", 19},
{true, 1451606400, 123456000, 1451606400.123456, "", 26},
{true, 1451606400, 999999000, 1451606400.999999, "", 26},
{true, 1451606400, 999999900, 1451606400.9999999, "", 19},
{false, 1451606400, 0, 0, "%Y %D %M %h:%i:%s %x", 19},
{true, 1451606400, 123456000, 1451606400.123456, "%Y %D %M %h:%i:%s %x", 26},
{true, 1451606400, 999999000, 1451606400.999999, "%Y %D %M %h:%i:%s %x", 26},
{true, 1451606400, 999999900, 1451606400.9999999, "%Y %D %M %h:%i:%s %x", 19},
}
for _, t := range tbl {
var timestamp types.Datum
if !t.isDecimal {
timestamp.SetInt64(t.integralPart)
} else {
timestamp.SetFloat64(t.decimal)
}
// result of from_unixtime() is dependent on specific time zone.
unixTime := time.Unix(t.integralPart, t.fractionalPart).Round(time.Microsecond).String()[:t.ansLen]
if len(t.format) == 0 {
v, err := builtinFromUnixTime([]types.Datum{timestamp}, s.ctx)
c.Assert(err, IsNil)
ans := v.GetMysqlTime()
c.Assert(ans.String(), Equals, unixTime)
} else {
format := types.NewStringDatum(t.format)
v, err := builtinFromUnixTime([]types.Datum{timestamp, format}, s.ctx)
c.Assert(err, IsNil)
result, err := builtinDateFormat([]types.Datum{types.NewStringDatum(unixTime), format}, s.ctx)
c.Assert(err, IsNil)
c.Assert(v.GetString(), Equals, result.GetString())
}
}
v, err := builtinFromUnixTime([]types.Datum{types.NewIntDatum(-12345)}, s.ctx)
c.Assert(err, IsNil)
c.Assert(v.Kind(), Equals, types.KindNull)
_, err = builtinFromUnixTime([]types.Datum{types.NewIntDatum(math.MaxInt32 + 1)}, s.ctx)
c.Assert(err, IsNil)
c.Assert(v.Kind(), Equals, types.KindNull)
}
示例5: HashCode
// HashCode implements Expression interface.
func (col *Column) HashCode() []byte {
if len(col.hashcode) != 0 {
return col.hashcode
}
col.hashcode, _ = codec.EncodeValue(col.hashcode, types.NewStringDatum(col.FromID), types.NewIntDatum(int64(col.Position)))
return col.hashcode
}
示例6: TestTimeDiff
func (s *testEvaluatorSuite) TestTimeDiff(c *C) {
// Test cases from https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_timediff
tests := []struct {
t1 string
t2 string
expectStr string
}{
{"2000:01:01 00:00:00", "2000:01:01 00:00:00.000001", "-00:00:00.000001"},
{"2008-12-31 23:59:59.000001", "2008-12-30 01:01:01.000002", "46:58:57.999999"},
}
for _, test := range tests {
t1 := types.NewStringDatum(test.t1)
t2 := types.NewStringDatum(test.t2)
result, err := builtinTimeDiff([]types.Datum{t1, t2}, s.ctx)
c.Assert(err, IsNil)
c.Assert(result.GetMysqlDuration().String(), Equals, test.expectStr)
}
}
示例7: TestSession
func (*testSessionSuite) TestSession(c *C) {
ctx := mock.NewContext()
variable.BindSessionVars(ctx)
v := variable.GetSessionVars(ctx)
c.Assert(v, NotNil)
// For AffectedRows
v.AddAffectedRows(1)
c.Assert(v.AffectedRows, Equals, uint64(1))
v.AddAffectedRows(1)
c.Assert(v.AffectedRows, Equals, uint64(2))
// For FoundRows
v.AddFoundRows(1)
c.Assert(v.FoundRows, Equals, uint64(1))
v.AddFoundRows(1)
c.Assert(v.FoundRows, Equals, uint64(2))
// For last insert id
v.SetLastInsertID(uint64(1))
c.Assert(v.LastInsertID, Equals, uint64(1))
v.SetSystemVar("autocommit", types.NewStringDatum("1"))
val := v.GetSystemVar("autocommit")
c.Assert(val.GetString(), Equals, "1")
c.Assert(v.SetSystemVar("autocommit", types.Datum{}), NotNil)
v.SetSystemVar("sql_mode", types.NewStringDatum("strict_trans_tables"))
val = v.GetSystemVar("sql_mode")
c.Assert(val.GetString(), Equals, "STRICT_TRANS_TABLES")
c.Assert(v.StrictSQLMode, IsTrue)
v.SetSystemVar("sql_mode", types.NewStringDatum(""))
c.Assert(v.StrictSQLMode, IsFalse)
v.SetSystemVar("character_set_connection", types.NewStringDatum("utf8"))
v.SetSystemVar("collation_connection", types.NewStringDatum("utf8_general_ci"))
charset, collation := variable.GetCharsetInfo(ctx)
c.Assert(charset, Equals, "utf8")
c.Assert(collation, Equals, "utf8_general_ci")
c.Assert(v.SetSystemVar("character_set_results", types.Datum{}), IsNil)
}
示例8: getVarValue
func (e *SetExecutor) getVarValue(v *expression.VarAssignment, sysVar *variable.SysVar) (value types.Datum, err error) {
if v.IsDefault {
// To set a SESSION variable to the GLOBAL value or a GLOBAL value
// to the compiled-in MySQL default value, use the DEFAULT keyword.
// See http://dev.mysql.com/doc/refman/5.7/en/set-statement.html
if sysVar != nil {
value = types.NewStringDatum(sysVar.Value)
} else {
s, err1 := e.ctx.GetSessionVars().GlobalVarsAccessor.GetGlobalSysVar(strings.ToLower(v.Name))
if err1 != nil {
return value, errors.Trace(err1)
}
value = types.NewStringDatum(s)
}
return
}
value, err = v.Expr.Eval(nil, e.ctx)
return value, errors.Trace(err)
}
示例9: getVarValue
func (e *SimpleExec) getVarValue(v *ast.VariableAssignment, sysVar *variable.SysVar, globalVars variable.GlobalVarAccessor) (value types.Datum, err error) {
switch v.Value.(type) {
case *ast.DefaultExpr:
// To set a SESSION variable to the GLOBAL value or a GLOBAL value
// to the compiled-in MySQL default value, use the DEFAULT keyword.
// See http://dev.mysql.com/doc/refman/5.7/en/set-statement.html
if sysVar != nil {
value = types.NewStringDatum(sysVar.Value)
} else {
s, err1 := globalVars.GetGlobalSysVar(e.ctx, strings.ToLower(v.Name))
if err1 != nil {
return value, errors.Trace(err1)
}
value = types.NewStringDatum(s)
}
default:
value, err = evaluator.Eval(e.ctx, v.Value)
}
return value, errors.Trace(err)
}
示例10: HashCode
// HashCode implements Expression interface.
func (sf *ScalarFunction) HashCode() []byte {
var bytes []byte
v := make([]types.Datum, 0, len(sf.Args)+1)
bytes, _ = codec.EncodeValue(bytes, types.NewStringDatum(sf.FuncName.L))
v = append(v, types.NewBytesDatum(bytes))
for _, arg := range sf.Args {
v = append(v, types.NewBytesDatum(arg.HashCode()))
}
bytes = bytes[:0]
bytes, _ = codec.EncodeValue(bytes, v...)
return bytes
}
示例11: setCharset
func (e *SimpleExec) setCharset(cs, co string) error {
var err error
if len(co) == 0 {
co, err = charset.GetDefaultCollation(cs)
if err != nil {
return errors.Trace(err)
}
}
sessionVars := variable.GetSessionVars(e.ctx)
for _, v := range variable.SetNamesVariables {
err = sessionVars.SetSystemVar(v, types.NewStringDatum(cs))
if err != nil {
return errors.Trace(err)
}
}
err = sessionVars.SetSystemVar(variable.CollationConnection, types.NewStringDatum(co))
if err != nil {
return errors.Trace(err)
}
return nil
}
示例12: executeUse
func (e *SimpleExec) executeUse(s *ast.UseStmt) error {
dbname := model.NewCIStr(s.DBName)
dbinfo, exists := sessionctx.GetDomain(e.ctx).InfoSchema().SchemaByName(dbname)
if !exists {
return infoschema.ErrDatabaseNotExists.Gen("database %s not exists", dbname)
}
db.BindCurrentSchema(e.ctx, dbname.O)
// character_set_database is the character set used by the default database.
// The server sets this variable whenever the default database changes.
// See http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_character_set_database
sessionVars := variable.GetSessionVars(e.ctx)
err := sessionVars.SetSystemVar(variable.CharsetDatabase, types.NewStringDatum(dbinfo.Charset))
if err != nil {
return errors.Trace(err)
}
err = sessionVars.SetSystemVar(variable.CollationDatabase, types.NewStringDatum(dbinfo.Collate))
if err != nil {
return errors.Trace(err)
}
return nil
}
示例13: executeSetCharset
func (e *SimpleExec) executeSetCharset(s *ast.SetCharsetStmt) error {
collation := s.Collate
var err error
if len(collation) == 0 {
collation, err = charset.GetDefaultCollation(s.Charset)
if err != nil {
return errors.Trace(err)
}
}
sessionVars := variable.GetSessionVars(e.ctx)
for _, v := range variable.SetNamesVariables {
err = sessionVars.SetSystemVar(v, types.NewStringDatum(s.Charset))
if err != nil {
return errors.Trace(err)
}
}
err = sessionVars.SetSystemVar(variable.CollationConnection, types.NewStringDatum(collation))
if err != nil {
return errors.Trace(err)
}
return nil
}
示例14: TestVarsutil
func (s *testVarsutilSuite) TestVarsutil(c *C) {
defer testleak.AfterTest(c)()
v := variable.NewSessionVars()
SetSystemVar(v, "autocommit", types.NewStringDatum("1"))
val := GetSystemVar(v, "autocommit")
c.Assert(val.GetString(), Equals, "1")
c.Assert(SetSystemVar(v, "autocommit", types.Datum{}), NotNil)
SetSystemVar(v, "sql_mode", types.NewStringDatum("strict_trans_tables"))
val = GetSystemVar(v, "sql_mode")
c.Assert(val.GetString(), Equals, "STRICT_TRANS_TABLES")
c.Assert(v.StrictSQLMode, IsTrue)
SetSystemVar(v, "sql_mode", types.NewStringDatum(""))
c.Assert(v.StrictSQLMode, IsFalse)
SetSystemVar(v, "character_set_connection", types.NewStringDatum("utf8"))
SetSystemVar(v, "collation_connection", types.NewStringDatum("utf8_general_ci"))
charset, collation := v.GetCharsetInfo()
c.Assert(charset, Equals, "utf8")
c.Assert(collation, Equals, "utf8_general_ci")
c.Assert(SetSystemVar(v, "character_set_results", types.Datum{}), IsNil)
// Test case for get TiDBSkipConstraintCheck session variable
d := GetSystemVar(v, variable.TiDBSkipConstraintCheck)
c.Assert(d.GetString(), Equals, "0")
// Test case for tidb_skip_constraint_check
c.Assert(v.SkipConstraintCheck, IsFalse)
SetSystemVar(v, variable.TiDBSkipConstraintCheck, types.NewStringDatum("0"))
c.Assert(v.SkipConstraintCheck, IsFalse)
SetSystemVar(v, variable.TiDBSkipConstraintCheck, types.NewStringDatum("1"))
c.Assert(v.SkipConstraintCheck, IsTrue)
SetSystemVar(v, variable.TiDBSkipConstraintCheck, types.NewStringDatum("0"))
c.Assert(v.SkipConstraintCheck, IsFalse)
// Test case for change TiDBSkipConstraintCheck session variable.
SetSystemVar(v, variable.TiDBSkipConstraintCheck, types.NewStringDatum("1"))
d = GetSystemVar(v, variable.TiDBSkipConstraintCheck)
c.Assert(d.GetString(), Equals, "1")
}
示例15: abbrDayOfMonth
func abbrDayOfMonth(arg types.Datum, ctx context.Context) (types.Datum, error) {
day, err := builtinDayOfMonth([]types.Datum{arg}, ctx)
if err != nil || arg.IsNull() {
return types.Datum{}, errors.Trace(err)
}
var str string
switch day.GetInt64() {
case 1, 21, 31:
str = "st"
case 2, 22:
str = "nd"
case 3, 23:
str = "rd"
default:
str = "th"
}
d := types.NewStringDatum(fmt.Sprintf("%d%s", day.GetInt64(), str))
return d, nil
}