本文整理汇总了Golang中github.com/araddon/qlbridge/value.NewIntValue函数的典型用法代码示例。如果您正苦于以下问题:Golang NewIntValue函数的具体用法?Golang NewIntValue怎么用?Golang NewIntValue使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewIntValue函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Yy
// Get year in integer from date
func Yy(ctx expr.EvalContext, items ...value.Value) (value.IntValue, bool) {
yy := 0
if len(items) == 0 {
if !ctx.Ts().IsZero() {
yy = ctx.Ts().Year()
}
} else if len(items) == 1 {
dateStr, ok := value.ToString(items[0].Rv())
if !ok {
return value.NewIntValue(0), false
}
//u.Infof("v=%v %v ", v, item.Rv())
if t, err := dateparse.ParseAny(dateStr); err != nil {
return value.NewIntValue(0), false
} else {
yy = t.Year()
}
} else {
return value.NewIntValue(0), false
}
if yy >= 2000 {
yy = yy - 2000
} else if yy >= 1900 {
yy = yy - 1900
}
//u.Infof("%v yy = %v", item, yy)
return value.NewIntValue(int64(yy)), true
}
示例2: ToInt
// Convert to Integer: Best attempt at converting to integer
//
// toint("5") => 5
// toint("5.75") => 5
// toint("5,555") => 5555
// toint("$5") => 5
// toint("5,555.00") => 5555
//
func ToInt(ctx expr.EvalContext, item value.Value) (value.IntValue, bool) {
iv, ok := value.ToInt64(reflect.ValueOf(item.Value()))
if !ok {
return value.NewIntValue(0), false
}
return value.NewIntValue(iv), true
}
示例3: Yy
// Get year in integer from field, must be able to convert to date
//
// yy() => 15, true // assuming it is 2015
// yy("2014-03-01") => 14, true
//
func Yy(ctx expr.EvalContext, items ...value.Value) (value.IntValue, bool) {
yy := 0
if len(items) == 0 {
if !ctx.Ts().IsZero() {
yy = ctx.Ts().Year()
} else {
// Do we want to use Now()?
}
} else if len(items) == 1 {
//u.Debugf("has 1 items? %#v", items[0].Rv())
dateStr, ok := value.ToString(items[0].Rv())
if !ok {
return value.NewIntValue(0), false
}
//u.Debugf("v=%v %v", dateStr, items[0].Rv())
if t, err := dateparse.ParseAny(dateStr); err != nil {
return value.NewIntValue(0), false
} else {
yy = t.Year()
}
} else {
return value.NewIntValue(0), false
}
if yy >= 2000 {
yy = yy - 2000
} else if yy >= 1900 {
yy = yy - 1900
}
//u.Debugf("yy = %v", yy)
return value.NewIntValue(int64(yy)), true
}
示例4: CountFunc
// Count
func CountFunc(ctx EvalContext, val value.Value) (value.IntValue, bool) {
if val.Err() || val.Nil() {
return value.NewIntValue(0), false
}
//u.Infof("??? vals=[%v]", val.Value())
return value.NewIntValue(1), true
}
示例5: TestSqlDelete
func TestSqlDelete(t *testing.T) {
db := datasource.NewContextSimple()
user1 := map[string]value.Value{
"user_id": value.NewIntValue(5),
"item_count": value.NewStringValue("5"),
"bval": value.NewBoolValue(true),
"bvalf": value.NewBoolValue(false),
"reg_date": value.NewStringValue("2014/11/01"),
"name": value.NewStringValue("bob")}
db.Insert(user1)
user2 := map[string]value.Value{
"user_id": value.NewIntValue(6),
"item_count": value.NewStringValue("5"),
"reg_date": value.NewStringValue("2012/11/01"),
"name": value.NewStringValue("allison")}
db.Insert(user2)
assert.Tf(t, len(db.Rows) == 2, "has 2 users")
verifySqlDelete(t, `
DELETE FROM mytable
WHERE yy(reg_date) == 14
`, db)
assert.Tf(t, len(db.Rows) == 1, "must have 1 rows: %v %v", len(db.Rows), db.Rows)
assert.Tf(t, db.Rows[0]["name"].ToString() == "allison", "%v", db.Rows)
}
示例6: ToInt
// Convert to Integer: Best attempt at converting to integer
//
// toint("5") => 5, true
// toint("5.75") => 5, true
// toint("5,555") => 5555, true
// toint("$5") => 5, true
// toint("5,555.00") => 5555, true
//
func ToInt(ctx expr.EvalContext, item value.Value) (value.IntValue, bool) {
switch itemT := item.(type) {
case value.TimeValue:
iv := itemT.Val().UnixNano() / 1e6 // Milliseconds
return value.NewIntValue(iv), true
}
iv, ok := value.ToInt64(reflect.ValueOf(item.Value()))
if !ok {
return value.NewIntValue(0), false
}
return value.NewIntValue(iv), true
}
示例7: ToTimestamp
// totimestamp: convert to date, then to unix Seconds
//
func ToTimestamp(ctx expr.EvalContext, item value.Value) (value.IntValue, bool) {
dateStr, ok := value.ToString(item.Rv())
if !ok {
return value.NewIntValue(0), false
}
if t, err := dateparse.ParseAny(dateStr); err == nil {
//u.Infof("v=%v %v unix=%v", item, item.Rv(), t.Unix())
return value.NewIntValue(int64(t.Unix())), true
}
return value.NewIntValue(0), false
}
示例8: parseUpdateList
func (m *Sqlbridge) parseUpdateList() (map[string]*ValueColumn, error) {
cols := make(map[string]*ValueColumn)
lastColName := ""
for {
//u.Debug(m.Cur().String())
switch m.Cur().T {
case lex.TokenWhere, lex.TokenLimit, lex.TokenEOS, lex.TokenEOF:
return cols, nil
case lex.TokenValue:
cols[lastColName] = &ValueColumn{Value: value.NewStringValue(m.Cur().V)}
case lex.TokenInteger:
iv, _ := strconv.ParseInt(m.Cur().V, 10, 64)
cols[lastColName] = &ValueColumn{Value: value.NewIntValue(iv)}
case lex.TokenComma, lex.TokenEqual:
// don't need to do anything
case lex.TokenIdentity:
lastColName = m.Cur().V
case lex.TokenUdfExpr:
tree := NewTree(m.SqlTokenPager)
if err := m.parseNode(tree); err != nil {
u.Errorf("could not parse: %v", err)
return nil, err
}
cols[lastColName] = &ValueColumn{Expr: tree.Root}
default:
u.Warnf("don't know how to handle ? %v", m.Cur())
return nil, fmt.Errorf("expected column but got: %v", m.Cur().String())
}
m.Next()
}
panic("unreachable")
}
示例9: HourOfDay
// hour of day [0-23]
func HourOfDay(ctx expr.EvalContext, items ...value.Value) (value.IntValue, bool) {
if len(items) == 0 {
if !ctx.Ts().IsZero() {
return value.NewIntValue(int64(ctx.Ts().Hour())), true
}
} else if len(items) == 1 {
dateStr, ok := value.ToString(items[0].Rv())
if !ok {
return value.NewIntValue(0), false
}
//u.Infof("v=%v %v ", v, items[0].Rv())
if t, err := dateparse.ParseAny(dateStr); err == nil {
return value.NewIntValue(int64(t.Hour())), true
}
}
return value.NewIntValue(0), false
}
示例10: Yy
func Yy(ctx expr.EvalContext, item value.Value) (value.IntValue, bool) {
//u.Info("yy: %T", item)
val, ok := value.ToString(item.Rv())
if !ok || val == "" {
return value.NewIntValue(0), false
}
//u.Infof("v=%v %v ", val, item.Rv())
if t, err := dateparse.ParseAny(val); err == nil {
yy := t.Year()
if yy >= 2000 {
yy = yy - 2000
} else if yy >= 1900 {
yy = yy - 1900
}
//u.Infof("Yy = %v yy = %v", item, yy)
return value.NewIntValue(int64(yy)), true
}
return value.NewIntValue(0), false
}
示例11: numberNodeToValue
// creates a new Value with a nil group and given value.
// TODO: convert this to an interface method on nodes called Value()
func numberNodeToValue(t *expr.NumberNode) (v value.Value) {
//u.Debugf("nodeToValue() isFloat?%v", t.IsFloat)
if t.IsInt {
v = value.NewIntValue(t.Int64)
} else if t.IsFloat {
v = value.NewNumberValue(value.ToFloat64(reflect.ValueOf(t.Text)))
} else {
u.Errorf("Could not find type? %v", t.Type())
}
//u.Debugf("return nodeToValue() %v %T arg:%T", v, v, t)
return v
}
示例12: BenchmarkVmExecute
func BenchmarkVmExecute(b *testing.B) {
msg := datasource.NewContextSimpleData(
map[string]value.Value{
"int5": value.NewIntValue(5),
"item_count": value.NewStringValue("5"),
"reg_date": value.NewStringValue("2014/11/01"),
"user_id": value.NewStringValue("abc")},
)
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
for _, sqlText := range bmSql {
verifyBenchmarkSql(b, sqlText, msg)
}
}
}
示例13: numberNodeToValue
// creates a new Value with a nil group and given value.
// TODO: convert this to an interface method on nodes called Value()
func numberNodeToValue(t *expr.NumberNode) (value.Value, bool) {
//u.Debugf("nodeToValue() isFloat?%v", t.IsFloat)
var v value.Value
if t.IsInt {
v = value.NewIntValue(t.Int64)
} else if t.IsFloat {
fv, ok := value.ToFloat64(reflect.ValueOf(t.Text))
if !ok {
u.Warnf("Could not perform numeric conversion for %q", t.Text)
return value.NilValueVal, false
}
v = value.NewNumberValue(fv)
} else {
u.Warnf("Could not find numeric conversion for %v", t.Type())
return value.NilValueVal, false
}
//u.Debugf("return nodeToValue() %v %T arg:%T", v, v, t)
return v, true
}
示例14: BenchmarkVmExecuteNoParse
func BenchmarkVmExecuteNoParse(b *testing.B) {
readContext := datasource.NewContextSimpleData(
map[string]value.Value{
"int5": value.NewIntValue(5),
"item_count": value.NewStringValue("5"),
"reg_date": value.NewStringValue("2014/11/01"),
"user_id": value.NewStringValue("abc")},
)
sqlVm, err := NewSqlVm(bmSql[0])
if err != nil {
b.Fail()
}
writeContext := datasource.NewContextSimple()
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
err = sqlVm.Execute(writeContext, readContext)
if err != nil {
b.Fail()
}
}
}
示例15: parseValueList
func (m *Sqlbridge) parseValueList(stmt *SqlInsert) error {
if m.Cur().T != lex.TokenLeftParenthesis {
return fmt.Errorf("Expecting opening paren ( but got %v", m.Cur())
}
//m.Next()
stmt.Rows = make([][]value.Value, 0)
var row []value.Value
for {
//u.Debug(m.Cur().String())
switch m.Cur().T {
case lex.TokenLeftParenthesis:
// start of row
row = make([]value.Value, 0)
case lex.TokenRightParenthesis:
stmt.Rows = append(stmt.Rows, row)
case lex.TokenFrom, lex.TokenInto, lex.TokenLimit, lex.TokenEOS, lex.TokenEOF:
// This indicates we have come to the End of the values
//u.Debugf("Ending %v ", m.Cur())
return nil
case lex.TokenValue:
row = append(row, value.NewStringValue(m.Cur().V))
case lex.TokenInteger:
iv, _ := strconv.ParseInt(m.Cur().V, 10, 64)
row = append(row, value.NewIntValue(iv))
case lex.TokenComma:
//row = append(row, col)
//u.Debugf("comma, added cols: %v", len(stmt.Columns))
default:
u.Warnf("don't know how to handle ? %v", m.Cur())
return fmt.Errorf("expected column but got: %v", m.Cur().String())
}
m.Next()
}
//u.Debugf("cols: %d", len(stmt.Columns))
return nil
}