fix(neo4j): pick node identity function by server version

This commit is contained in:
zipg authored and GitHub committed 2026-09-28 23:01:49 +08:00
1 parent 9bb52c198f
commit 625d84bb4a
25 files changed
+461 -30

No files matched your search

@@ -873,6 +873,38 @@ pub(super) fn build_db2_table_select_page_sql(
)
}
/// Neo4j 5.0 replaced `id()` with `elementId()`, and servers before that only know `id()`, so the
/// generated Cypher has to pick the spelling the connected server accepts. An unknown version keeps
/// `elementId()`, which is what every caller without connection metadata got before.
pub fn neo4j_element_id_function(server_version: Option<&str>) -> &'static str {
match neo4j_major_version(server_version) {
Some(major) if major < NEO4J_ELEMENT_ID_MIN_MAJOR_VERSION => NEO4J_LEGACY_ELEMENT_ID_FUNCTION,
_ => "elementId",
}
}
/// The identity function Neo4j used before 5.0. Unlike `elementId()`, which returns a string, it
/// returns the node's internal `Integer` id, so callers comparing a value read from a grid have to
/// compare numbers.
pub const NEO4J_LEGACY_ELEMENT_ID_FUNCTION: &str = "id";
/// Extracts the leading `major` from version strings such as `4.4.44`, `Neo4j/5.26.0` or
/// `Neo4j/2025.01.0`. The product name itself contains a digit ("Neo4j"), so only a token that
/// *starts* with digits counts as the version. Anything without one is treated as unknown.
fn neo4j_major_version(server_version: Option<&str>) -> Option<u32> {
let version = server_version?.trim();
version.split(|character: char| !character.is_ascii_alphanumeric()).find_map(|token| {
let digits: String = token.chars().take_while(char::is_ascii_digit).collect();
if digits.is_empty() {
None
} else {
digits.parse().ok()
}
})
}
const NEO4J_ELEMENT_ID_MIN_MAJOR_VERSION: u32 = 5;
pub(super) fn build_neo4j_table_select_sql(options: &TableDataSelectSqlOptions, limit: usize) -> String {
let label = quote_table_identifier(Some(DatabaseType::Neo4j), &options.table_name);
let predicate = normalize_where_input(options.where_input.as_deref());
@@ -891,7 +923,8 @@ pub(super) fn build_neo4j_table_select_sql(options: &TableDataSelectSqlOptions,
.join(", ")
};
let returns = format!(
"elementId(n) AS {}, {returned_columns}",
"{}(n) AS {}, {returned_columns}",
neo4j_element_id_function(options.server_version.as_deref()),
quote_table_identifier(Some(DatabaseType::Neo4j), DBX_NEO4J_ELEMENT_ID_COLUMN)
);
let order_by = options.order_by.as_deref().filter(|order| !order.trim().is_empty());
@@ -979,6 +1012,7 @@ mod tests {
database_type: Some(database_type),
driver_profile: None,
identifier_quote: None,
server_version: None,
schema: None,
table_name: table.to_string(),
catalog: catalog.map(|c| c.to_string()),