Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions napi/NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions napi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1127,11 +1127,12 @@ export declare class Connection {
*/
getPrimaryKeys(catalog: string, schema: string, table: string): Promise<Statement>
/**
* 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<Statement>
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<Statement>
}
/**
* Opaque executed-statement handle.
Expand Down
34 changes: 23 additions & 11 deletions napi/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,29 +319,30 @@ 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(
&self,
parent_catalog: Option<String>,
parent_schema: Option<String>,
parent_table: Option<String>,
foreign_catalog: String,
foreign_schema: String,
foreign_table: String,
foreign_catalog: Option<String>,
foreign_schema: Option<String>,
foreign_table: Option<String>,
) -> napi::Result<Statement> {
let meta = take_metadata(&self.inner).await?;
guarded(async move {
let stream = run_on_blocking(move || async move {
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?;
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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
Expand All @@ -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");
Expand Down
Loading