Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: add quote to column name with camel case (#10557)

boojack9db764750d0e
diff --git a/backend/api/lsp/completion.go b/backend/api/lsp/completion.go
index db1adcceba..74d5143f43 100644
--- a/backend/api/lsp/completion.go
+++ b/backend/api/lsp/completion.go
@@ -9,6 +9,7 @@ import (
"github.com/sourcegraph/go-lsp"
"github.com/sourcegraph/jsonrpc2"
+ "github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store"
"github.com/bytebase/bytebase/backend/store/model"
@@ -68,6 +69,7 @@ func (h *Handler) handleTextDocumentCompletion(ctx context.Context, _ *jsonrpc2.
Kind: convertLSPCompletionItemKind(candidate.Type),
Documentation: candidate.Comment,
SortText: generateSortText(params, candidate),
+ InsertText: generateInsertText(engine, candidate),
}
items = append(items, completionItem)
}
@@ -104,6 +106,23 @@ func generateSortTextAfterDot(candidate base.Candidate) string {
}
}
+func generateInsertText(engine storepb.Engine, candidate base.Candidate) string {
+ // For non-postgres engine, we return the candidate text as the insert text.
+ if engine != storepb.Engine_POSTGRES {
+ return candidate.Text
+ }
+
+ insertText := candidate.Text
+ // If the candidate is a column and it's a camel case, we need to quote it.
+ //
+ // TODO(rebelice): there are lots of special cases that we don't need to add quotes.
+ // For example, if the previous token is a quote, we don't need to quote the column name.
+ if candidate.Type == base.CandidateTypeColumn && common.IsCamelCase(insertText) {
+ insertText = fmt.Sprintf(`"%s"`, insertText)
+ }
+ return insertText
+}
+
func convertLSPCompletionItemKind(tp base.CandidateType) lsp.CompletionItemKind {
switch tp {
case base.CandidateTypeDatabase:
diff --git a/backend/common/util.go b/backend/common/util.go
index 4c2546fd2a..d7792ffc8b 100644
--- a/backend/common/util.go
+++ b/backend/common/util.go
@@ -12,6 +12,7 @@ import (
"strconv"
"strings"
"time"
+ "unicode"
"unicode/utf8"
"github.com/nyaruka/phonenumbers"
@@ -256,3 +257,34 @@ func SanitizeUTF8String(s string) string {
return b.String()
}
+
+// IsCamelCase checks whether a string conforms to the camel case format.
+func IsCamelCase(s string) bool {
+ // If the string is empty, it does not conform to camel case format.
+ if s == "" {
+ return false
+ }
+ // The first character must be a lowercase letter.
+ if !unicode.IsLower(rune(s[0])) {
+ return false
+ }
+ // Check if the string contains uppercase letters.
+ hasUpperCase := false
+ for _, char := range s {
+ if unicode.IsUpper(char) {
+ hasUpperCase = true
+ break
+ }
+ }
+ // If the string does not contain uppercase letters, it is not in camel case format.
+ if !hasUpperCase {
+ return false
+ }
+ // Check for consecutive uppercase letters.
+ for i := 1; i < len(s); i++ {
+ if unicode.IsUpper(rune(s[i])) && unicode.IsUpper(rune(s[i-1])) {
+ return false
+ }
+ }
+ return true
+}
diff --git a/backend/common/util_test.go b/backend/common/util_test.go
index b85fe75752..129cdb8e05 100644
--- a/backend/common/util_test.go
+++ b/backend/common/util_test.go
@@ -250,3 +250,39 @@ func TestValidatePhone(t *testing.T) {
}
}
}
+
+func TestIsCamelCase(t *testing.T) {
+ tests := []struct {
+ text string
+ want bool
+ }{
+ {
+ text: "snake_case",
+ want: false,
+ },
+ {
+ text: "PascalCase",
+ want: false,
+ },
+ {
+ text: "",
+ want: false,
+ },
+ {
+ text: "non",
+ want: false,
+ },
+ {
+ text: "camelCase",
+ want: true,
+ },
+ }
+
+ for i := range tests {
+ test := tests[i]
+ t.Run(test.text, func(t *testing.T) {
+ got := IsCamelCase(test.text)
+ assert.Equal(t, test.want, got)
+ })
+ }
+}