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"
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -233,9 +233,8 @@ impl Database {
|
|||||||
|
|
||||||
// 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();
|
||||||
|
|
||||||
@ -281,9 +280,12 @@ impl Database {
|
|||||||
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]);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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,7 +312,8 @@ impl Merger {
|
|||||||
&entity_fields,
|
&entity_fields,
|
||||||
);
|
);
|
||||||
|
|
||||||
let merged_relative = match self.merge_internal(Value::Object(relative_item), notifications)? {
|
let merged_relative =
|
||||||
|
match self.merge_internal(Value::Object(relative_item), notifications)? {
|
||||||
Value::Object(m) => m,
|
Value::Object(m) => m,
|
||||||
_ => continue,
|
_ => continue,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -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
|
||||||
@ -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,9 +470,13 @@ 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()
|
||||||
@ -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],
|
||||||
@ -526,9 +593,7 @@ impl SqlCompiler {
|
|||||||
cast = "::uuid";
|
cast = "::uuid";
|
||||||
} else if pg_type == "boolean" || pg_type == "bool" {
|
} else if pg_type == "boolean" || pg_type == "bool" {
|
||||||
cast = "::boolean";
|
cast = "::boolean";
|
||||||
} else if pg_type.contains("timestamp")
|
} else if pg_type.contains("timestamp") || pg_type == "timestamptz" || pg_type == "date"
|
||||||
|| pg_type == "timestamptz"
|
|
||||||
|| pg_type == "date"
|
|
||||||
{
|
{
|
||||||
cast = "::timestamptz";
|
cast = "::timestamptz";
|
||||||
} else if pg_type == "numeric"
|
} else if pg_type == "numeric"
|
||||||
@ -613,19 +678,43 @@ impl SqlCompiler {
|
|||||||
|
|
||||||
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 {
|
||||||
@ -668,12 +764,14 @@ impl SqlCompiler {
|
|||||||
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 {
|
||||||
@ -711,12 +811,14 @@ impl SqlCompiler {
|
|||||||
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,7 +1,4 @@
|
|||||||
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;
|
||||||
@ -22,10 +19,10 @@ pub fn validate_semantic_sql(sql: &str) -> Result<(), String> {
|
|||||||
|
|
||||||
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) => {
|
Statement::Update(update) => {
|
||||||
@ -43,15 +40,23 @@ fn validate_statement(stmt: &Statement, original_sql: &str) -> Result<(), String
|
|||||||
Ok(())
|
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,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
if let sqlparser::ast::SetExpr::Select(select) = &*query.body {
|
||||||
|
validate_select(&select, available_aliases, original_sql)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
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 {
|
||||||
@ -71,11 +76,12 @@ fn validate_select(select: &Select, original_sql: &str) -> Result<(), String> {
|
|||||||
// 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)?;
|
||||||
}
|
}
|
||||||
@ -104,14 +110,26 @@ fn collect_aliases_from_table_factor(tf: &TableFactor, aliases: &mut HashSet<Str
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TableFactor::Derived { alias: Some(table_alias), .. } => {
|
TableFactor::Derived {
|
||||||
|
subquery,
|
||||||
|
alias: Some(table_alias),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
aliases.insert(table_alias.name.value.clone());
|
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(
|
||||||
|
expr: &Expr,
|
||||||
|
available_aliases: &HashSet<String>,
|
||||||
|
sql: &str,
|
||||||
|
) -> Result<(), String> {
|
||||||
match expr {
|
match expr {
|
||||||
Expr::CompoundIdentifier(idents) => {
|
Expr::CompoundIdentifier(idents) => {
|
||||||
if idents.len() == 2 {
|
if idents.len() == 2 {
|
||||||
@ -132,20 +150,40 @@ fn validate_expr(expr: &Expr, available_aliases: &HashSet<String>, sql: &str) ->
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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, .. } => {
|
Expr::BinaryOp { left, right, .. } => {
|
||||||
validate_expr(left, available_aliases, sql)?;
|
validate_expr(left, available_aliases, sql)?;
|
||||||
validate_expr(right, available_aliases, sql)?;
|
validate_expr(right, available_aliases, sql)?;
|
||||||
}
|
}
|
||||||
Expr::IsFalse(e) | Expr::IsNotFalse(e) | Expr::IsTrue(e) | Expr::IsNotTrue(e)
|
Expr::IsFalse(e)
|
||||||
| Expr::IsNull(e) | Expr::IsNotNull(e) | Expr::InList { expr: e, .. }
|
| Expr::IsNotFalse(e)
|
||||||
| Expr::Nested(e) | Expr::UnaryOp { expr: e, .. } | Expr::Cast { expr: e, .. }
|
| Expr::IsTrue(e)
|
||||||
| Expr::Like { expr: e, .. } | Expr::ILike { expr: e, .. } | Expr::AnyOp { left: 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, .. } => {
|
| Expr::AllOp { left: e, .. } => {
|
||||||
validate_expr(e, available_aliases, sql)?;
|
validate_expr(e, available_aliases, sql)?;
|
||||||
}
|
}
|
||||||
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(e))) = args.args.get(0) {
|
if let Some(sqlparser::ast::FunctionArg::Unnamed(sqlparser::ast::FunctionArgExpr::Expr(
|
||||||
|
e,
|
||||||
|
))) = args.args.get(0)
|
||||||
|
{
|
||||||
validate_expr(e, available_aliases, sql)?;
|
validate_expr(e, available_aliases, sql)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user