diff --git a/fixtures/queryer.json b/fixtures/queryer.json index 3956265..e6e440b 100644 --- a/fixtures/queryer.json +++ b/fixtures/queryer.json @@ -1669,7 +1669,118 @@ "success": true, "sql": [ [ - "FIX ME" + "(SELECT jsonb_strip_nulls((SELECT jsonb_build_object(", + " 'addresses', (SELECT COALESCE(jsonb_agg(jsonb_build_object(", + " 'archived', entity_5.archived,", + " 'created_at', entity_5.created_at,", + " 'id', entity_5.id,", + " 'is_primary', contact_3.is_primary,", + " 'target', (SELECT jsonb_build_object(", + " 'archived', entity_7.archived,", + " 'city', address_6.city,", + " 'created_at', entity_7.created_at,", + " 'id', entity_7.id,", + " 'type', entity_7.type", + " )", + " FROM agreego.address address_6", + " JOIN agreego.entity entity_7 ON entity_7.id = address_6.id", + " WHERE NOT entity_7.archived AND relationship_4.target_id = entity_7.id),", + " 'type', entity_5.type", + " )), '[]'::jsonb)", + " FROM agreego.contact contact_3", + " JOIN agreego.relationship relationship_4 ON relationship_4.id = contact_3.id", + " JOIN agreego.entity entity_5 ON entity_5.id = relationship_4.id", + " WHERE NOT entity_5.archived AND relationship_4.target_type = 'address' AND relationship_4.source_id = entity_2.id),", + " 'archived', entity_2.archived,", + " 'contacts', (SELECT COALESCE(jsonb_agg(jsonb_build_object(", + " 'archived', entity_10.archived,", + " 'created_at', entity_10.created_at,", + " 'id', entity_10.id,", + " 'is_primary', contact_8.is_primary,", + " 'target', CASE WHEN entity_10.target_type = 'address' THEN ((SELECT jsonb_build_object(", + " 'archived', entity_12.archived,", + " 'city', address_11.city,", + " 'created_at', entity_12.created_at,", + " 'id', entity_12.id,", + " 'type', entity_12.type", + " )", + " FROM agreego.address address_11", + " JOIN agreego.entity entity_12 ON entity_12.id = address_11.id", + " WHERE NOT entity_12.archived AND relationship_9.target_id = entity_12.id)) WHEN entity_10.target_type = 'email_address' THEN ((SELECT jsonb_build_object(", + " 'address', email_address_13.address,", + " 'archived', entity_14.archived,", + " 'created_at', entity_14.created_at,", + " 'id', entity_14.id,", + " 'type', entity_14.type", + " )", + " FROM agreego.email_address email_address_13", + " JOIN agreego.entity entity_14 ON entity_14.id = email_address_13.id", + " WHERE NOT entity_14.archived AND relationship_9.target_id = entity_14.id)) WHEN entity_10.target_type = 'phone_number' THEN ((SELECT jsonb_build_object(", + " 'archived', entity_16.archived,", + " 'created_at', entity_16.created_at,", + " 'id', entity_16.id,", + " 'number', phone_number_15.number,", + " 'type', entity_16.type", + " )", + " FROM agreego.phone_number phone_number_15", + " JOIN agreego.entity entity_16 ON entity_16.id = phone_number_15.id", + " WHERE NOT entity_16.archived AND relationship_9.target_id = entity_16.id)) ELSE NULL END,", + " 'type', entity_10.type", + " )), '[]'::jsonb)", + " FROM agreego.contact contact_8", + " JOIN agreego.relationship relationship_9 ON relationship_9.id = contact_8.id", + " JOIN agreego.entity entity_10 ON entity_10.id = relationship_9.id", + " WHERE NOT entity_10.archived AND relationship_9.source_id = entity_2.id),", + " 'created_at', entity_2.created_at,", + " 'email_addresses', (SELECT COALESCE(jsonb_agg(jsonb_build_object(", + " 'archived', entity_19.archived,", + " 'created_at', entity_19.created_at,", + " 'id', entity_19.id,", + " 'is_primary', contact_17.is_primary,", + " 'target', (SELECT jsonb_build_object(", + " 'address', email_address_20.address,", + " 'archived', entity_21.archived,", + " 'created_at', entity_21.created_at,", + " 'id', entity_21.id,", + " 'type', entity_21.type", + " )", + " FROM agreego.email_address email_address_20", + " JOIN agreego.entity entity_21 ON entity_21.id = email_address_20.id", + " WHERE NOT entity_21.archived AND relationship_18.target_id = entity_21.id),", + " 'type', entity_19.type", + " )), '[]'::jsonb)", + " FROM agreego.contact contact_17", + " JOIN agreego.relationship relationship_18 ON relationship_18.id = contact_17.id", + " JOIN agreego.entity entity_19 ON entity_19.id = relationship_18.id", + " WHERE NOT entity_19.archived AND relationship_18.target_type = 'email_address' AND relationship_18.source_id = entity_2.id),", + " 'id', entity_2.id,", + " 'name', organization_1.name,", + " 'phone_numbers', (SELECT COALESCE(jsonb_agg(jsonb_build_object(", + " 'archived', entity_24.archived,", + " 'created_at', entity_24.created_at,", + " 'id', entity_24.id,", + " 'is_primary', contact_22.is_primary,", + " 'target', (SELECT jsonb_build_object(", + " 'archived', entity_26.archived,", + " 'created_at', entity_26.created_at,", + " 'id', entity_26.id,", + " 'number', phone_number_25.number,", + " 'type', entity_26.type", + " )", + " FROM agreego.phone_number phone_number_25", + " JOIN agreego.entity entity_26 ON entity_26.id = phone_number_25.id", + " WHERE NOT entity_26.archived AND relationship_23.target_id = entity_26.id),", + " 'type', entity_24.type", + " )), '[]'::jsonb)", + " FROM agreego.contact contact_22", + " JOIN agreego.relationship relationship_23 ON relationship_23.id = contact_22.id", + " JOIN agreego.entity entity_24 ON entity_24.id = relationship_23.id", + " WHERE NOT entity_24.archived AND relationship_23.target_type = 'phone_number' AND relationship_23.source_id = entity_2.id),", + " 'type', entity_2.type", + ")))", + "FROM agreego.organization organization_1", + "JOIN agreego.entity entity_2 ON entity_2.id = organization_1.id", + "WHERE NOT entity_2.archived)" ] ] } diff --git a/src/database/schema.rs b/src/database/schema.rs index 3a75b7c..44e410a 100644 --- a/src/database/schema.rs +++ b/src/database/schema.rs @@ -551,10 +551,6 @@ impl Schema { return; } - if let Some(id) = &self.obj.id { - println!("[DEBUG POLYMORPHISM] ID: {}, Strategy: {}, Options: {:?}", id, strategy, options); - } - if !options.is_empty() { let _ = self.obj.compiled_discriminator.set(strategy); let _ = self.obj.compiled_options.set(options); diff --git a/src/queryer/compiler.rs b/src/queryer/compiler.rs index aea7acf..6873eda 100644 --- a/src/queryer/compiler.rs +++ b/src/queryer/compiler.rs @@ -102,7 +102,11 @@ impl<'a> Compiler<'a> { let mut resolved_type = None; if let Some(family_target) = node.schema.obj.family.as_ref() { - resolved_type = self.db.types.get(family_target); + let base_type_name = family_target + .split('.') + .next_back() + .unwrap_or(family_target); + resolved_type = self.db.types.get(base_type_name); } else if let Some(base_type_name) = node.schema.obj.identifier() { resolved_type = self.db.types.get(&base_type_name); } @@ -221,7 +225,6 @@ impl<'a> Compiler<'a> { } let options = node.schema.obj.compiled_options.get(); - println!("[DEBUG QUERYER] Evaluating node. Target family: {:?}, disc: {:?}, options: {:?}", node.schema.obj.family, disc, options); if options.is_none() { return Ok(select_args); } @@ -352,7 +355,9 @@ impl<'a> Compiler<'a> { ) -> Result, 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 = node.schema.obj.compiled_properties.get().unwrap(); + let default_props = std::collections::BTreeMap::new(); + let merged_props = node.schema.obj.compiled_properties.get().unwrap_or(&default_props); + let mut sorted_keys: Vec<&String> = merged_props.keys().collect(); sorted_keys.sort();