Scan group key improve.

This commit is contained in:
sub0Zero 2019-03-17 10:21:44 +01:00 committed by zer0sub
parent 1cb997fc54
commit 83a8b2b70f
2 changed files with 32 additions and 23 deletions

View file

@ -102,29 +102,13 @@ func allProcessed(arr []bool) bool {
func getGroupKey(scanContext *scanContext, row []interface{}, structType reflect.Type) string { func getGroupKey(scanContext *scanContext, row []interface{}, structType reflect.Type) string {
structName := structType.Name() structName := structType.Name()
groupKey := "" groupKeys := []string{}
for i := 0; i < structType.NumField(); i++ { for i := 0; i < structType.NumField(); i++ {
fieldType := structType.Field(i) fieldType := structType.Field(i)
////fmt.Println(fieldType.Tag) ////fmt.Println(fieldType.Tag)
if !isDbBaseType(fieldType.Type) {
if fieldType.Tag == `sql:"unique"` {
fieldName := fieldType.Name
columnName := snaker.CamelToSnake(structName) + "." + snaker.CamelToSnake(fieldName)
//fmt.Println(fieldName)
index := getIndex(scanContext.columnNames, columnName)
if index < 0 {
continue
}
cellValue := cellValue(row, index)
groupKey = groupKey + reflectValueToString(cellValue)
} else if !isDbBaseType(fieldType.Type) {
var structType reflect.Type var structType reflect.Type
if fieldType.Type.Kind() == reflect.Struct { if fieldType.Type.Kind() == reflect.Struct {
structType = fieldType.Type structType = fieldType.Type
@ -140,12 +124,34 @@ func getGroupKey(scanContext *scanContext, row []interface{}, structType reflect
//groupKey = strings.Join([]string{structGroupKey, groupKey}, ":") //groupKey = strings.Join([]string{structGroupKey, groupKey}, ":")
groupKey = groupKey + structGroupKey if structGroupKey != "" {
groupKeys = append(groupKeys, structGroupKey)
}
} else if fieldType.Tag == `sql:"unique"` {
fieldName := fieldType.Name
columnName := snaker.CamelToSnake(structName) + "." + snaker.CamelToSnake(fieldName)
//fmt.Println(fieldName)
index := getIndex(scanContext.columnNames, columnName)
if index < 0 {
continue
}
cellValue := cellValue(row, index)
subKey := reflectValueToString(cellValue)
if subKey != "" {
groupKeys = append(groupKeys, subKey)
}
} }
} }
//fmt.Println(groupKey) if len(groupKeys) == 0 {
return groupKey return ""
}
return "|" + structType.Name() + "(" + strings.Join(groupKeys, ", ") + ")|"
} }
func cellValue(row []interface{}, index int) interface{} { func cellValue(row []interface{}, index int) interface{} {
@ -196,11 +202,13 @@ func mapRowToSlice(scanContext *scanContext, groupKey string, columnProcessed []
structGroupKey := getGroupKey(scanContext, row, structType) structGroupKey := getGroupKey(scanContext, row, structType)
if structGroupKey == "" { if structGroupKey == "" {
structGroupKey = strconv.Itoa(scanContext.rowNum) structGroupKey = "|ROW: " + strconv.Itoa(scanContext.rowNum) + "|"
} }
groupKey = groupKey + ":" + structGroupKey groupKey = groupKey + ":" + structGroupKey
//fmt.Println(groupKey)
objPtr, ok := scanContext.uniqueObjectsMap[groupKey] objPtr, ok := scanContext.uniqueObjectsMap[groupKey]
if ok { if ok {

View file

@ -154,7 +154,6 @@ func TestJoinQuerySlice(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, len(filmsPerLanguage), 1) assert.Equal(t, len(filmsPerLanguage), 1)
assert.Equal(t, len(*filmsPerLanguage[0].Films), limit) assert.Equal(t, len(*filmsPerLanguage[0].Films), limit)
} }
func TestJoinQuerySliceWithPtrs(t *testing.T) { func TestJoinQuerySliceWithPtrs(t *testing.T) {
@ -289,6 +288,8 @@ func TestSelectFullCrossJoin(t *testing.T) {
err = query.Execute(db, &customerAddresCrosJoined) err = query.Execute(db, &customerAddresCrosJoined)
assert.Equal(t, len(customerAddresCrosJoined), 1000)
assert.NilError(t, err) assert.NilError(t, err)
} }