queryer fixes in place
This commit is contained in:
@ -179,32 +179,49 @@ impl SqlCompiler {
|
||||
}
|
||||
// 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 base_type_name = family_target.split('.').next_back().unwrap_or(family_target).to_string();
|
||||
|
||||
if let Some(type_def) = self.db.types.get(&base_type_name) {
|
||||
if type_def.variations.len() == 1 {
|
||||
let mut bypass_schema = crate::database::schema::Schema::default();
|
||||
bypass_schema.obj.r#ref = Some(family_target.clone());
|
||||
return self.walk_schema(
|
||||
&std::sync::Arc::new(bypass_schema),
|
||||
parent_alias,
|
||||
parent_table_aliases,
|
||||
parent_type_def,
|
||||
prop_name_context,
|
||||
filter_keys,
|
||||
is_stem_query,
|
||||
depth,
|
||||
current_path,
|
||||
alias_counter,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
let mut sorted_variations: Vec<String> = type_def.variations.iter().cloned().collect();
|
||||
sorted_variations.sort();
|
||||
|
||||
return self.compile_one_of(
|
||||
&family_schemas,
|
||||
parent_alias,
|
||||
parent_table_aliases,
|
||||
parent_type_def,
|
||||
prop_name_context,
|
||||
filter_keys,
|
||||
is_stem_query,
|
||||
depth,
|
||||
current_path,
|
||||
alias_counter,
|
||||
);
|
||||
let mut family_schemas = Vec::new();
|
||||
for variation in &sorted_variations {
|
||||
let mut ref_schema = crate::database::schema::Schema::default();
|
||||
ref_schema.obj.r#ref = Some(variation.clone());
|
||||
family_schemas.push(std::sync::Arc::new(ref_schema));
|
||||
}
|
||||
|
||||
return self.compile_one_of(
|
||||
&family_schemas,
|
||||
parent_alias,
|
||||
parent_table_aliases,
|
||||
parent_type_def,
|
||||
prop_name_context,
|
||||
filter_keys,
|
||||
is_stem_query,
|
||||
depth,
|
||||
current_path,
|
||||
alias_counter,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Handle oneOf Polymorphism fallbacks for relations
|
||||
@ -305,45 +322,56 @@ impl SqlCompiler {
|
||||
|
||||
// 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();
|
||||
let base_type_name = family_target.split('.').next_back().unwrap_or(family_target).to_string();
|
||||
|
||||
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));
|
||||
if let Some(fam_type_def) = self.db.types.get(&base_type_name) {
|
||||
if fam_type_def.variations.len() == 1 {
|
||||
let mut bypass_schema = crate::database::schema::Schema::default();
|
||||
bypass_schema.obj.r#ref = Some(family_target.clone());
|
||||
|
||||
let mut bypassed_args = self.map_properties_to_aliases(
|
||||
&bypass_schema,
|
||||
type_def,
|
||||
&table_aliases,
|
||||
parent_alias,
|
||||
filter_keys,
|
||||
is_stem_query,
|
||||
depth,
|
||||
¤t_path,
|
||||
alias_counter,
|
||||
)?;
|
||||
select_args.append(&mut bypassed_args);
|
||||
} else {
|
||||
let mut family_schemas = Vec::new();
|
||||
let mut sorted_fam_variations: Vec<String> = fam_type_def.variations.iter().cloned().collect();
|
||||
sorted_fam_variations.sort();
|
||||
|
||||
for variation in &sorted_fam_variations {
|
||||
let mut ref_schema = crate::database::schema::Schema::default();
|
||||
ref_schema.obj.r#ref = Some(variation.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,
|
||||
Some(&table_aliases),
|
||||
parent_type_def,
|
||||
None,
|
||||
filter_keys,
|
||||
is_stem_query,
|
||||
depth,
|
||||
current_path.clone(),
|
||||
alias_counter,
|
||||
)?;
|
||||
select_args.push(format!("'type', {}", case_sql));
|
||||
}
|
||||
} 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,
|
||||
Some(&table_aliases),
|
||||
parent_type_def,
|
||||
None,
|
||||
filter_keys,
|
||||
is_stem_query,
|
||||
depth,
|
||||
current_path.clone(),
|
||||
alias_counter,
|
||||
)?;
|
||||
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)
|
||||
@ -448,8 +476,11 @@ impl SqlCompiler {
|
||||
let mut select_args = Vec::new();
|
||||
let grouped_fields = type_def.grouped_fields.as_ref().and_then(|v| v.as_object());
|
||||
let merged_props = self.get_merged_properties(schema);
|
||||
let mut sorted_keys: Vec<&String> = merged_props.keys().collect();
|
||||
sorted_keys.sort();
|
||||
|
||||
for (prop_key, prop_schema) in &merged_props {
|
||||
for prop_key in sorted_keys {
|
||||
let prop_schema = &merged_props[prop_key];
|
||||
let mut owner_alias = table_aliases
|
||||
.get("entity")
|
||||
.cloned()
|
||||
@ -832,6 +863,8 @@ impl SqlCompiler {
|
||||
return Ok(("NULL".to_string(), "string".to_string()));
|
||||
}
|
||||
|
||||
case_statements.sort();
|
||||
|
||||
let sql = format!("CASE {} ELSE NULL END", case_statements.join(" "));
|
||||
|
||||
Ok((sql, "object".to_string()))
|
||||
|
||||
Reference in New Issue
Block a user