jspg query with familties fixes

This commit is contained in:
2026-03-16 06:07:13 -04:00
parent 25239d635b
commit 5d11c4c92c
4 changed files with 641 additions and 18 deletions

View File

@ -113,9 +113,9 @@ impl SqlCompiler {
// Determine if this schema represents a Database Entity
let mut resolved_type = None;
// Target is generally a specific schema (e.g. 'base.person'), but it tells us what physical
// database table hierarchy it maps to via the `schema.id` prefix/suffix convention.
if let Some(lookup_key) = schema.obj.id.as_ref().or(schema.obj.r#ref.as_ref()) {
if let Some(family_target) = schema.obj.family.as_ref() {
resolved_type = self.db.types.get(family_target);
} else if let Some(lookup_key) = schema.obj.id.as_ref().or(schema.obj.r#ref.as_ref()) {
let base_type_name = lookup_key.split('.').next_back().unwrap_or("").to_string();
resolved_type = self.db.types.get(&base_type_name);
}
@ -150,6 +150,45 @@ impl SqlCompiler {
}
return Err(format!("Unresolved $ref: {}", ref_id));
}
// Handle $family Polymorphism fallbacks for relations
if let Some(family_target) = &schema.obj.family {
let mut all_targets = vec![family_target.clone()];
if let Some(schema_id) = &schema.obj.id {
if let Some(descendants) = self.db.descendants.get(schema_id) {
all_targets.extend(descendants.clone());
}
}
let mut family_schemas = Vec::new();
for target in all_targets {
let mut ref_schema = crate::database::schema::Schema::default();
ref_schema.obj.r#ref = Some(target);
family_schemas.push(std::sync::Arc::new(ref_schema));
}
return self.compile_one_of(
&family_schemas,
parent_alias,
prop_name_context,
filter_keys,
is_stem_query,
depth,
current_path,
);
}
// Handle oneOf Polymorphism fallbacks for relations
if let Some(one_of) = &schema.obj.one_of {
return self.compile_one_of(
one_of,
parent_alias,
prop_name_context,
filter_keys,
is_stem_query,
depth,
current_path,
);
}
// Just an inline object definition?
if let Some(props) = &schema.obj.properties {
@ -215,7 +254,7 @@ impl SqlCompiler {
let (table_aliases, from_clauses) = self.build_hierarchy_from_clauses(type_def, &local_ctx);
// 2. Map properties and build jsonb_build_object args
let select_args = self.map_properties_to_aliases(
let mut select_args = self.map_properties_to_aliases(
schema,
type_def,
&table_aliases,
@ -226,6 +265,40 @@ impl SqlCompiler {
&current_path,
)?;
// 2.5 Inject polymorphism directly into the query object
if let Some(family_target) = &schema.obj.family {
let mut family_schemas = Vec::new();
if let Some(base_type) = self.db.types.get(family_target) {
let mut sorted_targets: Vec<String> = base_type.variations.iter().cloned().collect();
// Ensure the base type is included if not listed in variations by default
if !sorted_targets.contains(family_target) {
sorted_targets.push(family_target.clone());
}
sorted_targets.sort();
for target in sorted_targets {
let mut ref_schema = crate::database::schema::Schema::default();
ref_schema.obj.r#ref = Some(target);
family_schemas.push(std::sync::Arc::new(ref_schema));
}
} else {
// Fallback for types not strictly defined in physical DB
let mut ref_schema = crate::database::schema::Schema::default();
ref_schema.obj.r#ref = Some(family_target.clone());
family_schemas.push(std::sync::Arc::new(ref_schema));
}
let base_alias = table_aliases.get(&type_def.name).cloned().unwrap_or_else(|| parent_alias.to_string());
select_args.push(format!("'id', {}.id", base_alias));
let (case_sql, _) = self.compile_one_of(&family_schemas, &base_alias, None, filter_keys, is_stem_query, depth, current_path.clone())?;
select_args.push(format!("'type', {}", case_sql));
} else if let Some(one_of) = &schema.obj.one_of {
let base_alias = table_aliases.get(&type_def.name).cloned().unwrap_or_else(|| parent_alias.to_string());
select_args.push(format!("'id', {}.id", base_alias));
let (case_sql, _) = self.compile_one_of(one_of, &base_alias, None, filter_keys, is_stem_query, depth, current_path.clone())?;
select_args.push(format!("'type', {}", case_sql));
}
let jsonb_obj_sql = if select_args.is_empty() {
"jsonb_build_object()".to_string()
} else {
@ -326,6 +399,26 @@ impl SqlCompiler {
}
}
let is_object_or_array = match &prop_schema.obj.type_ {
Some(crate::database::schema::SchemaTypeOrArray::Single(s)) => s == "object" || s == "array",
Some(crate::database::schema::SchemaTypeOrArray::Multiple(v)) => v.contains(&"object".to_string()) || v.contains(&"array".to_string()),
_ => false
};
let is_primitive = prop_schema.obj.r#ref.is_none()
&& prop_schema.obj.items.is_none()
&& prop_schema.obj.properties.is_none()
&& prop_schema.obj.one_of.is_none()
&& !is_object_or_array;
if is_primitive {
if let Some(ft) = type_def.field_types.as_ref().and_then(|v| v.as_object()) {
if !ft.contains_key(prop_key) {
continue; // Skip frontend virtual properties (e.g. `computer` fields, `created`) missing from physical table fields
}
}
}
let next_path = if current_path.is_empty() {
prop_key.clone()
} else {
@ -497,8 +590,12 @@ impl SqlCompiler {
}
}
if let Some(_prop) = prop_name {
where_clauses.push(format!("{}.parent_id = {}.id", base_alias, parent_alias));
if let Some(prop) = prop_name {
if prop == "target" || prop == "source" {
where_clauses.push(format!("{}.id = {}.{}_id", base_alias, parent_alias, prop));
} else {
where_clauses.push(format!("{}.parent_id = {}.id", base_alias, parent_alias));
}
}
Ok(where_clauses)
@ -538,4 +635,56 @@ impl SqlCompiler {
let combined = format!("jsonb_build_object({})", build_args.join(", "));
Ok((combined, "object".to_string()))
}
fn compile_one_of(
&self,
schemas: &[Arc<crate::database::schema::Schema>],
parent_alias: &str,
prop_name_context: Option<&str>,
filter_keys: &[String],
is_stem_query: bool,
depth: usize,
current_path: String,
) -> Result<(String, String), String> {
let mut case_statements = Vec::new();
let type_col = if let Some(prop) = prop_name_context {
format!("{}_type", prop)
} else {
"type".to_string()
};
for option_schema in schemas {
if let Some(ref_id) = &option_schema.obj.r#ref {
// Find the physical type this ref maps to
let base_type_name = ref_id.split('.').next_back().unwrap_or("").to_string();
// Generate the nested SQL for this specific target type
let (val_sql, _) = self.walk_schema(
option_schema,
parent_alias,
prop_name_context,
filter_keys,
is_stem_query,
depth,
current_path.clone(),
)?;
case_statements.push(format!(
"WHEN {}.{} = '{}' THEN ({})",
parent_alias, type_col, base_type_name, val_sql
));
}
}
if case_statements.is_empty() {
return Ok(("NULL".to_string(), "string".to_string()));
}
let sql = format!(
"CASE {} ELSE NULL END",
case_statements.join(" ")
);
Ok((sql, "object".to_string()))
}
}