feat(spanner): add Google Cloud Spanner support

This commit is contained in:
Frost authored and GitHub committed 2026-08-20 16:33:09 +08:00
1 parent 2cc8d78e64
commit f494f90c30
74 files changed
+2920 -53

No files matched your search

+127 -3
View File
@@ -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);