queryer test checkpoint

This commit is contained in:
2026-03-17 15:06:02 -04:00
parent 70a27b430d
commit e1314496dd
3 changed files with 172 additions and 117 deletions

View File

@ -32,7 +32,7 @@ pub struct Database {
pub enums: HashMap<String, Enum>,
pub types: HashMap<String, Type>,
pub puncs: HashMap<String, Punc>,
pub relations: HashMap<String, Relation>,
pub relations: HashMap<(String, String), Vec<Relation>>,
pub schemas: HashMap<String, Schema>,
// Map of Schema ID -> { Entity Type -> Target Subschema Arc }
pub stems: HashMap<String, HashMap<String, Arc<Stem>>>,
@ -74,11 +74,12 @@ impl Database {
}
}
let mut raw_relations = Vec::new();
if let Some(arr) = val.get("relations").and_then(|v| v.as_array()) {
for item in arr {
match serde_json::from_value::<Relation>(item.clone()) {
Ok(def) => {
db.relations.insert(def.constraint.clone(), def);
raw_relations.push(def);
}
Err(e) => println!("DATABASE RELATION PARSE FAILED: {:?}", e),
}
@ -107,7 +108,7 @@ impl Database {
}
}
db.compile()?;
db.compile(raw_relations)?;
Ok(db)
}
@ -138,10 +139,11 @@ impl Database {
}
/// Organizes the graph of the database, compiling regex, format functions, and caching relationships.
pub fn compile(&mut self) -> Result<(), crate::drop::Drop> {
pub fn compile(&mut self, raw_relations: Vec<Relation>) -> Result<(), crate::drop::Drop> {
self.collect_schemas();
self.collect_depths();
self.collect_descendants();
self.collect_relations(raw_relations);
self.compile_schemas();
self.collect_stems()?;
@ -226,6 +228,93 @@ impl Database {
self.descendants = descendants;
}
fn collect_relations(&mut self, raw_relations: Vec<Relation>) {
let mut edges: HashMap<(String, String), Vec<Relation>> = HashMap::new();
// For every relation, map it across all polymorphic inheritance permutations
for relation in raw_relations {
if let Some(source_type_def) = self.types.get(&relation.source_type) {
if let Some(dest_type_def) = self.types.get(&relation.destination_type) {
let mut src_descendants = Vec::new();
let mut dest_descendants = Vec::new();
for (t_name, t_def) in &self.types {
if t_def.hierarchy.contains(&relation.source_type) {
src_descendants.push(t_name.clone());
}
if t_def.hierarchy.contains(&relation.destination_type) {
dest_descendants.push(t_name.clone());
}
}
for p_type in &src_descendants {
for c_type in &dest_descendants {
// Ignore entity <-> entity generic fallbacks, they aren't useful edges
if p_type == "entity" && c_type == "entity" {
continue;
}
// Forward edge
edges
.entry((p_type.clone(), c_type.clone()))
.or_default()
.push(relation.clone());
// Reverse edge (only if types are different to avoid duplicating self-referential edges like activity parent_id)
if p_type != c_type {
edges
.entry((c_type.clone(), p_type.clone()))
.or_default()
.push(relation.clone());
}
}
}
}
}
}
self.relations = edges;
}
pub fn get_relation(
&self,
parent_type: &str,
child_type: &str,
prop_name: &str,
relative_keys: Option<&Vec<String>>
) -> Option<&Relation> {
if let Some(relations) = self.relations.get(&(parent_type.to_string(), child_type.to_string())) {
if relations.len() == 1 {
return Some(&relations[0]);
}
// Reduce ambiguity with prefix
for rel in relations {
if let Some(prefix) = &rel.prefix {
if prefix == prop_name {
return Some(rel);
}
}
}
// Reduce ambiguity by checking if relative payload OMITS the prefix (M:M heuristic)
if let Some(keys) = relative_keys {
let mut missing_prefix_rels = Vec::new();
for rel in relations {
if let Some(prefix) = &rel.prefix {
if !keys.contains(prefix) {
missing_prefix_rels.push(rel);
}
}
}
if missing_prefix_rels.len() == 1 {
return Some(missing_prefix_rels[0]);
}
}
}
None
}
fn collect_descendants_recursively(
target: &str,
direct_refs: &HashMap<String, Vec<String>>,
@ -335,17 +424,14 @@ impl Database {
if let (Some(pt), Some(prop)) = (&parent_type, &property_name) {
let expected_col = format!("{}_id", prop);
let mut found = false;
for rel in db.relations.values() {
if (rel.source_type == *pt && rel.destination_type == entity_type)
|| (rel.source_type == entity_type && rel.destination_type == *pt)
{
if rel.source_columns.contains(&expected_col) {
relation_col = Some(expected_col.clone());
found = true;
break;
}
if let Some(rel) = db.get_relation(pt, &entity_type, prop, None) {
if rel.source_columns.contains(&expected_col) {
relation_col = Some(expected_col.clone());
found = true;
}
}
if !found {
relation_col = Some(expected_col);
}