本文整理汇总了Golang中database/sql.NullInt64.Scan方法的典型用法代码示例。如果您正苦于以下问题:Golang NullInt64.Scan方法的具体用法?Golang NullInt64.Scan怎么用?Golang NullInt64.Scan使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类database/sql.NullInt64
的用法示例。
在下文中一共展示了NullInt64.Scan方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Scan
// Scan a value into the Int64, error on nil or unparsable
func (i *Int64) Scan(value interface{}) error {
tmp := sql.NullInt64{}
tmp.Scan(value)
if tmp.Valid == false {
// TODO: maybe nil should be simply allowed to be empty int64?
return errors.New("Value should be a int64 and not nil")
}
i.Int64 = tmp.Int64
i.DoInit(func() {
i.shadow = tmp.Int64
})
return nil
}
示例2: TestNullTypeInt64
func TestNullTypeInt64(t *testing.T) {
var userID sql.NullInt64
userID.Scan(nil)
b := Eq{"user_id": userID}
sql, args, err := b.ToSql()
assert.NoError(t, err)
assert.Empty(t, args)
assert.Equal(t, "user_id IS NULL", sql)
userID.Scan(int64(10))
b = Eq{"user_id": userID}
sql, args, err = b.ToSql()
assert.NoError(t, err)
assert.Equal(t, []interface{}{int64(10)}, args)
assert.Equal(t, "user_id = ?", sql)
}
示例3: Scan
func (s *scanner) Scan(src interface{}) error {
var err error
switch s.value.Type().Kind() {
case reflect.Struct:
nt := mysql.NullTime{}
err := nt.Scan(src)
if err != nil {
return err
}
s.value.Set(reflect.ValueOf(nt.Time))
case reflect.Bool:
nb := sql.NullBool{}
err := nb.Scan(src)
if err != nil {
return err
}
s.value.SetBool(nb.Bool)
case reflect.String:
ns := sql.NullString{}
err = ns.Scan(src)
if err != nil {
return err
}
s.value.SetString(ns.String)
case reflect.Int64:
ni := sql.NullInt64{}
err = ni.Scan(src)
if err != nil {
return err
}
s.value.SetInt(ni.Int64)
case reflect.Float64:
ni := sql.NullFloat64{}
err = ni.Scan(src)
if err != nil {
return err
}
s.value.SetFloat(ni.Float64)
}
return nil
}
示例4: fetchResult
func fetchResult(itemT reflect.Type, rows *sql.Rows, columns []string) (reflect.Value, error) {
var item reflect.Value
var err error
switch itemT.Kind() {
case reflect.Map:
item = reflect.MakeMap(itemT)
case reflect.Struct:
item = reflect.New(itemT)
default:
return item, db.ErrExpectingMapOrStruct
}
expecting := len(columns)
// Allocating results.
values := make([]*sql.RawBytes, expecting)
scanArgs := make([]interface{}, expecting)
for i := range columns {
scanArgs[i] = &values[i]
}
if err = rows.Scan(scanArgs...); err != nil {
return item, err
}
// Range over row values.
for i, value := range values {
if value != nil {
// Real column name
column := columns[i]
// Value as string.
svalue := string(*value)
var cv reflect.Value
v, _ := to.Convert(svalue, reflect.String)
cv = reflect.ValueOf(v)
switch itemT.Kind() {
// Destination is a map.
case reflect.Map:
if cv.Type() != itemT.Elem() {
if itemT.Elem().Kind() == reflect.Interface {
cv, _ = util.StringToType(svalue, cv.Type())
} else {
cv, _ = util.StringToType(svalue, itemT.Elem())
}
}
if cv.IsValid() {
item.SetMapIndex(reflect.ValueOf(column), cv)
}
// Destionation is a struct.
case reflect.Struct:
index := util.GetStructFieldIndex(itemT, column)
if index == nil {
continue
} else {
// Destination field.
destf := item.Elem().FieldByIndex(index)
if destf.IsValid() {
if cv.Type() != destf.Type() {
if destf.Type().Kind() != reflect.Interface {
switch destf.Type() {
case nullFloat64Type:
nullFloat64 := sql.NullFloat64{}
if svalue != `` {
nullFloat64.Scan(svalue)
}
cv = reflect.ValueOf(nullFloat64)
case nullInt64Type:
nullInt64 := sql.NullInt64{}
if svalue != `` {
nullInt64.Scan(svalue)
}
cv = reflect.ValueOf(nullInt64)
case nullBoolType:
nullBool := sql.NullBool{}
if svalue != `` {
nullBool.Scan(svalue)
}
cv = reflect.ValueOf(nullBool)
case nullStringType:
nullString := sql.NullString{}
nullString.Scan(svalue)
cv = reflect.ValueOf(nullString)
default:
var decodingNull bool
if svalue == "" {
//.........这里部分代码省略.........
示例5: Scan
// Scan implements the Scanner interface.
func (ns *Int) Scan(value interface{}) error {
n := sql.NullInt64{Int64: int64(ns.Int)}
err := n.Scan(value)
ns.Int, ns.Valid = int(n.Int64), n.Valid
return err
}
示例6: Scan
// Scan implements the Scanner interface.
func (ns *UInt32) Scan(value interface{}) error {
n := sql.NullInt64{Int64: int64(ns.UInt32)}
err := n.Scan(value)
ns.UInt32, ns.Valid = uint32(n.Int64), n.Valid
return err
}