mirror of https://github.com/go-gorm/gorm.git
fix: join and select mytable.* not working (#6761)
* fix: select mytable.* not working * fix: select mytable.*: will not match `mytable."*"`. feat: increase readability of code matching table name column name
This commit is contained in:
parent
a2cac75218
commit
436cca753c
22
statement.go
22
statement.go
|
@ -665,7 +665,21 @@ func (stmt *Statement) Changed(fields ...string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
var nameMatcher = regexp.MustCompile(`^(?:\W?(\w+?)\W?\.)?\W?(\w+?)\W?$`)
|
var matchName = func() func(tableColumn string) (table, column string) {
|
||||||
|
nameMatcher := regexp.MustCompile(`^(?:\W?(\w+?)\W?\.)?(?:(\*)|\W?(\w+?)\W?)$`)
|
||||||
|
return func(tableColumn string) (table, column string) {
|
||||||
|
if matches := nameMatcher.FindStringSubmatch(tableColumn); len(matches) == 4 {
|
||||||
|
table = matches[1]
|
||||||
|
star := matches[2]
|
||||||
|
columnName := matches[3]
|
||||||
|
if star != "" {
|
||||||
|
return table, star
|
||||||
|
}
|
||||||
|
return table, columnName
|
||||||
|
}
|
||||||
|
return "", ""
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// SelectAndOmitColumns get select and omit columns, select -> true, omit -> false
|
// SelectAndOmitColumns get select and omit columns, select -> true, omit -> false
|
||||||
func (stmt *Statement) SelectAndOmitColumns(requireCreate, requireUpdate bool) (map[string]bool, bool) {
|
func (stmt *Statement) SelectAndOmitColumns(requireCreate, requireUpdate bool) (map[string]bool, bool) {
|
||||||
|
@ -686,13 +700,13 @@ func (stmt *Statement) SelectAndOmitColumns(requireCreate, requireUpdate bool) (
|
||||||
}
|
}
|
||||||
} else if field := stmt.Schema.LookUpField(column); field != nil && field.DBName != "" {
|
} else if field := stmt.Schema.LookUpField(column); field != nil && field.DBName != "" {
|
||||||
results[field.DBName] = result
|
results[field.DBName] = result
|
||||||
} else if matches := nameMatcher.FindStringSubmatch(column); len(matches) == 3 && (matches[1] == stmt.Table || matches[1] == "") {
|
} else if table, col := matchName(column); col != "" && (table == stmt.Table || table == "") {
|
||||||
if matches[2] == "*" {
|
if col == "*" {
|
||||||
for _, dbName := range stmt.Schema.DBNames {
|
for _, dbName := range stmt.Schema.DBNames {
|
||||||
results[dbName] = result
|
results[dbName] = result
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
results[matches[2]] = result
|
results[col] = result
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
results[column] = result
|
results[column] = result
|
||||||
|
|
|
@ -56,9 +56,15 @@ func TestNameMatcher(t *testing.T) {
|
||||||
"`name_1`": {"", "name_1"},
|
"`name_1`": {"", "name_1"},
|
||||||
"`Name_1`": {"", "Name_1"},
|
"`Name_1`": {"", "Name_1"},
|
||||||
"`Table`.`nAme`": {"Table", "nAme"},
|
"`Table`.`nAme`": {"Table", "nAme"},
|
||||||
|
"my_table.*": {"my_table", "*"},
|
||||||
|
"`my_table`.*": {"my_table", "*"},
|
||||||
|
"User__Company.*": {"User__Company", "*"},
|
||||||
|
"`User__Company`.*": {"User__Company", "*"},
|
||||||
|
`"User__Company".*`: {"User__Company", "*"},
|
||||||
|
`"table"."*"`: {"", ""},
|
||||||
} {
|
} {
|
||||||
if matches := nameMatcher.FindStringSubmatch(k); len(matches) < 3 || matches[1] != v[0] || matches[2] != v[1] {
|
if table, column := matchName(k); table != v[0] || column != v[1] {
|
||||||
t.Errorf("failed to match value: %v, got %v, expect: %v", k, matches, v)
|
t.Errorf("failed to match value: %v, got %v, expect: %v", k, []string{table, column}, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue