Skip to content

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

chore: remove quote indentifier for pg (#1935)

rebelice5cfb2514d8f1
diff --git a/plugin/db/pg/keyword.go b/plugin/db/pg/keyword.go
deleted file mode 100644
index 7a89c40c59..0000000000
--- a/plugin/db/pg/keyword.go
+++ /dev/null
@@ -1,152 +0,0 @@
-package pg
-
-var (
- reserved = map[string]bool{
- "AES128": true,
- "AES256": true,
- "ALL": true,
- "ALLOWOVERWRITE": true,
- "ANALYSE": true,
- "ANALYZE": true,
- "AND": true,
- "ANY": true,
- "ARRAY": true,
- "AS": true,
- "ASC": true,
- "AUTHORIZATION": true,
- "BACKUP": true,
- "BETWEEN": true,
- "BINARY": true,
- "BLANKSASNULL": true,
- "BOTH": true,
- "BYTEDICT": true,
- "CASE": true,
- "CAST": true,
- "CHECK": true,
- "COLLATE": true,
- "COLUMN": true,
- "CONSTRAINT": true,
- "CREATE": true,
- "CREDENTIALS": true,
- "CROSS": true,
- "CURRENT_DATE": true,
- "CURRENT_TIME": true,
- "CURRENT_TIMESTAMP": true,
- "CURRENT_USER": true,
- "CURRENT_USER_ID": true,
- "DEFAULT": true,
- "DEFERRABLE": true,
- "DEFLATE": true,
- "DEFRAG": true,
- "DELTA": true,
- "DELTA32K": true,
- "DESC": true,
- "DISABLE": true,
- "DISTINCT": true,
- "DO": true,
- "ELSE": true,
- "EMPTYASNULL": true,
- "ENABLE": true,
- "ENCODE": true,
- "ENCRYPT": true,
- "ENCRYPTION": true,
- "END": true,
- "EXCEPT": true,
- "EXPLICIT": true,
- "FALSE": true,
- "FOR": true,
- "FOREIGN": true,
- "FREEZE": true,
- "FROM": true,
- "FULL": true,
- "GLOBALDICT256": true,
- "GLOBALDICT64K": true,
- "GRANT": true,
- "GROUP": true,
- "GZIP": true,
- "HAVING": true,
- "IDENTITY": true,
- "IGNORE": true,
- "ILIKE": true,
- "IN": true,
- "INITIALLY": true,
- "INNER": true,
- "INTERSECT": true,
- "INTO": true,
- "IS": true,
- "ISNULL": true,
- "JOIN": true,
- "LEADING": true,
- "LEFT": true,
- "LIKE": true,
- "LIMIT": true,
- "LOCALTIME": true,
- "LOCALTIMESTAMP": true,
- "LUN": true,
- "LUNS": true,
- "LZO": true,
- "LZOP": true,
- "MINUS": true,
- "MOSTLY13": true,
- "MOSTLY32": true,
- "MOSTLY8": true,
- "NATURAL": true,
- "NEW": true,
- "NOT": true,
- "NOTNULL": true,
- "NULL": true,
- "NULLS": true,
- "OFF": true,
- "OFFLINE": true,
- "OFFSET": true,
- "OLD": true,
- "ON": true,
- "ONLY": true,
- "OPEN": true,
- "OR": true,
- "ORDER": true,
- "OUTER": true,
- "OVERLAPS": true,
- "PARALLEL": true,
- "PARTITION": true,
- "PERCENT": true,
- "PLACING": true,
- "PRIMARY": true,
- "RAW": true,
- "READRATIO": true,
- "RECOVER": true,
- "REFERENCES": true,
- "REJECTLOG": true,
- "RESORT": true,
- "RESTORE": true,
- "RIGHT": true,
- "SELECT": true,
- "SESSION_USER": true,
- "SIMILAR": true,
- "SOME": true,
- "SYSDATE": true,
- "SYSTEM": true,
- "TABLE": true,
- "TAG": true,
- "TDES": true,
- "TEXT255": true,
- "TEXT32K": true,
- "THEN": true,
- "TO": true,
- "TOP": true,
- "TRAILING": true,
- "TRUE": true,
- "TRUNCATECOLUMNS": true,
- "UNION": true,
- "UNIQUE": true,
- "USER": true,
- "USING": true,
- "VERBOSE": true,
- "WALLET": true,
- "WHEN": true,
- "WHERE": true,
- "WITH": true,
- "WITHIN": true,
- "WITHOUT": true,
- }
-)
diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.go
index dc3642c451..1397a2f327 100644
--- a/plugin/db/pg/pg.go
+++ b/plugin/db/pg/pg.go
@@ -5,7 +5,6 @@ import (
"context"
"database/sql"
"fmt"
- "regexp"
"strings"
// Import pg driver.
@@ -22,7 +21,6 @@ var (
"template0": true,
"template1": true,
}
- ident = regexp.MustCompile(`(?i)^[a-z_][a-z0-9_$]*$`)
createBytebaseDatabaseStmt = "CREATE DATABASE bytebase;"
// driverName is the driver name that our driver dependence register, now is "pgx".
diff --git a/plugin/db/pg/sync.go b/plugin/db/pg/sync.go
index 57302be292..49d479f213 100644
--- a/plugin/db/pg/sync.go
+++ b/plugin/db/pg/sync.go
@@ -318,8 +318,8 @@ func getPgTables(txn *sql.Tx) ([]*tableSchema, error) {
if err := rows.Scan(&schemaname, &tablename, &tableowner, &tableSizeByte, &indexSizeByte); err != nil {
return nil, err
}
- tbl.schemaName = quoteIdentifier(schemaname)
- tbl.name = quoteIdentifier(tablename)
+ tbl.schemaName = schemaname
+ tbl.name = tablename
tbl.tableowner = tableowner
tbl.tableSizeByte = tableSizeByte
tbl.indexSizeByte = indexSizeByte
@@ -363,7 +363,7 @@ func getTable(txn *sql.Tx, tbl *tableSchema) error {
return err
}
- commentQuery := fmt.Sprintf(`SELECT obj_description(E'"%s"."%s"'::regclass);`, tbl.schemaName, tbl.name)
+ commentQuery := fmt.Sprintf(`SELECT obj_description('"%s"."%s"'::regclass);`, tbl.schemaName, tbl.name)
crows, err := txn.Query(commentQuery)
if err != nil {
return err
@@ -463,7 +463,6 @@ func getTableConstraints(txn *sql.Tx) (map[string][]*tableConstraint, error) {
if strings.Contains(constraint.tableName, ".") {
constraint.tableName = constraint.tableName[1+strings.Index(constraint.tableName, "."):]
}
- constraint.schemaName, constraint.tableName, constraint.name = quoteIdentifier(constraint.schemaName), quoteIdentifier(constraint.tableName), quoteIdentifier(constraint.name)
key := fmt.Sprintf("%s.%s", constraint.schemaName, constraint.tableName)
ret[key] = append(ret[key], &constraint)
}
@@ -496,7 +495,7 @@ func getViews(txn *sql.Tx) ([]*viewSchema, error) {
if !def.Valid {
return nil, fmt.Errorf("schema %q view %q has empty definition; please check whether proper privileges have been granted to Bytebase", view.schemaName, view.name)
}
- view.schemaName, view.name, view.definition = quoteIdentifier(view.schemaName), quoteIdentifier(view.name), def.String
+ view.definition = def.String
views = append(views, &view)
}
if err := rows.Err(); err != nil {
@@ -513,7 +512,7 @@ func getViews(txn *sql.Tx) ([]*viewSchema, error) {
// getView gets the schema of a view.
func getView(txn *sql.Tx, view *viewSchema) error {
- query := fmt.Sprintf(`SELECT obj_description(E'"%s"."%s"'::regclass);`, view.schemaName, view.name)
+ query := fmt.Sprintf(`SELECT obj_description('"%s"."%s"'::regclass);`, view.schemaName, view.name)
rows, err := txn.Query(query)
if err != nil {
return err
@@ -580,7 +579,6 @@ func getIndices(txn *sql.Tx) ([]*indexSchema, error) {
if err := rows.Scan(&idx.schemaName, &idx.tableName, &idx.name, &idx.statement); err != nil {
return nil, err
}
- idx.schemaName, idx.tableName, idx.name = quoteIdentifier(idx.schemaName), quoteIdentifier(idx.tableName), quoteIdentifier(idx.name)
idx.unique = strings.Contains(idx.statement, " UNIQUE INDEX ")
idx.methodType = getIndexMethodType(idx.statement)
idx.columnExpressions, err = getIndexColumnExpressions(idx.statement)
@@ -603,7 +601,7 @@ func getIndices(txn *sql.Tx) ([]*indexSchema, error) {
}
func getIndex(txn *sql.Tx, idx *indexSchema) error {
- commentQuery := fmt.Sprintf(`SELECT obj_description(E'"%s"."%s"'::regclass);`, idx.schemaName, idx.name)
+ commentQuery := fmt.Sprintf(`SELECT obj_description('"%s"."%s"'::regclass);`, idx.schemaName, idx.name)
rows, err := txn.Query(commentQuery)
if err != nil {
return err
@@ -687,18 +685,3 @@ func getIndexColumnExpressions(stmt string) ([]string, error) {
return cols, nil
}
-
-// quoteIdentifier will quote identifiers including keywords, capital characters, or special characters.
-func quoteIdentifier(s string) string {
- quote := false
- if reserved[strings.ToUpper(s)] {
- quote = true
- }
- if !ident.MatchString(s) {
- quote = true
- }
- if quote {
- return fmt.Sprintf("\"%s\"", s)
- }
- return s
-}