From 08f6d06e47b2ee6285577d726c59e5e2c3ff99ac Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 13 Jul 2022 17:21:19 +0800 Subject: [PATCH] Fix select with quoted column name --- statement.go | 2 +- statement_test.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/statement.go b/statement.go index 850af6cb..79e29915 100644 --- a/statement.go +++ b/statement.go @@ -650,7 +650,7 @@ func (stmt *Statement) Changed(fields ...string) bool { return false } -var nameMatcher = regexp.MustCompile(`^[\W]?(?:[a-z_0-9]+?)[\W]?\.[\W]?([a-z_0-9]+?)[\W]?$`) +var nameMatcher = regexp.MustCompile(`^(?:[\W]?(?:[a-z_0-9]+?)[\W]?\.)?[\W]?([a-z_0-9]+?)[\W]?$`) // SelectAndOmitColumns get select and omit columns, select -> true, omit -> false func (stmt *Statement) SelectAndOmitColumns(requireCreate, requireUpdate bool) (map[string]bool, bool) { diff --git a/statement_test.go b/statement_test.go index a89cc7d2..4432cda4 100644 --- a/statement_test.go +++ b/statement_test.go @@ -45,6 +45,8 @@ func TestNameMatcher(t *testing.T) { "`table_1`.`name23`": "name23", "'table23'.'name_1'": "name_1", "'table23'.name1": "name1", + "'name1'": "name1", + "`name_1`": "name_1", } { if matches := nameMatcher.FindStringSubmatch(k); len(matches) < 2 || matches[1] != v { t.Errorf("failed to match value: %v, got %v, expect: %v", k, matches, v)