queryer test checkpoit
This commit is contained in:
5
.vscode/extensions.json
vendored
Normal file
5
.vscode/extensions.json
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"recommendations": [
|
||||||
|
"rust-lang.rust-analyzer"
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -230,43 +230,42 @@ impl Database {
|
|||||||
|
|
||||||
fn collect_relations(&mut self, raw_relations: Vec<Relation>) {
|
fn collect_relations(&mut self, raw_relations: Vec<Relation>) {
|
||||||
let mut edges: HashMap<(String, String), Vec<Relation>> = HashMap::new();
|
let mut edges: HashMap<(String, String), Vec<Relation>> = HashMap::new();
|
||||||
|
|
||||||
// For every relation, map it across all polymorphic inheritance permutations
|
// For every relation, map it across all polymorphic inheritance permutations
|
||||||
for relation in raw_relations {
|
for relation in raw_relations {
|
||||||
if let Some(source_type_def) = self.types.get(&relation.source_type) {
|
if let Some(_source_type_def) = self.types.get(&relation.source_type) {
|
||||||
if let Some(dest_type_def) = self.types.get(&relation.destination_type) {
|
if let Some(_dest_type_def) = self.types.get(&relation.destination_type) {
|
||||||
|
|
||||||
let mut src_descendants = Vec::new();
|
let mut src_descendants = Vec::new();
|
||||||
let mut dest_descendants = Vec::new();
|
let mut dest_descendants = Vec::new();
|
||||||
|
|
||||||
for (t_name, t_def) in &self.types {
|
for (t_name, t_def) in &self.types {
|
||||||
if t_def.hierarchy.contains(&relation.source_type) {
|
if t_def.hierarchy.contains(&relation.source_type) {
|
||||||
src_descendants.push(t_name.clone());
|
src_descendants.push(t_name.clone());
|
||||||
}
|
}
|
||||||
if t_def.hierarchy.contains(&relation.destination_type) {
|
if t_def.hierarchy.contains(&relation.destination_type) {
|
||||||
dest_descendants.push(t_name.clone());
|
dest_descendants.push(t_name.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for p_type in &src_descendants {
|
for p_type in &src_descendants {
|
||||||
for c_type in &dest_descendants {
|
for c_type in &dest_descendants {
|
||||||
// Ignore entity <-> entity generic fallbacks, they aren't useful edges
|
// Ignore entity <-> entity generic fallbacks, they aren't useful edges
|
||||||
if p_type == "entity" && c_type == "entity" {
|
if p_type == "entity" && c_type == "entity" {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Forward edge
|
// Forward edge
|
||||||
edges
|
edges
|
||||||
.entry((p_type.clone(), c_type.clone()))
|
.entry((p_type.clone(), c_type.clone()))
|
||||||
.or_default()
|
.or_default()
|
||||||
.push(relation.clone());
|
.push(relation.clone());
|
||||||
|
|
||||||
// Reverse edge (only if types are different to avoid duplicating self-referential edges like activity parent_id)
|
// Reverse edge (only if types are different to avoid duplicating self-referential edges like activity parent_id)
|
||||||
if p_type != c_type {
|
if p_type != c_type {
|
||||||
edges
|
edges
|
||||||
.entry((c_type.clone(), p_type.clone()))
|
.entry((c_type.clone(), p_type.clone()))
|
||||||
.or_default()
|
.or_default()
|
||||||
.push(relation.clone());
|
.push(relation.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -277,17 +276,20 @@ impl Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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> {
|
||||||
if let Some(relations) = self.relations.get(&(parent_type.to_string(), child_type.to_string())) {
|
if let Some(relations) = self
|
||||||
|
.relations
|
||||||
|
.get(&(parent_type.to_string(), child_type.to_string()))
|
||||||
|
{
|
||||||
if relations.len() == 1 {
|
if relations.len() == 1 {
|
||||||
return Some(&relations[0]);
|
return Some(&relations[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reduce ambiguity with prefix
|
// Reduce ambiguity with prefix
|
||||||
for rel in relations {
|
for rel in relations {
|
||||||
if let Some(prefix) = &rel.prefix {
|
if let Some(prefix) = &rel.prefix {
|
||||||
@ -302,13 +304,13 @@ impl Database {
|
|||||||
let mut missing_prefix_rels = Vec::new();
|
let mut missing_prefix_rels = Vec::new();
|
||||||
for rel in relations {
|
for rel in relations {
|
||||||
if let Some(prefix) = &rel.prefix {
|
if let Some(prefix) = &rel.prefix {
|
||||||
if !keys.contains(prefix) {
|
if !keys.contains(prefix) {
|
||||||
missing_prefix_rels.push(rel);
|
missing_prefix_rels.push(rel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if missing_prefix_rels.len() == 1 {
|
if missing_prefix_rels.len() == 1 {
|
||||||
return Some(missing_prefix_rels[0]);
|
return Some(missing_prefix_rels[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -424,14 +426,14 @@ impl Database {
|
|||||||
if let (Some(pt), Some(prop)) = (&parent_type, &property_name) {
|
if let (Some(pt), Some(prop)) = (&parent_type, &property_name) {
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
relation_col = Some(expected_col);
|
relation_col = Some(expected_col);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 val_resolved = Value::Null;
|
let mut val_resolved = Value::Null;
|
||||||
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, &mut notifications_queue);
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
@ -44,7 +44,7 @@ impl Merger {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Execute the globally collected, pre-ordered notifications last!
|
// Execute the globally collected, pre-ordered notifications last!
|
||||||
for notify_sql in notifications_queue {
|
for notify_sql in notifications_queue {
|
||||||
if let Err(e) = self.db.execute(¬ify_sql, None) {
|
if let Err(e) = self.db.execute(¬ify_sql, None) {
|
||||||
return crate::drop::Drop::with_errors(vec![crate::drop::Error {
|
return crate::drop::Drop::with_errors(vec![crate::drop::Error {
|
||||||
@ -88,7 +88,11 @@ impl Merger {
|
|||||||
crate::drop::Drop::success_with_val(stripped_val)
|
crate::drop::Drop::success_with_val(stripped_val)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn merge_internal(&self, data: Value, notifications: &mut Vec<String>) -> Result<Value, String> {
|
pub(crate) fn merge_internal(
|
||||||
|
&self,
|
||||||
|
data: Value,
|
||||||
|
notifications: &mut Vec<String>,
|
||||||
|
) -> Result<Value, String> {
|
||||||
match data {
|
match data {
|
||||||
Value::Array(items) => self.merge_array(items, notifications),
|
Value::Array(items) => self.merge_array(items, notifications),
|
||||||
Value::Object(map) => self.merge_object(map, notifications),
|
Value::Object(map) => self.merge_object(map, notifications),
|
||||||
@ -96,7 +100,11 @@ impl Merger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn merge_array(&self, items: Vec<Value>, notifications: &mut Vec<String>) -> Result<Value, String> {
|
fn merge_array(
|
||||||
|
&self,
|
||||||
|
items: Vec<Value>,
|
||||||
|
notifications: &mut Vec<String>,
|
||||||
|
) -> Result<Value, String> {
|
||||||
let mut resolved_items = Vec::new();
|
let mut resolved_items = Vec::new();
|
||||||
for item in items {
|
for item in items {
|
||||||
let resolved = self.merge_internal(item, notifications)?;
|
let resolved = self.merge_internal(item, notifications)?;
|
||||||
@ -105,7 +113,11 @@ impl Merger {
|
|||||||
Ok(Value::Array(resolved_items))
|
Ok(Value::Array(resolved_items))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn merge_object(&self, obj: serde_json::Map<String, Value>, notifications: &mut Vec<String>) -> Result<Value, String> {
|
fn merge_object(
|
||||||
|
&self,
|
||||||
|
obj: serde_json::Map<String, Value>,
|
||||||
|
notifications: &mut Vec<String>,
|
||||||
|
) -> Result<Value, String> {
|
||||||
let queue_start = notifications.len();
|
let queue_start = notifications.len();
|
||||||
|
|
||||||
let type_name = match obj.get("type").and_then(|v| v.as_str()) {
|
let type_name = match obj.get("type").and_then(|v| v.as_str()) {
|
||||||
@ -173,7 +185,12 @@ impl Merger {
|
|||||||
let relative_keys: Vec<String> = relative.keys().cloned().collect();
|
let relative_keys: Vec<String> = relative.keys().cloned().collect();
|
||||||
|
|
||||||
// Call central Database O(1) graph logic
|
// Call central Database O(1) graph logic
|
||||||
let relative_relation = self.db.get_relation(&type_def.name, relative_type_name, &relation_name, Some(&relative_keys));
|
let relative_relation = self.db.get_relation(
|
||||||
|
&type_def.name,
|
||||||
|
relative_type_name,
|
||||||
|
&relation_name,
|
||||||
|
Some(&relative_keys),
|
||||||
|
);
|
||||||
|
|
||||||
if let Some(relation) = relative_relation {
|
if let Some(relation) = relative_relation {
|
||||||
let parent_is_source = type_def.hierarchy.contains(&relation.source_type);
|
let parent_is_source = type_def.hierarchy.contains(&relation.source_type);
|
||||||
@ -271,7 +288,12 @@ impl Merger {
|
|||||||
let relative_keys: Vec<String> = first_relative.keys().cloned().collect();
|
let relative_keys: Vec<String> = first_relative.keys().cloned().collect();
|
||||||
|
|
||||||
// Call central Database O(1) graph logic
|
// Call central Database O(1) graph logic
|
||||||
let relative_relation = self.db.get_relation(&type_def.name, relative_type_name, &relation_name, Some(&relative_keys));
|
let relative_relation = self.db.get_relation(
|
||||||
|
&type_def.name,
|
||||||
|
relative_type_name,
|
||||||
|
&relation_name,
|
||||||
|
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();
|
||||||
@ -290,10 +312,11 @@ impl Merger {
|
|||||||
&entity_fields,
|
&entity_fields,
|
||||||
);
|
);
|
||||||
|
|
||||||
let merged_relative = match self.merge_internal(Value::Object(relative_item), notifications)? {
|
let merged_relative =
|
||||||
Value::Object(m) => m,
|
match self.merge_internal(Value::Object(relative_item), notifications)? {
|
||||||
_ => continue,
|
Value::Object(m) => m,
|
||||||
};
|
_ => continue,
|
||||||
|
};
|
||||||
|
|
||||||
relative_responses.push(Value::Object(merged_relative));
|
relative_responses.push(Value::Object(merged_relative));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -47,7 +47,19 @@ impl SqlCompiler {
|
|||||||
|
|
||||||
// We expect the top level to typically be an Object or Array
|
// We expect the top level to typically be an Object or Array
|
||||||
let is_stem_query = stem_path.is_some();
|
let is_stem_query = stem_path.is_some();
|
||||||
let (sql, _) = self.walk_schema(target_schema, "t1", None, None, filter_keys, is_stem_query, 0, String::new())?;
|
let mut alias_counter: usize = 0;
|
||||||
|
let (sql, _) = self.walk_schema(
|
||||||
|
target_schema,
|
||||||
|
"t1",
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
filter_keys,
|
||||||
|
is_stem_query,
|
||||||
|
0,
|
||||||
|
String::new(),
|
||||||
|
&mut alias_counter,
|
||||||
|
)?;
|
||||||
Ok(sql)
|
Ok(sql)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,12 +69,14 @@ impl SqlCompiler {
|
|||||||
&self,
|
&self,
|
||||||
schema: &crate::database::schema::Schema,
|
schema: &crate::database::schema::Schema,
|
||||||
parent_alias: &str,
|
parent_alias: &str,
|
||||||
|
parent_table_aliases: Option<&std::collections::HashMap<String, String>>,
|
||||||
parent_type_def: Option<&crate::database::r#type::Type>,
|
parent_type_def: Option<&crate::database::r#type::Type>,
|
||||||
prop_name_context: Option<&str>,
|
prop_name_context: Option<&str>,
|
||||||
filter_keys: &[String],
|
filter_keys: &[String],
|
||||||
is_stem_query: bool,
|
is_stem_query: bool,
|
||||||
depth: usize,
|
depth: usize,
|
||||||
current_path: String,
|
current_path: String,
|
||||||
|
alias_counter: &mut usize,
|
||||||
) -> Result<(String, String), String> {
|
) -> Result<(String, String), String> {
|
||||||
// Determine the base schema type (could be an array, object, or literal)
|
// Determine the base schema type (could be an array, object, or literal)
|
||||||
match &schema.obj.type_ {
|
match &schema.obj.type_ {
|
||||||
@ -81,6 +95,7 @@ impl SqlCompiler {
|
|||||||
items,
|
items,
|
||||||
type_def,
|
type_def,
|
||||||
parent_alias,
|
parent_alias,
|
||||||
|
parent_table_aliases,
|
||||||
parent_type_def,
|
parent_type_def,
|
||||||
prop_name_context,
|
prop_name_context,
|
||||||
true,
|
true,
|
||||||
@ -88,18 +103,21 @@ impl SqlCompiler {
|
|||||||
is_stem_query,
|
is_stem_query,
|
||||||
depth,
|
depth,
|
||||||
next_path,
|
next_path,
|
||||||
|
alias_counter,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let (item_sql, _) = self.walk_schema(
|
let (item_sql, _) = self.walk_schema(
|
||||||
items,
|
items,
|
||||||
parent_alias,
|
parent_alias,
|
||||||
|
parent_table_aliases,
|
||||||
parent_type_def,
|
parent_type_def,
|
||||||
prop_name_context,
|
prop_name_context,
|
||||||
filter_keys,
|
filter_keys,
|
||||||
is_stem_query,
|
is_stem_query,
|
||||||
depth + 1,
|
depth + 1,
|
||||||
next_path,
|
next_path,
|
||||||
|
alias_counter,
|
||||||
)?;
|
)?;
|
||||||
return Ok((
|
return Ok((
|
||||||
format!("(SELECT jsonb_agg({}) FROM TODO)", item_sql),
|
format!("(SELECT jsonb_agg({}) FROM TODO)", item_sql),
|
||||||
@ -128,6 +146,7 @@ impl SqlCompiler {
|
|||||||
schema,
|
schema,
|
||||||
type_def,
|
type_def,
|
||||||
parent_alias,
|
parent_alias,
|
||||||
|
parent_table_aliases,
|
||||||
parent_type_def,
|
parent_type_def,
|
||||||
prop_name_context,
|
prop_name_context,
|
||||||
false,
|
false,
|
||||||
@ -135,6 +154,7 @@ impl SqlCompiler {
|
|||||||
is_stem_query,
|
is_stem_query,
|
||||||
depth,
|
depth,
|
||||||
current_path,
|
current_path,
|
||||||
|
alias_counter,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,12 +165,14 @@ impl SqlCompiler {
|
|||||||
return self.walk_schema(
|
return self.walk_schema(
|
||||||
target_schema,
|
target_schema,
|
||||||
parent_alias,
|
parent_alias,
|
||||||
|
parent_table_aliases,
|
||||||
parent_type_def,
|
parent_type_def,
|
||||||
prop_name_context,
|
prop_name_context,
|
||||||
filter_keys,
|
filter_keys,
|
||||||
is_stem_query,
|
is_stem_query,
|
||||||
depth,
|
depth,
|
||||||
current_path,
|
current_path,
|
||||||
|
alias_counter,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return Err(format!("Unresolved $ref: {}", ref_id));
|
return Err(format!("Unresolved $ref: {}", ref_id));
|
||||||
@ -174,12 +196,14 @@ impl SqlCompiler {
|
|||||||
return self.compile_one_of(
|
return self.compile_one_of(
|
||||||
&family_schemas,
|
&family_schemas,
|
||||||
parent_alias,
|
parent_alias,
|
||||||
|
parent_table_aliases,
|
||||||
parent_type_def,
|
parent_type_def,
|
||||||
prop_name_context,
|
prop_name_context,
|
||||||
filter_keys,
|
filter_keys,
|
||||||
is_stem_query,
|
is_stem_query,
|
||||||
depth,
|
depth,
|
||||||
current_path,
|
current_path,
|
||||||
|
alias_counter,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,12 +212,14 @@ impl SqlCompiler {
|
|||||||
return self.compile_one_of(
|
return self.compile_one_of(
|
||||||
one_of,
|
one_of,
|
||||||
parent_alias,
|
parent_alias,
|
||||||
|
parent_table_aliases,
|
||||||
parent_type_def,
|
parent_type_def,
|
||||||
prop_name_context,
|
prop_name_context,
|
||||||
filter_keys,
|
filter_keys,
|
||||||
is_stem_query,
|
is_stem_query,
|
||||||
depth,
|
depth,
|
||||||
current_path,
|
current_path,
|
||||||
|
alias_counter,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,11 +228,13 @@ impl SqlCompiler {
|
|||||||
return self.compile_inline_object(
|
return self.compile_inline_object(
|
||||||
props,
|
props,
|
||||||
parent_alias,
|
parent_alias,
|
||||||
|
parent_table_aliases,
|
||||||
parent_type_def,
|
parent_type_def,
|
||||||
filter_keys,
|
filter_keys,
|
||||||
is_stem_query,
|
is_stem_query,
|
||||||
depth,
|
depth,
|
||||||
current_path,
|
current_path,
|
||||||
|
alias_counter,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -249,6 +277,7 @@ impl SqlCompiler {
|
|||||||
schema: &crate::database::schema::Schema,
|
schema: &crate::database::schema::Schema,
|
||||||
type_def: &crate::database::r#type::Type,
|
type_def: &crate::database::r#type::Type,
|
||||||
parent_alias: &str,
|
parent_alias: &str,
|
||||||
|
parent_table_aliases: Option<&std::collections::HashMap<String, String>>,
|
||||||
parent_type_def: Option<&crate::database::r#type::Type>,
|
parent_type_def: Option<&crate::database::r#type::Type>,
|
||||||
prop_name: Option<&str>,
|
prop_name: Option<&str>,
|
||||||
is_array: bool,
|
is_array: bool,
|
||||||
@ -256,11 +285,10 @@ impl SqlCompiler {
|
|||||||
is_stem_query: bool,
|
is_stem_query: bool,
|
||||||
depth: usize,
|
depth: usize,
|
||||||
current_path: String,
|
current_path: String,
|
||||||
|
alias_counter: &mut usize,
|
||||||
) -> Result<(String, String), String> {
|
) -> Result<(String, String), String> {
|
||||||
let local_ctx = format!("{}_{}", parent_alias, prop_name.unwrap_or("obj"));
|
|
||||||
|
|
||||||
// 1. Build FROM clauses and table aliases
|
// 1. Build FROM clauses and table aliases
|
||||||
let (table_aliases, from_clauses) = self.build_hierarchy_from_clauses(type_def, &local_ctx);
|
let (table_aliases, from_clauses) = self.build_hierarchy_from_clauses(type_def, alias_counter);
|
||||||
|
|
||||||
// 2. Map properties and build jsonb_build_object args
|
// 2. Map properties and build jsonb_build_object args
|
||||||
let mut select_args = self.map_properties_to_aliases(
|
let mut select_args = self.map_properties_to_aliases(
|
||||||
@ -272,6 +300,7 @@ impl SqlCompiler {
|
|||||||
is_stem_query,
|
is_stem_query,
|
||||||
depth,
|
depth,
|
||||||
¤t_path,
|
¤t_path,
|
||||||
|
alias_counter,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// 2.5 Inject polymorphism directly into the query object
|
// 2.5 Inject polymorphism directly into the query object
|
||||||
@ -281,10 +310,10 @@ impl SqlCompiler {
|
|||||||
let mut sorted_targets: Vec<String> = base_type.variations.iter().cloned().collect();
|
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
|
// Ensure the base type is included if not listed in variations by default
|
||||||
if !sorted_targets.contains(family_target) {
|
if !sorted_targets.contains(family_target) {
|
||||||
sorted_targets.push(family_target.clone());
|
sorted_targets.push(family_target.clone());
|
||||||
}
|
}
|
||||||
sorted_targets.sort();
|
sorted_targets.sort();
|
||||||
|
|
||||||
for target in sorted_targets {
|
for target in sorted_targets {
|
||||||
let mut ref_schema = crate::database::schema::Schema::default();
|
let mut ref_schema = crate::database::schema::Schema::default();
|
||||||
ref_schema.obj.r#ref = Some(target);
|
ref_schema.obj.r#ref = Some(target);
|
||||||
@ -297,14 +326,42 @@ impl SqlCompiler {
|
|||||||
family_schemas.push(std::sync::Arc::new(ref_schema));
|
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());
|
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));
|
select_args.push(format!("'id', {}.id", base_alias));
|
||||||
let (case_sql, _) = self.compile_one_of(&family_schemas, &base_alias, parent_type_def, None, filter_keys, is_stem_query, depth, current_path.clone())?;
|
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));
|
select_args.push(format!("'type', {}", case_sql));
|
||||||
} else if let Some(one_of) = &schema.obj.one_of {
|
} 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());
|
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));
|
select_args.push(format!("'id', {}.id", base_alias));
|
||||||
let (case_sql, _) = self.compile_one_of(one_of, &base_alias, parent_type_def, None, filter_keys, is_stem_query, depth, current_path.clone())?;
|
let (case_sql, _) = self.compile_one_of(
|
||||||
|
one_of,
|
||||||
|
&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));
|
select_args.push(format!("'type', {}", case_sql));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -320,6 +377,7 @@ impl SqlCompiler {
|
|||||||
type_def,
|
type_def,
|
||||||
&table_aliases,
|
&table_aliases,
|
||||||
parent_alias,
|
parent_alias,
|
||||||
|
parent_table_aliases,
|
||||||
parent_type_def,
|
parent_type_def,
|
||||||
prop_name,
|
prop_name,
|
||||||
filter_keys,
|
filter_keys,
|
||||||
@ -352,19 +410,20 @@ impl SqlCompiler {
|
|||||||
fn build_hierarchy_from_clauses(
|
fn build_hierarchy_from_clauses(
|
||||||
&self,
|
&self,
|
||||||
type_def: &crate::database::r#type::Type,
|
type_def: &crate::database::r#type::Type,
|
||||||
local_ctx: &str,
|
alias_counter: &mut usize,
|
||||||
) -> (std::collections::HashMap<String, String>, Vec<String>) {
|
) -> (std::collections::HashMap<String, String>, Vec<String>) {
|
||||||
let mut table_aliases = std::collections::HashMap::new();
|
let mut table_aliases = std::collections::HashMap::new();
|
||||||
let mut from_clauses = Vec::new();
|
let mut from_clauses = Vec::new();
|
||||||
|
|
||||||
for (i, table_name) in type_def.hierarchy.iter().enumerate() {
|
for (i, table_name) in type_def.hierarchy.iter().enumerate() {
|
||||||
let alias = format!("{}_t{}", local_ctx, i + 1);
|
*alias_counter += 1;
|
||||||
|
let alias = format!("{}_{}", table_name, alias_counter);
|
||||||
table_aliases.insert(table_name.clone(), alias.clone());
|
table_aliases.insert(table_name.clone(), alias.clone());
|
||||||
|
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
from_clauses.push(format!("agreego.{} {}", table_name, alias));
|
from_clauses.push(format!("agreego.{} {}", table_name, alias));
|
||||||
} else {
|
} else {
|
||||||
let prev_alias = format!("{}_t{}", local_ctx, i);
|
let prev_alias = format!("{}_{}", type_def.hierarchy[i - 1], *alias_counter - 1);
|
||||||
from_clauses.push(format!(
|
from_clauses.push(format!(
|
||||||
"JOIN agreego.{} {} ON {}.id = {}.id",
|
"JOIN agreego.{} {} ON {}.id = {}.id",
|
||||||
table_name, alias, alias, prev_alias
|
table_name, alias, alias, prev_alias
|
||||||
@ -384,6 +443,7 @@ impl SqlCompiler {
|
|||||||
is_stem_query: bool,
|
is_stem_query: bool,
|
||||||
depth: usize,
|
depth: usize,
|
||||||
current_path: &str,
|
current_path: &str,
|
||||||
|
alias_counter: &mut usize,
|
||||||
) -> Result<Vec<String>, String> {
|
) -> Result<Vec<String>, String> {
|
||||||
let mut select_args = Vec::new();
|
let mut select_args = Vec::new();
|
||||||
let grouped_fields = type_def.grouped_fields.as_ref().and_then(|v| v.as_object());
|
let grouped_fields = type_def.grouped_fields.as_ref().and_then(|v| v.as_object());
|
||||||
@ -410,16 +470,20 @@ impl SqlCompiler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let is_object_or_array = match &prop_schema.obj.type_ {
|
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::Single(s)) => {
|
||||||
Some(crate::database::schema::SchemaTypeOrArray::Multiple(v)) => v.contains(&"object".to_string()) || v.contains(&"array".to_string()),
|
s == "object" || s == "array"
|
||||||
_ => false
|
}
|
||||||
|
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()
|
let is_primitive = prop_schema.obj.r#ref.is_none()
|
||||||
&& prop_schema.obj.items.is_none()
|
&& prop_schema.obj.items.is_none()
|
||||||
&& prop_schema.obj.properties.is_none()
|
&& prop_schema.obj.properties.is_none()
|
||||||
&& prop_schema.obj.one_of.is_none()
|
&& prop_schema.obj.one_of.is_none()
|
||||||
&& !is_object_or_array;
|
&& !is_object_or_array;
|
||||||
|
|
||||||
if is_primitive {
|
if is_primitive {
|
||||||
if let Some(ft) = type_def.field_types.as_ref().and_then(|v| v.as_object()) {
|
if let Some(ft) = type_def.field_types.as_ref().and_then(|v| v.as_object()) {
|
||||||
@ -438,12 +502,14 @@ impl SqlCompiler {
|
|||||||
let (val_sql, val_type) = self.walk_schema(
|
let (val_sql, val_type) = self.walk_schema(
|
||||||
prop_schema,
|
prop_schema,
|
||||||
&owner_alias,
|
&owner_alias,
|
||||||
|
Some(table_aliases),
|
||||||
Some(type_def), // Pass current type_def as parent_type_def for child properties
|
Some(type_def), // Pass current type_def as parent_type_def for child properties
|
||||||
Some(prop_key),
|
Some(prop_key),
|
||||||
filter_keys,
|
filter_keys,
|
||||||
is_stem_query,
|
is_stem_query,
|
||||||
depth + 1,
|
depth + 1,
|
||||||
next_path,
|
next_path,
|
||||||
|
alias_counter,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
if val_type != "abort" {
|
if val_type != "abort" {
|
||||||
@ -459,6 +525,7 @@ impl SqlCompiler {
|
|||||||
type_def: &crate::database::r#type::Type,
|
type_def: &crate::database::r#type::Type,
|
||||||
table_aliases: &std::collections::HashMap<String, String>,
|
table_aliases: &std::collections::HashMap<String, String>,
|
||||||
parent_alias: &str,
|
parent_alias: &str,
|
||||||
|
parent_table_aliases: Option<&std::collections::HashMap<String, String>>,
|
||||||
parent_type_def: Option<&crate::database::r#type::Type>,
|
parent_type_def: Option<&crate::database::r#type::Type>,
|
||||||
prop_name: Option<&str>,
|
prop_name: Option<&str>,
|
||||||
filter_keys: &[String],
|
filter_keys: &[String],
|
||||||
@ -503,129 +570,151 @@ impl SqlCompiler {
|
|||||||
let mut filter_alias = base_alias.clone();
|
let mut filter_alias = base_alias.clone();
|
||||||
|
|
||||||
if let Some(gf) = type_def.grouped_fields.as_ref().and_then(|v| v.as_object()) {
|
if let Some(gf) = type_def.grouped_fields.as_ref().and_then(|v| v.as_object()) {
|
||||||
for (t_name, fields_val) in gf {
|
for (t_name, fields_val) in gf {
|
||||||
if let Some(fields_arr) = fields_val.as_array() {
|
if let Some(fields_arr) = fields_val.as_array() {
|
||||||
if fields_arr.iter().any(|v| v.as_str() == Some(field_name)) {
|
if fields_arr.iter().any(|v| v.as_str() == Some(field_name)) {
|
||||||
filter_alias = table_aliases
|
filter_alias = table_aliases
|
||||||
.get(t_name)
|
.get(t_name)
|
||||||
.cloned()
|
.cloned()
|
||||||
.unwrap_or_else(|| base_alias.clone());
|
.unwrap_or_else(|| base_alias.clone());
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut is_ilike = false;
|
||||||
|
let mut cast = "";
|
||||||
|
|
||||||
|
if let Some(field_types) = type_def.field_types.as_ref().and_then(|v| v.as_object()) {
|
||||||
|
if let Some(pg_type_val) = field_types.get(field_name) {
|
||||||
|
if let Some(pg_type) = pg_type_val.as_str() {
|
||||||
|
if pg_type == "uuid" {
|
||||||
|
cast = "::uuid";
|
||||||
|
} else if pg_type == "boolean" || pg_type == "bool" {
|
||||||
|
cast = "::boolean";
|
||||||
|
} else if pg_type.contains("timestamp") || pg_type == "timestamptz" || pg_type == "date"
|
||||||
|
{
|
||||||
|
cast = "::timestamptz";
|
||||||
|
} else if pg_type == "numeric"
|
||||||
|
|| pg_type.contains("int")
|
||||||
|
|| pg_type == "real"
|
||||||
|
|| pg_type == "double precision"
|
||||||
|
{
|
||||||
|
cast = "::numeric";
|
||||||
|
} else if pg_type == "text" || pg_type.contains("char") {
|
||||||
|
let mut is_enum = false;
|
||||||
|
if let Some(props) = &schema.obj.properties {
|
||||||
|
if let Some(ps) = props.get(field_name) {
|
||||||
|
is_enum = ps.obj.enum_.is_some();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !is_enum {
|
||||||
|
is_ilike = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut is_ilike = false;
|
let param_index = i + 1;
|
||||||
let mut cast = "";
|
let p_val = format!("${}#>>'{{}}'", param_index);
|
||||||
|
|
||||||
if let Some(field_types) = type_def.field_types.as_ref().and_then(|v| v.as_object()) {
|
if op == "$in" || op == "$nin" {
|
||||||
if let Some(pg_type_val) = field_types.get(field_name) {
|
let sql_op = if op == "$in" { "IN" } else { "NOT IN" };
|
||||||
if let Some(pg_type) = pg_type_val.as_str() {
|
let subquery = format!(
|
||||||
if pg_type == "uuid" {
|
"(SELECT value{} FROM jsonb_array_elements_text(({})::jsonb))",
|
||||||
cast = "::uuid";
|
cast, p_val
|
||||||
} else if pg_type == "boolean" || pg_type == "bool" {
|
);
|
||||||
cast = "::boolean";
|
where_clauses.push(format!(
|
||||||
} else if pg_type.contains("timestamp")
|
"{}.{} {} {}",
|
||||||
|| pg_type == "timestamptz"
|
filter_alias, field_name, sql_op, subquery
|
||||||
|| pg_type == "date"
|
));
|
||||||
{
|
} else {
|
||||||
cast = "::timestamptz";
|
let sql_op = match op {
|
||||||
} else if pg_type == "numeric"
|
"$eq" => {
|
||||||
|| pg_type.contains("int")
|
if is_ilike {
|
||||||
|| pg_type == "real"
|
"ILIKE"
|
||||||
|| pg_type == "double precision"
|
} else {
|
||||||
{
|
"="
|
||||||
cast = "::numeric";
|
|
||||||
} else if pg_type == "text" || pg_type.contains("char") {
|
|
||||||
let mut is_enum = false;
|
|
||||||
if let Some(props) = &schema.obj.properties {
|
|
||||||
if let Some(ps) = props.get(field_name) {
|
|
||||||
is_enum = ps.obj.enum_.is_some();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !is_enum {
|
|
||||||
is_ilike = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
"$ne" => {
|
||||||
|
if is_ilike {
|
||||||
|
"NOT ILIKE"
|
||||||
|
} else {
|
||||||
|
"!="
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"$gt" => ">",
|
||||||
|
"$gte" => ">=",
|
||||||
|
"$lt" => "<",
|
||||||
|
"$lte" => "<=",
|
||||||
|
_ => {
|
||||||
|
if is_ilike {
|
||||||
|
"ILIKE"
|
||||||
|
} else {
|
||||||
|
"="
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let param_index = i + 1;
|
let param_sql = if is_ilike && (op == "$eq" || op == "$ne") {
|
||||||
let p_val = format!("${}#>>'{{}}'", param_index);
|
p_val
|
||||||
|
|
||||||
if op == "$in" || op == "$nin" {
|
|
||||||
let sql_op = if op == "$in" { "IN" } else { "NOT IN" };
|
|
||||||
let subquery = format!(
|
|
||||||
"(SELECT value{} FROM jsonb_array_elements_text(({})::jsonb))",
|
|
||||||
cast, p_val
|
|
||||||
);
|
|
||||||
where_clauses.push(format!(
|
|
||||||
"{}.{} {} {}",
|
|
||||||
filter_alias, field_name, sql_op, subquery
|
|
||||||
));
|
|
||||||
} else {
|
} else {
|
||||||
let sql_op = match op {
|
format!("({}){}", p_val, cast)
|
||||||
"$eq" => {
|
};
|
||||||
if is_ilike {
|
|
||||||
"ILIKE"
|
|
||||||
} else {
|
|
||||||
"="
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"$ne" => {
|
|
||||||
if is_ilike {
|
|
||||||
"NOT ILIKE"
|
|
||||||
} else {
|
|
||||||
"!="
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"$gt" => ">",
|
|
||||||
"$gte" => ">=",
|
|
||||||
"$lt" => "<",
|
|
||||||
"$lte" => "<=",
|
|
||||||
_ => {
|
|
||||||
if is_ilike {
|
|
||||||
"ILIKE"
|
|
||||||
} else {
|
|
||||||
"="
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let param_sql = if is_ilike && (op == "$eq" || op == "$ne") {
|
where_clauses.push(format!(
|
||||||
p_val
|
"{}.{} {} {}",
|
||||||
} else {
|
filter_alias, field_name, sql_op, param_sql
|
||||||
format!("({}){}", p_val, cast)
|
));
|
||||||
};
|
|
||||||
|
|
||||||
where_clauses.push(format!(
|
|
||||||
"{}.{} {} {}",
|
|
||||||
filter_alias, field_name, sql_op, param_sql
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(prop) = prop_name {
|
if let Some(prop) = prop_name {
|
||||||
// Find what type the parent alias is actually mapping to
|
// Find what type the parent alias is actually mapping to
|
||||||
let mut relation_alias = parent_alias.to_string();
|
let mut relation_alias = parent_alias.to_string();
|
||||||
|
|
||||||
let mut relation_resolved = false;
|
let mut relation_resolved = false;
|
||||||
if let Some(parent_type) = parent_type_def {
|
if let Some(parent_type) = parent_type_def {
|
||||||
if let Some(relation) = self.db.get_relation(&parent_type.name, &type_def.name, prop, None) {
|
if let Some(relation) = self
|
||||||
|
.db
|
||||||
|
.get_relation(&parent_type.name, &type_def.name, prop, None)
|
||||||
|
{
|
||||||
let source_col = &relation.source_columns[0];
|
let source_col = &relation.source_columns[0];
|
||||||
let dest_col = &relation.destination_columns[0];
|
let dest_col = &relation.destination_columns[0];
|
||||||
|
|
||||||
|
let mut possible_relation_alias = None;
|
||||||
|
if let Some(pta) = parent_table_aliases {
|
||||||
|
if let Some(a) = pta.get(&relation.source_type) {
|
||||||
|
possible_relation_alias = Some(a.clone());
|
||||||
|
} else if let Some(a) = pta.get(&relation.destination_type) {
|
||||||
|
possible_relation_alias = Some(a.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(pa) = possible_relation_alias {
|
||||||
|
relation_alias = pa;
|
||||||
|
}
|
||||||
|
|
||||||
// Determine directionality based on the Relation metadata
|
// Determine directionality based on the Relation metadata
|
||||||
if relation.source_type == parent_type.name || parent_type.hierarchy.contains(&relation.source_type) {
|
if relation.source_type == parent_type.name
|
||||||
|
|| parent_type.hierarchy.contains(&relation.source_type)
|
||||||
|
{
|
||||||
// Parent is the source
|
// Parent is the source
|
||||||
where_clauses.push(format!("{}.{} = {}.{}", parent_alias, source_col, base_alias, dest_col));
|
where_clauses.push(format!(
|
||||||
|
"{}.{} = {}.{}",
|
||||||
|
relation_alias, source_col, base_alias, dest_col
|
||||||
|
));
|
||||||
relation_resolved = true;
|
relation_resolved = true;
|
||||||
} else if relation.destination_type == parent_type.name || parent_type.hierarchy.contains(&relation.destination_type) {
|
} else if relation.destination_type == parent_type.name
|
||||||
|
|| parent_type.hierarchy.contains(&relation.destination_type)
|
||||||
|
{
|
||||||
// Parent is the destination
|
// Parent is the destination
|
||||||
where_clauses.push(format!("{}.{} = {}.{}", base_alias, source_col, parent_alias, dest_col));
|
where_clauses.push(format!(
|
||||||
|
"{}.{} = {}.{}",
|
||||||
|
base_alias, source_col, relation_alias, dest_col
|
||||||
|
));
|
||||||
relation_resolved = true;
|
relation_resolved = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -634,10 +723,15 @@ impl SqlCompiler {
|
|||||||
if !relation_resolved {
|
if !relation_resolved {
|
||||||
// Fallback heuristics for unmapped polymorphism or abstract models
|
// Fallback heuristics for unmapped polymorphism or abstract models
|
||||||
if prop == "target" || prop == "source" {
|
if prop == "target" || prop == "source" {
|
||||||
if parent_alias.ends_with("_t1") {
|
if let Some(pta) = parent_table_aliases {
|
||||||
relation_alias = parent_alias.replace("_t1", "_t2");
|
if let Some(a) = pta.get("relationship") {
|
||||||
|
relation_alias = a.clone();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
where_clauses.push(format!("{}.id = {}.{}_id", base_alias, relation_alias, prop));
|
where_clauses.push(format!(
|
||||||
|
"{}.id = {}.{}_id",
|
||||||
|
base_alias, relation_alias, prop
|
||||||
|
));
|
||||||
} else {
|
} else {
|
||||||
where_clauses.push(format!("{}.parent_id = {}.id", base_alias, relation_alias));
|
where_clauses.push(format!("{}.parent_id = {}.id", base_alias, relation_alias));
|
||||||
}
|
}
|
||||||
@ -651,11 +745,13 @@ impl SqlCompiler {
|
|||||||
&self,
|
&self,
|
||||||
props: &std::collections::BTreeMap<String, std::sync::Arc<crate::database::schema::Schema>>,
|
props: &std::collections::BTreeMap<String, std::sync::Arc<crate::database::schema::Schema>>,
|
||||||
parent_alias: &str,
|
parent_alias: &str,
|
||||||
|
parent_table_aliases: Option<&std::collections::HashMap<String, String>>,
|
||||||
parent_type_def: Option<&crate::database::r#type::Type>,
|
parent_type_def: Option<&crate::database::r#type::Type>,
|
||||||
filter_keys: &[String],
|
filter_keys: &[String],
|
||||||
is_stem_query: bool,
|
is_stem_query: bool,
|
||||||
depth: usize,
|
depth: usize,
|
||||||
current_path: String,
|
current_path: String,
|
||||||
|
alias_counter: &mut usize,
|
||||||
) -> Result<(String, String), String> {
|
) -> Result<(String, String), String> {
|
||||||
let mut build_args = Vec::new();
|
let mut build_args = Vec::new();
|
||||||
for (k, v) in props {
|
for (k, v) in props {
|
||||||
@ -664,16 +760,18 @@ impl SqlCompiler {
|
|||||||
} else {
|
} else {
|
||||||
format!("{}.{}", current_path, k)
|
format!("{}.{}", current_path, k)
|
||||||
};
|
};
|
||||||
|
|
||||||
let (child_sql, val_type) = self.walk_schema(
|
let (child_sql, val_type) = self.walk_schema(
|
||||||
v,
|
v,
|
||||||
parent_alias,
|
parent_alias,
|
||||||
|
parent_table_aliases,
|
||||||
parent_type_def,
|
parent_type_def,
|
||||||
Some(k),
|
Some(k),
|
||||||
filter_keys,
|
filter_keys,
|
||||||
is_stem_query,
|
is_stem_query,
|
||||||
depth + 1,
|
depth + 1,
|
||||||
next_path,
|
next_path,
|
||||||
|
alias_counter,
|
||||||
)?;
|
)?;
|
||||||
if val_type == "abort" {
|
if val_type == "abort" {
|
||||||
continue;
|
continue;
|
||||||
@ -688,12 +786,14 @@ impl SqlCompiler {
|
|||||||
&self,
|
&self,
|
||||||
schemas: &[Arc<crate::database::schema::Schema>],
|
schemas: &[Arc<crate::database::schema::Schema>],
|
||||||
parent_alias: &str,
|
parent_alias: &str,
|
||||||
|
parent_table_aliases: Option<&std::collections::HashMap<String, String>>,
|
||||||
parent_type_def: Option<&crate::database::r#type::Type>,
|
parent_type_def: Option<&crate::database::r#type::Type>,
|
||||||
prop_name_context: Option<&str>,
|
prop_name_context: Option<&str>,
|
||||||
filter_keys: &[String],
|
filter_keys: &[String],
|
||||||
is_stem_query: bool,
|
is_stem_query: bool,
|
||||||
depth: usize,
|
depth: usize,
|
||||||
current_path: String,
|
current_path: String,
|
||||||
|
alias_counter: &mut usize,
|
||||||
) -> Result<(String, String), String> {
|
) -> Result<(String, String), String> {
|
||||||
let mut case_statements = Vec::new();
|
let mut case_statements = Vec::new();
|
||||||
let type_col = if let Some(prop) = prop_name_context {
|
let type_col = if let Some(prop) = prop_name_context {
|
||||||
@ -706,17 +806,19 @@ impl SqlCompiler {
|
|||||||
if let Some(ref_id) = &option_schema.obj.r#ref {
|
if let Some(ref_id) = &option_schema.obj.r#ref {
|
||||||
// Find the physical type this ref maps to
|
// Find the physical type this ref maps to
|
||||||
let base_type_name = ref_id.split('.').next_back().unwrap_or("").to_string();
|
let base_type_name = ref_id.split('.').next_back().unwrap_or("").to_string();
|
||||||
|
|
||||||
// Generate the nested SQL for this specific target type
|
// Generate the nested SQL for this specific target type
|
||||||
let (val_sql, _) = self.walk_schema(
|
let (val_sql, _) = self.walk_schema(
|
||||||
option_schema,
|
option_schema,
|
||||||
parent_alias,
|
parent_alias,
|
||||||
|
parent_table_aliases,
|
||||||
parent_type_def,
|
parent_type_def,
|
||||||
prop_name_context,
|
prop_name_context,
|
||||||
filter_keys,
|
filter_keys,
|
||||||
is_stem_query,
|
is_stem_query,
|
||||||
depth,
|
depth,
|
||||||
current_path.clone(),
|
current_path.clone(),
|
||||||
|
alias_counter,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
case_statements.push(format!(
|
case_statements.push(format!(
|
||||||
@ -730,10 +832,7 @@ impl SqlCompiler {
|
|||||||
return Ok(("NULL".to_string(), "string".to_string()));
|
return Ok(("NULL".to_string(), "string".to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let sql = format!(
|
let sql = format!("CASE {} ELSE NULL END", case_statements.join(" "));
|
||||||
"CASE {} ELSE NULL END",
|
|
||||||
case_statements.join(" ")
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok((sql, "object".to_string()))
|
Ok((sql, "object".to_string()))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,156 +1,194 @@
|
|||||||
use sqlparser::ast::{
|
use sqlparser::ast::{Expr, Query, SelectItem, Statement, TableFactor};
|
||||||
Expr, Join, JoinConstraint, JoinOperator, Query, Select, SelectItem, SetExpr, Statement,
|
|
||||||
TableFactor, TableWithJoins, Ident,
|
|
||||||
};
|
|
||||||
use sqlparser::dialect::PostgreSqlDialect;
|
use sqlparser::dialect::PostgreSqlDialect;
|
||||||
use sqlparser::parser::Parser;
|
use sqlparser::parser::Parser;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
pub fn validate_semantic_sql(sql: &str) -> Result<(), String> {
|
pub fn validate_semantic_sql(sql: &str) -> Result<(), String> {
|
||||||
let dialect = PostgreSqlDialect {};
|
let dialect = PostgreSqlDialect {};
|
||||||
let statements = match Parser::parse_sql(&dialect, sql) {
|
let statements = match Parser::parse_sql(&dialect, sql) {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(e) => return Err(format!("SQL Syntax Error: {}\nSQL: {}", e, sql)),
|
Err(e) => return Err(format!("SQL Syntax Error: {}\nSQL: {}", e, sql)),
|
||||||
};
|
};
|
||||||
|
|
||||||
for statement in statements {
|
for statement in statements {
|
||||||
validate_statement(&statement, sql)?;
|
validate_statement(&statement, sql)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_statement(stmt: &Statement, original_sql: &str) -> Result<(), String> {
|
fn validate_statement(stmt: &Statement, original_sql: &str) -> Result<(), String> {
|
||||||
match stmt {
|
match stmt {
|
||||||
Statement::Query(query) => validate_query(query, original_sql)?,
|
Statement::Query(query) => validate_query(query, &HashSet::new(), original_sql)?,
|
||||||
Statement::Insert(insert) => {
|
Statement::Insert(insert) => {
|
||||||
if let Some(query) = &insert.source {
|
if let Some(query) = &insert.source {
|
||||||
validate_query(query, original_sql)?
|
validate_query(query, &HashSet::new(), original_sql)?
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Statement::Update(update) => {
|
|
||||||
if let Some(expr) = &update.selection {
|
|
||||||
validate_expr(expr, &HashSet::new(), original_sql)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Statement::Delete(delete) => {
|
|
||||||
if let Some(expr) = &delete.selection {
|
|
||||||
validate_expr(expr, &HashSet::new(), original_sql)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Statement::Update(update) => {
|
||||||
|
if let Some(expr) = &update.selection {
|
||||||
|
validate_expr(expr, &HashSet::new(), original_sql)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Statement::Delete(delete) => {
|
||||||
|
if let Some(expr) = &delete.selection {
|
||||||
|
validate_expr(expr, &HashSet::new(), original_sql)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_query(query: &Query, original_sql: &str) -> Result<(), String> {
|
fn validate_query(
|
||||||
if let SetExpr::Select(select) = &*query.body {
|
query: &Query,
|
||||||
validate_select(select, original_sql)?;
|
available_aliases: &HashSet<String>,
|
||||||
}
|
original_sql: &str,
|
||||||
Ok(())
|
) -> Result<(), String> {
|
||||||
|
if let sqlparser::ast::SetExpr::Select(select) = &*query.body {
|
||||||
|
validate_select(&select, available_aliases, original_sql)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_select(select: &Select, original_sql: &str) -> Result<(), String> {
|
fn validate_select(
|
||||||
let mut available_aliases = HashSet::new();
|
select: &sqlparser::ast::Select,
|
||||||
|
parent_aliases: &HashSet<String>,
|
||||||
|
original_sql: &str,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let mut available_aliases = parent_aliases.clone();
|
||||||
|
|
||||||
// 1. Collect all declared table aliases in the FROM clause and JOINs
|
// 1. Collect all declared table aliases in the FROM clause and JOINs
|
||||||
for table_with_joins in &select.from {
|
for table_with_joins in &select.from {
|
||||||
collect_aliases_from_table_factor(&table_with_joins.relation, &mut available_aliases);
|
collect_aliases_from_table_factor(&table_with_joins.relation, &mut available_aliases);
|
||||||
for join in &table_with_joins.joins {
|
for join in &table_with_joins.joins {
|
||||||
collect_aliases_from_table_factor(&join.relation, &mut available_aliases);
|
collect_aliases_from_table_factor(&join.relation, &mut available_aliases);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 2. Validate all SELECT projection fields
|
// 2. Validate all SELECT projection fields
|
||||||
for projection in &select.projection {
|
for projection in &select.projection {
|
||||||
if let SelectItem::UnnamedExpr(expr) | SelectItem::ExprWithAlias { expr, .. } = projection {
|
if let SelectItem::UnnamedExpr(expr) | SelectItem::ExprWithAlias { expr, .. } = projection {
|
||||||
validate_expr(expr, &available_aliases, original_sql)?;
|
validate_expr(expr, &available_aliases, original_sql)?;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 3. Validate ON conditions in joins
|
// 3. Validate ON conditions in joins
|
||||||
for table_with_joins in &select.from {
|
for table_with_joins in &select.from {
|
||||||
for join in &table_with_joins.joins {
|
for join in &table_with_joins.joins {
|
||||||
if let JoinOperator::Inner(JoinConstraint::On(expr))
|
if let sqlparser::ast::JoinOperator::Inner(sqlparser::ast::JoinConstraint::On(expr))
|
||||||
| JoinOperator::LeftOuter(JoinConstraint::On(expr))
|
| sqlparser::ast::JoinOperator::LeftOuter(sqlparser::ast::JoinConstraint::On(expr))
|
||||||
| JoinOperator::RightOuter(JoinConstraint::On(expr))
|
| sqlparser::ast::JoinOperator::RightOuter(sqlparser::ast::JoinConstraint::On(expr))
|
||||||
| JoinOperator::FullOuter(JoinConstraint::On(expr))
|
| sqlparser::ast::JoinOperator::FullOuter(sqlparser::ast::JoinConstraint::On(expr))
|
||||||
| JoinOperator::Join(JoinConstraint::On(expr)) = &join.join_operator
|
| sqlparser::ast::JoinOperator::Join(sqlparser::ast::JoinConstraint::On(expr)) =
|
||||||
{
|
&join.join_operator
|
||||||
validate_expr(expr, &available_aliases, original_sql)?;
|
{
|
||||||
}
|
validate_expr(expr, &available_aliases, original_sql)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 4. Validate WHERE conditions
|
// 4. Validate WHERE conditions
|
||||||
if let Some(selection) = &select.selection {
|
if let Some(selection) = &select.selection {
|
||||||
validate_expr(selection, &available_aliases, original_sql)?;
|
validate_expr(selection, &available_aliases, original_sql)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_aliases_from_table_factor(tf: &TableFactor, aliases: &mut HashSet<String>) {
|
fn collect_aliases_from_table_factor(tf: &TableFactor, aliases: &mut HashSet<String>) {
|
||||||
match tf {
|
match tf {
|
||||||
TableFactor::Table { name, alias, .. } => {
|
TableFactor::Table { name, alias, .. } => {
|
||||||
if let Some(table_alias) = alias {
|
if let Some(table_alias) = alias {
|
||||||
aliases.insert(table_alias.name.value.clone());
|
aliases.insert(table_alias.name.value.clone());
|
||||||
} else if let Some(last) = name.0.last() {
|
} else if let Some(last) = name.0.last() {
|
||||||
match last {
|
match last {
|
||||||
sqlparser::ast::ObjectNamePart::Identifier(i) => {
|
sqlparser::ast::ObjectNamePart::Identifier(i) => {
|
||||||
aliases.insert(i.value.clone());
|
aliases.insert(i.value.clone());
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
TableFactor::Derived { alias: Some(table_alias), .. } => {
|
}
|
||||||
aliases.insert(table_alias.name.value.clone());
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
TableFactor::Derived {
|
||||||
|
subquery,
|
||||||
|
alias: Some(table_alias),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
aliases.insert(table_alias.name.value.clone());
|
||||||
|
// A derived table is technically a nested scope which is opaque outside, but for pure semantic checks
|
||||||
|
// its internal contents should be validated purely within its own scope (not leaking external aliases in, usually)
|
||||||
|
// but Postgres allows lateral correlation. We will validate its interior with an empty scope.
|
||||||
|
let _ = validate_query(subquery, &HashSet::new(), "");
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_expr(expr: &Expr, available_aliases: &HashSet<String>, sql: &str) -> Result<(), String> {
|
fn validate_expr(
|
||||||
match expr {
|
expr: &Expr,
|
||||||
Expr::CompoundIdentifier(idents) => {
|
available_aliases: &HashSet<String>,
|
||||||
if idents.len() == 2 {
|
sql: &str,
|
||||||
let alias = &idents[0].value;
|
) -> Result<(), String> {
|
||||||
if !available_aliases.is_empty() && !available_aliases.contains(alias) {
|
match expr {
|
||||||
return Err(format!(
|
Expr::CompoundIdentifier(idents) => {
|
||||||
"Semantic Error: Orchestrated query referenced table alias '{}' but it was not declared in the query's FROM/JOIN clauses.\nAvailable aliases: {:?}\nSQL: {}",
|
if idents.len() == 2 {
|
||||||
alias, available_aliases, sql
|
let alias = &idents[0].value;
|
||||||
));
|
if !available_aliases.is_empty() && !available_aliases.contains(alias) {
|
||||||
}
|
return Err(format!(
|
||||||
} else if idents.len() > 2 {
|
"Semantic Error: Orchestrated query referenced table alias '{}' but it was not declared in the query's FROM/JOIN clauses.\nAvailable aliases: {:?}\nSQL: {}",
|
||||||
let alias = &idents[1].value; // In form schema.table.column, 'table' is idents[1]
|
alias, available_aliases, sql
|
||||||
if !available_aliases.is_empty() && !available_aliases.contains(alias) {
|
));
|
||||||
return Err(format!(
|
|
||||||
"Semantic Error: Orchestrated query referenced table '{}' but it was not mapped.\nAvailable aliases: {:?}\nSQL: {}",
|
|
||||||
alias, available_aliases, sql
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Expr::BinaryOp { left, right, .. } => {
|
} else if idents.len() > 2 {
|
||||||
validate_expr(left, available_aliases, sql)?;
|
let alias = &idents[1].value; // In form schema.table.column, 'table' is idents[1]
|
||||||
validate_expr(right, available_aliases, sql)?;
|
if !available_aliases.is_empty() && !available_aliases.contains(alias) {
|
||||||
|
return Err(format!(
|
||||||
|
"Semantic Error: Orchestrated query referenced table '{}' but it was not mapped.\nAvailable aliases: {:?}\nSQL: {}",
|
||||||
|
alias, available_aliases, sql
|
||||||
|
));
|
||||||
}
|
}
|
||||||
Expr::IsFalse(e) | Expr::IsNotFalse(e) | Expr::IsTrue(e) | Expr::IsNotTrue(e)
|
}
|
||||||
| Expr::IsNull(e) | Expr::IsNotNull(e) | Expr::InList { expr: e, .. }
|
|
||||||
| Expr::Nested(e) | Expr::UnaryOp { expr: e, .. } | Expr::Cast { expr: e, .. }
|
|
||||||
| Expr::Like { expr: e, .. } | Expr::ILike { expr: e, .. } | Expr::AnyOp { left: e, .. }
|
|
||||||
| Expr::AllOp { left: e, .. } => {
|
|
||||||
validate_expr(e, available_aliases, sql)?;
|
|
||||||
}
|
|
||||||
Expr::Function(func) => {
|
|
||||||
if let sqlparser::ast::FunctionArguments::List(args) = &func.args {
|
|
||||||
if let Some(sqlparser::ast::FunctionArg::Unnamed(sqlparser::ast::FunctionArgExpr::Expr(e))) = args.args.get(0) {
|
|
||||||
validate_expr(e, available_aliases, sql)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Expr::Subquery(subquery) => validate_query(subquery, available_aliases, sql)?,
|
||||||
|
Expr::Exists { subquery, .. } => validate_query(subquery, available_aliases, sql)?,
|
||||||
|
Expr::InSubquery {
|
||||||
|
expr: e, subquery, ..
|
||||||
|
} => {
|
||||||
|
validate_expr(e, available_aliases, sql)?;
|
||||||
|
validate_query(subquery, available_aliases, sql)?;
|
||||||
|
}
|
||||||
|
Expr::BinaryOp { left, right, .. } => {
|
||||||
|
validate_expr(left, available_aliases, sql)?;
|
||||||
|
validate_expr(right, available_aliases, sql)?;
|
||||||
|
}
|
||||||
|
Expr::IsFalse(e)
|
||||||
|
| Expr::IsNotFalse(e)
|
||||||
|
| Expr::IsTrue(e)
|
||||||
|
| Expr::IsNotTrue(e)
|
||||||
|
| Expr::IsNull(e)
|
||||||
|
| Expr::IsNotNull(e)
|
||||||
|
| Expr::InList { expr: e, .. }
|
||||||
|
| Expr::Nested(e)
|
||||||
|
| Expr::UnaryOp { expr: e, .. }
|
||||||
|
| Expr::Cast { expr: e, .. }
|
||||||
|
| Expr::Like { expr: e, .. }
|
||||||
|
| Expr::ILike { expr: e, .. }
|
||||||
|
| Expr::AnyOp { left: e, .. }
|
||||||
|
| Expr::AllOp { left: e, .. } => {
|
||||||
|
validate_expr(e, available_aliases, sql)?;
|
||||||
|
}
|
||||||
|
Expr::Function(func) => {
|
||||||
|
if let sqlparser::ast::FunctionArguments::List(args) = &func.args {
|
||||||
|
if let Some(sqlparser::ast::FunctionArg::Unnamed(sqlparser::ast::FunctionArgExpr::Expr(
|
||||||
|
e,
|
||||||
|
))) = args.args.get(0)
|
||||||
|
{
|
||||||
|
validate_expr(e, available_aliases, sql)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -68,11 +68,11 @@ impl Validator {
|
|||||||
code: e.code,
|
code: e.code,
|
||||||
message: e.message,
|
message: e.message,
|
||||||
details: crate::drop::ErrorDetails {
|
details: crate::drop::ErrorDetails {
|
||||||
path: e.path,
|
path: e.path,
|
||||||
cause: None,
|
cause: None,
|
||||||
context: None,
|
context: None,
|
||||||
schema: None,
|
schema: None,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
crate::drop::Drop::with_errors(errors)
|
crate::drop::Drop::with_errors(errors)
|
||||||
|
|||||||
Reference in New Issue
Block a user