added support for root schema compiled properties for the mixer
This commit is contained in:
@ -19,11 +19,7 @@ pub struct Node<'a> {
|
||||
|
||||
impl<'a> Compiler<'a> {
|
||||
/// Compiles a JSON schema into a nested PostgreSQL query returning JSONB
|
||||
pub fn compile(
|
||||
&self,
|
||||
schema_id: &str,
|
||||
filter_keys: &[String],
|
||||
) -> Result<String, String> {
|
||||
pub fn compile(&self, schema_id: &str, filter_keys: &[String]) -> Result<String, String> {
|
||||
let schema = self
|
||||
.db
|
||||
.schemas
|
||||
@ -251,8 +247,7 @@ impl<'a> Compiler<'a> {
|
||||
let mut bypass_node = node.clone();
|
||||
bypass_node.schema = std::sync::Arc::new(bypass_schema);
|
||||
|
||||
let mut bypassed_args =
|
||||
self.compile_select_clause(r#type, table_aliases, bypass_node)?;
|
||||
let mut bypassed_args = self.compile_select_clause(r#type, table_aliases, bypass_node)?;
|
||||
select_args.append(&mut bypassed_args);
|
||||
} else {
|
||||
let mut family_schemas = Vec::new();
|
||||
@ -400,7 +395,9 @@ impl<'a> Compiler<'a> {
|
||||
) -> Result<Vec<String>, String> {
|
||||
let mut select_args = Vec::new();
|
||||
let grouped_fields = r#type.grouped_fields.as_ref().and_then(|v| v.as_object());
|
||||
let merged_props = self.get_merged_properties(node.schema.as_ref());
|
||||
let merged_props = self
|
||||
.db
|
||||
.merged_properties(node.schema.as_ref(), &mut std::collections::HashSet::new());
|
||||
let mut sorted_keys: Vec<&String> = merged_props.keys().collect();
|
||||
sorted_keys.sort();
|
||||
|
||||
@ -417,9 +414,9 @@ impl<'a> Compiler<'a> {
|
||||
_ => false,
|
||||
};
|
||||
|
||||
let is_primitive = prop_schema.obj.r#ref.is_none()
|
||||
&& !is_object_or_array
|
||||
&& prop_schema.obj.family.is_none()
|
||||
let is_primitive = prop_schema.obj.r#ref.is_none()
|
||||
&& !is_object_or_array
|
||||
&& prop_schema.obj.family.is_none()
|
||||
&& prop_schema.obj.one_of.is_none();
|
||||
|
||||
if is_primitive {
|
||||
@ -494,7 +491,13 @@ impl<'a> Compiler<'a> {
|
||||
where_clauses.push(format!("NOT {}.archived", entity_alias));
|
||||
|
||||
self.compile_filter_conditions(r#type, type_aliases, &node, &base_alias, &mut where_clauses);
|
||||
self.compile_relation_conditions(r#type, type_aliases, &node, &base_alias, &mut where_clauses)?;
|
||||
self.compile_relation_conditions(
|
||||
r#type,
|
||||
type_aliases,
|
||||
&node,
|
||||
&base_alias,
|
||||
&mut where_clauses,
|
||||
)?;
|
||||
|
||||
Ok(where_clauses)
|
||||
}
|
||||
@ -509,7 +512,10 @@ impl<'a> Compiler<'a> {
|
||||
for (t_name, fields_val) in gf {
|
||||
if let Some(fields_arr) = fields_val.as_array() {
|
||||
if fields_arr.iter().any(|v| v.as_str() == Some(field_name)) {
|
||||
return type_aliases.get(t_name).cloned().unwrap_or_else(|| base_alias.to_string());
|
||||
return type_aliases
|
||||
.get(t_name)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| base_alias.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -606,13 +612,31 @@ impl<'a> Compiler<'a> {
|
||||
));
|
||||
} else {
|
||||
let sql_op = match op {
|
||||
"$eq" => if is_ilike { "ILIKE" } else { "=" },
|
||||
"$ne" => if is_ilike { "NOT ILIKE" } else { "!=" },
|
||||
"$eq" => {
|
||||
if is_ilike {
|
||||
"ILIKE"
|
||||
} else {
|
||||
"="
|
||||
}
|
||||
}
|
||||
"$ne" => {
|
||||
if is_ilike {
|
||||
"NOT ILIKE"
|
||||
} else {
|
||||
"!="
|
||||
}
|
||||
}
|
||||
"$gt" => ">",
|
||||
"$gte" => ">=",
|
||||
"$lt" => "<",
|
||||
"$lte" => "<=",
|
||||
_ => if is_ilike { "ILIKE" } else { "=" },
|
||||
_ => {
|
||||
if is_ilike {
|
||||
"ILIKE"
|
||||
} else {
|
||||
"="
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let param_sql = if is_ilike && (op == "$eq" || op == "$ne") {
|
||||
@ -643,7 +667,9 @@ impl<'a> Compiler<'a> {
|
||||
let mut child_relation_alias = base_alias.to_string();
|
||||
|
||||
if let Some(parent_type) = node.parent_type {
|
||||
let merged_props = self.get_merged_properties(node.schema.as_ref());
|
||||
let merged_props = self
|
||||
.db
|
||||
.merged_properties(node.schema.as_ref(), &mut std::collections::HashSet::new());
|
||||
let relative_keys: Vec<String> = merged_props.keys().cloned().collect();
|
||||
|
||||
let (relation, is_parent_source) = self
|
||||
@ -695,25 +721,4 @@ impl<'a> Compiler<'a> {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_merged_properties(
|
||||
&self,
|
||||
schema: &crate::database::schema::Schema,
|
||||
) -> std::collections::BTreeMap<String, Arc<crate::database::schema::Schema>> {
|
||||
let mut props = std::collections::BTreeMap::new();
|
||||
|
||||
if let Some(ref_id) = &schema.obj.r#ref {
|
||||
if let Some(parent_schema) = self.db.schemas.get(ref_id) {
|
||||
props.extend(self.get_merged_properties(parent_schema));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(local_props) = &schema.obj.properties {
|
||||
for (k, v) in local_props {
|
||||
props.insert(k.clone(), v.clone());
|
||||
}
|
||||
}
|
||||
|
||||
props
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user