Skip to main content

uqa_sql/catalog/
constraints.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7use uqa_core::RelationIdentity;
8
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
10pub struct ConstraintIdentity {
11    pub relation: RelationIdentity,
12    pub name: String,
13    pub object_id: Option<[u8; 16]>,
14}
15
16pub fn constraint_identities_match(left: &ConstraintIdentity, right: &ConstraintIdentity) -> bool {
17    match (left.object_id, right.object_id) {
18        (Some(left), Some(right)) => left == right,
19        _ => left == right,
20    }
21}
22
23pub fn foreign_key_identity(
24    table: &str,
25    foreign_key: &crate::ast::ForeignKey,
26) -> Result<ConstraintIdentity, crate::SQLError> {
27    let relation = RelationIdentity::from_legacy_name(table).map_err(|error| {
28        crate::SQLError::Internal(format!(
29            "decode foreign-key relation identity '{table}': {error}"
30        ))
31    })?;
32    let name = foreign_key.name.clone().ok_or_else(|| {
33        crate::SQLError::Internal(format!(
34            "foreign key on '{table}' has no materialized constraint name"
35        ))
36    })?;
37    let object_id = foreign_key.object_id.ok_or_else(|| {
38        crate::SQLError::Internal(format!(
39            "foreign key '{name}' on '{table}' has no materialized object identity"
40        ))
41    })?;
42    Ok(ConstraintIdentity {
43        relation,
44        name,
45        object_id: Some(object_id),
46    })
47}