Scan group key improve.
This commit is contained in:
parent
1cb997fc54
commit
83a8b2b70f
2 changed files with 32 additions and 23 deletions
|
|
@ -102,29 +102,13 @@ func allProcessed(arr []bool) bool {
|
|||
|
||||
func getGroupKey(scanContext *scanContext, row []interface{}, structType reflect.Type) string {
|
||||
structName := structType.Name()
|
||||
groupKey := ""
|
||||
groupKeys := []string{}
|
||||
|
||||
for i := 0; i < structType.NumField(); i++ {
|
||||
fieldType := structType.Field(i)
|
||||
|
||||
////fmt.Println(fieldType.Tag)
|
||||
|
||||
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) {
|
||||
if !isDbBaseType(fieldType.Type) {
|
||||
var structType reflect.Type
|
||||
if fieldType.Type.Kind() == reflect.Struct {
|
||||
structType = fieldType.Type
|
||||
|
|
@ -140,12 +124,34 @@ func getGroupKey(scanContext *scanContext, row []interface{}, structType reflect
|
|||
|
||||
//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)
|
||||
return groupKey
|
||||
if len(groupKeys) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
return "|" + structType.Name() + "(" + strings.Join(groupKeys, ", ") + ")|"
|
||||
}
|
||||
|
||||
func cellValue(row []interface{}, index int) interface{} {
|
||||
|
|
@ -196,11 +202,13 @@ func mapRowToSlice(scanContext *scanContext, groupKey string, columnProcessed []
|
|||
structGroupKey := getGroupKey(scanContext, row, structType)
|
||||
|
||||
if structGroupKey == "" {
|
||||
structGroupKey = strconv.Itoa(scanContext.rowNum)
|
||||
structGroupKey = "|ROW: " + strconv.Itoa(scanContext.rowNum) + "|"
|
||||
}
|
||||
|
||||
groupKey = groupKey + ":" + structGroupKey
|
||||
|
||||
//fmt.Println(groupKey)
|
||||
|
||||
objPtr, ok := scanContext.uniqueObjectsMap[groupKey]
|
||||
|
||||
if ok {
|
||||
|
|
|
|||
|
|
@ -154,7 +154,6 @@ func TestJoinQuerySlice(t *testing.T) {
|
|||
assert.NilError(t, err)
|
||||
assert.Equal(t, len(filmsPerLanguage), 1)
|
||||
assert.Equal(t, len(*filmsPerLanguage[0].Films), limit)
|
||||
|
||||
}
|
||||
|
||||
func TestJoinQuerySliceWithPtrs(t *testing.T) {
|
||||
|
|
@ -289,6 +288,8 @@ func TestSelectFullCrossJoin(t *testing.T) {
|
|||
|
||||
err = query.Execute(db, &customerAddresCrosJoined)
|
||||
|
||||
assert.Equal(t, len(customerAddresCrosJoined), 1000)
|
||||
|
||||
assert.NilError(t, err)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue