Do not ever quote asterisk symbol. Fixes #1780 (#1781)

Do not ever quote asterisk symbol. Fixes #1780

Co-authored-by: Lauris BH <lauris@nix.lv>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/1781
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
lafriks 2020-09-06 08:22:34 +00:00 committed by Lunny Xiao
parent 3e0179ff26
commit 01e1e98dd5
2 changed files with 10 additions and 7 deletions

View File

@ -82,10 +82,8 @@ func (q Quoter) JoinWrite(b *strings.Builder, a []string, sep string) error {
return err return err
} }
} }
if s != "*" {
q.QuoteTo(b, strings.TrimSpace(s)) q.QuoteTo(b, strings.TrimSpace(s))
} }
}
return nil return nil
} }
@ -143,7 +141,7 @@ func (q Quoter) quoteWordTo(buf *strings.Builder, word string) error {
} }
isReserved := q.IsReserved(realWord) isReserved := q.IsReserved(realWord)
if isReserved { if isReserved && realWord != "*" {
if err := buf.WriteByte(q.Prefix); err != nil { if err := buf.WriteByte(q.Prefix); err != nil {
return err return err
} }
@ -151,7 +149,7 @@ func (q Quoter) quoteWordTo(buf *strings.Builder, word string) error {
if _, err := buf.WriteString(realWord); err != nil { if _, err := buf.WriteString(realWord); err != nil {
return err return err
} }
if isReserved { if isReserved && realWord != "*" {
return buf.WriteByte(q.Suffix) return buf.WriteByte(q.Suffix)
} }

View File

@ -22,6 +22,7 @@ func TestAlwaysQuoteTo(t *testing.T) {
{"[mytable]", "`mytable`"}, {"[mytable]", "`mytable`"},
{"[mytable]", `[mytable]`}, {"[mytable]", `[mytable]`},
{`["mytable"]`, `"mytable"`}, {`["mytable"]`, `"mytable"`},
{`[mytable].*`, `[mytable].*`},
{"[myschema].[mytable]", "myschema.mytable"}, {"[myschema].[mytable]", "myschema.mytable"},
{"[myschema].[mytable]", "`myschema`.mytable"}, {"[myschema].[mytable]", "`myschema`.mytable"},
{"[myschema].[mytable]", "myschema.`mytable`"}, {"[myschema].[mytable]", "myschema.`mytable`"},
@ -65,6 +66,7 @@ func TestReversedQuoteTo(t *testing.T) {
{"[mytable]", "mytable"}, {"[mytable]", "mytable"},
{"[mytable]", "`mytable`"}, {"[mytable]", "`mytable`"},
{"[mytable]", `[mytable]`}, {"[mytable]", `[mytable]`},
{"[mytable].*", `[mytable].*`},
{`"mytable"`, `"mytable"`}, {`"mytable"`, `"mytable"`},
{"myschema.[mytable]", "myschema.mytable"}, {"myschema.[mytable]", "myschema.mytable"},
{"myschema.[mytable]", "`myschema`.mytable"}, {"myschema.[mytable]", "`myschema`.mytable"},
@ -98,6 +100,7 @@ func TestNoQuoteTo(t *testing.T) {
{"mytable", "mytable"}, {"mytable", "mytable"},
{"mytable", "`mytable`"}, {"mytable", "`mytable`"},
{"mytable", `[mytable]`}, {"mytable", `[mytable]`},
{"mytable.*", `[mytable].*`},
{`"mytable"`, `"mytable"`}, {`"mytable"`, `"mytable"`},
{"myschema.mytable", "myschema.mytable"}, {"myschema.mytable", "myschema.mytable"},
{"myschema.mytable", "`myschema`.mytable"}, {"myschema.mytable", "`myschema`.mytable"},
@ -127,6 +130,8 @@ func TestJoin(t *testing.T) {
assert.EqualValues(t, "[a],[b]", quoter.Join([]string{"a", " b"}, ",")) assert.EqualValues(t, "[a],[b]", quoter.Join([]string{"a", " b"}, ","))
assert.EqualValues(t, "[a].*,[b].[c]", quoter.Join([]string{"a.*", " b.c"}, ","))
assert.EqualValues(t, "[f1], [f2], [f3]", quoter.Join(cols, ", ")) assert.EqualValues(t, "[f1], [f2], [f3]", quoter.Join(cols, ", "))
quoter.IsReserved = AlwaysNoReserve quoter.IsReserved = AlwaysNoReserve
@ -134,11 +139,11 @@ func TestJoin(t *testing.T) {
} }
func TestStrings(t *testing.T) { func TestStrings(t *testing.T) {
cols := []string{"f1", "f2", "t3.f3"} cols := []string{"f1", "f2", "t3.f3", "t4.*"}
quoter := Quoter{'[', ']', AlwaysReserve} quoter := Quoter{'[', ']', AlwaysReserve}
quotedCols := quoter.Strings(cols) quotedCols := quoter.Strings(cols)
assert.EqualValues(t, []string{"[f1]", "[f2]", "[t3].[f3]"}, quotedCols) assert.EqualValues(t, []string{"[f1]", "[f2]", "[t3].[f3]", "[t4].*"}, quotedCols)
} }
func TestTrim(t *testing.T) { func TestTrim(t *testing.T) {