diff --git a/schemas/quote.go b/schemas/quote.go index 6df7bf0b..63230bf4 100644 --- a/schemas/quote.go +++ b/schemas/quote.go @@ -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 diff --git a/schemas/quote_test.go b/schemas/quote_test.go index f84dfb7d..8f39db0d 100644 --- a/schemas/quote_test.go +++ b/schemas/quote_test.go @@ -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`'", diff --git a/tests/session_query_test.go b/tests/session_query_test.go index 4df85f79..726b19e2 100644 --- a/tests/session_query_test.go +++ b/tests/session_query_test.go @@ -493,4 +493,4 @@ func TestRowsReset(t *testing.T) { assert.EqualValues(t, "4", rrs[0].Name) assert.EqualValues(t, "5", rrs[1].Name) assert.EqualValues(t, "6", rrs[2].Name) -} +} \ No newline at end of file