merge master

This commit is contained in:
Lunny Xiao 2014-01-09 13:24:51 +08:00
commit b7c8a79a1a
3 changed files with 55 additions and 37 deletions

View File

@ -590,7 +590,7 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
sqlType := Type2SQLType(fieldType)
col = &Column{engine.columnMapper.Obj2Table(t.Field(i).Name), t.Field(i).Name, sqlType,
sqlType.DefaultLength, sqlType.DefaultLength2, true, "", make(map[string]bool), false, false,
TWOSIDES, false, false, false, false}
TWOSIDES, false, false, false, false, nil}
}
if col.IsAutoIncrement {
col.Nullable = false

View File

@ -1092,23 +1092,39 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
fieldsCount := len(fields)
for rawRows.Next() {
var newValue reflect.Value
var newElemFunc func() reflect.Value
if sliceElementType.Kind() == reflect.Ptr {
newValue = reflect.New(sliceElementType.Elem())
} else {
newValue = reflect.New(sliceElementType)
newElemFunc = func() reflect.Value {
return reflect.New(sliceElementType.Elem())
}
} else {
newElemFunc = func() reflect.Value {
return reflect.New(sliceElementType)
}
}
var sliceValueSetFunc func(*reflect.Value)
if sliceValue.Kind() == reflect.Slice {
if sliceElementType.Kind() == reflect.Ptr {
sliceValueSetFunc = func(newValue *reflect.Value) {
sliceValue.Set(reflect.Append(sliceValue, reflect.ValueOf(newValue.Interface())))
}
} else {
sliceValueSetFunc = func(newValue *reflect.Value) {
sliceValue.Set(reflect.Append(sliceValue, reflect.Indirect(reflect.ValueOf(newValue.Interface()))))
}
}
}
for rawRows.Next() {
var newValue reflect.Value = newElemFunc()
if sliceValueSetFunc != nil {
err := session.row2Bean(rawRows, fields, fieldsCount, newValue.Interface())
if err != nil {
return err
}
if sliceValue.Kind() == reflect.Slice {
if sliceElementType.Kind() == reflect.Ptr {
sliceValue.Set(reflect.Append(sliceValue, reflect.ValueOf(newValue.Interface())))
} else {
sliceValue.Set(reflect.Append(sliceValue, reflect.Indirect(reflect.ValueOf(newValue.Interface()))))
}
sliceValueSetFunc(&newValue)
}
}
} else {
@ -1346,22 +1362,23 @@ func row2map(rows *sql.Rows, fields []string) (resultsMap map[string][]byte, err
}
func (session *Session) getField(dataStruct *reflect.Value, key string, table *Table) *reflect.Value {
key = strings.ToLower(key)
if _, ok := table.Columns[key]; !ok {
if col, ok := table.Columns[strings.ToLower(key)]; !ok {
session.Engine.LogWarn(fmt.Sprintf("table %v's has not column %v. %v", table.Name, key, table.ColumnsSeq))
return nil
}
col := table.Columns[key]
} else {
fieldName := col.FieldName
fieldPath := strings.Split(fieldName, ".")
if col.fieldPath == nil {
col.fieldPath = strings.Split(fieldName, ".")
}
var fieldValue reflect.Value
if len(fieldPath) > 2 {
fieldPathLen := len(col.fieldPath)
if fieldPathLen > 2 {
session.Engine.LogError("Unsupported mutliderive", fieldName)
return nil
} else if len(fieldPath) == 2 {
parentField := dataStruct.FieldByName(fieldPath[0])
} else if fieldPathLen == 2 {
parentField := dataStruct.FieldByName(col.fieldPath[0])
if parentField.IsValid() {
fieldValue = parentField.FieldByName(fieldPath[1])
fieldValue = parentField.FieldByName(col.fieldPath[1])
}
} else {
fieldValue = dataStruct.FieldByName(fieldName)
@ -1373,6 +1390,7 @@ func (session *Session) getField(dataStruct *reflect.Value, key string, table *T
}
return &fieldValue
}
}
func (session *Session) row2Bean(rows *sql.Rows, fields []string, fieldsCount int, bean interface{}) error {
@ -1394,7 +1412,6 @@ func (session *Session) row2Bean(rows *sql.Rows, fields []string, fieldsCount in
for ii, key := range fields {
if fieldValue := session.getField(&dataStruct, key, table); fieldValue != nil {
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
//if row is null then ignore

View File

@ -275,6 +275,7 @@ type Column struct {
IsUpdated bool
IsCascade bool
IsVersion bool
fieldPath []string
}
// generate column description string according dialect