bugs fixed

This commit is contained in:
Lunny Xiao 2013-08-29 10:18:33 +08:00
parent 675f6f5f83
commit b742ca6f3b
3 changed files with 55 additions and 15 deletions

View File

@ -152,7 +152,7 @@ func insertTwoTable(engine *Engine, t *testing.T) {
func update(engine *Engine, t *testing.T) { func update(engine *Engine, t *testing.T) {
// update by id // update by id
user := Userinfo{Username: "xxx"} user := Userinfo{Username: "xxx", Height: 1.2}
_, err := engine.Id(1).Update(&user) _, err := engine.Id(1).Update(&user)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@ -640,13 +640,28 @@ type MyUInt uint
type MyFloat float64 type MyFloat float64
type MyString string type MyString string
func (s MyString) FromDB(data []byte) error {
s = MyString(string(data))
return nil
}
func (s MyString) ToDB() ([]byte, error) {
return []byte(string(s)), nil
}
type MyStruct struct { type MyStruct struct {
Type MyInt Type MyInt
U MyUInt U MyUInt
F MyFloat F MyFloat
//S MyString //S MyString
//IA []MyInt
//UA []MyUInt
//FA []MyFloat
//SA []MyString
//NameArray []string
Name string Name string
UI uint //UIA []uint
UI uint
} }
func testCustomType(engine *Engine, t *testing.T) { func testCustomType(engine *Engine, t *testing.T) {

View File

@ -206,7 +206,19 @@ func (session *Session) scanMapIntoStruct(obj interface{}, objMap map[string][]b
switch structField.Type().Kind() { switch structField.Type().Kind() {
case reflect.Slice: case reflect.Slice:
v = data v = data
structField.Set(reflect.ValueOf(v)) vv := reflect.ValueOf(v)
if structField.Type().String() == "[]byte" {
fmt.Println("...[]byte...")
}
if vv.Type().Kind() == reflect.Slice {
for i := 0; i < vv.Len(); i++ {
//vv.Index(i)
structField = reflect.AppendSlice(structField, vv)
//reflect.Append(structField, vv.Index(i))
}
} else {
return errors.New(fmt.Sprintf("unsupported from other %v to %v", vv.Type().Kind(), structField.Type().Kind()))
}
case reflect.Array: case reflect.Array:
if structField.Type().Elem() == reflect.TypeOf(b) { if structField.Type().Elem() == reflect.TypeOf(b) {
v = data v = data
@ -825,6 +837,17 @@ func (session *Session) InsertMulti(rowsSlicePtr interface{}) (int64, error) {
} }
func (session *Session) value2Interface(fieldValue reflect.Value) (interface{}, error) { func (session *Session) value2Interface(fieldValue reflect.Value) (interface{}, error) {
if fieldValue.CanAddr() {
if fieldConvert, ok := fieldValue.Addr().Interface().(Conversion); ok {
data, err := fieldConvert.ToDB()
if err != nil {
return 0, err
} else {
return string(data), nil
}
}
}
if fieldValue.Type().Kind() == reflect.Bool { if fieldValue.Type().Kind() == reflect.Bool {
if fieldValue.Bool() { if fieldValue.Bool() {
return 1, nil return 1, nil
@ -834,17 +857,6 @@ func (session *Session) value2Interface(fieldValue reflect.Value) (interface{},
} else if fieldValue.Type().String() == "time.Time" { } else if fieldValue.Type().String() == "time.Time" {
return fieldValue.Interface(), nil return fieldValue.Interface(), nil
} else if fieldValue.Type().Kind() == reflect.Struct { } else if fieldValue.Type().Kind() == reflect.Struct {
if fieldValue.CanAddr() {
if fieldConvert, ok := fieldValue.Addr().Interface().(Conversion); ok {
data, err := fieldConvert.ToDB()
if err != nil {
return 0, err
} else {
return string(data), nil
}
}
}
if fieldTable, ok := session.Engine.Tables[fieldValue.Type()]; ok { if fieldTable, ok := session.Engine.Tables[fieldValue.Type()]; ok {
if fieldTable.PrimaryKey != "" { if fieldTable.PrimaryKey != "" {
pkField := reflect.Indirect(fieldValue).FieldByName(fieldTable.PKColumn().FieldName) pkField := reflect.Indirect(fieldValue).FieldByName(fieldTable.PKColumn().FieldName)
@ -855,6 +867,11 @@ func (session *Session) value2Interface(fieldValue reflect.Value) (interface{},
} else { } else {
return 0, errors.New(fmt.Sprintf("Unsupported type %v", fieldValue.Type())) return 0, errors.New(fmt.Sprintf("Unsupported type %v", fieldValue.Type()))
} }
} else if fieldValue.Type().Kind() == reflect.Array ||
fieldValue.Type().Kind() == reflect.Slice {
data := fmt.Sprintf("%v", fieldValue.Interface())
fmt.Println(data, "--------")
return data, nil
} else { } else {
return fieldValue.Interface(), nil return fieldValue.Interface(), nil
} }

View File

@ -93,10 +93,18 @@ func BuildConditions(engine *Engine, table *Table, bean interface{}) ([]string,
if fieldValue.String() == "" { if fieldValue.String() == "" {
continue continue
} }
case reflect.Int, reflect.Int32, reflect.Int64: case reflect.Int8, reflect.Int16, reflect.Int, reflect.Int32, reflect.Int64:
if fieldValue.Int() == 0 { if fieldValue.Int() == 0 {
continue continue
} }
case reflect.Float32, reflect.Float64:
if fieldValue.Float() == 0.0 {
continue
}
case reflect.Uint8, reflect.Uint16, reflect.Uint, reflect.Uint32, reflect.Uint64:
if fieldValue.Uint() == 0 {
continue
}
case reflect.Struct: case reflect.Struct:
if fieldType == reflect.TypeOf(time.Now()) { if fieldType == reflect.TypeOf(time.Now()) {
t := fieldValue.Interface().(time.Time) t := fieldValue.Interface().(time.Time)