fix(gaussdb): quote identifiers by compatibility mode
This commit is contained in:
28 files changed
+1207
-134
No files matched your search
@@ -111,6 +111,11 @@ pub fn qualified_table_name_with_catalog(
|
||||
}
|
||||
|
||||
pub fn quote_table_identifier(database_type: Option<DatabaseType>, name: &str) -> String {
|
||||
if matches!(database_type, Some(DatabaseType::Gaussdb | DatabaseType::OpenGauss))
|
||||
&& is_explicitly_quoted_identifier(name)
|
||||
{
|
||||
return name.to_string();
|
||||
}
|
||||
match database_type {
|
||||
Some(DatabaseType::Iotdb) => name.to_string(),
|
||||
// JDBC connections use the driver-reported identifier quote string
|
||||
@@ -141,6 +146,221 @@ pub fn quote_table_identifier(database_type: Option<DatabaseType>, name: &str) -
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn quote_gaussdb_jdbc_identifier(name: &str, identifier_quote: &str) -> String {
|
||||
if is_explicitly_quoted_identifier(name) {
|
||||
return name.to_string();
|
||||
}
|
||||
let quote = identifier_quote.trim();
|
||||
if quote.is_empty() {
|
||||
return name.to_string();
|
||||
}
|
||||
let requires_quote = !is_simple_lower_identifier(name)
|
||||
|| is_postgres_reserved_identifier(name)
|
||||
|| (quote == "`" && is_mysql_only_reserved_identifier(name));
|
||||
if !requires_quote {
|
||||
return name.to_string();
|
||||
}
|
||||
format!("{quote}{}{quote}", name.replace(quote, &format!("{quote}{quote}")))
|
||||
}
|
||||
|
||||
fn is_explicitly_quoted_identifier(name: &str) -> bool {
|
||||
name.len() >= 2
|
||||
&& ((name.starts_with('"') && name.ends_with('"'))
|
||||
|| (name.starts_with('`') && name.ends_with('`'))
|
||||
|| (name.starts_with('[') && name.ends_with(']')))
|
||||
}
|
||||
|
||||
fn is_simple_lower_identifier(name: &str) -> bool {
|
||||
let mut chars = name.chars();
|
||||
let Some(first) = chars.next() else {
|
||||
return false;
|
||||
};
|
||||
(first == '_' || first.is_ascii_lowercase())
|
||||
&& chars.all(|ch| ch == '_' || ch == '$' || ch.is_ascii_lowercase() || ch.is_ascii_digit())
|
||||
}
|
||||
|
||||
fn is_postgres_reserved_identifier(name: &str) -> bool {
|
||||
matches!(
|
||||
name,
|
||||
"all"
|
||||
| "analyse"
|
||||
| "analyze"
|
||||
| "and"
|
||||
| "any"
|
||||
| "array"
|
||||
| "as"
|
||||
| "asc"
|
||||
| "asymmetric"
|
||||
| "authorization"
|
||||
| "binary"
|
||||
| "both"
|
||||
| "case"
|
||||
| "cast"
|
||||
| "check"
|
||||
| "collate"
|
||||
| "collation"
|
||||
| "column"
|
||||
| "concurrently"
|
||||
| "constraint"
|
||||
| "create"
|
||||
| "cross"
|
||||
| "current_catalog"
|
||||
| "current_date"
|
||||
| "current_role"
|
||||
| "current_schema"
|
||||
| "current_time"
|
||||
| "current_timestamp"
|
||||
| "current_user"
|
||||
| "default"
|
||||
| "deferrable"
|
||||
| "desc"
|
||||
| "distinct"
|
||||
| "do"
|
||||
| "else"
|
||||
| "end"
|
||||
| "except"
|
||||
| "false"
|
||||
| "fetch"
|
||||
| "for"
|
||||
| "foreign"
|
||||
| "freeze"
|
||||
| "from"
|
||||
| "full"
|
||||
| "grant"
|
||||
| "group"
|
||||
| "having"
|
||||
| "ilike"
|
||||
| "in"
|
||||
| "initially"
|
||||
| "inner"
|
||||
| "intersect"
|
||||
| "into"
|
||||
| "is"
|
||||
| "isnull"
|
||||
| "join"
|
||||
| "lateral"
|
||||
| "leading"
|
||||
| "left"
|
||||
| "like"
|
||||
| "limit"
|
||||
| "localtime"
|
||||
| "localtimestamp"
|
||||
| "natural"
|
||||
| "not"
|
||||
| "notnull"
|
||||
| "null"
|
||||
| "offset"
|
||||
| "on"
|
||||
| "only"
|
||||
| "or"
|
||||
| "order"
|
||||
| "outer"
|
||||
| "overlaps"
|
||||
| "placing"
|
||||
| "primary"
|
||||
| "references"
|
||||
| "returning"
|
||||
| "right"
|
||||
| "select"
|
||||
| "session_user"
|
||||
| "similar"
|
||||
| "some"
|
||||
| "symmetric"
|
||||
| "system_user"
|
||||
| "table"
|
||||
| "tablesample"
|
||||
| "then"
|
||||
| "to"
|
||||
| "trailing"
|
||||
| "true"
|
||||
| "union"
|
||||
| "unique"
|
||||
| "user"
|
||||
| "using"
|
||||
| "variadic"
|
||||
| "verbose"
|
||||
| "when"
|
||||
| "where"
|
||||
| "window"
|
||||
| "with"
|
||||
)
|
||||
}
|
||||
|
||||
fn is_mysql_only_reserved_identifier(name: &str) -> bool {
|
||||
matches!(
|
||||
name,
|
||||
"accessible"
|
||||
| "auto_increment"
|
||||
| "change"
|
||||
| "database"
|
||||
| "databases"
|
||||
| "delayed"
|
||||
| "describe"
|
||||
| "div"
|
||||
| "dual"
|
||||
| "enclosed"
|
||||
| "escaped"
|
||||
| "explain"
|
||||
| "force"
|
||||
| "fulltext"
|
||||
| "high_priority"
|
||||
| "ignore"
|
||||
| "index"
|
||||
| "infile"
|
||||
| "key"
|
||||
| "keys"
|
||||
| "kill"
|
||||
| "linear"
|
||||
| "lines"
|
||||
| "load"
|
||||
| "lock"
|
||||
| "low_priority"
|
||||
| "master_ssl_verify_server_cert"
|
||||
| "maxvalue"
|
||||
| "mediumint"
|
||||
| "mod"
|
||||
| "no_write_to_binlog"
|
||||
| "optimize"
|
||||
| "optionally"
|
||||
| "outfile"
|
||||
| "partition"
|
||||
| "purge"
|
||||
| "range"
|
||||
| "read_write"
|
||||
| "regexp"
|
||||
| "release"
|
||||
| "rename"
|
||||
| "replace"
|
||||
| "require"
|
||||
| "rlike"
|
||||
| "schema"
|
||||
| "schemas"
|
||||
| "separator"
|
||||
| "show"
|
||||
| "spatial"
|
||||
| "sql_big_result"
|
||||
| "sql_calc_found_rows"
|
||||
| "sql_small_result"
|
||||
| "ssl"
|
||||
| "starting"
|
||||
| "straight_join"
|
||||
| "terminated"
|
||||
| "tinyint"
|
||||
| "unlock"
|
||||
| "unsigned"
|
||||
| "use"
|
||||
| "utc_date"
|
||||
| "utc_time"
|
||||
| "utc_timestamp"
|
||||
| "values"
|
||||
| "varbinary"
|
||||
| "varchar"
|
||||
| "write"
|
||||
| "xor"
|
||||
| "zerofill"
|
||||
)
|
||||
}
|
||||
|
||||
pub fn normalize_where_input(where_input: Option<&str>) -> String {
|
||||
let trimmed = where_input.unwrap_or("").trim().trim_end_matches(';').trim();
|
||||
let mut chars = trimmed.chars();
|
||||
|
||||
@@ -4,7 +4,8 @@ use super::capabilities::{
|
||||
firebird_rows_clause, table_pagination_strategy, uses_oracle_row_id, TablePaginationStrategy,
|
||||
};
|
||||
use super::identifiers::{
|
||||
normalize_where_input, qualified_table_name, qualified_table_name_with_catalog, quote_table_identifier,
|
||||
normalize_where_input, qualified_table_name, qualified_table_name_with_catalog, quote_gaussdb_jdbc_identifier,
|
||||
quote_table_identifier,
|
||||
};
|
||||
use super::types::{
|
||||
TableDataSelectSqlOptions, TableSelectSqlOptions, DBX_NEO4J_ELEMENT_ID_COLUMN, DBX_ROWID_COLUMN,
|
||||
@@ -23,7 +24,7 @@ pub fn build_table_data_select_sql(options: TableDataSelectSqlOptions) -> String
|
||||
}
|
||||
|
||||
// Doris / StarRocks multi-catalog: prefix the catalog for external-catalog tables.
|
||||
let table = if database_type == Some(DatabaseType::Kingbase) {
|
||||
let table = if uses_connection_identifier_quote(database_type, options.identifier_quote.as_deref()) {
|
||||
table_data_qualified_table_name(
|
||||
database_type,
|
||||
options.schema.as_deref(),
|
||||
@@ -166,7 +167,7 @@ pub(crate) fn table_data_qualified_table_name(
|
||||
table_name: &str,
|
||||
identifier_quote: Option<&str>,
|
||||
) -> String {
|
||||
if database_type != Some(DatabaseType::Kingbase) {
|
||||
if !uses_connection_identifier_quote(database_type, identifier_quote) {
|
||||
return qualified_table_name(database_type, schema, table_name);
|
||||
}
|
||||
let table = quote_table_data_identifier(database_type, table_name, identifier_quote);
|
||||
@@ -182,18 +183,30 @@ pub(crate) fn quote_table_data_identifier(
|
||||
name: &str,
|
||||
identifier_quote: Option<&str>,
|
||||
) -> String {
|
||||
if database_type != Some(DatabaseType::Kingbase) {
|
||||
if !uses_connection_identifier_quote(database_type, identifier_quote) {
|
||||
return quote_table_identifier(database_type, name);
|
||||
}
|
||||
let Some(quote) = identifier_quote else {
|
||||
return quote_table_identifier(database_type, name);
|
||||
};
|
||||
if matches!(database_type, Some(DatabaseType::Gaussdb | DatabaseType::OpenGauss | DatabaseType::Postgres)) {
|
||||
return quote_gaussdb_jdbc_identifier(name, quote);
|
||||
}
|
||||
if quote.is_empty() {
|
||||
return name.to_string();
|
||||
}
|
||||
format!("{quote}{}{quote}", name.replace(quote, &format!("{quote}{quote}")))
|
||||
}
|
||||
|
||||
pub(crate) fn uses_connection_identifier_quote(
|
||||
database_type: Option<DatabaseType>,
|
||||
identifier_quote: Option<&str>,
|
||||
) -> bool {
|
||||
database_type == Some(DatabaseType::Kingbase)
|
||||
|| (matches!(database_type, Some(DatabaseType::Gaussdb | DatabaseType::OpenGauss | DatabaseType::Postgres))
|
||||
&& identifier_quote.is_some())
|
||||
}
|
||||
|
||||
fn is_view_table_type(table_type: Option<&str>) -> bool {
|
||||
table_type.is_some_and(|value| value.to_ascii_uppercase().contains("VIEW"))
|
||||
}
|
||||
|
||||
@@ -26,12 +26,39 @@ fn quotes_identifiers_by_database_type() {
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Kingbase), "order"), "\"order\"");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Kingbase), "MixedCase"), "\"MixedCase\"");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Kingbase), "order detail"), "\"order detail\"");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Gaussdb), "\"MixedCase\""), "\"MixedCase\"");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::OpenGauss), "\"MixedCase\""), "\"MixedCase\"");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Informix), "users_1"), "users_1");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Jdbc), "users_1"), "users_1");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Jdbc), "user name"), "user name");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Iotdb), "root.test.device2"), "root.test.device2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quotes_gaussdb_jdbc_identifiers_selectively() {
|
||||
for (name, expected) in [
|
||||
("schema_01", "schema_01"),
|
||||
("MixedCase", "\"MixedCase\""),
|
||||
("order", "\"order\""),
|
||||
("order detail", "\"order detail\""),
|
||||
("already\"quoted", "\"already\"\"quoted\""),
|
||||
("\"AlreadyQuoted\"", "\"AlreadyQuoted\""),
|
||||
] {
|
||||
assert_eq!(quote_table_data_identifier(Some(DatabaseType::Gaussdb), name, Some("\"")), expected);
|
||||
}
|
||||
|
||||
for (name, expected) in [
|
||||
("schema_01", "schema_01"),
|
||||
("MixedCase", "`MixedCase`"),
|
||||
("order", "`order`"),
|
||||
("order detail", "`order detail`"),
|
||||
("already`quoted", "`already``quoted`"),
|
||||
("`AlreadyQuoted`", "`AlreadyQuoted`"),
|
||||
] {
|
||||
assert_eq!(quote_table_data_identifier(Some(DatabaseType::Gaussdb), name, Some("`")), expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qualifies_schema_only_for_schema_aware_databases() {
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::Postgres), Some("public"), "users"), "\"public\".\"users\"");
|
||||
@@ -342,6 +369,72 @@ fn builds_table_data_where_and_schema_queries() {
|
||||
}),
|
||||
"SELECT * FROM \"App Schema\".\"ANALYZE\" LIMIT 100;"
|
||||
);
|
||||
assert_eq!(
|
||||
build_table_data_select_sql(TableDataSelectSqlOptions {
|
||||
database_type: Some(DatabaseType::Gaussdb),
|
||||
identifier_quote: Some("\"".to_string()),
|
||||
schema: Some("schema_01".to_string()),
|
||||
table_name: "table_01".to_string(),
|
||||
limit: Some(100),
|
||||
..Default::default()
|
||||
}),
|
||||
"SELECT * FROM schema_01.table_01 LIMIT 100;"
|
||||
);
|
||||
assert_eq!(
|
||||
build_table_data_select_sql(TableDataSelectSqlOptions {
|
||||
database_type: Some(DatabaseType::Gaussdb),
|
||||
identifier_quote: Some("`".to_string()),
|
||||
schema: Some("App Schema".to_string()),
|
||||
table_name: "order".to_string(),
|
||||
limit: Some(100),
|
||||
..Default::default()
|
||||
}),
|
||||
"SELECT * FROM `App Schema`.`order` LIMIT 100;"
|
||||
);
|
||||
for database_type in [DatabaseType::Postgres, DatabaseType::OpenGauss] {
|
||||
assert_eq!(
|
||||
build_table_data_select_sql(TableDataSelectSqlOptions {
|
||||
database_type: Some(database_type),
|
||||
identifier_quote: Some("`".to_string()),
|
||||
schema: Some("App Schema".to_string()),
|
||||
table_name: "order".to_string(),
|
||||
limit: Some(100),
|
||||
..Default::default()
|
||||
}),
|
||||
"SELECT * FROM `App Schema`.`order` LIMIT 100;"
|
||||
);
|
||||
assert_eq!(
|
||||
build_table_data_select_sql(TableDataSelectSqlOptions {
|
||||
database_type: Some(database_type),
|
||||
identifier_quote: Some("\"".to_string()),
|
||||
schema: Some("schema_01".to_string()),
|
||||
table_name: "MixedCase".to_string(),
|
||||
limit: Some(100),
|
||||
..Default::default()
|
||||
}),
|
||||
"SELECT * FROM schema_01.\"MixedCase\" LIMIT 100;"
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
build_table_data_select_sql(TableDataSelectSqlOptions {
|
||||
database_type: Some(DatabaseType::Gaussdb),
|
||||
schema: Some("schema_01".to_string()),
|
||||
table_name: "table_01".to_string(),
|
||||
limit: Some(100),
|
||||
..Default::default()
|
||||
}),
|
||||
"SELECT * FROM \"schema_01\".\"table_01\" LIMIT 100;"
|
||||
);
|
||||
assert_eq!(
|
||||
build_table_data_select_sql(TableDataSelectSqlOptions {
|
||||
database_type: Some(DatabaseType::OpenGauss),
|
||||
schema: Some("schema_01".to_string()),
|
||||
table_name: "table_01".to_string(),
|
||||
limit: Some(100),
|
||||
..Default::default()
|
||||
}),
|
||||
"SELECT * FROM \"schema_01\".\"table_01\" LIMIT 100;"
|
||||
);
|
||||
assert_eq!(
|
||||
build_table_data_select_sql(TableDataSelectSqlOptions {
|
||||
database_type: Some(DatabaseType::Xugu),
|
||||
|
||||
Reference in new issue
Block a user