Skip to content

Commit 436cca7

Browse files
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
1 parent a2cac75 commit 436cca7

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

statement.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,21 @@ func (stmt *Statement) Changed(fields ...string) bool {
665665
return false
666666
}
667667

668-
var nameMatcher = regexp.MustCompile(`^(?:\W?(\w+?)\W?\.)?\W?(\w+?)\W?$`)
668+
var matchName = func() func(tableColumn string) (table, column string) {
669+
nameMatcher := regexp.MustCompile(`^(?:\W?(\w+?)\W?\.)?(?:(\*)|\W?(\w+?)\W?)$`)
670+
return func(tableColumn string) (table, column string) {
671+
if matches := nameMatcher.FindStringSubmatch(tableColumn); len(matches) == 4 {
672+
table = matches[1]
673+
star := matches[2]
674+
columnName := matches[3]
675+
if star != "" {
676+
return table, star
677+
}
678+
return table, columnName
679+
}
680+
return "", ""
681+
}
682+
}()
669683

670684
// SelectAndOmitColumns get select and omit columns, select -> true, omit -> false
671685
func (stmt *Statement) SelectAndOmitColumns(requireCreate, requireUpdate bool) (map[string]bool, bool) {
@@ -686,13 +700,13 @@ func (stmt *Statement) SelectAndOmitColumns(requireCreate, requireUpdate bool) (
686700
}
687701
} else if field := stmt.Schema.LookUpField(column); field != nil && field.DBName != "" {
688702
results[field.DBName] = result
689-
} else if matches := nameMatcher.FindStringSubmatch(column); len(matches) == 3 && (matches[1] == stmt.Table || matches[1] == "") {
690-
if matches[2] == "*" {
703+
} else if table, col := matchName(column); col != "" && (table == stmt.Table || table == "") {
704+
if col == "*" {
691705
for _, dbName := range stmt.Schema.DBNames {
692706
results[dbName] = result
693707
}
694708
} else {
695-
results[matches[2]] = result
709+
results[col] = result
696710
}
697711
} else {
698712
results[column] = result

statement_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,15 @@ func TestNameMatcher(t *testing.T) {
5656
"`name_1`": {"", "name_1"},
5757
"`Name_1`": {"", "Name_1"},
5858
"`Table`.`nAme`": {"Table", "nAme"},
59+
"my_table.*": {"my_table", "*"},
60+
"`my_table`.*": {"my_table", "*"},
61+
"User__Company.*": {"User__Company", "*"},
62+
"`User__Company`.*": {"User__Company", "*"},
63+
`"User__Company".*`: {"User__Company", "*"},
64+
`"table"."*"`: {"", ""},
5965
} {
60-
if matches := nameMatcher.FindStringSubmatch(k); len(matches) < 3 || matches[1] != v[0] || matches[2] != v[1] {
61-
t.Errorf("failed to match value: %v, got %v, expect: %v", k, matches, v)
66+
if table, column := matchName(k); table != v[0] || column != v[1] {
67+
t.Errorf("failed to match value: %v, got %v, expect: %v", k, []string{table, column}, v)
6268
}
6369
}
6470
}

0 commit comments

Comments
 (0)