Compare commits
9 Commits
091007006d
...
1.0.75
| Author | SHA1 | Date | |
|---|---|---|---|
| e19e1921e5 | |||
| 94d011e729 | |||
| 263cf04ffb | |||
| 00375c2926 | |||
| 885b9b5e44 | |||
| 298645ffdb | |||
| 330280ba48 | |||
| 02e661d219 | |||
| f7163e2689 |
@ -1016,7 +1016,7 @@
|
|||||||
" JOIN agreego.entity entity_8 ON entity_8.id = address_7.id",
|
" JOIN agreego.entity entity_8 ON entity_8.id = address_7.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_8.archived",
|
" NOT entity_8.archived",
|
||||||
" AND relationship_5.target_id = address_7.id),",
|
" AND relationship_5.target_id = entity_8.id),",
|
||||||
" 'type', entity_6.type",
|
" 'type', entity_6.type",
|
||||||
" )), '[]'::jsonb)",
|
" )), '[]'::jsonb)",
|
||||||
" FROM agreego.contact contact_4",
|
" FROM agreego.contact contact_4",
|
||||||
@ -1024,7 +1024,7 @@
|
|||||||
" JOIN agreego.entity entity_6 ON entity_6.id = relationship_5.id",
|
" JOIN agreego.entity entity_6 ON entity_6.id = relationship_5.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_6.archived",
|
" NOT entity_6.archived",
|
||||||
" AND contact_4.parent_id = entity_3.id),",
|
" AND relationship_5.source_id = entity_3.id),",
|
||||||
" 'age', person_1.age,",
|
" 'age', person_1.age,",
|
||||||
" 'archived', entity_3.archived,",
|
" 'archived', entity_3.archived,",
|
||||||
" 'contacts',",
|
" 'contacts',",
|
||||||
@ -1048,7 +1048,7 @@
|
|||||||
" JOIN agreego.entity entity_17 ON entity_17.id = address_16.id",
|
" JOIN agreego.entity entity_17 ON entity_17.id = address_16.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_17.archived",
|
" NOT entity_17.archived",
|
||||||
" AND relationship_10.target_id = address_16.id))",
|
" AND relationship_10.target_id = entity_17.id))",
|
||||||
" WHEN entity_11.target_type = 'email_address' THEN",
|
" WHEN entity_11.target_type = 'email_address' THEN",
|
||||||
" ((SELECT jsonb_build_object(",
|
" ((SELECT jsonb_build_object(",
|
||||||
" 'address', email_address_14.address,",
|
" 'address', email_address_14.address,",
|
||||||
@ -1062,7 +1062,7 @@
|
|||||||
" JOIN agreego.entity entity_15 ON entity_15.id = email_address_14.id",
|
" JOIN agreego.entity entity_15 ON entity_15.id = email_address_14.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_15.archived",
|
" NOT entity_15.archived",
|
||||||
" AND relationship_10.target_id = email_address_14.id))",
|
" AND relationship_10.target_id = entity_15.id))",
|
||||||
" WHEN entity_11.target_type = 'phone_number' THEN",
|
" WHEN entity_11.target_type = 'phone_number' THEN",
|
||||||
" ((SELECT jsonb_build_object(",
|
" ((SELECT jsonb_build_object(",
|
||||||
" 'archived', entity_13.archived,",
|
" 'archived', entity_13.archived,",
|
||||||
@ -1076,7 +1076,7 @@
|
|||||||
" JOIN agreego.entity entity_13 ON entity_13.id = phone_number_12.id",
|
" JOIN agreego.entity entity_13 ON entity_13.id = phone_number_12.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_13.archived",
|
" NOT entity_13.archived",
|
||||||
" AND relationship_10.target_id = phone_number_12.id))",
|
" AND relationship_10.target_id = entity_13.id))",
|
||||||
" ELSE NULL END,",
|
" ELSE NULL END,",
|
||||||
" 'type', entity_11.type",
|
" 'type', entity_11.type",
|
||||||
" )), '[]'::jsonb)",
|
" )), '[]'::jsonb)",
|
||||||
@ -1085,7 +1085,7 @@
|
|||||||
" JOIN agreego.entity entity_11 ON entity_11.id = relationship_10.id",
|
" JOIN agreego.entity entity_11 ON entity_11.id = relationship_10.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_11.archived",
|
" NOT entity_11.archived",
|
||||||
" AND contact_9.parent_id = entity_3.id),",
|
" AND relationship_10.source_id = entity_3.id),",
|
||||||
" 'created_at', entity_3.created_at,",
|
" 'created_at', entity_3.created_at,",
|
||||||
" 'email_addresses',",
|
" 'email_addresses',",
|
||||||
" (SELECT COALESCE(jsonb_agg(jsonb_build_object(",
|
" (SELECT COALESCE(jsonb_agg(jsonb_build_object(",
|
||||||
@ -1107,7 +1107,7 @@
|
|||||||
" JOIN agreego.entity entity_22 ON entity_22.id = email_address_21.id",
|
" JOIN agreego.entity entity_22 ON entity_22.id = email_address_21.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_22.archived",
|
" NOT entity_22.archived",
|
||||||
" AND relationship_19.target_id = email_address_21.id),",
|
" AND relationship_19.target_id = entity_22.id),",
|
||||||
" 'type', entity_20.type",
|
" 'type', entity_20.type",
|
||||||
" )), '[]'::jsonb)",
|
" )), '[]'::jsonb)",
|
||||||
" FROM agreego.contact contact_18",
|
" FROM agreego.contact contact_18",
|
||||||
@ -1115,7 +1115,7 @@
|
|||||||
" JOIN agreego.entity entity_20 ON entity_20.id = relationship_19.id",
|
" JOIN agreego.entity entity_20 ON entity_20.id = relationship_19.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_20.archived",
|
" NOT entity_20.archived",
|
||||||
" AND contact_18.parent_id = entity_3.id),",
|
" AND relationship_19.source_id = entity_3.id),",
|
||||||
" 'first_name', person_1.first_name,",
|
" 'first_name', person_1.first_name,",
|
||||||
" 'id', entity_3.id,",
|
" 'id', entity_3.id,",
|
||||||
" 'last_name', person_1.last_name,",
|
" 'last_name', person_1.last_name,",
|
||||||
@ -1140,7 +1140,7 @@
|
|||||||
" JOIN agreego.entity entity_27 ON entity_27.id = phone_number_26.id",
|
" JOIN agreego.entity entity_27 ON entity_27.id = phone_number_26.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_27.archived",
|
" NOT entity_27.archived",
|
||||||
" AND relationship_24.target_id = phone_number_26.id),",
|
" AND relationship_24.target_id = entity_27.id),",
|
||||||
" 'type', entity_25.type",
|
" 'type', entity_25.type",
|
||||||
" )), '[]'::jsonb)",
|
" )), '[]'::jsonb)",
|
||||||
" FROM agreego.contact contact_23",
|
" FROM agreego.contact contact_23",
|
||||||
@ -1148,7 +1148,7 @@
|
|||||||
" JOIN agreego.entity entity_25 ON entity_25.id = relationship_24.id",
|
" JOIN agreego.entity entity_25 ON entity_25.id = relationship_24.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_25.archived",
|
" NOT entity_25.archived",
|
||||||
" AND contact_23.parent_id = entity_3.id),",
|
" AND relationship_24.source_id = entity_3.id),",
|
||||||
" 'type', entity_3.type",
|
" 'type', entity_3.type",
|
||||||
")",
|
")",
|
||||||
"FROM agreego.person person_1",
|
"FROM agreego.person person_1",
|
||||||
@ -1253,7 +1253,7 @@
|
|||||||
" JOIN agreego.entity entity_8 ON entity_8.id = address_7.id",
|
" JOIN agreego.entity entity_8 ON entity_8.id = address_7.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_8.archived",
|
" NOT entity_8.archived",
|
||||||
" AND relationship_5.target_id = address_7.id),",
|
" AND relationship_5.target_id = entity_8.id),",
|
||||||
" 'type', entity_6.type",
|
" 'type', entity_6.type",
|
||||||
" )), '[]'::jsonb)",
|
" )), '[]'::jsonb)",
|
||||||
" FROM agreego.contact contact_4",
|
" FROM agreego.contact contact_4",
|
||||||
@ -1261,7 +1261,7 @@
|
|||||||
" JOIN agreego.entity entity_6 ON entity_6.id = relationship_5.id",
|
" JOIN agreego.entity entity_6 ON entity_6.id = relationship_5.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_6.archived",
|
" NOT entity_6.archived",
|
||||||
" AND contact_4.parent_id = entity_3.id),",
|
" AND relationship_5.source_id = entity_3.id),",
|
||||||
" 'age', person_1.age,",
|
" 'age', person_1.age,",
|
||||||
" 'archived', entity_3.archived,",
|
" 'archived', entity_3.archived,",
|
||||||
" 'contacts',",
|
" 'contacts',",
|
||||||
@ -1285,7 +1285,7 @@
|
|||||||
" JOIN agreego.entity entity_17 ON entity_17.id = address_16.id",
|
" JOIN agreego.entity entity_17 ON entity_17.id = address_16.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_17.archived",
|
" NOT entity_17.archived",
|
||||||
" AND relationship_10.target_id = address_16.id))",
|
" AND relationship_10.target_id = entity_17.id))",
|
||||||
" WHEN entity_11.target_type = 'email_address' THEN",
|
" WHEN entity_11.target_type = 'email_address' THEN",
|
||||||
" ((SELECT jsonb_build_object(",
|
" ((SELECT jsonb_build_object(",
|
||||||
" 'address', email_address_14.address,",
|
" 'address', email_address_14.address,",
|
||||||
@ -1299,7 +1299,7 @@
|
|||||||
" JOIN agreego.entity entity_15 ON entity_15.id = email_address_14.id",
|
" JOIN agreego.entity entity_15 ON entity_15.id = email_address_14.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_15.archived",
|
" NOT entity_15.archived",
|
||||||
" AND relationship_10.target_id = email_address_14.id))",
|
" AND relationship_10.target_id = entity_15.id))",
|
||||||
" WHEN entity_11.target_type = 'phone_number' THEN",
|
" WHEN entity_11.target_type = 'phone_number' THEN",
|
||||||
" ((SELECT jsonb_build_object(",
|
" ((SELECT jsonb_build_object(",
|
||||||
" 'archived', entity_13.archived,",
|
" 'archived', entity_13.archived,",
|
||||||
@ -1313,7 +1313,7 @@
|
|||||||
" JOIN agreego.entity entity_13 ON entity_13.id = phone_number_12.id",
|
" JOIN agreego.entity entity_13 ON entity_13.id = phone_number_12.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_13.archived",
|
" NOT entity_13.archived",
|
||||||
" AND relationship_10.target_id = phone_number_12.id))",
|
" AND relationship_10.target_id = entity_13.id))",
|
||||||
" ELSE NULL END,",
|
" ELSE NULL END,",
|
||||||
" 'type', entity_11.type",
|
" 'type', entity_11.type",
|
||||||
" )), '[]'::jsonb)",
|
" )), '[]'::jsonb)",
|
||||||
@ -1323,7 +1323,7 @@
|
|||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_11.archived",
|
" NOT entity_11.archived",
|
||||||
" AND contact_9.is_primary = ($11#>>'{}')::boolean",
|
" AND contact_9.is_primary = ($11#>>'{}')::boolean",
|
||||||
" AND contact_9.parent_id = entity_3.id),",
|
" AND relationship_10.source_id = entity_3.id),",
|
||||||
" 'created_at', entity_3.created_at,",
|
" 'created_at', entity_3.created_at,",
|
||||||
" 'email_addresses',",
|
" 'email_addresses',",
|
||||||
" (SELECT COALESCE(jsonb_agg(jsonb_build_object(",
|
" (SELECT COALESCE(jsonb_agg(jsonb_build_object(",
|
||||||
@ -1345,7 +1345,7 @@
|
|||||||
" JOIN agreego.entity entity_22 ON entity_22.id = email_address_21.id",
|
" JOIN agreego.entity entity_22 ON entity_22.id = email_address_21.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_22.archived",
|
" NOT entity_22.archived",
|
||||||
" AND relationship_19.target_id = email_address_21.id),",
|
" AND relationship_19.target_id = entity_22.id),",
|
||||||
" 'type', entity_20.type",
|
" 'type', entity_20.type",
|
||||||
" )), '[]'::jsonb)",
|
" )), '[]'::jsonb)",
|
||||||
" FROM agreego.contact contact_18",
|
" FROM agreego.contact contact_18",
|
||||||
@ -1353,7 +1353,7 @@
|
|||||||
" JOIN agreego.entity entity_20 ON entity_20.id = relationship_19.id",
|
" JOIN agreego.entity entity_20 ON entity_20.id = relationship_19.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_20.archived",
|
" NOT entity_20.archived",
|
||||||
" AND contact_18.parent_id = entity_3.id),",
|
" AND relationship_19.source_id = entity_3.id),",
|
||||||
" 'first_name', person_1.first_name,",
|
" 'first_name', person_1.first_name,",
|
||||||
" 'id', entity_3.id,",
|
" 'id', entity_3.id,",
|
||||||
" 'last_name', person_1.last_name,",
|
" 'last_name', person_1.last_name,",
|
||||||
@ -1379,7 +1379,7 @@
|
|||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_27.archived",
|
" NOT entity_27.archived",
|
||||||
" AND phone_number_26.number ILIKE $32#>>'{}'",
|
" AND phone_number_26.number ILIKE $32#>>'{}'",
|
||||||
" AND relationship_24.target_id = phone_number_26.id),",
|
" AND relationship_24.target_id = entity_27.id),",
|
||||||
" 'type', entity_25.type",
|
" 'type', entity_25.type",
|
||||||
" )), '[]'::jsonb)",
|
" )), '[]'::jsonb)",
|
||||||
" FROM agreego.contact contact_23",
|
" FROM agreego.contact contact_23",
|
||||||
@ -1387,7 +1387,7 @@
|
|||||||
" JOIN agreego.entity entity_25 ON entity_25.id = relationship_24.id",
|
" JOIN agreego.entity entity_25 ON entity_25.id = relationship_24.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_25.archived",
|
" NOT entity_25.archived",
|
||||||
" AND contact_23.parent_id = entity_3.id),",
|
" AND relationship_24.source_id = entity_3.id),",
|
||||||
" 'type', entity_3.type",
|
" 'type', entity_3.type",
|
||||||
")",
|
")",
|
||||||
"FROM agreego.person person_1",
|
"FROM agreego.person person_1",
|
||||||
@ -1457,7 +1457,7 @@
|
|||||||
" JOIN agreego.entity entity_5 ON entity_5.id = phone_number_4.id",
|
" JOIN agreego.entity entity_5 ON entity_5.id = phone_number_4.id",
|
||||||
" WHERE",
|
" WHERE",
|
||||||
" NOT entity_5.archived",
|
" NOT entity_5.archived",
|
||||||
" AND relationship_2.target_id = phone_number_4.id",
|
" AND relationship_2.target_id = entity_5.id",
|
||||||
" ),",
|
" ),",
|
||||||
" 'type', entity_3.type",
|
" 'type', entity_3.type",
|
||||||
")",
|
")",
|
||||||
|
|||||||
@ -32,7 +32,7 @@ pub struct Database {
|
|||||||
pub enums: HashMap<String, Enum>,
|
pub enums: HashMap<String, Enum>,
|
||||||
pub types: HashMap<String, Type>,
|
pub types: HashMap<String, Type>,
|
||||||
pub puncs: HashMap<String, Punc>,
|
pub puncs: HashMap<String, Punc>,
|
||||||
pub relations: HashMap<(String, String), Vec<Relation>>,
|
pub relations: Vec<Relation>,
|
||||||
pub schemas: HashMap<String, Schema>,
|
pub schemas: HashMap<String, Schema>,
|
||||||
// Map of Schema ID -> { Entity Type -> Target Subschema Arc }
|
// Map of Schema ID -> { Entity Type -> Target Subschema Arc }
|
||||||
pub stems: HashMap<String, HashMap<String, Arc<Stem>>>,
|
pub stems: HashMap<String, HashMap<String, Arc<Stem>>>,
|
||||||
@ -46,7 +46,7 @@ impl Database {
|
|||||||
let mut db = Self {
|
let mut db = Self {
|
||||||
enums: HashMap::new(),
|
enums: HashMap::new(),
|
||||||
types: HashMap::new(),
|
types: HashMap::new(),
|
||||||
relations: HashMap::new(),
|
relations: Vec::new(),
|
||||||
puncs: HashMap::new(),
|
puncs: HashMap::new(),
|
||||||
schemas: HashMap::new(),
|
schemas: HashMap::new(),
|
||||||
stems: HashMap::new(),
|
stems: HashMap::new(),
|
||||||
@ -74,12 +74,15 @@ impl Database {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut raw_relations = Vec::new();
|
|
||||||
if let Some(arr) = val.get("relations").and_then(|v| v.as_array()) {
|
if let Some(arr) = val.get("relations").and_then(|v| v.as_array()) {
|
||||||
for item in arr {
|
for item in arr {
|
||||||
match serde_json::from_value::<Relation>(item.clone()) {
|
match serde_json::from_value::<Relation>(item.clone()) {
|
||||||
Ok(def) => {
|
Ok(def) => {
|
||||||
raw_relations.push(def);
|
if db.types.contains_key(&def.source_type)
|
||||||
|
&& db.types.contains_key(&def.destination_type)
|
||||||
|
{
|
||||||
|
db.relations.push(def);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => println!("DATABASE RELATION PARSE FAILED: {:?}", e),
|
Err(e) => println!("DATABASE RELATION PARSE FAILED: {:?}", e),
|
||||||
}
|
}
|
||||||
@ -108,7 +111,7 @@ impl Database {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
db.compile(raw_relations)?;
|
db.compile()?;
|
||||||
Ok(db)
|
Ok(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,12 +141,10 @@ impl Database {
|
|||||||
self.executor.timestamp()
|
self.executor.timestamp()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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_schemas();
|
||||||
self.collect_depths();
|
self.collect_depths();
|
||||||
self.collect_descendants();
|
self.collect_descendants();
|
||||||
self.collect_relations(raw_relations);
|
|
||||||
self.compile_schemas();
|
self.compile_schemas();
|
||||||
self.collect_stems()?;
|
self.collect_stems()?;
|
||||||
|
|
||||||
@ -228,93 +229,77 @@ impl Database {
|
|||||||
self.descendants = descendants;
|
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(
|
pub fn get_relation(
|
||||||
&self,
|
&self,
|
||||||
parent_type: &str,
|
parent_type: &str,
|
||||||
child_type: &str,
|
child_type: &str,
|
||||||
prop_name: &str,
|
prop_name: &str,
|
||||||
relative_keys: Option<&Vec<String>>,
|
relative_keys: Option<&Vec<String>>,
|
||||||
) -> Option<&Relation> {
|
) -> Option<(&Relation, bool)> {
|
||||||
if let Some(relations) = self
|
if parent_type == "entity" && child_type == "entity" {
|
||||||
.relations
|
return None; // Ignore entity <-> entity generic fallbacks, they aren't useful edges
|
||||||
.get(&(parent_type.to_string(), child_type.to_string()))
|
}
|
||||||
{
|
|
||||||
if relations.len() == 1 {
|
|
||||||
return Some(&relations[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reduce ambiguity with prefix
|
let p_def = self.types.get(parent_type)?;
|
||||||
for rel in relations {
|
let c_def = self.types.get(child_type)?;
|
||||||
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)
|
let mut matching_rels = Vec::new();
|
||||||
if let Some(keys) = relative_keys {
|
let mut directions = Vec::new();
|
||||||
let mut missing_prefix_rels = Vec::new();
|
|
||||||
for rel in relations {
|
for rel in &self.relations {
|
||||||
if let Some(prefix) = &rel.prefix {
|
let is_forward = p_def.hierarchy.contains(&rel.source_type)
|
||||||
if !keys.contains(prefix) {
|
&& c_def.hierarchy.contains(&rel.destination_type);
|
||||||
missing_prefix_rels.push(rel);
|
let is_reverse = p_def.hierarchy.contains(&rel.destination_type)
|
||||||
}
|
&& c_def.hierarchy.contains(&rel.source_type);
|
||||||
}
|
|
||||||
}
|
if is_forward {
|
||||||
if missing_prefix_rels.len() == 1 {
|
matching_rels.push(rel);
|
||||||
return Some(missing_prefix_rels[0]);
|
directions.push(true);
|
||||||
|
} else if is_reverse {
|
||||||
|
matching_rels.push(rel);
|
||||||
|
directions.push(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if matching_rels.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
if matching_rels.len() == 1 {
|
||||||
|
return Some((matching_rels[0], directions[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut chosen_idx = 0;
|
||||||
|
let mut resolved = false;
|
||||||
|
|
||||||
|
// Reduce ambiguity with prefix
|
||||||
|
for (i, rel) in matching_rels.iter().enumerate() {
|
||||||
|
if let Some(prefix) = &rel.prefix {
|
||||||
|
if prefix == prop_name {
|
||||||
|
chosen_idx = i;
|
||||||
|
resolved = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
|
||||||
|
// Reduce ambiguity by checking if relative payload OMITS the prefix (M:M heuristic)
|
||||||
|
if !resolved && relative_keys.is_some() {
|
||||||
|
let keys = relative_keys.unwrap();
|
||||||
|
let mut missing_prefix_ids = Vec::new();
|
||||||
|
for (i, rel) in matching_rels.iter().enumerate() {
|
||||||
|
if let Some(prefix) = &rel.prefix {
|
||||||
|
if !keys.contains(prefix) {
|
||||||
|
missing_prefix_ids.push(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if missing_prefix_ids.len() == 1 {
|
||||||
|
chosen_idx = missing_prefix_ids[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Some((matching_rels[chosen_idx], directions[chosen_idx]))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_descendants_recursively(
|
fn collect_descendants_recursively(
|
||||||
@ -427,7 +412,7 @@ impl Database {
|
|||||||
let expected_col = format!("{}_id", prop);
|
let expected_col = format!("{}_id", prop);
|
||||||
let mut found = false;
|
let mut found = false;
|
||||||
|
|
||||||
if let Some(rel) = db.get_relation(pt, &entity_type, prop, None) {
|
if let Some((rel, _)) = db.get_relation(pt, &entity_type, prop, None) {
|
||||||
if rel.source_columns.contains(&expected_col) {
|
if rel.source_columns.contains(&expected_col) {
|
||||||
relation_col = Some(expected_col.clone());
|
relation_col = Some(expected_col.clone());
|
||||||
found = true;
|
found = true;
|
||||||
|
|||||||
@ -70,7 +70,7 @@ pub struct ErrorDetails {
|
|||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub cause: Option<String>,
|
pub cause: Option<String>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub context: Option<Vec<String>>,
|
pub context: Option<Value>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub schema: Option<String>,
|
pub schema: Option<String>,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,7 +23,7 @@ impl Merger {
|
|||||||
pub fn merge(&self, data: Value) -> crate::drop::Drop {
|
pub fn merge(&self, data: Value) -> crate::drop::Drop {
|
||||||
let mut notifications_queue = Vec::new();
|
let mut notifications_queue = Vec::new();
|
||||||
|
|
||||||
let result = self.merge_internal(data, &mut notifications_queue);
|
let result = self.merge_internal(data.clone(), &mut notifications_queue);
|
||||||
|
|
||||||
let val_resolved = match result {
|
let val_resolved = match result {
|
||||||
Ok(val) => val,
|
Ok(val) => val,
|
||||||
@ -34,7 +34,7 @@ impl Merger {
|
|||||||
details: crate::drop::ErrorDetails {
|
details: crate::drop::ErrorDetails {
|
||||||
path: "".to_string(),
|
path: "".to_string(),
|
||||||
cause: None,
|
cause: None,
|
||||||
context: None,
|
context: Some(data),
|
||||||
schema: None,
|
schema: None,
|
||||||
},
|
},
|
||||||
}]);
|
}]);
|
||||||
@ -189,8 +189,7 @@ impl Merger {
|
|||||||
Some(&relative_keys),
|
Some(&relative_keys),
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some(relation) = relative_relation {
|
if let Some((relation, parent_is_source)) = relative_relation {
|
||||||
let parent_is_source = type_def.hierarchy.contains(&relation.source_type);
|
|
||||||
|
|
||||||
if parent_is_source {
|
if parent_is_source {
|
||||||
// Parent holds FK to Child. Child MUST be generated FIRST.
|
// Parent holds FK to Child. Child MUST be generated FIRST.
|
||||||
@ -292,7 +291,7 @@ impl Merger {
|
|||||||
Some(&relative_keys),
|
Some(&relative_keys),
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some(relation) = relative_relation {
|
if let Some((relation, _)) = relative_relation {
|
||||||
let mut relative_responses = Vec::new();
|
let mut relative_responses = Vec::new();
|
||||||
for relative_item_val in relative_arr {
|
for relative_item_val in relative_arr {
|
||||||
if let Value::Object(mut relative_item) = relative_item_val {
|
if let Value::Object(mut relative_item) = relative_item_val {
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -36,7 +36,7 @@ impl Queryer {
|
|||||||
details: crate::drop::ErrorDetails {
|
details: crate::drop::ErrorDetails {
|
||||||
path: "".to_string(), // filters apply to the root query
|
path: "".to_string(), // filters apply to the root query
|
||||||
cause: Some(msg),
|
cause: Some(msg),
|
||||||
context: filters.map(|f| vec![f.to_string()]),
|
context: filters.cloned(),
|
||||||
schema: Some(schema_id.to_string()),
|
schema: Some(schema_id.to_string()),
|
||||||
},
|
},
|
||||||
}]);
|
}]);
|
||||||
@ -97,7 +97,13 @@ impl Queryer {
|
|||||||
return Ok(cached_sql.value().clone());
|
return Ok(cached_sql.value().clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
let compiler = compiler::SqlCompiler::new(self.db.clone());
|
let compiler = compiler::Compiler {
|
||||||
|
db: &self.db,
|
||||||
|
filter_keys: filter_keys,
|
||||||
|
is_stem_query: stem_opt.is_some(),
|
||||||
|
alias_counter: 0,
|
||||||
|
};
|
||||||
|
|
||||||
match compiler.compile(schema_id, stem_opt, filter_keys) {
|
match compiler.compile(schema_id, stem_opt, filter_keys) {
|
||||||
Ok(compiled_sql) => {
|
Ok(compiled_sql) => {
|
||||||
self
|
self
|
||||||
@ -138,7 +144,7 @@ impl Queryer {
|
|||||||
details: crate::drop::ErrorDetails {
|
details: crate::drop::ErrorDetails {
|
||||||
path: "".to_string(),
|
path: "".to_string(),
|
||||||
cause: Some(format!("Expected array, got {}", other)),
|
cause: Some(format!("Expected array, got {}", other)),
|
||||||
context: Some(vec![sql.to_string()]),
|
context: Some(serde_json::json!([sql])),
|
||||||
schema: Some(schema_id.to_string()),
|
schema: Some(schema_id.to_string()),
|
||||||
},
|
},
|
||||||
}]),
|
}]),
|
||||||
@ -148,7 +154,7 @@ impl Queryer {
|
|||||||
details: crate::drop::ErrorDetails {
|
details: crate::drop::ErrorDetails {
|
||||||
path: "".to_string(),
|
path: "".to_string(),
|
||||||
cause: Some(format!("SPI error in queryer: {}", e)),
|
cause: Some(format!("SPI error in queryer: {}", e)),
|
||||||
context: Some(vec![sql.to_string()]),
|
context: Some(serde_json::json!([sql])),
|
||||||
schema: Some(schema_id.to_string()),
|
schema: Some(schema_id.to_string()),
|
||||||
},
|
},
|
||||||
}]),
|
}]),
|
||||||
|
|||||||
@ -191,9 +191,9 @@ impl Expect {
|
|||||||
}
|
}
|
||||||
Expr::Function(func) => {
|
Expr::Function(func) => {
|
||||||
if let sqlparser::ast::FunctionArguments::List(args) = &func.args {
|
if let sqlparser::ast::FunctionArguments::List(args) = &func.args {
|
||||||
if let Some(sqlparser::ast::FunctionArg::Unnamed(sqlparser::ast::FunctionArgExpr::Expr(
|
if let Some(sqlparser::ast::FunctionArg::Unnamed(
|
||||||
e,
|
sqlparser::ast::FunctionArgExpr::Expr(e),
|
||||||
))) = args.args.get(0)
|
)) = args.args.get(0)
|
||||||
{
|
{
|
||||||
Self::validate_expr(e, available_aliases, sql)?;
|
Self::validate_expr(e, available_aliases, sql)?;
|
||||||
}
|
}
|
||||||
|
|||||||
54
t10.json
54
t10.json
@ -1,54 +0,0 @@
|
|||||||
[
|
|
||||||
[
|
|
||||||
"(SELECT jsonb_build_object(",
|
|
||||||
" 'id', organization_1.id,",
|
|
||||||
" 'type', CASE",
|
|
||||||
" WHEN organization_1.type = 'person' THEN",
|
|
||||||
" ((SELECT jsonb_build_object(",
|
|
||||||
" 'age', person_3.age,",
|
|
||||||
" 'archived', entity_5.archived,",
|
|
||||||
" 'created_at', entity_5.created_at,",
|
|
||||||
" 'first_name', person_3.first_name,",
|
|
||||||
" 'id', entity_5.id,",
|
|
||||||
" 'last_name', person_3.last_name,",
|
|
||||||
" 'name', entity_5.name,",
|
|
||||||
" 'type', entity_5.type",
|
|
||||||
" )",
|
|
||||||
" FROM agreego.person person_3",
|
|
||||||
" JOIN agreego.organization organization_4 ON organization_4.id = person_3.id",
|
|
||||||
" JOIN agreego.entity entity_5 ON entity_5.id = organization_4.id",
|
|
||||||
" WHERE",
|
|
||||||
" NOT entity_5.archived))",
|
|
||||||
" WHEN organization_1.type = 'bot' THEN",
|
|
||||||
" ((SELECT jsonb_build_object(",
|
|
||||||
" 'archived', entity_8.archived,",
|
|
||||||
" 'created_at', entity_8.created_at,",
|
|
||||||
" 'id', entity_8.id,",
|
|
||||||
" 'name', entity_8.name,",
|
|
||||||
" 'token', bot_6.token,",
|
|
||||||
" 'type', entity_8.type",
|
|
||||||
" )",
|
|
||||||
" FROM agreego.bot bot_6",
|
|
||||||
" JOIN agreego.organization organization_7 ON organization_7.id = bot_6.id",
|
|
||||||
" JOIN agreego.entity entity_8 ON entity_8.id = organization_7.id",
|
|
||||||
" WHERE",
|
|
||||||
" NOT entity_8.archived))",
|
|
||||||
" WHEN organization_1.type = 'organization' THEN",
|
|
||||||
" ((SELECT jsonb_build_object(",
|
|
||||||
" 'archived', entity_10.archived,",
|
|
||||||
" 'created_at', entity_10.created_at,",
|
|
||||||
" 'id', entity_10.id,",
|
|
||||||
" 'name', entity_10.name,",
|
|
||||||
" 'type', entity_10.type",
|
|
||||||
" )",
|
|
||||||
" FROM agreego.organization organization_9",
|
|
||||||
" JOIN agreego.entity entity_10 ON entity_10.id = organization_9.id",
|
|
||||||
" WHERE",
|
|
||||||
" NOT entity_10.archived))",
|
|
||||||
" ELSE NULL END",
|
|
||||||
")",
|
|
||||||
"FROM agreego.organization organization_1",
|
|
||||||
"JOIN agreego.entity entity_2 ON entity_2.id = organization_1.id",
|
|
||||||
"WHERE NOT entity_2.archived)"
|
|
||||||
]
|
|
||||||
]
|
|
||||||
164
t4.json
164
t4.json
@ -1,164 +0,0 @@
|
|||||||
[
|
|
||||||
[
|
|
||||||
"(SELECT jsonb_build_object(",
|
|
||||||
" 'addresses',",
|
|
||||||
" (SELECT COALESCE(jsonb_agg(jsonb_build_object(",
|
|
||||||
" 'archived', entity_6.archived,",
|
|
||||||
" 'created_at', entity_6.created_at,",
|
|
||||||
" 'id', entity_6.id,",
|
|
||||||
" 'is_primary', contact_4.is_primary,",
|
|
||||||
" 'name', entity_6.name,",
|
|
||||||
" 'target',",
|
|
||||||
" (SELECT jsonb_build_object(",
|
|
||||||
" 'archived', entity_8.archived,",
|
|
||||||
" 'city', address_7.city,",
|
|
||||||
" 'created_at', entity_8.created_at,",
|
|
||||||
" 'id', entity_8.id,",
|
|
||||||
" 'name', entity_8.name,",
|
|
||||||
" 'type', entity_8.type",
|
|
||||||
" )",
|
|
||||||
" FROM agreego.address address_7",
|
|
||||||
" JOIN agreego.entity entity_8 ON entity_8.id = address_7.id",
|
|
||||||
" WHERE",
|
|
||||||
" NOT entity_8.archived",
|
|
||||||
" AND relationship_5.target_id = address_7.id),",
|
|
||||||
" 'type', entity_6.type",
|
|
||||||
" )), '[]'::jsonb)",
|
|
||||||
" FROM agreego.contact contact_4",
|
|
||||||
" JOIN agreego.relationship relationship_5 ON relationship_5.id = contact_4.id",
|
|
||||||
" JOIN agreego.entity entity_6 ON entity_6.id = relationship_5.id",
|
|
||||||
" WHERE",
|
|
||||||
" NOT entity_6.archived",
|
|
||||||
" AND contact_4.parent_id = entity_3.id),",
|
|
||||||
" 'age', person_1.age,",
|
|
||||||
" 'archived', entity_3.archived,",
|
|
||||||
" 'contacts',",
|
|
||||||
" (SELECT COALESCE(jsonb_agg(jsonb_build_object(",
|
|
||||||
" 'archived', entity_11.archived,",
|
|
||||||
" 'created_at', entity_11.created_at,",
|
|
||||||
" 'id', entity_11.id,",
|
|
||||||
" 'is_primary', contact_9.is_primary,",
|
|
||||||
" 'name', entity_11.name,",
|
|
||||||
" 'target', CASE",
|
|
||||||
" WHEN entity_11.target_type = 'address' THEN",
|
|
||||||
" ((SELECT jsonb_build_object(",
|
|
||||||
" 'archived', entity_17.archived,",
|
|
||||||
" 'city', address_16.city,",
|
|
||||||
" 'created_at', entity_17.created_at,",
|
|
||||||
" 'id', entity_17.id,",
|
|
||||||
" 'name', entity_17.name,",
|
|
||||||
" 'type', entity_17.type",
|
|
||||||
" )",
|
|
||||||
" FROM agreego.address address_16",
|
|
||||||
" JOIN agreego.entity entity_17 ON entity_17.id = address_16.id",
|
|
||||||
" WHERE",
|
|
||||||
" NOT entity_17.archived",
|
|
||||||
" AND relationship_10.target_id = address_16.id))",
|
|
||||||
" WHEN entity_11.target_type = 'email_address' THEN",
|
|
||||||
" ((SELECT jsonb_build_object(",
|
|
||||||
" 'address', email_address_14.address,",
|
|
||||||
" 'archived', entity_15.archived,",
|
|
||||||
" 'created_at', entity_15.created_at,",
|
|
||||||
" 'id', entity_15.id,",
|
|
||||||
" 'name', entity_15.name,",
|
|
||||||
" 'type', entity_15.type",
|
|
||||||
" )",
|
|
||||||
" FROM agreego.email_address email_address_14",
|
|
||||||
" JOIN agreego.entity entity_15 ON entity_15.id = email_address_14.id",
|
|
||||||
" WHERE",
|
|
||||||
" NOT entity_15.archived",
|
|
||||||
" AND relationship_10.target_id = email_address_14.id))",
|
|
||||||
" WHEN entity_11.target_type = 'phone_number' THEN",
|
|
||||||
" ((SELECT jsonb_build_object(",
|
|
||||||
" 'archived', entity_13.archived,",
|
|
||||||
" 'created_at', entity_13.created_at,",
|
|
||||||
" 'id', entity_13.id,",
|
|
||||||
" 'name', entity_13.name,",
|
|
||||||
" 'number', phone_number_12.number,",
|
|
||||||
" 'type', entity_13.type",
|
|
||||||
" )",
|
|
||||||
" FROM agreego.phone_number phone_number_12",
|
|
||||||
" JOIN agreego.entity entity_13 ON entity_13.id = phone_number_12.id",
|
|
||||||
" WHERE",
|
|
||||||
" NOT entity_13.archived",
|
|
||||||
" AND relationship_10.target_id = phone_number_12.id))",
|
|
||||||
" ELSE NULL END,",
|
|
||||||
" 'type', entity_11.type",
|
|
||||||
" )), '[]'::jsonb)",
|
|
||||||
" FROM agreego.contact contact_9",
|
|
||||||
" JOIN agreego.relationship relationship_10 ON relationship_10.id = contact_9.id",
|
|
||||||
" JOIN agreego.entity entity_11 ON entity_11.id = relationship_10.id",
|
|
||||||
" WHERE",
|
|
||||||
" NOT entity_11.archived",
|
|
||||||
" AND contact_9.parent_id = entity_3.id),",
|
|
||||||
" 'created_at', entity_3.created_at,",
|
|
||||||
" 'email_addresses',",
|
|
||||||
" (SELECT COALESCE(jsonb_agg(jsonb_build_object(",
|
|
||||||
" 'archived', entity_20.archived,",
|
|
||||||
" 'created_at', entity_20.created_at,",
|
|
||||||
" 'id', entity_20.id,",
|
|
||||||
" 'is_primary', contact_18.is_primary,",
|
|
||||||
" 'name', entity_20.name,",
|
|
||||||
" 'target',",
|
|
||||||
" (SELECT jsonb_build_object(",
|
|
||||||
" 'address', email_address_21.address,",
|
|
||||||
" 'archived', entity_22.archived,",
|
|
||||||
" 'created_at', entity_22.created_at,",
|
|
||||||
" 'id', entity_22.id,",
|
|
||||||
" 'name', entity_22.name,",
|
|
||||||
" 'type', entity_22.type",
|
|
||||||
" )",
|
|
||||||
" FROM agreego.email_address email_address_21",
|
|
||||||
" JOIN agreego.entity entity_22 ON entity_22.id = email_address_21.id",
|
|
||||||
" WHERE",
|
|
||||||
" NOT entity_22.archived",
|
|
||||||
" AND relationship_19.target_id = email_address_21.id),",
|
|
||||||
" 'type', entity_20.type",
|
|
||||||
" )), '[]'::jsonb)",
|
|
||||||
" FROM agreego.contact contact_18",
|
|
||||||
" JOIN agreego.relationship relationship_19 ON relationship_19.id = contact_18.id",
|
|
||||||
" JOIN agreego.entity entity_20 ON entity_20.id = relationship_19.id",
|
|
||||||
" WHERE",
|
|
||||||
" NOT entity_20.archived",
|
|
||||||
" AND contact_18.parent_id = entity_3.id),",
|
|
||||||
" 'first_name', person_1.first_name,",
|
|
||||||
" 'id', entity_3.id,",
|
|
||||||
" 'last_name', person_1.last_name,",
|
|
||||||
" 'name', entity_3.name,",
|
|
||||||
" 'phone_numbers',",
|
|
||||||
" (SELECT COALESCE(jsonb_agg(jsonb_build_object(",
|
|
||||||
" 'archived', entity_25.archived,",
|
|
||||||
" 'created_at', entity_25.created_at,",
|
|
||||||
" 'id', entity_25.id,",
|
|
||||||
" 'is_primary', contact_23.is_primary,",
|
|
||||||
" 'name', entity_25.name,",
|
|
||||||
" 'target',",
|
|
||||||
" (SELECT jsonb_build_object(",
|
|
||||||
" 'archived', entity_27.archived,",
|
|
||||||
" 'created_at', entity_27.created_at,",
|
|
||||||
" 'id', entity_27.id,",
|
|
||||||
" 'name', entity_27.name,",
|
|
||||||
" 'number', phone_number_26.number,",
|
|
||||||
" 'type', entity_27.type",
|
|
||||||
" )",
|
|
||||||
" FROM agreego.phone_number phone_number_26",
|
|
||||||
" JOIN agreego.entity entity_27 ON entity_27.id = phone_number_26.id",
|
|
||||||
" WHERE",
|
|
||||||
" NOT entity_27.archived",
|
|
||||||
" AND relationship_24.target_id = phone_number_26.id),",
|
|
||||||
" 'type', entity_25.type",
|
|
||||||
" )), '[]'::jsonb)",
|
|
||||||
" FROM agreego.contact contact_23",
|
|
||||||
" JOIN agreego.relationship relationship_24 ON relationship_24.id = contact_23.id",
|
|
||||||
" JOIN agreego.entity entity_25 ON entity_25.id = relationship_24.id",
|
|
||||||
" WHERE",
|
|
||||||
" NOT entity_25.archived",
|
|
||||||
" AND contact_23.parent_id = entity_3.id),",
|
|
||||||
" 'type', entity_3.type",
|
|
||||||
")",
|
|
||||||
"FROM agreego.person person_1",
|
|
||||||
"JOIN agreego.organization organization_2 ON organization_2.id = person_1.id",
|
|
||||||
"JOIN agreego.entity entity_3 ON entity_3.id = organization_2.id",
|
|
||||||
"WHERE NOT entity_3.archived)"
|
|
||||||
]
|
|
||||||
]
|
|
||||||
Reference in New Issue
Block a user