Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 271828ebe9 | |||
| 8c430d42e3 | |||
| 4cc5245336 | |||
| c71e99527d | |||
| 843891f67e | |||
| 8bb7085f76 | |||
| ea03584bbd | |||
| 3736c9d8f0 | |||
| ccca9129b2 | |||
| 333fc69735 | |||
| b0fc6c12ef |
@ -295,6 +295,7 @@ The Queryer transforms Postgres into a pre-compiled Semantic Query Engine, desig
|
||||
* **The Dot Convention**: When a schema requests `family: "target.schema"`, the compiler extracts the base type (e.g. `schema`) and looks up its Physical Table definition.
|
||||
* **Multi-Table Branching**: If the Physical Table is a parent to other tables (e.g. `organization` has variations `["organization", "bot", "person"]`), the compiler generates a dynamic `CASE WHEN type = '...' THEN ...` query, expanding into sub-queries for each variation. To ensure safe resolution, the compiler dynamically evaluates correlation boundaries: it attempts standard Relational Edge discovery first. If no explicit relational edge exists (indicating pure Table Inheritance rather than a standard foreign-key graph relationship), it safely invokes a **Table Parity Fallback**. This generates an explicit ID correlation constraint (`AND inner.id = outer.id`), perfectly binding the structural variations back to the parent row to eliminate Cartesian products.
|
||||
* **Single-Table Bypass**: If the Physical Table is a leaf node with only one variation (e.g. `person` has variations `["person"]`), the compiler cleanly bypasses `CASE` generation and compiles a simple `SELECT` across the base table, as all schema extensions (e.g. `light.person`, `full.person`) are guaranteed to reside in the exact same physical row.
|
||||
* **Polymorphic Relation Type Filtering**: When a relationship maps to a polymorphic target with variations, the Queryer compiles an `IN` clause containing all allowed table variations (e.g., `counterparty_type IN ('bot', 'organization', 'person')`) rather than matching the base type literal, ensuring all polymorphic types are loaded correctly.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@ -37,6 +37,14 @@
|
||||
},
|
||||
"filter": {
|
||||
"type": "$kind.filter"
|
||||
},
|
||||
"conditions": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"new": { "type": "$kind.filter" },
|
||||
"old": { "type": "$kind.filter" },
|
||||
"complete": { "type": "$kind.filter" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -149,7 +157,48 @@
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Valid nested filter payload",
|
||||
"data": {
|
||||
"kind": "person",
|
||||
"conditions": {
|
||||
"new": {
|
||||
"age": 30
|
||||
}
|
||||
}
|
||||
},
|
||||
"schema_id": "search",
|
||||
"action": "validate",
|
||||
"expect": {
|
||||
"success": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Invalid nested filter payload (fails constraint)",
|
||||
"data": {
|
||||
"kind": "person",
|
||||
"conditions": {
|
||||
"new": {
|
||||
"age": "thirty"
|
||||
}
|
||||
}
|
||||
},
|
||||
"schema_id": "search",
|
||||
"action": "validate",
|
||||
"expect": {
|
||||
"success": false,
|
||||
"errors": [
|
||||
{
|
||||
"code": "INVALID_TYPE",
|
||||
"details": {
|
||||
"path": "conditions/new/age"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
@ -70,6 +70,10 @@
|
||||
"type": "string",
|
||||
"format": "date-time"
|
||||
},
|
||||
"uuid_field": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
},
|
||||
"tags": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
@ -181,6 +185,17 @@
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"uuid.condition": {
|
||||
"type": "condition",
|
||||
"properties": {
|
||||
"$eq": {
|
||||
"type": [
|
||||
"string",
|
||||
"null"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -244,6 +259,7 @@
|
||||
"billing_address",
|
||||
"gender",
|
||||
"birth_date",
|
||||
"uuid_field",
|
||||
"tags",
|
||||
"ad_hoc",
|
||||
"$and",
|
||||
@ -258,6 +274,7 @@
|
||||
"billing_address",
|
||||
"gender",
|
||||
"birth_date",
|
||||
"uuid_field",
|
||||
"tags",
|
||||
"ad_hoc",
|
||||
"$and",
|
||||
@ -278,6 +295,7 @@
|
||||
"billing_address",
|
||||
"gender",
|
||||
"birth_date",
|
||||
"uuid_field",
|
||||
"tags",
|
||||
"ad_hoc",
|
||||
"$and",
|
||||
@ -325,6 +343,12 @@
|
||||
"null"
|
||||
]
|
||||
},
|
||||
"uuid_field": {
|
||||
"type": [
|
||||
"uuid.condition",
|
||||
"null"
|
||||
]
|
||||
},
|
||||
"first_name": {
|
||||
"type": [
|
||||
"string.condition",
|
||||
@ -396,6 +420,7 @@
|
||||
"string.condition": {},
|
||||
"integer.condition": {},
|
||||
"date.condition": {},
|
||||
"uuid.condition": {},
|
||||
"search": {},
|
||||
"search.filter": {
|
||||
"type": "filter",
|
||||
|
||||
@ -59,6 +59,17 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "get_counterparty_orders",
|
||||
"schemas": {
|
||||
"get_counterparty_orders.response": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "counterparty.order"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"enums": [],
|
||||
@ -734,6 +745,14 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"counterparty.order": {
|
||||
"type": "order",
|
||||
"properties": {
|
||||
"counterparty": {
|
||||
"family": "organization"
|
||||
}
|
||||
}
|
||||
},
|
||||
"light.order": {
|
||||
"type": "order",
|
||||
"properties": {
|
||||
@ -2332,6 +2351,92 @@
|
||||
]
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Order select with nested polymorphic counterparty family relation",
|
||||
"action": "query",
|
||||
"schema_id": "get_counterparty_orders.response",
|
||||
"expect": {
|
||||
"success": true,
|
||||
"sql": [
|
||||
[
|
||||
"((SELECT jsonb_strip_nulls((",
|
||||
" SELECT COALESCE(jsonb_agg(jsonb_build_object(",
|
||||
" 'id', order_1.id,",
|
||||
" 'type', order_1.type,",
|
||||
" 'archived', entity_2.archived,",
|
||||
" 'created_at', entity_2.created_at,",
|
||||
" 'total', order_1.total,",
|
||||
" 'customer_id', order_1.customer_id,",
|
||||
" 'counterparty', (",
|
||||
" SELECT CASE",
|
||||
" WHEN organization_3.type = 'bot' THEN ((",
|
||||
" SELECT jsonb_build_object(",
|
||||
" 'id', entity_7.id,",
|
||||
" 'type', entity_7.type,",
|
||||
" 'archived', entity_7.archived,",
|
||||
" 'created_at', entity_7.created_at,",
|
||||
" 'name', organization_6.name,",
|
||||
" 'token', bot_5.token,",
|
||||
" 'role', bot_5.role",
|
||||
" )",
|
||||
" FROM agreego.bot bot_5",
|
||||
" JOIN agreego.organization organization_6 ON organization_6.id = bot_5.id",
|
||||
" JOIN agreego.entity entity_7 ON entity_7.id = organization_6.id",
|
||||
" WHERE",
|
||||
" NOT entity_7.archived",
|
||||
" AND entity_7.id = entity_4.id",
|
||||
" ))",
|
||||
" WHEN organization_3.type = 'organization' THEN ((",
|
||||
" SELECT jsonb_build_object(",
|
||||
" 'id', entity_9.id,",
|
||||
" 'type', entity_9.type,",
|
||||
" 'archived', entity_9.archived,",
|
||||
" 'created_at', entity_9.created_at,",
|
||||
" 'name', organization_8.name",
|
||||
" )",
|
||||
" FROM agreego.organization organization_8",
|
||||
" JOIN agreego.entity entity_9 ON entity_9.id = organization_8.id",
|
||||
" WHERE",
|
||||
" NOT entity_9.archived",
|
||||
" AND entity_9.id = entity_4.id",
|
||||
" ))",
|
||||
" WHEN organization_3.type = 'person' THEN ((",
|
||||
" SELECT jsonb_build_object(",
|
||||
" 'id', entity_12.id,",
|
||||
" 'type', entity_12.type,",
|
||||
" 'archived', entity_12.archived,",
|
||||
" 'created_at', entity_12.created_at,",
|
||||
" 'name', organization_11.name,",
|
||||
" 'first_name', person_10.first_name,",
|
||||
" 'last_name', person_10.last_name,",
|
||||
" 'age', person_10.age",
|
||||
" )",
|
||||
" FROM agreego.person person_10",
|
||||
" JOIN agreego.organization organization_11 ON organization_11.id = person_10.id",
|
||||
" JOIN agreego.entity entity_12 ON entity_12.id = organization_11.id",
|
||||
" WHERE",
|
||||
" NOT entity_12.archived",
|
||||
" AND entity_12.id = entity_4.id",
|
||||
" ))",
|
||||
" ELSE NULL",
|
||||
" END",
|
||||
" FROM agreego.organization organization_3",
|
||||
" JOIN agreego.entity entity_4 ON entity_4.id = organization_3.id",
|
||||
" WHERE",
|
||||
" NOT entity_4.archived",
|
||||
" AND order_1.counterparty_id = entity_4.id",
|
||||
" )",
|
||||
" )), '[]'::jsonb)",
|
||||
" FROM agreego.order order_1",
|
||||
" JOIN agreego.entity entity_2 ON entity_2.id = order_1.id",
|
||||
" WHERE",
|
||||
" NOT entity_2.archived",
|
||||
" AND order_1.counterparty_type IN ('bot', 'organization', 'person')",
|
||||
"))))"
|
||||
]
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@ -141,6 +141,8 @@ impl Schema {
|
||||
if let Some(fmt) = &schema.obj.format {
|
||||
if fmt == "date-time" {
|
||||
return Some(vec!["date.condition".to_string()]);
|
||||
} else if fmt == "uuid" {
|
||||
return Some(vec!["uuid.condition".to_string()]);
|
||||
}
|
||||
}
|
||||
Some(vec!["string.condition".to_string()])
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use serde_json::Value;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
pub fn compose(val: &mut Value, errors: &mut Vec<crate::drop::Error>) -> Result<(), String> {
|
||||
let mut traits = HashMap::new();
|
||||
@ -73,7 +73,9 @@ fn resolve_in_place(
|
||||
return;
|
||||
}
|
||||
|
||||
let include_opt = current.as_object_mut().and_then(|obj| obj.remove("include"));
|
||||
let include_opt = current
|
||||
.as_object_mut()
|
||||
.and_then(|obj| obj.remove("include"));
|
||||
if let Some(include_val) = include_opt {
|
||||
if let Some(include_arr) = include_val.as_array() {
|
||||
let mut merged_props = serde_json::Map::new();
|
||||
@ -144,7 +146,10 @@ fn resolve_in_place(
|
||||
visited.remove(inc_name);
|
||||
|
||||
// Merge properties (host overrides trait)
|
||||
if let Some(target_props) = resolved_target.get("properties").and_then(|v| v.as_object()) {
|
||||
if let Some(target_props) = resolved_target
|
||||
.get("properties")
|
||||
.and_then(|v| v.as_object())
|
||||
{
|
||||
for (k, v) in target_props {
|
||||
if !merged_props.contains_key(k) {
|
||||
merged_props.insert(k.clone(), v.clone());
|
||||
@ -153,7 +158,10 @@ fn resolve_in_place(
|
||||
}
|
||||
|
||||
// Merge patternProperties (host overrides trait)
|
||||
if let Some(target_pat_props) = resolved_target.get("patternProperties").and_then(|v| v.as_object()) {
|
||||
if let Some(target_pat_props) = resolved_target
|
||||
.get("patternProperties")
|
||||
.and_then(|v| v.as_object())
|
||||
{
|
||||
for (k, v) in target_pat_props {
|
||||
if !merged_pattern_props.contains_key(k) {
|
||||
merged_pattern_props.insert(k.clone(), v.clone());
|
||||
@ -180,11 +188,19 @@ fn resolve_in_place(
|
||||
}
|
||||
|
||||
// Merge dependencies
|
||||
if let Some(target_deps) = resolved_target.get("dependencies").and_then(|v| v.as_object()) {
|
||||
if let Some(target_deps) = resolved_target
|
||||
.get("dependencies")
|
||||
.and_then(|v| v.as_object())
|
||||
{
|
||||
for (dep_prop, dep_val) in target_deps {
|
||||
if let Some(existing_val) = merged_dependencies.get_mut(dep_prop) {
|
||||
if let (Some(arr_existing), Some(arr_target)) = (existing_val.as_array_mut(), dep_val.as_array()) {
|
||||
let mut set: HashSet<String> = arr_existing.iter().filter_map(|x| x.as_str().map(String::from)).collect();
|
||||
if let (Some(arr_existing), Some(arr_target)) =
|
||||
(existing_val.as_array_mut(), dep_val.as_array())
|
||||
{
|
||||
let mut set: HashSet<String> = arr_existing
|
||||
.iter()
|
||||
.filter_map(|x| x.as_str().map(String::from))
|
||||
.collect();
|
||||
for x in arr_target {
|
||||
if let Some(s) = x.as_str() {
|
||||
if set.insert(s.to_string()) {
|
||||
@ -202,7 +218,13 @@ fn resolve_in_place(
|
||||
// Inherit other non-merged schemas/scalars if not defined in host (type, items, cases, family, format, etc.)
|
||||
if let Some(obj) = current.as_object_mut() {
|
||||
for (k, v) in resolved_target.as_object().unwrap() {
|
||||
if k != "properties" && k != "patternProperties" && k != "required" && k != "display" && k != "dependencies" && k != "include" {
|
||||
if k != "properties"
|
||||
&& k != "patternProperties"
|
||||
&& k != "required"
|
||||
&& k != "display"
|
||||
&& k != "dependencies"
|
||||
&& k != "include"
|
||||
{
|
||||
if !obj.contains_key(k) {
|
||||
obj.insert(k.clone(), v.clone());
|
||||
}
|
||||
@ -228,7 +250,10 @@ fn resolve_in_place(
|
||||
obj.insert("properties".to_string(), Value::Object(merged_props));
|
||||
}
|
||||
if !merged_pattern_props.is_empty() {
|
||||
obj.insert("patternProperties".to_string(), Value::Object(merged_pattern_props));
|
||||
obj.insert(
|
||||
"patternProperties".to_string(),
|
||||
Value::Object(merged_pattern_props),
|
||||
);
|
||||
}
|
||||
if !merged_required.is_empty() {
|
||||
let mut req_vec: Vec<Value> = merged_required.into_iter().map(Value::String).collect();
|
||||
@ -241,7 +266,10 @@ fn resolve_in_place(
|
||||
obj.insert("display".to_string(), Value::Array(disp_vec));
|
||||
}
|
||||
if !merged_dependencies.is_empty() {
|
||||
obj.insert("dependencies".to_string(), Value::Object(merged_dependencies));
|
||||
obj.insert(
|
||||
"dependencies".to_string(),
|
||||
Value::Object(merged_dependencies),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -251,47 +279,138 @@ fn resolve_in_place(
|
||||
if let Some(obj) = current.as_object_mut() {
|
||||
if let Some(props) = obj.get_mut("properties").and_then(|v| v.as_object_mut()) {
|
||||
for (k, v) in props {
|
||||
resolve_in_place(v, traits, schemas, errors, schema_id, &format!("{}/{}", path, k), visited);
|
||||
resolve_in_place(
|
||||
v,
|
||||
traits,
|
||||
schemas,
|
||||
errors,
|
||||
schema_id,
|
||||
&format!("{}/{}", path, k),
|
||||
visited,
|
||||
);
|
||||
}
|
||||
}
|
||||
if let Some(pat_props) = obj.get_mut("patternProperties").and_then(|v| v.as_object_mut()) {
|
||||
if let Some(pat_props) = obj
|
||||
.get_mut("patternProperties")
|
||||
.and_then(|v| v.as_object_mut())
|
||||
{
|
||||
for (k, v) in pat_props {
|
||||
resolve_in_place(v, traits, schemas, errors, schema_id, &format!("{}/{}", path, k), visited);
|
||||
resolve_in_place(
|
||||
v,
|
||||
traits,
|
||||
schemas,
|
||||
errors,
|
||||
schema_id,
|
||||
&format!("{}/{}", path, k),
|
||||
visited,
|
||||
);
|
||||
}
|
||||
}
|
||||
if let Some(items) = obj.get_mut("items") {
|
||||
resolve_in_place(items, traits, schemas, errors, schema_id, &format!("{}/items", path), visited);
|
||||
resolve_in_place(
|
||||
items,
|
||||
traits,
|
||||
schemas,
|
||||
errors,
|
||||
schema_id,
|
||||
&format!("{}/items", path),
|
||||
visited,
|
||||
);
|
||||
}
|
||||
if let Some(prefix_items) = obj.get_mut("prefixItems").and_then(|v| v.as_array_mut()) {
|
||||
for (i, v) in prefix_items.iter_mut().enumerate() {
|
||||
resolve_in_place(v, traits, schemas, errors, schema_id, &format!("{}/prefixItems/{}", path, i), visited);
|
||||
resolve_in_place(
|
||||
v,
|
||||
traits,
|
||||
schemas,
|
||||
errors,
|
||||
schema_id,
|
||||
&format!("{}/prefixItems/{}", path, i),
|
||||
visited,
|
||||
);
|
||||
}
|
||||
}
|
||||
if let Some(additional_props) = obj.get_mut("additionalProperties") {
|
||||
resolve_in_place(additional_props, traits, schemas, errors, schema_id, &format!("{}/additionalProperties", path), visited);
|
||||
resolve_in_place(
|
||||
additional_props,
|
||||
traits,
|
||||
schemas,
|
||||
errors,
|
||||
schema_id,
|
||||
&format!("{}/additionalProperties", path),
|
||||
visited,
|
||||
);
|
||||
}
|
||||
if let Some(one_of) = obj.get_mut("oneOf").and_then(|v| v.as_array_mut()) {
|
||||
for (i, v) in one_of.iter_mut().enumerate() {
|
||||
resolve_in_place(v, traits, schemas, errors, schema_id, &format!("{}/oneOf/{}", path, i), visited);
|
||||
resolve_in_place(
|
||||
v,
|
||||
traits,
|
||||
schemas,
|
||||
errors,
|
||||
schema_id,
|
||||
&format!("{}/oneOf/{}", path, i),
|
||||
visited,
|
||||
);
|
||||
}
|
||||
}
|
||||
if let Some(contains) = obj.get_mut("contains") {
|
||||
resolve_in_place(contains, traits, schemas, errors, schema_id, &format!("{}/contains", path), visited);
|
||||
resolve_in_place(
|
||||
contains,
|
||||
traits,
|
||||
schemas,
|
||||
errors,
|
||||
schema_id,
|
||||
&format!("{}/contains", path),
|
||||
visited,
|
||||
);
|
||||
}
|
||||
if let Some(not) = obj.get_mut("not") {
|
||||
resolve_in_place(not, traits, schemas, errors, schema_id, &format!("{}/not", path), visited);
|
||||
resolve_in_place(
|
||||
not,
|
||||
traits,
|
||||
schemas,
|
||||
errors,
|
||||
schema_id,
|
||||
&format!("{}/not", path),
|
||||
visited,
|
||||
);
|
||||
}
|
||||
if let Some(cases) = obj.get_mut("cases").and_then(|v| v.as_array_mut()) {
|
||||
for (i, c_val) in cases.iter_mut().enumerate() {
|
||||
if let Some(c_obj) = c_val.as_object_mut() {
|
||||
if let Some(when) = c_obj.get_mut("when") {
|
||||
resolve_in_place(when, traits, schemas, errors, schema_id, &format!("{}/cases/{}/when", path, i), visited);
|
||||
resolve_in_place(
|
||||
when,
|
||||
traits,
|
||||
schemas,
|
||||
errors,
|
||||
schema_id,
|
||||
&format!("{}/cases/{}/when", path, i),
|
||||
visited,
|
||||
);
|
||||
}
|
||||
if let Some(then) = c_obj.get_mut("then") {
|
||||
resolve_in_place(then, traits, schemas, errors, schema_id, &format!("{}/cases/{}/then", path, i), visited);
|
||||
resolve_in_place(
|
||||
then,
|
||||
traits,
|
||||
schemas,
|
||||
errors,
|
||||
schema_id,
|
||||
&format!("{}/cases/{}/then", path, i),
|
||||
visited,
|
||||
);
|
||||
}
|
||||
if let Some(else_) = c_obj.get_mut("else") {
|
||||
resolve_in_place(else_, traits, schemas, errors, schema_id, &format!("{}/cases/{}/else", path, i), visited);
|
||||
resolve_in_place(
|
||||
else_,
|
||||
traits,
|
||||
schemas,
|
||||
errors,
|
||||
schema_id,
|
||||
&format!("{}/cases/{}/else", path, i),
|
||||
visited,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -85,6 +85,14 @@ impl DatabaseExecutor for MockExecutor {
|
||||
Ok("2026-03-10T00:00:00Z".to_string())
|
||||
}
|
||||
|
||||
fn auth_origin(&self) -> Result<Option<Value>, String> {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn punc_trigger(&self) -> Result<Option<String>, String> {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn get_queries(&self) -> Vec<String> {
|
||||
MOCK_STATE.with(|state| state.borrow().captured_queries.clone())
|
||||
|
||||
@ -20,6 +20,12 @@ pub trait DatabaseExecutor: Send + Sync {
|
||||
/// Returns the current transaction timestamp
|
||||
fn timestamp(&self) -> Result<String, String>;
|
||||
|
||||
/// Returns the current auth.origin session context if configured
|
||||
fn auth_origin(&self) -> Result<Option<Value>, String>;
|
||||
|
||||
/// Returns the current punc.name session context if configured
|
||||
fn punc_trigger(&self) -> Result<Option<String>, String>;
|
||||
|
||||
#[cfg(test)]
|
||||
fn get_queries(&self) -> Vec<String>;
|
||||
|
||||
|
||||
@ -150,4 +150,46 @@ impl DatabaseExecutor for SpiExecutor {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn auth_origin(&self) -> Result<Option<Value>, String> {
|
||||
self.transact(|| {
|
||||
Spi::connect(|client| {
|
||||
let mut tup_table = client
|
||||
.select(
|
||||
"SELECT NULLIF(current_setting('auth.origin', true), '')::jsonb",
|
||||
None,
|
||||
&[],
|
||||
)
|
||||
.map_err(|e| format!("SPI Select Error: {}", e))?;
|
||||
|
||||
if let Some(row) = tup_table.next() {
|
||||
if let Ok(Some(jsonb)) = row.get::<pgrx::JsonB>(1) {
|
||||
return Ok(Some(jsonb.0));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn punc_trigger(&self) -> Result<Option<String>, String> {
|
||||
self.transact(|| {
|
||||
Spi::connect(|client| {
|
||||
let mut tup_table = client
|
||||
.select(
|
||||
"SELECT NULLIF(current_setting('punc.name', true), '')",
|
||||
None,
|
||||
&[],
|
||||
)
|
||||
.map_err(|e| format!("SPI Select Error: {}", e))?;
|
||||
|
||||
if let Some(row) = tup_table.next() {
|
||||
if let Ok(val_opt) = row.get::<String>(1) {
|
||||
return Ok(val_opt);
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -206,6 +206,16 @@ impl Database {
|
||||
self.executor.timestamp()
|
||||
}
|
||||
|
||||
/// Returns the current auth.origin session context if configured
|
||||
pub fn auth_origin(&self) -> Result<Option<Value>, String> {
|
||||
self.executor.auth_origin()
|
||||
}
|
||||
|
||||
/// Returns the current punc.name session context if configured
|
||||
pub fn punc_trigger(&self) -> Result<Option<String>, String> {
|
||||
self.executor.punc_trigger()
|
||||
}
|
||||
|
||||
pub fn compile(&mut self, errors: &mut Vec<crate::drop::Error>) {
|
||||
// Phase 1: Registration
|
||||
self.collect_schemas(errors);
|
||||
|
||||
@ -946,7 +946,25 @@ impl Merger {
|
||||
Value::Object(old_vals)
|
||||
};
|
||||
|
||||
let origin = match self.db.auth_origin() {
|
||||
Ok(Some(orig)) => orig,
|
||||
_ => serde_json::json!({
|
||||
"kind": "user",
|
||||
"user_id": user_id
|
||||
}),
|
||||
};
|
||||
|
||||
let trigger = match self.db.punc_trigger() {
|
||||
Ok(Some(trig)) => trig,
|
||||
_ => "merge_entity".to_string(),
|
||||
};
|
||||
|
||||
let entity_type_name = type_name.as_str().unwrap_or(&type_obj.name);
|
||||
|
||||
let mut notification = serde_json::Map::new();
|
||||
notification.insert("type".to_string(), Value::String(entity_type_name.to_string()));
|
||||
notification.insert("trigger".to_string(), Value::String(trigger));
|
||||
notification.insert("origin".to_string(), origin.clone());
|
||||
notification.insert("complete".to_string(), Value::Object(complete));
|
||||
notification.insert("new".to_string(), new_val_obj.clone());
|
||||
|
||||
@ -961,14 +979,16 @@ impl Merger {
|
||||
let mut notify_sql = None;
|
||||
if type_obj.historical && change_kind != "replace" {
|
||||
let change_sql = format!(
|
||||
"INSERT INTO agreego.change (\"old\", \"new\", entity_id, id, kind, modified_at, modified_by) VALUES ({}, {}, {}, {}, {}, {}, {})",
|
||||
"INSERT INTO agreego.change (\"old\", \"new\", entity_id, id, kind, modified_at, modified_by, origin, entity_type) VALUES ({}, {}, {}, {}, {}, {}, {}, {}, {})",
|
||||
Self::quote_literal(&old_val_obj),
|
||||
Self::quote_literal(&new_val_obj),
|
||||
Self::quote_literal(id_str),
|
||||
Self::quote_literal(&Value::String(uuid::Uuid::new_v4().to_string())),
|
||||
Self::quote_literal(&Value::String(change_kind.to_string())),
|
||||
Self::quote_literal(&Value::String(timestamp.to_string())),
|
||||
Self::quote_literal(&Value::String(user_id.to_string()))
|
||||
Self::quote_literal(&Value::String(user_id.to_string())),
|
||||
Self::quote_literal(&origin),
|
||||
Self::quote_literal(&Value::String(entity_type_name.to_string()))
|
||||
);
|
||||
|
||||
self.db.execute(&change_sql, None)?;
|
||||
|
||||
@ -213,6 +213,7 @@ impl<'a> Compiler<'a> {
|
||||
|
||||
let mut case_node = node.clone();
|
||||
case_node.parent_alias = base_alias.clone();
|
||||
case_node.property_name = None;
|
||||
let arc_aliases = std::sync::Arc::new(table_aliases.clone());
|
||||
case_node.parent_type_aliases = Some(arc_aliases);
|
||||
case_node.parent_type = Some(r#type);
|
||||
@ -602,7 +603,7 @@ impl<'a> Compiler<'a> {
|
||||
|
||||
if let Some(type_name) = bound_type_name {
|
||||
// Ensure this type actually exists
|
||||
if self.db.types.contains_key(&type_name) {
|
||||
if let Some(type_def) = self.db.types.get(&type_name) {
|
||||
if let Some(relation) = self.db.relations.get(&edge.constraint) {
|
||||
let mut poly_col = None;
|
||||
let mut table_to_alias = "";
|
||||
@ -620,7 +621,21 @@ impl<'a> Compiler<'a> {
|
||||
.get(table_to_alias)
|
||||
.or_else(|| type_aliases.get(&node.parent_alias))
|
||||
{
|
||||
where_clauses.push(format!("{}.{} = '{}'", alias, col, type_name));
|
||||
if type_def.variations.len() > 1 {
|
||||
let quoted: Vec<String> = type_def
|
||||
.variations
|
||||
.iter()
|
||||
.map(|v| format!("'{}'", v))
|
||||
.collect();
|
||||
where_clauses.push(format!(
|
||||
"{}.{} IN ({})",
|
||||
alias,
|
||||
col,
|
||||
quoted.join(", ")
|
||||
));
|
||||
} else {
|
||||
where_clauses.push(format!("{}.{} = '{}'", alias, col, type_name));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1277,6 +1277,18 @@ fn test_dynamic_type_0_4() {
|
||||
crate::tests::runner::run_test_case(&path, 0, 4).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_dynamic_type_0_5() {
|
||||
let path = format!("{}/fixtures/dynamicType.json", env!("CARGO_MANIFEST_DIR"));
|
||||
crate::tests::runner::run_test_case(&path, 0, 5).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_dynamic_type_0_6() {
|
||||
let path = format!("{}/fixtures/dynamicType.json", env!("CARGO_MANIFEST_DIR"));
|
||||
crate::tests::runner::run_test_case(&path, 0, 6).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_property_names_0_0() {
|
||||
let path = format!("{}/fixtures/propertyNames.json", env!("CARGO_MANIFEST_DIR"));
|
||||
@ -1499,6 +1511,12 @@ fn test_queryer_0_14() {
|
||||
crate::tests::runner::run_test_case(&path, 0, 14).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_queryer_0_15() {
|
||||
let path = format!("{}/fixtures/queryer.json", env!("CARGO_MANIFEST_DIR"));
|
||||
crate::tests::runner::run_test_case(&path, 0, 15).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_polymorphism_0_0() {
|
||||
let path = format!("{}/fixtures/polymorphism.json", env!("CARGO_MANIFEST_DIR"));
|
||||
|
||||
@ -15,7 +15,7 @@ pub struct ValidationContext<'a> {
|
||||
pub extensible: bool,
|
||||
pub reporter: bool,
|
||||
pub overrides: HashSet<String>,
|
||||
pub parent: Option<&'a serde_json::Value>,
|
||||
pub parents: Vec<&'a serde_json::Value>,
|
||||
}
|
||||
|
||||
impl<'a> ValidationContext<'a> {
|
||||
@ -39,7 +39,7 @@ impl<'a> ValidationContext<'a> {
|
||||
extensible: effective_extensible,
|
||||
reporter,
|
||||
overrides,
|
||||
parent: None,
|
||||
parents: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,6 +63,11 @@ impl<'a> ValidationContext<'a> {
|
||||
) -> Self {
|
||||
let effective_extensible = schema.extensible.unwrap_or(extensible);
|
||||
|
||||
let mut parents = self.parents.clone();
|
||||
if let Some(p) = parent_instance {
|
||||
parents.push(p);
|
||||
}
|
||||
|
||||
Self {
|
||||
db: self.db,
|
||||
root: self.root,
|
||||
@ -73,7 +78,7 @@ impl<'a> ValidationContext<'a> {
|
||||
extensible: effective_extensible,
|
||||
reporter,
|
||||
overrides,
|
||||
parent: parent_instance,
|
||||
parents,
|
||||
}
|
||||
}
|
||||
|
||||
@ -85,7 +90,7 @@ impl<'a> ValidationContext<'a> {
|
||||
HashSet::new(),
|
||||
self.extensible,
|
||||
reporter,
|
||||
self.parent,
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@ -59,12 +59,13 @@ impl<'a> ValidationContext<'a> {
|
||||
};
|
||||
|
||||
let mut resolved = false;
|
||||
if let Some(parent) = self.parent {
|
||||
for parent in self.parents.iter().rev() {
|
||||
if let Some(obj) = parent.as_object() {
|
||||
if let Some(val) = obj.get(var_name) {
|
||||
if let Some(str_val) = val.as_str() {
|
||||
target_id = format!("{}{}", str_val, suffix);
|
||||
resolved = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -97,7 +98,7 @@ impl<'a> ValidationContext<'a> {
|
||||
new_overrides,
|
||||
self.extensible,
|
||||
true, // Reporter mode
|
||||
self.parent,
|
||||
None,
|
||||
);
|
||||
shadow.root = &global_schema;
|
||||
result.merge(shadow.validate()?);
|
||||
|
||||
Reference in New Issue
Block a user