Skip to content

Commit

Permalink
fix: join and select mytable.* not working (#6761)
Browse files Browse the repository at this point in the history
* fix: select mytable.* not working

* fix: select mytable.*: will not match `mytable."*"`.
feat: increase readability of code matching table name column name
  • Loading branch information
StephanoGeorge authored Dec 23, 2023
1 parent a2cac75 commit 436cca7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
22 changes: 18 additions & 4 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,21 @@ func (stmt *Statement) Changed(fields ...string) bool {
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
func (stmt *Statement) SelectAndOmitColumns(requireCreate, requireUpdate bool) (map[string]bool, bool) {
Expand All @@ -686,13 +700,13 @@ func (stmt *Statement) SelectAndOmitColumns(requireCreate, requireUpdate bool) (
}
} else if field := stmt.Schema.LookUpField(column); field != nil && field.DBName != "" {
results[field.DBName] = result
} else if matches := nameMatcher.FindStringSubmatch(column); len(matches) == 3 && (matches[1] == stmt.Table || matches[1] == "") {
if matches[2] == "*" {
} else if table, col := matchName(column); col != "" && (table == stmt.Table || table == "") {
if col == "*" {
for _, dbName := range stmt.Schema.DBNames {
results[dbName] = result
}
} else {
results[matches[2]] = result
results[col] = result
}
} else {
results[column] = result
Expand Down
10 changes: 8 additions & 2 deletions statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ func TestNameMatcher(t *testing.T) {
"`name_1`": {"", "name_1"},
"`Name_1`": {"", "Name_1"},
"`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] {
t.Errorf("failed to match value: %v, got %v, expect: %v", k, matches, v)
if table, column := matchName(k); table != v[0] || column != v[1] {
t.Errorf("failed to match value: %v, got %v, expect: %v", k, []string{table, column}, v)
}
}
}

0 comments on commit 436cca7

Please sign in to comment.