diff --git a/sqlbuilder/execution/execution.go b/sqlbuilder/execution/execution.go index 2f2b03f..86584b3 100644 --- a/sqlbuilder/execution/execution.go +++ b/sqlbuilder/execution/execution.go @@ -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 { diff --git a/tests/generator_test.go b/tests/generator_test.go index 99717c2..894d8f6 100644 --- a/tests/generator_test.go +++ b/tests/generator_test.go @@ -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) }