Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: update pg ac (#16036)
Vincent Huang918f5cd3a0ac
diff --git a/backend/api/lsp/completion.go b/backend/api/lsp/completion.goindex b877b3c0e5..a9c521dea9 100644--- a/backend/api/lsp/completion.go+++ b/backend/api/lsp/completion.go@@ -4,6 +4,7 @@ import ("context""fmt""log/slog"+ "strings"lsp "github.com/bytebase/lsp-protocol""github.com/pkg/errors"@@ -74,6 +75,7 @@ func (h *Handler) handleTextDocumentCompletion(ctx context.Context, _ *jsonrpc2.// Remove quotes or brackets from label.if len(label) > 1 && (label[0] == '"' && label[len(label)-1] == '"' || label[0] == '`' && label[len(label)-1] == '`' || label[0] == '[' && label[len(label)-1] == ']') {label = label[1 : len(label)-1]+ label = strings.ReplaceAll(label, `""`, `"`)}completionItem := lsp.CompletionItem{Label: label,diff --git a/backend/plugin/parser/pg/completion.go b/backend/plugin/parser/pg/completion.goindex 96b6950200..cb5ea9e6e6 100644--- a/backend/plugin/parser/pg/completion.go+++ b/backend/plugin/parser/pg/completion.go@@ -5,6 +5,7 @@ import ("fmt""sort""strings"+ "unicode""github.com/antlr4-go/antlr/v4"pg "github.com/bytebase/postgresql-parser"@@ -839,7 +840,7 @@ func (c *Completer) determineQualifiedName() (string, ObjectFlags) {qualifier := ""temp := ""if c.lexer.IsIdentifier(c.scanner.GetTokenType()) {- temp = unquote(c.scanner.GetTokenText())+ temp = normalizeIdentifier(c.scanner.GetTokenText())c.scanner.Forward(true /* skipHidden */)}@@ -884,7 +885,7 @@ func (c *Completer) determineColumnRef() (schema, table string, flags ObjectFlagtable = ""temp := ""if c.lexer.IsIdentifier(c.scanner.GetTokenType()) {- temp = unquote(c.scanner.GetTokenText())+ temp = normalizeIdentifier(c.scanner.GetTokenText())c.scanner.Forward(true /* skipHidden */)}@@ -896,7 +897,7 @@ func (c *Completer) determineColumnRef() (schema, table string, flags ObjectFlagtable = tempschema = tempif c.lexer.IsIdentifier(c.scanner.GetTokenType()) {- temp = unquote(c.scanner.GetTokenText())+ temp = normalizeIdentifier(c.scanner.GetTokenText())c.scanner.Forward(true /* skipHidden */)if !c.scanner.IsTokenType(pg.PostgreSQLLexerDOT) || position <= c.scanner.GetIndex() {@@ -910,6 +911,13 @@ func (c *Completer) determineColumnRef() (schema, table string, flags ObjectFlagreturn schema, table, ObjectFlagsShowTables | ObjectFlagsShowColumns}+func normalizeIdentifier(tokenText string) string {+ if len(tokenText) >= 2 && tokenText[0] == '"' && tokenText[len(tokenText)-1] == '"' {+ return normalizePostgreSQLQuotedIdentifier(tokenText)+ }+ return unquote(tokenText)+}+func unquote(s string) string {if len(s) < 2 {return s@@ -1338,5 +1346,38 @@ func (c *Completer) quotedIdentifierIfNeeded(s string) string {if c.lexer.IsReservedKeyword(strings.ToUpper(s)) {return fmt.Sprintf(`"%s"`, s)}+ // PostgreSQL requires double quotes for identifiers with special characters or start with digits+ if !isValidUnquotedIdentifier(s) {+ // If the identifier contains double quotes, we need to escape them by doubling them+ if strings.Contains(s, `"`) {+ s = strings.ReplaceAll(s, `"`, `""`)+ }+ return fmt.Sprintf(`"%s"`, s)+ }return s}++// isValidUnquotedIdentifier checks if the identifier can be used without quotes in PostgreSQL.+// PostgreSQL unquoted identifiers must:+// - Begin with a letter (a-z, A-Z) or underscore+// - Contain only letters, digits, and underscores+func isValidUnquotedIdentifier(s string) bool {+ if len(s) == 0 {+ return false+ }++ // First character must be a letter or underscore+ first := rune(s[0])+ if !unicode.IsLetter(first) && first != '_' {+ return false+ }++ // Remaining characters must be letters, digits, or underscores+ for _, ch := range s[1:] {+ if !unicode.IsLetter(ch) && !unicode.IsDigit(ch) && ch != '_' {+ return false+ }+ }++ return true+}