fix(oracle): preserve jdbc descriptors
This commit is contained in:
@@ -141,6 +141,30 @@ function parseJdbcSqlServerUrl(source: string): ParsedConnectionUrl | null {
|
||||
}
|
||||
|
||||
function parseJdbcOracleUrl(source: string): ParsedConnectionUrl | null {
|
||||
const descriptorMatch = source.match(/^jdbc:oracle:thin:@\s*\((.+)\)\s*$/i);
|
||||
if (descriptorMatch) {
|
||||
const profile = SCHEME_PROFILES.oracle;
|
||||
const host = oracleDescriptorValue(source, "HOST");
|
||||
const port = oracleDescriptorValue(source, "PORT");
|
||||
const serviceName = oracleDescriptorValue(source, "SERVICE_NAME");
|
||||
const sid = oracleDescriptorValue(source, "SID");
|
||||
if (!host) return null;
|
||||
return {
|
||||
dbType: profile.type,
|
||||
driverProfile: profile.profile,
|
||||
driverLabel: profile.label,
|
||||
host,
|
||||
port: port ? Number(port) : profile.defaultPort,
|
||||
username: "",
|
||||
password: "",
|
||||
database: serviceName || sid || undefined,
|
||||
urlParams: "",
|
||||
ssl: false,
|
||||
connectionString: source,
|
||||
oracleConnectionType: sid && !serviceName ? "sid" : "service_name",
|
||||
};
|
||||
}
|
||||
|
||||
const serviceMatch = source.match(/^jdbc:oracle:thin:@\/\/([^:/?#]+)(?::(\d+))?\/([^?]+)(?:\?(.*))?$/i);
|
||||
if (serviceMatch) {
|
||||
const profile = SCHEME_PROFILES.oracle;
|
||||
@@ -180,6 +204,11 @@ function parseJdbcOracleUrl(source: string): ParsedConnectionUrl | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
function oracleDescriptorValue(source: string, key: string): string | undefined {
|
||||
const match = new RegExp(`\\(${key}\\s*=\\s*([^\\)]+)\\)`, "i").exec(source);
|
||||
return match?.[1]?.trim();
|
||||
}
|
||||
|
||||
function parseJdbcUCanAccessUrl(source: string): ParsedConnectionUrl | null {
|
||||
const match = source.match(/^jdbc:ucanaccess:\/\/(.+?)(?:;.*)?$/i);
|
||||
if (!match) return null;
|
||||
|
||||
@@ -77,9 +77,13 @@ pub fn mongo_legacy_error_with_auth_hint(err: &str) -> String {
|
||||
}
|
||||
|
||||
fn oracle_jdbc_connection_string(config: &ConnectionConfig, host: &str, port: u16, database: &str) -> String {
|
||||
if let Some(connection_string) = config.connection_string.as_deref().filter(|value| !value.trim().is_empty()) {
|
||||
return crate::models::connection::rewrite_jdbc_url_host(connection_string.trim(), host, port);
|
||||
}
|
||||
|
||||
let database = database.trim();
|
||||
if database.is_empty() {
|
||||
return config.connection_string.as_deref().unwrap_or("").to_string();
|
||||
return String::new();
|
||||
}
|
||||
|
||||
if config.oracle_connection_type.as_deref() == Some("sid") {
|
||||
@@ -113,11 +117,21 @@ pub fn should_retry_oracle_with_10g_driver(config: &ConnectionConfig, err: &str)
|
||||
return false;
|
||||
}
|
||||
let normalized = err.to_lowercase();
|
||||
normalized.contains("ora-12541") || normalized.contains("no listener") || err.contains("没有监听程序")
|
||||
normalized.contains("ora-28040") || normalized.contains("no matching authentication protocol")
|
||||
}
|
||||
|
||||
pub fn oracle_alternate_connect_config(config: &ConnectionConfig, err: &str) -> Option<ConnectionConfig> {
|
||||
if !should_retry_oracle_with_10g_driver(config, err) {
|
||||
if config.db_type != DatabaseType::Oracle {
|
||||
return None;
|
||||
}
|
||||
if config.driver_profile.as_deref() == Some("oracle-10g") {
|
||||
return None;
|
||||
}
|
||||
if config.connection_string.as_deref().is_some_and(|value| !value.trim().is_empty()) {
|
||||
return None;
|
||||
}
|
||||
let normalized = err.to_lowercase();
|
||||
if !normalized.contains("ora-12505") && !normalized.contains("ora-12514") {
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -250,16 +264,45 @@ mod tests {
|
||||
assert_eq!(service["connection_string"], "jdbc:oracle:thin:@//oracle.example.com:1521/ORCL");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oracle_url_preserves_custom_jdbc_descriptor_and_rewrites_host_port() {
|
||||
let mut cfg = config(DatabaseType::Oracle, Some("ORCL"));
|
||||
cfg.host = "oracle.example.com".to_string();
|
||||
cfg.port = 1521;
|
||||
cfg.connection_string = Some(
|
||||
"jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oracle.example.com)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL)))"
|
||||
.to_string(),
|
||||
);
|
||||
|
||||
let params = agent_connect_params(&cfg, "127.0.0.1", 11521, "ORCL");
|
||||
|
||||
assert_eq!(
|
||||
params["connection_string"],
|
||||
"jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=11521))(CONNECT_DATA=(SERVICE_NAME=ORCL)))"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oracle_listener_errors_can_switch_descriptor() {
|
||||
let mut cfg = config(DatabaseType::Oracle, Some("ORCL"));
|
||||
cfg.driver_profile = Some("oracle".to_string());
|
||||
cfg.oracle_connection_type = Some("service_name".to_string());
|
||||
|
||||
let retry = oracle_alternate_connect_config(&cfg, "ORA-12541: TNS:no listener").unwrap();
|
||||
let retry = oracle_alternate_connect_config(&cfg, "ORA-12514: listener does not know service").unwrap();
|
||||
|
||||
assert_eq!(retry.oracle_connection_type.as_deref(), Some("sid"));
|
||||
assert!(oracle_alternate_connect_config(&retry, "ORA-01017: invalid username/password").is_none());
|
||||
assert!(oracle_alternate_connect_config(&cfg, "ORA-12541: TNS:no listener").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oracle_custom_connection_string_skips_alternate_descriptor_retry() {
|
||||
let mut cfg = config(DatabaseType::Oracle, Some("ORCL"));
|
||||
cfg.driver_profile = Some("oracle".to_string());
|
||||
cfg.oracle_connection_type = Some("service_name".to_string());
|
||||
cfg.connection_string = Some("jdbc:oracle:thin:@//oracle.example.com:1521/ORCL".to_string());
|
||||
|
||||
assert!(oracle_alternate_connect_config(&cfg, "ORA-12514: listener does not know service").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1081,8 +1081,12 @@ mod tests {
|
||||
config.db_type = DatabaseType::Oracle;
|
||||
config.driver_profile = Some("oracle".to_string());
|
||||
|
||||
assert!(should_retry_oracle_with_10g_driver(&config, "Agent RPC error (-1): ORA-12541: TNS:no listener"));
|
||||
assert!(should_retry_oracle_with_10g_driver(&config, "host xxx port 1521 中没有监听程序"));
|
||||
assert!(should_retry_oracle_with_10g_driver(
|
||||
&config,
|
||||
"Agent RPC error (-1): ORA-28040: No matching authentication protocol"
|
||||
));
|
||||
assert!(!should_retry_oracle_with_10g_driver(&config, "Agent RPC error (-1): ORA-12541: TNS:no listener"));
|
||||
assert!(!should_retry_oracle_with_10g_driver(&config, "host xxx port 1521 中没有监听程序"));
|
||||
|
||||
config.driver_profile = Some("oracle-10g".to_string());
|
||||
assert!(!should_retry_oracle_with_10g_driver(&config, "Agent RPC error (-1): ORA-12541: TNS:no listener"));
|
||||
@@ -1101,17 +1105,22 @@ mod tests {
|
||||
config.driver_profile = Some("oracle".to_string());
|
||||
config.oracle_connection_type = Some("service_name".to_string());
|
||||
|
||||
let retry = oracle_alternate_connect_config(&config, "Agent RPC error (-1): ORA-12541: TNS:no listener")
|
||||
.expect("listener errors should allow alternate descriptor retry");
|
||||
let retry = oracle_alternate_connect_config(
|
||||
&config,
|
||||
"Agent RPC error (-1): ORA-12514: listener does not currently know of service requested",
|
||||
)
|
||||
.expect("listener errors should allow alternate descriptor retry");
|
||||
assert_eq!(retry.driver_profile.as_deref(), Some("oracle"));
|
||||
assert_eq!(retry.oracle_connection_type.as_deref(), Some("sid"));
|
||||
|
||||
let service_retry = oracle_alternate_connect_config(
|
||||
&retry,
|
||||
"Agent RPC error (-1): ORA-12541: host xxx port 1521 中没有监听程序",
|
||||
"Agent RPC error (-1): ORA-12505: listener does not currently know of SID given",
|
||||
)
|
||||
.expect("SID listener errors should allow service-name retry");
|
||||
assert_eq!(service_retry.oracle_connection_type.as_deref(), Some("service_name"));
|
||||
|
||||
assert!(oracle_alternate_connect_config(&config, "ORA-12541: TNS:no listener").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1123,7 +1132,17 @@ mod tests {
|
||||
assert!(oracle_alternate_connect_config(&config, "ORA-01017: invalid username/password").is_none());
|
||||
|
||||
config.driver_profile = Some("oracle-10g".to_string());
|
||||
assert!(oracle_alternate_connect_config(&config, "ORA-12541: TNS:no listener").is_none());
|
||||
assert!(oracle_alternate_connect_config(&config, "ORA-12514: listener does not know service").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oracle_alternate_descriptor_retry_skips_custom_connection_strings() {
|
||||
let mut config = mysql_config(Some("ORCL"));
|
||||
config.db_type = DatabaseType::Oracle;
|
||||
config.driver_profile = Some("oracle".to_string());
|
||||
config.connection_string = Some("jdbc:oracle:thin:@//oracle.example.com:1521/ORCL".to_string());
|
||||
|
||||
assert!(oracle_alternate_connect_config(&config, "ORA-12514: listener does not know service").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -791,6 +791,11 @@ pub fn parse_jdbc_host_port(url: &str) -> Option<(String, u16)> {
|
||||
if let Some(after) = rest.strip_prefix("oracle:") {
|
||||
let at_pos = after.find('@')?;
|
||||
let after_at = &after[at_pos + 1..];
|
||||
if after_at.trim_start().starts_with('(') {
|
||||
let host = oracle_descriptor_value(after_at, "HOST")?;
|
||||
let port = oracle_descriptor_value(after_at, "PORT")?;
|
||||
return Some((host, port.parse().ok()?));
|
||||
}
|
||||
let after_at = after_at.strip_prefix("//").unwrap_or(after_at);
|
||||
let host_port = after_at.split(&['/', ':', '?'][..]).next()?;
|
||||
let port_str = after_at.strip_prefix(host_port)?.strip_prefix(':')?.split(&[':', '/', ';', '?'][..]).next()?;
|
||||
@@ -823,6 +828,14 @@ pub fn parse_jdbc_host_port(url: &str) -> Option<(String, u16)> {
|
||||
}
|
||||
|
||||
pub fn rewrite_jdbc_url_host(url: &str, new_host: &str, new_port: u16) -> String {
|
||||
let normalized_url = url.to_ascii_uppercase();
|
||||
if normalized_url.starts_with("JDBC:ORACLE:")
|
||||
&& normalized_url.contains("(HOST=")
|
||||
&& normalized_url.contains("(PORT=")
|
||||
{
|
||||
return rewrite_oracle_descriptor_host(url, new_host, new_port);
|
||||
}
|
||||
|
||||
let Some((old_host, old_port)) = parse_jdbc_host_port(url) else {
|
||||
return url.to_string();
|
||||
};
|
||||
@@ -831,6 +844,31 @@ pub fn rewrite_jdbc_url_host(url: &str, new_host: &str, new_port: u16) -> String
|
||||
url.replacen(&old_authority, &new_authority, 1)
|
||||
}
|
||||
|
||||
fn oracle_descriptor_value(descriptor: &str, key: &str) -> Option<String> {
|
||||
let key = format!("({key}=");
|
||||
let start = descriptor.to_ascii_uppercase().find(&key)?;
|
||||
let value_start = start + key.len();
|
||||
let value_end = descriptor[value_start..].find(')')? + value_start;
|
||||
Some(descriptor[value_start..value_end].trim().to_string())
|
||||
}
|
||||
|
||||
fn rewrite_oracle_descriptor_host(url: &str, new_host: &str, new_port: u16) -> String {
|
||||
let rewritten_host = replace_oracle_descriptor_value(url, "HOST", new_host);
|
||||
replace_oracle_descriptor_value(&rewritten_host, "PORT", &new_port.to_string())
|
||||
}
|
||||
|
||||
fn replace_oracle_descriptor_value(input: &str, key: &str, value: &str) -> String {
|
||||
let token = format!("({key}=");
|
||||
let Some(start) = input.to_ascii_uppercase().find(&token) else {
|
||||
return input.to_string();
|
||||
};
|
||||
let value_start = start + token.len();
|
||||
let Some(value_end) = input[value_start..].find(')').map(|offset| value_start + offset) else {
|
||||
return input.to_string();
|
||||
};
|
||||
format!("{}{}{}", &input[..value_start], value, &input[value_end..])
|
||||
}
|
||||
|
||||
fn encode_url_part(value: &str) -> String {
|
||||
utf8_percent_encode(value, NON_ALPHANUMERIC).to_string()
|
||||
}
|
||||
@@ -1513,6 +1551,27 @@ mod tests {
|
||||
assert_eq!(p, 1521);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_jdbc_host_port_oracle_descriptor() {
|
||||
let (h, p) = super::parse_jdbc_host_port(
|
||||
"jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=orahost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)))",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(h, "orahost");
|
||||
assert_eq!(p, 1521);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rewrite_jdbc_url_host_oracle_descriptor() {
|
||||
let url =
|
||||
"jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=orahost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)))";
|
||||
|
||||
assert_eq!(
|
||||
super::rewrite_jdbc_url_host(url, "127.0.0.1", 11521),
|
||||
"jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=11521))(CONNECT_DATA=(SERVICE_NAME=orcl)))"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_jdbc_host_port_sqlserver() {
|
||||
let (h, p) = super::parse_jdbc_host_port("jdbc:sqlserver://mshost:1433;databaseName=master").unwrap();
|
||||
|
||||
@@ -134,6 +134,20 @@ test("parses Oracle JDBC SID URLs", () => {
|
||||
assert.equal(parsed.oracleConnectionType, "sid");
|
||||
});
|
||||
|
||||
test("parses Oracle JDBC descriptors and keeps the original connection string", () => {
|
||||
const source =
|
||||
"jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oracle.example.com)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)))";
|
||||
const parsed = parseConnectionUrl(source);
|
||||
|
||||
assert.equal(parsed.dbType, "oracle");
|
||||
assert.equal(parsed.driverProfile, "oracle");
|
||||
assert.equal(parsed.host, "oracle.example.com");
|
||||
assert.equal(parsed.port, 1521);
|
||||
assert.equal(parsed.database, "orcl");
|
||||
assert.equal(parsed.oracleConnectionType, "service_name");
|
||||
assert.equal(parsed.connectionString, source);
|
||||
});
|
||||
|
||||
test("keeps MongoDB URLs as connection strings", () => {
|
||||
const source = "mongodb+srv://reader:secret@cluster.example.com/app?retryWrites=true";
|
||||
const parsed = parseConnectionUrl(source);
|
||||
|
||||
Reference in New Issue
Block a user