feat(spanner): add Google Cloud Spanner support
This commit is contained in:
1 parent
2cc8d78e64
commit
f494f90c30
74 files changed
+2920
-53
No files matched your search
@@ -253,6 +253,13 @@ const AGENT_CATALOG: &[AgentCatalogEntry] = &[
|
||||
store_visible: true,
|
||||
profiles: &[],
|
||||
},
|
||||
AgentCatalogEntry {
|
||||
db_type: DatabaseType::Spanner,
|
||||
key: "spanner",
|
||||
label: "Google Cloud Spanner",
|
||||
store_visible: true,
|
||||
profiles: &[],
|
||||
},
|
||||
AgentCatalogEntry {
|
||||
db_type: DatabaseType::Kylin,
|
||||
key: "kylin",
|
||||
|
||||
@@ -221,6 +221,7 @@ macro_rules! agent_connection_pool_database_type {
|
||||
| DatabaseType::Neo4j
|
||||
| DatabaseType::Cassandra
|
||||
| DatabaseType::Bigquery
|
||||
| DatabaseType::Spanner
|
||||
| DatabaseType::Kylin
|
||||
| DatabaseType::Ignite
|
||||
| DatabaseType::Ignite3
|
||||
|
||||
@@ -5341,6 +5341,87 @@ mod tests {
|
||||
.all(|statement| statement.contains("`gc`.`docfileinfo`") && !statement.contains('"')));
|
||||
}
|
||||
|
||||
/// Spanner's dialect is fixed at database creation time and only the connected agent
|
||||
/// knows it: GoogleSQL quotes with backticks (and has an empty default schema),
|
||||
/// the PostgreSQL dialect quotes with double quotes and defaults to `public`.
|
||||
#[test]
|
||||
fn spanner_save_uses_connection_identifier_quote_per_dialect() {
|
||||
let googlesql = prepare_data_grid_save(DataGridSaveStatementOptions {
|
||||
database_type: Some(DatabaseType::Spanner),
|
||||
identifier_quote: Some("`".to_string()),
|
||||
table_meta: DataGridTableMeta {
|
||||
catalog: None,
|
||||
database: None,
|
||||
schema: Some(String::new()),
|
||||
table_name: "singers".to_string(),
|
||||
primary_keys: vec!["id".to_string()],
|
||||
columns: Some(vec![column("id", "INT64", false, None), column("name", "STRING(100)", false, None)]),
|
||||
},
|
||||
columns: vec!["id".to_string(), "name".to_string()],
|
||||
source_columns: None,
|
||||
rows: vec![vec![json!(1), json!("old")]],
|
||||
dirty_rows: vec![(0, vec![(1, json!("Ada"))])],
|
||||
deleted_rows: vec![],
|
||||
new_rows: vec![],
|
||||
});
|
||||
|
||||
assert_eq!(googlesql.validation_error, None);
|
||||
// Single-segment table name: `` ``.`singers` `` would be `Invalid empty identifier`.
|
||||
assert_eq!(googlesql.statements, vec!["UPDATE `singers` SET `name` = 'Ada' WHERE `id` = 1;"]);
|
||||
|
||||
let postgres_dialect = prepare_data_grid_save(DataGridSaveStatementOptions {
|
||||
database_type: Some(DatabaseType::Spanner),
|
||||
identifier_quote: Some("\"".to_string()),
|
||||
table_meta: DataGridTableMeta {
|
||||
catalog: None,
|
||||
database: None,
|
||||
schema: Some("public".to_string()),
|
||||
table_name: "singers".to_string(),
|
||||
primary_keys: vec!["id".to_string()],
|
||||
columns: Some(vec![
|
||||
column("id", "bigint", false, None),
|
||||
column("name", "character varying", false, None),
|
||||
]),
|
||||
},
|
||||
columns: vec!["id".to_string(), "name".to_string()],
|
||||
source_columns: None,
|
||||
rows: vec![vec![json!(1), json!("old")]],
|
||||
dirty_rows: vec![(0, vec![(1, json!("Ada"))])],
|
||||
deleted_rows: vec![],
|
||||
new_rows: vec![],
|
||||
});
|
||||
|
||||
assert_eq!(postgres_dialect.validation_error, None);
|
||||
assert_eq!(
|
||||
postgres_dialect.statements,
|
||||
vec!["UPDATE \"public\".\"singers\" SET \"name\" = 'Ada' WHERE \"id\" = 1;"]
|
||||
);
|
||||
|
||||
// No quote reported: fall back to the GoogleSQL default (backticks), never to the
|
||||
// ANSI double quote, which GoogleSQL parses as a string literal.
|
||||
let no_reported_quote = prepare_data_grid_save(DataGridSaveStatementOptions {
|
||||
database_type: Some(DatabaseType::Spanner),
|
||||
identifier_quote: None,
|
||||
table_meta: DataGridTableMeta {
|
||||
catalog: None,
|
||||
database: None,
|
||||
schema: Some(String::new()),
|
||||
table_name: "singers".to_string(),
|
||||
primary_keys: vec!["id".to_string()],
|
||||
columns: Some(vec![column("id", "INT64", false, None), column("name", "STRING(100)", false, None)]),
|
||||
},
|
||||
columns: vec!["id".to_string(), "name".to_string()],
|
||||
source_columns: None,
|
||||
rows: vec![vec![json!(1), json!("old")]],
|
||||
dirty_rows: vec![(0, vec![(1, json!("Ada"))])],
|
||||
deleted_rows: vec![],
|
||||
new_rows: vec![],
|
||||
});
|
||||
|
||||
assert_eq!(no_reported_quote.validation_error, None);
|
||||
assert_eq!(no_reported_quote.statements, vec!["UPDATE `singers` SET `name` = 'Ada' WHERE `id` = 1;"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kingbase_mysql_compat_copy_insert_uses_backtick_identifiers() {
|
||||
let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::models::connection::DatabaseType;
|
||||
use crate::sql_dialect::{is_schema_aware, profile_for, qualified_table_name, quote_table_identifier};
|
||||
use crate::sql_dialect::{
|
||||
is_schema_aware, profile_for, qualified_table_name, quote_table_data_identifier, quote_table_identifier,
|
||||
uses_connection_identifier_quote,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||
@@ -87,6 +90,8 @@ pub struct DropObjectSqlOptions {
|
||||
pub name: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub signature: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub identifier_quote: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
@@ -99,6 +104,10 @@ pub struct TableAdminSqlOptions {
|
||||
pub table_name: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub cascade: Option<bool>,
|
||||
/// Quote character reported by the connected server, for types whose quote is not a property of
|
||||
/// the database type alone. See [`qualified_name_with_quote`].
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub identifier_quote: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
@@ -193,6 +202,8 @@ pub struct DuplicateTableStructureSqlOptions {
|
||||
pub table_comment: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub column_comments: Vec<DuplicateTableColumnComment>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub identifier_quote: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
@@ -219,6 +230,8 @@ pub struct CopyTableDataSqlOptions {
|
||||
pub sqlserver_identity_insert: bool,
|
||||
#[serde(default)]
|
||||
pub normalize_new_target_name: bool,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub identifier_quote: Option<String>,
|
||||
}
|
||||
|
||||
const MYSQL_COMPATIBLE_PROFILES: &[&str] = &["mysql", "mariadb", "tidb", "oceanbase", "custom_mysql"];
|
||||
@@ -384,13 +397,23 @@ pub fn build_drop_object_sql(options: DropObjectSqlOptions) -> String {
|
||||
format!(
|
||||
"DROP {} {}{};",
|
||||
object_type_keyword(options.object_type),
|
||||
qualified_name(options.database_type, options.schema.as_deref(), &options.name),
|
||||
qualified_name_with_quote(
|
||||
options.database_type,
|
||||
options.schema.as_deref(),
|
||||
&options.name,
|
||||
options.identifier_quote.as_deref(),
|
||||
),
|
||||
signature
|
||||
)
|
||||
}
|
||||
|
||||
pub fn build_drop_table_sql(options: TableAdminSqlOptions) -> String {
|
||||
let table = qualified_name(options.database_type, options.schema.as_deref(), &options.table_name);
|
||||
let table = qualified_name_with_quote(
|
||||
options.database_type,
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
options.identifier_quote.as_deref(),
|
||||
);
|
||||
if matches!(options.database_type, Some(DatabaseType::Iotdb)) {
|
||||
return format!("DELETE TIMESERIES {};", iotdb_timeseries_pattern(&table));
|
||||
} else if matches!(options.database_type, Some(DatabaseType::InfluxDb)) {
|
||||
@@ -499,10 +522,17 @@ pub fn build_drop_table_child_object_sql(options: DropTableChildObjectSqlOptions
|
||||
}
|
||||
|
||||
pub fn build_empty_table_sql(options: TableAdminSqlOptions) -> String {
|
||||
let table = qualified_name(options.database_type, options.schema.as_deref(), &options.table_name);
|
||||
let table = qualified_name_with_quote(
|
||||
options.database_type,
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
options.identifier_quote.as_deref(),
|
||||
);
|
||||
match options.database_type {
|
||||
Some(DatabaseType::ClickHouse) => format!("ALTER TABLE {table} DELETE WHERE 1 = 1;"),
|
||||
Some(DatabaseType::Bigquery) => format!("DELETE FROM {table} WHERE TRUE;"),
|
||||
// BigQuery and Spanner both reject an unqualified `DELETE FROM t`; DML requires a
|
||||
// WHERE clause.
|
||||
Some(DatabaseType::Bigquery | DatabaseType::Spanner) => format!("DELETE FROM {table} WHERE TRUE;"),
|
||||
Some(
|
||||
DatabaseType::Cassandra
|
||||
| DatabaseType::Hive
|
||||
@@ -518,9 +548,18 @@ pub fn build_empty_table_sql(options: TableAdminSqlOptions) -> String {
|
||||
}
|
||||
|
||||
pub fn build_truncate_table_sql(options: TableAdminSqlOptions) -> String {
|
||||
let table = qualified_name(options.database_type, options.schema.as_deref(), &options.table_name);
|
||||
let table = qualified_name_with_quote(
|
||||
options.database_type,
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
options.identifier_quote.as_deref(),
|
||||
);
|
||||
if matches!(options.database_type, Some(DatabaseType::Iotdb)) {
|
||||
format!("DELETE FROM {};", iotdb_timeseries_pattern(&table))
|
||||
} else if matches!(options.database_type, Some(DatabaseType::Spanner)) {
|
||||
// Spanner has no TRUNCATE statement (it reports `Unknown statement`), and its DML
|
||||
// requires a WHERE clause. Unlike BigQuery it cannot use the TRUNCATE fallback.
|
||||
format!("DELETE FROM {table} WHERE TRUE;")
|
||||
} else if matches!(options.database_type, Some(DatabaseType::Sqlite | DatabaseType::DuckDb)) {
|
||||
format!("DELETE FROM {table};")
|
||||
} else {
|
||||
@@ -688,7 +727,12 @@ pub fn build_drop_schema_sql(options: SchemaNameSqlOptions) -> String {
|
||||
}
|
||||
|
||||
pub fn build_duplicate_table_structure_sql(options: DuplicateTableStructureSqlOptions) -> String {
|
||||
let source = qualified_name(options.database_type, options.schema.as_deref(), &options.source_name);
|
||||
let source = qualified_name_with_quote(
|
||||
options.database_type,
|
||||
options.schema.as_deref(),
|
||||
&options.source_name,
|
||||
options.identifier_quote.as_deref(),
|
||||
);
|
||||
let target =
|
||||
qualified_duplicate_target_name(options.database_type, options.schema.as_deref(), &options.target_name);
|
||||
let structure_sql =
|
||||
@@ -733,11 +777,21 @@ pub fn build_duplicate_table_structure_sql(options: DuplicateTableStructureSqlOp
|
||||
}
|
||||
|
||||
pub fn build_copy_table_data_sql(options: CopyTableDataSqlOptions) -> String {
|
||||
let source = qualified_name(options.database_type, options.schema.as_deref(), &options.source_name);
|
||||
let source = qualified_name_with_quote(
|
||||
options.database_type,
|
||||
options.schema.as_deref(),
|
||||
&options.source_name,
|
||||
options.identifier_quote.as_deref(),
|
||||
);
|
||||
let target = if options.normalize_new_target_name {
|
||||
qualified_duplicate_target_name(options.database_type, options.schema.as_deref(), &options.target_name)
|
||||
} else {
|
||||
qualified_name(options.database_type, options.schema.as_deref(), &options.target_name)
|
||||
qualified_name_with_quote(
|
||||
options.database_type,
|
||||
options.schema.as_deref(),
|
||||
&options.target_name,
|
||||
options.identifier_quote.as_deref(),
|
||||
)
|
||||
};
|
||||
let Some(columns) = options.columns.filter(|columns| !columns.is_empty()) else {
|
||||
return format!("INSERT INTO {target} SELECT * FROM {source};");
|
||||
@@ -931,6 +985,33 @@ fn qualified_name(database_type: Option<DatabaseType>, schema: Option<&str>, nam
|
||||
}
|
||||
}
|
||||
|
||||
/// Same as [`qualified_name`], but honours the quote character the connected server reported.
|
||||
///
|
||||
/// The static per-type mapping cannot describe a database whose quote depends on the connection.
|
||||
/// Cloud Spanner is the case that forces this: GoogleSQL quotes with backticks while its PostgreSQL
|
||||
/// dialect quotes with double quotes, the dialect is fixed when the database is created, and only
|
||||
/// the connected agent knows which one applies. Emitting the static answer produces admin SQL that
|
||||
/// is a syntax error for every PostgreSQL-dialect Spanner database.
|
||||
///
|
||||
/// Falls back to [`qualified_name`] whenever no connection quote applies, so every other database
|
||||
/// type — and Spanner itself before the agent has reported a quote — keeps its existing output.
|
||||
fn qualified_name_with_quote(
|
||||
database_type: Option<DatabaseType>,
|
||||
schema: Option<&str>,
|
||||
name: &str,
|
||||
identifier_quote: Option<&str>,
|
||||
) -> String {
|
||||
if !uses_connection_identifier_quote(database_type, identifier_quote) {
|
||||
return qualified_name(database_type, schema, name);
|
||||
}
|
||||
let quoted = |value: &str| quote_table_data_identifier(database_type, value, identifier_quote);
|
||||
if database_type.is_some_and(is_schema_aware) && schema.is_some_and(|schema| !schema.is_empty()) {
|
||||
format!("{}.{}", quoted(schema.unwrap()), quoted(name))
|
||||
} else {
|
||||
quoted(name)
|
||||
}
|
||||
}
|
||||
|
||||
fn qualified_duplicate_target_name(database_type: Option<DatabaseType>, schema: Option<&str>, name: &str) -> String {
|
||||
if database_type != Some(DatabaseType::Dameng) {
|
||||
return qualified_name(database_type, schema, name);
|
||||
@@ -1402,6 +1483,7 @@ mod tests {
|
||||
schema: Some("public".to_string()),
|
||||
table_name: "events".to_string(),
|
||||
cascade: None,
|
||||
identifier_quote: None,
|
||||
};
|
||||
assert_eq!(build_drop_table_sql(options.clone()), "DROP TABLE \"public\".\"events\";");
|
||||
assert_eq!(
|
||||
@@ -1410,6 +1492,7 @@ mod tests {
|
||||
schema: Some("public".to_string()),
|
||||
table_name: "events".to_string(),
|
||||
cascade: Some(true),
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"DROP TABLE \"public\".\"events\" CASCADE;"
|
||||
);
|
||||
@@ -1419,6 +1502,7 @@ mod tests {
|
||||
schema: None,
|
||||
table_name: "events".to_string(),
|
||||
cascade: Some(true),
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"DROP TABLE `events`;"
|
||||
);
|
||||
@@ -1430,6 +1514,7 @@ mod tests {
|
||||
schema: Some("public".to_string()),
|
||||
table_name: "events".to_string(),
|
||||
cascade: Some(true),
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"TRUNCATE TABLE \"public\".\"events\" CASCADE;"
|
||||
);
|
||||
@@ -1439,6 +1524,7 @@ mod tests {
|
||||
schema: None,
|
||||
table_name: "events".to_string(),
|
||||
cascade: Some(true),
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"TRUNCATE TABLE `events`;"
|
||||
);
|
||||
@@ -1448,6 +1534,7 @@ mod tests {
|
||||
schema: None,
|
||||
table_name: "PresetSubjectInfo".to_string(),
|
||||
cascade: None,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"ALTER TABLE `PresetSubjectInfo` DELETE WHERE 1 = 1;"
|
||||
);
|
||||
@@ -1457,6 +1544,7 @@ mod tests {
|
||||
schema: None,
|
||||
table_name: "PresetSubjectInfo".to_string(),
|
||||
cascade: None,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"TRUNCATE TABLE `PresetSubjectInfo`;"
|
||||
);
|
||||
@@ -1466,15 +1554,84 @@ mod tests {
|
||||
schema: None,
|
||||
table_name: "events".to_string(),
|
||||
cascade: None,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"DELETE FROM `events` WHERE TRUE;"
|
||||
);
|
||||
// Spanner rejects both a WHERE-less DELETE and TRUNCATE TABLE (`Unknown statement`),
|
||||
// so unlike BigQuery it must not fall through to the TRUNCATE default.
|
||||
assert_eq!(
|
||||
build_empty_table_sql(TableAdminSqlOptions {
|
||||
database_type: Some(DatabaseType::Spanner),
|
||||
schema: None,
|
||||
table_name: "events".to_string(),
|
||||
cascade: None,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"DELETE FROM `events` WHERE TRUE;"
|
||||
);
|
||||
assert_eq!(
|
||||
build_truncate_table_sql(TableAdminSqlOptions {
|
||||
database_type: Some(DatabaseType::Spanner),
|
||||
schema: None,
|
||||
table_name: "events".to_string(),
|
||||
cascade: None,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"DELETE FROM `events` WHERE TRUE;"
|
||||
);
|
||||
// A `public` schema means the PostgreSQL dialect, where backticks are a syntax error, so the
|
||||
// quote has to come from the connection rather than from the static per-type mapping.
|
||||
assert_eq!(
|
||||
build_truncate_table_sql(TableAdminSqlOptions {
|
||||
database_type: Some(DatabaseType::Spanner),
|
||||
schema: Some("public".to_string()),
|
||||
table_name: "events".to_string(),
|
||||
cascade: Some(true),
|
||||
identifier_quote: Some("\"".to_string()),
|
||||
}),
|
||||
"DELETE FROM \"public\".\"events\" WHERE TRUE;"
|
||||
);
|
||||
assert_eq!(
|
||||
build_empty_table_sql(TableAdminSqlOptions {
|
||||
database_type: Some(DatabaseType::Spanner),
|
||||
schema: Some("public".to_string()),
|
||||
table_name: "events".to_string(),
|
||||
cascade: None,
|
||||
identifier_quote: Some("\"".to_string()),
|
||||
}),
|
||||
"DELETE FROM \"public\".\"events\" WHERE TRUE;"
|
||||
);
|
||||
// GoogleSQL reports a backtick, which matches the static mapping.
|
||||
assert_eq!(
|
||||
build_truncate_table_sql(TableAdminSqlOptions {
|
||||
database_type: Some(DatabaseType::Spanner),
|
||||
schema: None,
|
||||
table_name: "OrderItems".to_string(),
|
||||
cascade: None,
|
||||
identifier_quote: Some("`".to_string()),
|
||||
}),
|
||||
"DELETE FROM `OrderItems` WHERE TRUE;"
|
||||
);
|
||||
// Before the agent has reported a quote the static GoogleSQL answer is the only one
|
||||
// available; this documents the fallback rather than a PostgreSQL-dialect expectation.
|
||||
assert_eq!(
|
||||
build_truncate_table_sql(TableAdminSqlOptions {
|
||||
database_type: Some(DatabaseType::Spanner),
|
||||
schema: Some("public".to_string()),
|
||||
table_name: "events".to_string(),
|
||||
cascade: None,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"DELETE FROM `public`.`events` WHERE TRUE;"
|
||||
);
|
||||
assert_eq!(
|
||||
build_empty_table_sql(TableAdminSqlOptions {
|
||||
database_type: Some(DatabaseType::Cassandra),
|
||||
schema: None,
|
||||
table_name: "events".to_string(),
|
||||
cascade: None,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"TRUNCATE TABLE \"events\";"
|
||||
);
|
||||
@@ -1484,6 +1641,7 @@ mod tests {
|
||||
schema: None,
|
||||
table_name: "events".to_string(),
|
||||
cascade: None,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"DELETE FROM \"events\";"
|
||||
);
|
||||
@@ -1493,6 +1651,7 @@ mod tests {
|
||||
schema: Some("root.test".to_string()),
|
||||
table_name: "DCU_101".to_string(),
|
||||
cascade: None,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"DELETE TIMESERIES root.test.DCU_101.*;"
|
||||
);
|
||||
@@ -1502,6 +1661,7 @@ mod tests {
|
||||
schema: Some("root.test".to_string()),
|
||||
table_name: "root.test.DCU_101".to_string(),
|
||||
cascade: None,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"DELETE FROM root.test.DCU_101.*;"
|
||||
);
|
||||
@@ -1511,6 +1671,7 @@ mod tests {
|
||||
schema: Some("root.test".to_string()),
|
||||
table_name: "DCU_101".to_string(),
|
||||
cascade: None,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"DELETE FROM root.test.DCU_101.*;"
|
||||
);
|
||||
@@ -1521,6 +1682,7 @@ mod tests {
|
||||
schema: None,
|
||||
table_name: "table_sample".to_string(),
|
||||
cascade: None,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"TRUNCATE TABLE `table_sample`;"
|
||||
);
|
||||
@@ -1530,6 +1692,7 @@ mod tests {
|
||||
schema: None,
|
||||
table_name: "table_sample".to_string(),
|
||||
cascade: None,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"TRUNCATE TABLE `table_sample`;"
|
||||
);
|
||||
@@ -1594,6 +1757,7 @@ mod tests {
|
||||
schema: Some("dbo".to_string()),
|
||||
name: "refresh_cache".to_string(),
|
||||
signature: None,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"DROP PROCEDURE [dbo].[refresh_cache];"
|
||||
);
|
||||
@@ -1604,6 +1768,7 @@ mod tests {
|
||||
schema: Some("public".to_string()),
|
||||
name: "calc".to_string(),
|
||||
signature: Some("integer, integer".to_string()),
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"DROP FUNCTION \"public\".\"calc\"(integer, integer);"
|
||||
);
|
||||
@@ -1771,6 +1936,7 @@ mod tests {
|
||||
target_name: "users_copy".to_string(),
|
||||
table_comment: None,
|
||||
column_comments: vec![],
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"CREATE TABLE `users_copy` LIKE `users`;"
|
||||
);
|
||||
@@ -1782,6 +1948,7 @@ mod tests {
|
||||
target_name: "users_copy".to_string(),
|
||||
table_comment: None,
|
||||
column_comments: vec![],
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"CREATE TABLE \"public\".\"users_copy\" (LIKE \"public\".\"users\" INCLUDING ALL);"
|
||||
);
|
||||
@@ -1793,6 +1960,7 @@ mod tests {
|
||||
target_name: "connection_test_copy".to_string(),
|
||||
table_comment: None,
|
||||
column_comments: vec![],
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"CREATE TABLE `dbx_demo`.`connection_test_copy` LIKE `dbx_demo`.`connection_test`;"
|
||||
);
|
||||
@@ -1804,6 +1972,7 @@ mod tests {
|
||||
target_name: "orders_copy".to_string(),
|
||||
table_comment: None,
|
||||
column_comments: vec![],
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"CREATE TABLE `dbx_demo`.`orders_copy` LIKE `dbx_demo`.`orders`;"
|
||||
);
|
||||
@@ -1815,6 +1984,7 @@ mod tests {
|
||||
target_name: "customer_orders_copy".to_string(),
|
||||
table_comment: Some(" Customer's orders; archive ".to_string()),
|
||||
column_comments: vec![],
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"CREATE TABLE \"public\".\"customer_orders_copy\" (LIKE \"public\".\"customer_orders\" INCLUDING ALL);\nCOMMENT ON TABLE \"public\".\"customer_orders_copy\" IS ' Customer''s orders; archive ';"
|
||||
);
|
||||
@@ -1826,6 +1996,7 @@ mod tests {
|
||||
target_name: "users_copy".to_string(),
|
||||
table_comment: None,
|
||||
column_comments: vec![],
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"CREATE TABLE \"public\".\"users_copy\" (LIKE \"public\".\"users\" INCLUDING ALL);"
|
||||
);
|
||||
@@ -1837,6 +2008,7 @@ mod tests {
|
||||
target_name: "users_copy".to_string(),
|
||||
table_comment: None,
|
||||
column_comments: vec![],
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"SELECT TOP 0 * INTO [dbo].[users_copy] FROM [dbo].[users];"
|
||||
);
|
||||
@@ -1848,6 +2020,7 @@ mod tests {
|
||||
target_name: "USERS_COPY".to_string(),
|
||||
table_comment: None,
|
||||
column_comments: vec![],
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"CREATE TABLE \"HR\".\"USERS_COPY\" AS SELECT * FROM \"HR\".\"USERS\" WHERE 1=0"
|
||||
);
|
||||
@@ -1865,6 +2038,7 @@ mod tests {
|
||||
DuplicateTableColumnComment { name: "STATUS".to_string(), comment: "active ".to_string() },
|
||||
DuplicateTableColumnComment { name: "EMPTY".to_string(), comment: " \t\n".to_string() },
|
||||
],
|
||||
identifier_quote: None,
|
||||
});
|
||||
assert_eq!(
|
||||
dameng_sql,
|
||||
@@ -1887,6 +2061,7 @@ mod tests {
|
||||
target_name: "users_copy".to_string(),
|
||||
table_comment: Some("line1\\path\nline2".to_string()),
|
||||
column_comments: vec![],
|
||||
identifier_quote: None,
|
||||
});
|
||||
assert_eq!(
|
||||
dameng_escape_sql,
|
||||
@@ -1901,6 +2076,7 @@ mod tests {
|
||||
target_name: "UsersCopy".to_string(),
|
||||
table_comment: None,
|
||||
column_comments: vec![],
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"CREATE TABLE \"APP\".\"UsersCopy\" AS SELECT * FROM \"APP\".\"USERS\" WHERE 1=0"
|
||||
);
|
||||
@@ -1918,6 +2094,7 @@ mod tests {
|
||||
target_name: "copy".to_string(),
|
||||
table_comment: Some("owner\\'s; archive".to_string()),
|
||||
column_comments: vec![],
|
||||
identifier_quote: None,
|
||||
});
|
||||
let expected_literal = if database_type == DatabaseType::Redshift {
|
||||
"'owner\\\\\\'s; archive'"
|
||||
@@ -1941,6 +2118,7 @@ mod tests {
|
||||
target_name: "tb_a_copy".to_string(),
|
||||
table_comment: None,
|
||||
column_comments: vec![],
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"CREATE TABLE \"SQLUSER\".\"tb_a_copy\" AS SELECT * FROM \"SQLUSER\".\"tb_a\" WHERE 1=0"
|
||||
);
|
||||
@@ -1952,6 +2130,7 @@ mod tests {
|
||||
target_name: "users_copy".to_string(),
|
||||
table_comment: Some("ignored by QuestDB".to_string()),
|
||||
column_comments: vec![],
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"CREATE TABLE `users_copy` (LIKE `users`);"
|
||||
);
|
||||
@@ -1969,6 +2148,7 @@ mod tests {
|
||||
postgres_overriding_system_value: false,
|
||||
sqlserver_identity_insert: false,
|
||||
normalize_new_target_name: false,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"INSERT INTO \"users_copy\" SELECT * FROM \"users\";"
|
||||
);
|
||||
@@ -1982,6 +2162,7 @@ mod tests {
|
||||
postgres_overriding_system_value: false,
|
||||
sqlserver_identity_insert: false,
|
||||
normalize_new_target_name: false,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"INSERT INTO `users_copy` (`id`, `name`) SELECT `id`, `name` FROM `users`;"
|
||||
);
|
||||
@@ -1995,6 +2176,7 @@ mod tests {
|
||||
postgres_overriding_system_value: true,
|
||||
sqlserver_identity_insert: false,
|
||||
normalize_new_target_name: false,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"INSERT INTO \"public\".\"users_copy\" (\"id\", \"name\") OVERRIDING SYSTEM VALUE SELECT \"id\", \"name\" FROM \"public\".\"users\";"
|
||||
);
|
||||
@@ -2008,6 +2190,7 @@ mod tests {
|
||||
postgres_overriding_system_value: false,
|
||||
sqlserver_identity_insert: true,
|
||||
normalize_new_target_name: false,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"SET IDENTITY_INSERT [dbo].[users_copy] ON;\nINSERT INTO [dbo].[users_copy] ([id], [name]) SELECT [id], [name] FROM [dbo].[users];\nSET IDENTITY_INSERT [dbo].[users_copy] OFF;"
|
||||
);
|
||||
@@ -2021,6 +2204,7 @@ mod tests {
|
||||
postgres_overriding_system_value: false,
|
||||
sqlserver_identity_insert: false,
|
||||
normalize_new_target_name: true,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"INSERT INTO \"APP\".USERS_COPY SELECT * FROM \"APP\".\"users\";"
|
||||
);
|
||||
@@ -2034,6 +2218,7 @@ mod tests {
|
||||
postgres_overriding_system_value: false,
|
||||
sqlserver_identity_insert: false,
|
||||
normalize_new_target_name: false,
|
||||
identifier_quote: None,
|
||||
}),
|
||||
"INSERT INTO \"APP\".\"users_copy\" SELECT * FROM \"APP\".\"users\";"
|
||||
);
|
||||
|
||||
@@ -577,6 +577,8 @@ pub enum DatabaseType {
|
||||
Cassandra,
|
||||
#[serde(rename = "bigquery")]
|
||||
Bigquery,
|
||||
#[serde(rename = "spanner")]
|
||||
Spanner,
|
||||
Kylin,
|
||||
Sundb,
|
||||
Oscar,
|
||||
@@ -929,12 +931,22 @@ impl ConnectionConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/// Cloud Spanner schema changes are long-running operations rather than plain
|
||||
/// statements. Measured against the real service, `CREATE INDEX` on an *empty*
|
||||
/// table took 22.9-33.6s over seven runs, so the generic default fails
|
||||
/// intermittently — and the failure is misleading, because the operation keeps
|
||||
/// running server-side and completes, leaving a retry to report
|
||||
/// `Duplicate name in schema`. Raising a floor mirrors what
|
||||
/// `connection::agent_connect_timeout` already does for Access.
|
||||
const SPANNER_MIN_QUERY_TIMEOUT_SECS: u64 = 120;
|
||||
|
||||
pub fn effective_query_timeout_secs(&self) -> u64 {
|
||||
if self.query_timeout_secs == 0 {
|
||||
0
|
||||
} else {
|
||||
self.query_timeout_secs.max(1)
|
||||
// An explicit 0 is the UI's "no limit"; a floor must not impose one.
|
||||
return 0;
|
||||
}
|
||||
let floor = if self.db_type == DatabaseType::Spanner { Self::SPANNER_MIN_QUERY_TIMEOUT_SECS } else { 1 };
|
||||
self.query_timeout_secs.max(floor)
|
||||
}
|
||||
|
||||
pub fn effective_database(&self) -> Option<&str> {
|
||||
@@ -1176,6 +1188,7 @@ impl ConnectionConfig {
|
||||
DatabaseType::Neo4j => format!("neo4j://{host}:{port}{db_part}"),
|
||||
DatabaseType::Cassandra => format!("cassandra://{host}:{port}{db_part}"),
|
||||
DatabaseType::Bigquery => format!("bigquery://{host}/{db_part}"),
|
||||
DatabaseType::Spanner => self.spanner_display_url(&host, port),
|
||||
DatabaseType::Kylin => format!("kylin://{host}:{port}{db_part}"),
|
||||
DatabaseType::Sundb => format!("sundb://{host}:{port}{db_part}"),
|
||||
DatabaseType::Oscar => format!("oscar://{host}:{port}{db_part}"),
|
||||
@@ -1426,6 +1439,9 @@ impl ConnectionConfig {
|
||||
DatabaseType::Bigquery => {
|
||||
format!("bigquery://{}:{}@{host}/{db_part}", username, password)
|
||||
}
|
||||
// Spanner authenticates through driver properties (`credentials=…`), never
|
||||
// through username/password, so the display URL carries neither.
|
||||
DatabaseType::Spanner => self.spanner_display_url(&host, port),
|
||||
DatabaseType::Kylin => {
|
||||
format!("kylin://{}:{}@{host}:{port}{db_part}", username, password)
|
||||
}
|
||||
@@ -1480,6 +1496,25 @@ impl ConnectionConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/// Display-only URL for Cloud Spanner connections.
|
||||
///
|
||||
/// Spanner is agent-backed: the real JDBC URL is assembled inside the Spanner
|
||||
/// agent from the structured `ConnectParams`, so this string is used purely for
|
||||
/// the sidebar / connection card / logs. `database` holds the full resource path
|
||||
/// `projects/{p}/instances/{i}/databases/{d}`, which the shared
|
||||
/// [`encode_url_part`] would mangle into `projects%2Fmy%2Dproject%2F…` because it
|
||||
/// escapes every non-alphanumeric byte. [`encode_spanner_resource_path`] keeps the
|
||||
/// separators and unreserved characters readable instead. `host` is empty for real
|
||||
/// Cloud Spanner and only set for the emulator or a custom endpoint.
|
||||
fn spanner_display_url(&self, host: &str, port: u16) -> String {
|
||||
let path = self.effective_database().map(encode_spanner_resource_path).unwrap_or_default();
|
||||
if host.is_empty() {
|
||||
format!("spanner:///{path}")
|
||||
} else {
|
||||
format!("spanner://{host}:{port}/{path}")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sqlserver_port_explicit(&self) -> bool {
|
||||
if self.db_type != DatabaseType::SqlServer {
|
||||
return false;
|
||||
@@ -2402,6 +2437,30 @@ fn encode_url_part(value: &str) -> String {
|
||||
utf8_percent_encode(value, NON_ALPHANUMERIC).to_string()
|
||||
}
|
||||
|
||||
/// Percent-encode set for Spanner resource paths: everything [`NON_ALPHANUMERIC`]
|
||||
/// escapes, minus the RFC 3986 *unreserved* characters, which must stay literal.
|
||||
///
|
||||
/// `-` matters most in practice: GCP project and instance IDs allow only lowercase
|
||||
/// letters, digits and hyphens, so without this a hyphenated project would render as
|
||||
/// `my%2Dproject` in every Spanner connection.
|
||||
const SPANNER_PATH_ENCODE_SET: &percent_encoding::AsciiSet =
|
||||
&NON_ALPHANUMERIC.remove(b'-').remove(b'_').remove(b'.').remove(b'~');
|
||||
|
||||
/// Percent-encode a Spanner resource path, keeping the `/` separators and the RFC 3986
|
||||
/// unreserved characters literal while still escaping everything else (spaces become
|
||||
/// `%20`).
|
||||
///
|
||||
/// Display-only. Native drivers must keep using [`encode_url_part`] — their URLs are
|
||||
/// real DSNs, where an unescaped `/` or `-` in a database name can change how the
|
||||
/// connection string parses.
|
||||
fn encode_spanner_resource_path(value: &str) -> String {
|
||||
value
|
||||
.split('/')
|
||||
.map(|segment| utf8_percent_encode(segment, SPANNER_PATH_ENCODE_SET).to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join("/")
|
||||
}
|
||||
|
||||
fn bracket_ipv6(host: &str) -> String {
|
||||
if host.contains(':') && !host.starts_with('[') {
|
||||
format!("[{host}]")
|
||||
@@ -2698,6 +2757,32 @@ mod tests {
|
||||
assert_eq!(config.connection_url(), "postgres://root:secret@10.1.2.3:2883/%20analytics%20?sslmode=prefer");
|
||||
}
|
||||
|
||||
/// Spanner keeps the full resource path in `database`. Its URL is display-only (the
|
||||
/// agent builds the real JDBC URL), so the separators and the RFC 3986 unreserved
|
||||
/// characters must stay literal instead of being mangled into
|
||||
/// `projects%2Fmy%2Dproject%2F…` by the shared `encode_url_part`. Hyphens matter in
|
||||
/// practice: GCP project and instance IDs allow only lowercase letters, digits and
|
||||
/// hyphens. Characters that genuinely need escaping (here a space) still are.
|
||||
#[test]
|
||||
fn spanner_display_url_keeps_resource_path_separators() {
|
||||
let mut config = mysql_config("", "", Some("projects/my-project/instances/my-instance/databases/my db"));
|
||||
config.db_type = DatabaseType::Spanner;
|
||||
config.host = String::new();
|
||||
config.port = 443;
|
||||
|
||||
assert_eq!(config.connection_url(), "spanner:///projects/my-project/instances/my-instance/databases/my%20db");
|
||||
// Display and redacted forms match: Spanner carries no credentials in the URL.
|
||||
assert_eq!(config.redacted_connection_url(), config.connection_url());
|
||||
|
||||
// Emulator / custom endpoint: the host and port are shown.
|
||||
config.host = "localhost".to_string();
|
||||
config.port = 9010;
|
||||
assert_eq!(
|
||||
config.connection_url(),
|
||||
"spanner://localhost:9010/projects/my-project/instances/my-instance/databases/my%20db"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn whitespace_only_database_uses_database_type_default() {
|
||||
let mut config = mysql_config("root", "secret", Some(" "));
|
||||
@@ -3002,6 +3087,45 @@ mod tests {
|
||||
assert_eq!(config.effective_query_timeout_secs(), 3600);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spanner_query_timeout_floor_covers_schema_change_latency() {
|
||||
// A Cloud Spanner CREATE INDEX is a long-running operation: measured against
|
||||
// the real service it took 22.9-33.6s on an empty table, so the generic
|
||||
// desktop default of 30s fails intermittently.
|
||||
let mut config = mysql_config("root", "", None);
|
||||
config.db_type = DatabaseType::Spanner;
|
||||
config.query_timeout_secs = 30;
|
||||
|
||||
assert_eq!(config.effective_query_timeout_secs(), 120);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spanner_query_timeout_floor_never_lowers_a_higher_setting() {
|
||||
let mut config = mysql_config("root", "", None);
|
||||
config.db_type = DatabaseType::Spanner;
|
||||
config.query_timeout_secs = 600;
|
||||
|
||||
assert_eq!(config.effective_query_timeout_secs(), 600);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spanner_query_timeout_floor_respects_the_no_limit_setting() {
|
||||
// 0 is the UI's "unlimited"; a floor must not turn that into a 120s cap.
|
||||
let mut config = mysql_config("root", "", None);
|
||||
config.db_type = DatabaseType::Spanner;
|
||||
config.query_timeout_secs = 0;
|
||||
|
||||
assert_eq!(config.effective_query_timeout_secs(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn query_timeout_floor_is_scoped_to_spanner() {
|
||||
let mut config = mysql_config("root", "", None);
|
||||
config.query_timeout_secs = 30;
|
||||
|
||||
assert_eq!(config.effective_query_timeout_secs(), 30);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_url_encodes_proxy_style_username_with_colons_and_at() {
|
||||
let config = mysql_config("1234:xxx@xx.xx:abc123", "p@ss:word", None);
|
||||
|
||||
@@ -426,6 +426,9 @@ fn schema_first_qualifier_type(db_type: &DatabaseType) -> bool {
|
||||
| DatabaseType::PrestoSql
|
||||
| DatabaseType::Databricks
|
||||
| DatabaseType::Bigquery
|
||||
// A Spanner JDBC connection is bound to a single database, so there is no
|
||||
// cross-database `db.table` syntax — the first part of `a.b` is always a schema.
|
||||
| DatabaseType::Spanner
|
||||
)
|
||||
}
|
||||
|
||||
@@ -768,6 +771,18 @@ mod tests {
|
||||
assert!(!targets_production_database(&sqlserver, "staging", "DELETE FROM prod_app.users WHERE id = 1"));
|
||||
}
|
||||
|
||||
/// A Spanner JDBC connection is bound to exactly one database, so `a.b` can only be
|
||||
/// `schema.table`. Treating `a` as a database name would match a database that cannot
|
||||
/// be addressed from this connection at all.
|
||||
#[test]
|
||||
fn resolves_spanner_qualifiers_as_schema_not_database() {
|
||||
let mut spanner = config();
|
||||
spanner.db_type = DatabaseType::Spanner;
|
||||
|
||||
assert!(!targets_production_database(&spanner, "staging", "DELETE FROM prod_app.users WHERE TRUE"));
|
||||
assert!(targets_production_database(&spanner, "prod_app", "DELETE FROM public.users WHERE TRUE"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detects_cross_database_mongo_aggregate_write_targets() {
|
||||
let mut mongo = config();
|
||||
|
||||
@@ -646,7 +646,9 @@ impl DbOperationBudget {
|
||||
}
|
||||
|
||||
pub fn from_connection_config(config: &ConnectionConfig) -> Self {
|
||||
Self::from_config(config.effective_connect_timeout_secs(), Some(config.query_timeout_secs))
|
||||
// effective_query_timeout_secs, not the raw field: it preserves 0 ("no limit")
|
||||
// and applies the per-database-type floor Cloud Spanner needs for DDL.
|
||||
Self::from_config(config.effective_connect_timeout_secs(), Some(config.effective_query_timeout_secs()))
|
||||
}
|
||||
|
||||
/// Use global default values (when no connection config is available).
|
||||
|
||||
@@ -63,6 +63,11 @@ pub fn is_schema_aware(database_type: DatabaseType) -> bool {
|
||||
| DatabaseType::Sqlite
|
||||
| DatabaseType::DuckDb
|
||||
| DatabaseType::Iris
|
||||
// Spanner supports named schemas; the PostgreSQL dialect defaults to `public`.
|
||||
// GoogleSQL's default schema is the empty string, which the blank-schema filters
|
||||
// in `qualified_table_name` / `table_data_qualified_table_name` drop along with
|
||||
// the dot separator (`` `s`.`t` `` with an empty `s` is a Spanner syntax error).
|
||||
| DatabaseType::Spanner
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -670,9 +670,8 @@ pub fn profile_for(db_type: DatabaseType) -> DdlDialectProfile {
|
||||
ClickHouse => clickhouse_profile(),
|
||||
|
||||
DuckDb | Questdb | SapHana | Teradata | Snowflake | Trino | PrestoSql | Hive | Kyuubi | Impala | Spark
|
||||
| Db2 | Informix | Bigquery | Kylin | Ignite | Ignite3 | Oscar | Tdengine | Iotdb | Databricks | Jdbc => {
|
||||
conservative_ansi(db_type)
|
||||
}
|
||||
| Db2 | Informix | Bigquery | Spanner | Kylin | Ignite | Ignite3 | Oscar | Tdengine | Iotdb | Databricks
|
||||
| Jdbc => conservative_ansi(db_type),
|
||||
|
||||
// Non-tabular / not applicable for relational CREATE TABLE
|
||||
Redis | MongoDb | DynamoDb | Elasticsearch | Easysearch | Meilisearch | Qdrant | Milvus | Weaviate
|
||||
|
||||
@@ -136,6 +136,11 @@ pub fn quote_table_identifier(database_type: Option<DatabaseType>, name: &str) -
|
||||
| DatabaseType::Tdengine
|
||||
| DatabaseType::Access
|
||||
| DatabaseType::Bigquery
|
||||
// GoogleSQL (Spanner's default dialect) quotes identifiers with backticks;
|
||||
// double quotes are string literals there, so `SELECT * FROM "users"` is a
|
||||
// syntax error. PostgreSQL-dialect Spanner databases override this through
|
||||
// the identifier quote reported by the agent on connect.
|
||||
| DatabaseType::Spanner
|
||||
| DatabaseType::Questdb,
|
||||
) => {
|
||||
format!("`{}`", name.replace('`', "``"))
|
||||
|
||||
@@ -385,6 +385,11 @@ pub(crate) fn uses_connection_identifier_quote(
|
||||
identifier_quote: Option<&str>,
|
||||
) -> bool {
|
||||
database_type == Some(DatabaseType::Kingbase)
|
||||
// Spanner is dual-dialect: GoogleSQL uses backticks, the PostgreSQL dialect uses
|
||||
// double quotes, and only the connected agent knows which. Unconditional like
|
||||
// Kingbase — when no quote was reported the callers fall back to
|
||||
// `quote_table_identifier`, whose static mapping is GoogleSQL-correct.
|
||||
|| database_type == Some(DatabaseType::Spanner)
|
||||
|| (database_type == Some(DatabaseType::Informix) && identifier_quote.is_some())
|
||||
|| (matches!(database_type, Some(DatabaseType::Gaussdb | DatabaseType::OpenGauss | DatabaseType::Postgres))
|
||||
&& identifier_quote.is_some())
|
||||
|
||||
@@ -34,6 +34,37 @@ fn quotes_identifiers_by_database_type() {
|
||||
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");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Spanner), "user`name"), "`user``name`");
|
||||
}
|
||||
|
||||
/// Spanner databases are created in one of two immutable dialects. The connected
|
||||
/// agent reports the correct identifier quote; when it is missing the static mapping
|
||||
/// must fall back to GoogleSQL (backticks), because GoogleSQL treats double quotes as
|
||||
/// string literals and would reject `SELECT * FROM "users"`.
|
||||
#[test]
|
||||
fn quotes_spanner_identifiers_by_connection_dialect() {
|
||||
// GoogleSQL dialect: agent reports a backtick.
|
||||
assert_eq!(quote_table_data_identifier(Some(DatabaseType::Spanner), "order", Some("`")), "`order`");
|
||||
assert_eq!(quote_table_data_identifier(Some(DatabaseType::Spanner), "user`name", Some("`")), "`user``name`");
|
||||
|
||||
// PostgreSQL dialect: agent reports a double quote.
|
||||
assert_eq!(quote_table_data_identifier(Some(DatabaseType::Spanner), "order", Some("\"")), "\"order\"");
|
||||
assert_eq!(quote_table_data_identifier(Some(DatabaseType::Spanner), "user\"name", Some("\"")), "\"user\"\"name\"");
|
||||
|
||||
// No quote reported (metadata probe failed / caller outside the desktop store):
|
||||
// fall back to the GoogleSQL default rather than the ANSI double quote.
|
||||
assert_eq!(quote_table_data_identifier(Some(DatabaseType::Spanner), "order", None), "`order`");
|
||||
|
||||
// GoogleSQL's default schema is the empty string and must not produce `` ``.`t` ``,
|
||||
// which Spanner rejects with `Invalid empty identifier`.
|
||||
assert_eq!(
|
||||
table_data_qualified_table_name(Some(DatabaseType::Spanner), Some(""), "singers", Some("`")),
|
||||
"`singers`"
|
||||
);
|
||||
assert_eq!(
|
||||
table_data_qualified_table_name(Some(DatabaseType::Spanner), Some("public"), "singers", Some("\"")),
|
||||
"\"public\".\"singers\""
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -82,6 +113,11 @@ fn qualifies_schema_only_for_schema_aware_databases() {
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::Informix), Some("xtdpcky"), "users"), "xtdpcky.users");
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::Sqlite), Some("analytics"), "users"), "\"analytics\".\"users\"");
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::Jdbc), Some("cbsdw_dwd"), "dwd_test_df"), "dwd_test_df");
|
||||
// GoogleSQL's default schema is the empty string: the qualifier (and its dot) must be
|
||||
// dropped entirely, otherwise Spanner reports `Invalid empty identifier`.
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::Spanner), Some(""), "users"), "`users`");
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::Spanner), None, "users"), "`users`");
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::Spanner), Some("public"), "users"), "`public`.`users`");
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::Iotdb), Some("root.test"), "device2"), "root.test.device2");
|
||||
assert_eq!(
|
||||
qualified_table_name(Some(DatabaseType::Iotdb), Some("root.test"), "root.test.device2"),
|
||||
@@ -127,6 +163,8 @@ fn maps_table_pagination_strategy_by_database_type() {
|
||||
TablePaginationStrategy::Unbounded
|
||||
);
|
||||
assert_eq!(table_pagination_strategy(Some(DatabaseType::Jdbc)), TablePaginationStrategy::AgentMaxRows);
|
||||
// Both Spanner dialects support `LIMIT n OFFSET m`; pin the fallback.
|
||||
assert_eq!(table_pagination_strategy(Some(DatabaseType::Spanner)), TablePaginationStrategy::LimitOffset);
|
||||
assert_eq!(table_pagination_strategy(None), TablePaginationStrategy::LimitOffset);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user