Fix bug when join with alias start with `a` (#2343)

Fix #2331

Reviewed-on: https://gitea.com/xorm/xorm/pulls/2343
(cherry picked from commit 6ef0a7798f)
This commit is contained in:
Lunny Xiao 2023-10-25 11:01:46 +00:00 committed by CyJaySong
parent 37fd7d3c15
commit 9c82535c2e
3 changed files with 17 additions and 9 deletions

View File

@ -111,7 +111,7 @@ func findStart(value string, start int) int {
return start return start
} }
var k = -1 k := -1
for j := start; j < len(value); j++ { for j := start; j < len(value); j++ {
if value[j] != ' ' { if value[j] != ' ' {
k = j k = j
@ -122,7 +122,9 @@ func findStart(value string, start int) int {
return len(value) return len(value)
} }
if (value[k] == 'A' || value[k] == 'a') && (value[k+1] == 'S' || value[k+1] == 's') { if k+1 < len(value) &&
(value[k] == 'A' || value[k] == 'a') &&
(value[k+1] == 'S' || value[k+1] == 's') {
k += 2 k += 2
} }
@ -135,7 +137,7 @@ func findStart(value string, start int) int {
} }
func (q Quoter) quoteWordTo(buf *strings.Builder, word string) error { func (q Quoter) quoteWordTo(buf *strings.Builder, word string) error {
var realWord = word realWord := word
if (word[0] == CommanQuoteMark && word[len(word)-1] == CommanQuoteMark) || if (word[0] == CommanQuoteMark && word[len(word)-1] == CommanQuoteMark) ||
(word[0] == q.Prefix && word[len(word)-1] == q.Suffix) { (word[0] == q.Prefix && word[len(word)-1] == q.Suffix) {
realWord = word[1 : len(word)-1] realWord = word[1 : len(word)-1]
@ -188,7 +190,7 @@ func (q Quoter) QuoteTo(buf *strings.Builder, value string) error {
return nil return nil
} }
var nextEnd = findWord(value, start) nextEnd := findWord(value, start)
if err := q.quoteWordTo(buf, value[start:nextEnd]); err != nil { if err := q.quoteWordTo(buf, value[start:nextEnd]); err != nil {
return err return err
} }
@ -199,7 +201,7 @@ func (q Quoter) QuoteTo(buf *strings.Builder, value string) error {
// Strings quotes a slice of string // Strings quotes a slice of string
func (q Quoter) Strings(s []string) []string { func (q Quoter) Strings(s []string) []string {
var res = make([]string, 0, len(s)) res := make([]string, 0, len(s))
for _, a := range s { for _, a := range s {
res = append(res, q.Quote(a)) res = append(res, q.Quote(a))
} }
@ -218,7 +220,7 @@ func (q Quoter) Replace(sql string) string {
var beginSingleQuote bool var beginSingleQuote bool
for i := 0; i < len(sql); i++ { for i := 0; i < len(sql); i++ {
if !beginSingleQuote && sql[i] == CommanQuoteMark { if !beginSingleQuote && sql[i] == CommanQuoteMark {
var j = i + 1 j := i + 1
for ; j < len(sql); j++ { for ; j < len(sql); j++ {
if sql[j] == CommanQuoteMark { if sql[j] == CommanQuoteMark {
break break

View File

@ -131,6 +131,8 @@ func TestJoin(t *testing.T) {
assert.EqualValues(t, "[a].*,[b].[c]", quoter.Join([]string{"a.*", " b.c"}, ",")) assert.EqualValues(t, "[a].*,[b].[c]", quoter.Join([]string{"a.*", " b.c"}, ","))
assert.EqualValues(t, "[b] [a]", quoter.Join([]string{"b a"}, ","))
assert.EqualValues(t, "[f1], [f2], [f3]", quoter.Join(cols, ", ")) assert.EqualValues(t, "[f1], [f2], [f3]", quoter.Join(cols, ", "))
quoter.IsReserved = AlwaysNoReserve quoter.IsReserved = AlwaysNoReserve
@ -146,7 +148,7 @@ func TestStrings(t *testing.T) {
} }
func TestTrim(t *testing.T) { func TestTrim(t *testing.T) {
var kases = map[string]string{ kases := map[string]string{
"[table_name]": "table_name", "[table_name]": "table_name",
"[schema].[table_name]": "schema.table_name", "[schema].[table_name]": "schema.table_name",
} }
@ -159,7 +161,7 @@ func TestTrim(t *testing.T) {
func TestReplace(t *testing.T) { func TestReplace(t *testing.T) {
q := Quoter{'[', ']', AlwaysReserve} q := Quoter{'[', ']', AlwaysReserve}
var kases = []struct { kases := []struct {
source string source string
expected string expected string
}{ }{
@ -171,6 +173,10 @@ func TestReplace(t *testing.T) {
"SELECT 'abc```test```''', `a` FROM b", "SELECT 'abc```test```''', `a` FROM b",
"SELECT 'abc```test```''', [a] FROM b", "SELECT 'abc```test```''', [a] FROM b",
}, },
{
"SELECT * FROM `a` INNER JOIN `b` `c` WHERE `a`.`id` = `c`.`a_id`",
"SELECT * FROM [a] INNER JOIN [b] [c] WHERE [a].[id] = [c].[a_id]",
},
{ {
"UPDATE table SET `a` = ~ `a`, `b`='abc`'", "UPDATE table SET `a` = ~ `a`, `b`='abc`'",
"UPDATE table SET [a] = ~ [a], [b]='abc`'", "UPDATE table SET [a] = ~ [a], [b]='abc`'",