hook
ModuleHooks: code a builder runs while compiling, to scope every query
(Filter, JoinFilter) or map attribute names to columns
(Attribute). Tenant and Map are the hooks the library ships.
PHP: Utopia\Query\Hook, Hook\Filter, Hook\Attribute, Hook\Join\Filter, Hook\Write.
| PHP | Rust |
|---|---|
Hook (a marker interface) | Hook (what Builder::add_hook takes) |
Hook\Filter | Filter |
Hook\Attribute | Attribute |
Hook\Join\Filter | JoinFilter |
Hook\Join\Condition | JoinCondition |
Hook\Join\Placement | Placement |
Hook\Write | Write |
Hook\Filter\Tenant | Tenant |
Hook\Attribute\Map | Map |
Contents
use utopia_query::hook::Hook;pub struct Hook {
pub filter: Option<Arc<dyn Filter>>,
pub attribute: Option<Arc<dyn Attribute>>,
pub join_filter: Option<Arc<dyn JoinFilter>>,
}A hook a builder accepts: one object may filter, resolve attributes and
filter joins at once (as Tenant filters both tables and joins).
PHP: Utopia\Query\Hook, whose implementations a builder sorts by the
interfaces they implement.
From<Tenant>, From<Map>.use utopia_query::hook::JoinCondition;pub struct JoinCondition {
pub condition: Condition,
pub placement: Placement,
}A join filter's condition and where it goes.
PHP: Utopia\Query\Hook\Join\Condition.
use utopia_query::hook::Map;pub struct Map {
// private fields
}Maps attribute names to columns through a table; other names stay as they are.
PHP: Utopia\Query\Hook\Attribute\Map.
use utopia_query::hook::{Attribute, Map};
let map = Map::new([("$id", "_uid")]);
assert_eq!(map.resolve("$id")?, "_uid");
assert_eq!(map.resolve("name")?, "name");use Utopia\Query\Hook\Attribute\Map;
$map = new Map(['$id' => '_uid']);
echo $map->resolve('$id'), "\n"; // _uid
echo $map->resolve('name'), "\n"; // nameFrom<Map>.use utopia_query::hook::Tenant;pub struct Tenant {
// private fields
}Scopes every query, and every joined table, to tenants: column IN (?, ...)
(in a left or right join's ON, elsewhere in the WHERE).
PHP: Utopia\Query\Hook\Filter\Tenant.
use utopia_query::builder::MySql;
use utopia_query::hook::Tenant;
let statement = MySql::new().from("docs", "").add_hook(Tenant::new(["7"], "tenant_id")?)?.build()?;
assert_eq!(statement.query, "SELECT * FROM `docs` WHERE tenant_id IN (?)");use Utopia\Query\Builder\MySQL;
use Utopia\Query\Hook\Filter\Tenant;
echo (new MySQL())->from('docs')->addHook(new Tenant(['7']))->build()->query, "\n"; // SELECT * FROM `docs` WHERE tenant_id IN (?)From<Tenant>.use utopia_query::hook::Placement;pub enum Placement {
On,
Where,
}Where a join filter's condition goes.
PHP: Utopia\Query\Hook\Join\Placement.
use utopia_query::hook::Attribute;pub trait Attribute: Send + SyncMaps an attribute name to the column it is stored in.
PHP: Utopia\Query\Hook\Attribute.
use utopia_query::hook::Filter;pub trait Filter: Send + SyncAdds a condition to the WHERE of every query on a table (tenancy,
permissions).
PHP: Utopia\Query\Hook\Filter.
use utopia_query::hook::JoinFilter;pub trait JoinFilter: Send + SyncScopes a joined table: a condition for the join, placed in its ON or in
the WHERE, or nothing.
PHP: Utopia\Query\Hook\Join\Filter.
use utopia_query::hook::Write;pub trait Write: Send + SyncDecorates rows before they are written and observes writes, for the database layer.
PHP: Utopia\Query\Hook\Write.