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

Fix #2331

Reviewed-on: https://gitea.com/xorm/xorm/pulls/2343
This commit is contained in:
Lunny Xiao 2023-10-25 11:01:46 +00:00
parent 0f085408af
commit 6ef0a7798f
3 changed files with 17 additions and 9 deletions

View File

@ -111,7 +111,7 @@ func findStart(value string, start int) int {
return start
}
var k = -1
k := -1
for j := start; j < len(value); j++ {
if value[j] != ' ' {
k = j
@ -122,7 +122,9 @@ func findStart(value string, start int) int {
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
}
@ -135,7 +137,7 @@ func findStart(value string, start int) int {
}
func (q Quoter) quoteWordTo(buf *strings.Builder, word string) error {
var realWord = word
realWord := word
if (word[0] == CommanQuoteMark && word[len(word)-1] == CommanQuoteMark) ||
(word[0] == q.Prefix && word[len(word)-1] == q.Suffix) {
realWord = word[1 : len(word)-1]
@ -188,7 +190,7 @@ func (q Quoter) QuoteTo(buf *strings.Builder, value string) error {
return nil
}
var nextEnd = findWord(value, start)
nextEnd := findWord(value, start)
if err := q.quoteWordTo(buf, value[start:nextEnd]); err != nil {
return err
}
@ -199,7 +201,7 @@ func (q Quoter) QuoteTo(buf *strings.Builder, value string) error {
// Strings quotes a slice of 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 {
res = append(res, q.Quote(a))
}
@ -218,7 +220,7 @@ func (q Quoter) Replace(sql string) string {
var beginSingleQuote bool
for i := 0; i < len(sql); i++ {
if !beginSingleQuote && sql[i] == CommanQuoteMark {
var j = i + 1
j := i + 1
for ; j < len(sql); j++ {
if sql[j] == CommanQuoteMark {
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, "[b] [a]", quoter.Join([]string{"b a"}, ","))
assert.EqualValues(t, "[f1], [f2], [f3]", quoter.Join(cols, ", "))
quoter.IsReserved = AlwaysNoReserve
@ -146,7 +148,7 @@ func TestStrings(t *testing.T) {
}
func TestTrim(t *testing.T) {
var kases = map[string]string{
kases := map[string]string{
"[table_name]": "table_name",
"[schema].[table_name]": "schema.table_name",
}
@ -159,7 +161,7 @@ func TestTrim(t *testing.T) {
func TestReplace(t *testing.T) {
q := Quoter{'[', ']', AlwaysReserve}
var kases = []struct {
kases := []struct {
source 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 * 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`'",