diff --git a/napi/NEXT_CHANGELOG.md b/napi/NEXT_CHANGELOG.md index 8597f31e..02f6246b 100644 --- a/napi/NEXT_CHANGELOG.md +++ b/napi/NEXT_CHANGELOG.md @@ -20,6 +20,8 @@ layout, or npm publishing. Pure-kernel changes go in the root ### Fixed +- Allow `getCrossReference` to omit the foreign-key side, returning an empty result without issuing a server statement. + --- *Note: When making changes, please add your change under the appropriate section with a brief description. At release time, move the entries here diff --git a/napi/index.d.ts b/napi/index.d.ts index 0f6d8b56..1c66b437 100644 --- a/napi/index.d.ts +++ b/napi/index.d.ts @@ -1127,11 +1127,12 @@ export declare class Connection { */ getPrimaryKeys(catalog: string, schema: string, table: string): Promise /** - * Foreign-key relationships. The foreign side must be fully - * specified (catalog + schema + table); the parent side is - * optional. All identifiers are exact — no LIKE patterns. + * Foreign-key relationships. The parent side is optional. When the + * foreign table is omitted, returns an empty result without issuing a + * server statement. When it is provided, its catalog and schema are + * required. All identifiers are exact — no LIKE patterns. */ - getCrossReference(parentCatalog: string | undefined | null, parentSchema: string | undefined | null, parentTable: string | undefined | null, foreignCatalog: string, foreignSchema: string, foreignTable: string): Promise + getCrossReference(parentCatalog: string | undefined | null, parentSchema: string | undefined | null, parentTable: string | undefined | null, foreignCatalog: string | undefined | null, foreignSchema: string | undefined | null, foreignTable: string | undefined | null): Promise } /** * Opaque executed-statement handle. diff --git a/napi/src/metadata.rs b/napi/src/metadata.rs index e3585a29..749f2d54 100644 --- a/napi/src/metadata.rs +++ b/napi/src/metadata.rs @@ -319,9 +319,10 @@ impl Connection { .await } - /// Foreign-key relationships. The foreign side must be fully - /// specified (catalog + schema + table); the parent side is - /// optional. All identifiers are exact — no LIKE patterns. + /// Foreign-key relationships. The parent side is optional. When the + /// foreign table is omitted, returns an empty result without issuing a + /// server statement. When it is provided, its catalog and schema are + /// required. All identifiers are exact — no LIKE patterns. #[napi] #[allow(clippy::too_many_arguments)] pub async fn get_cross_reference( @@ -329,9 +330,9 @@ impl Connection { parent_catalog: Option, parent_schema: Option, parent_table: Option, - foreign_catalog: String, - foreign_schema: String, - foreign_table: String, + foreign_catalog: Option, + foreign_schema: Option, + foreign_table: Option, ) -> napi::Result { let meta = take_metadata(&self.inner).await?; guarded(async move { @@ -339,9 +340,9 @@ impl Connection { let pc = Identifier::from_opt(&parent_catalog)?; let ps = Identifier::from_opt(&parent_schema)?; let pt = Identifier::from_opt(&parent_table)?; - let fc = Some(Identifier::new(&foreign_catalog)?); - let fs = Some(Identifier::new(&foreign_schema)?); - let ft = Some(Identifier::new(&foreign_table)?); + let fc = Identifier::from_opt(&foreign_catalog)?; + let fs = Identifier::from_opt(&foreign_schema)?; + let ft = Identifier::from_opt(&foreign_table)?; meta.foreign_keys(pc, ps, pt, fc, fs, ft).await }) .await?; @@ -393,7 +394,7 @@ mod tests { } #[tokio::test] - async fn empty_catalog_metadata_calls_return_empty_without_statement_request() { + async fn metadata_short_circuits_return_empty_without_statement_request() { let server = MockServer::start().await; Mock::given(method("POST")) .and(path("/api/2.0/sql/sessions")) @@ -435,6 +436,17 @@ mod tests { .list_columns(Some(String::new()), None, None, None) .await .expect("empty catalog returns empty columns"), + connection + .get_cross_reference( + Some("main".to_owned()), + Some("default".to_owned()), + Some("parent".to_owned()), + None, + None, + None, + ) + .await + .expect("missing foreign table returns empty cross references"), ]; for statement in statements { assert!(statement @@ -455,7 +467,7 @@ mod tests { }) .count(), 0, - "empty catalog must short-circuit before statement execution" + "metadata short-circuits must not execute a statement" ); connection.close().await.expect("connection closes");