Rust

hook

Module

Hooks: 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.

PHPRust
Hook (a marker interface)Hook (what Builder::add_hook takes)
Hook\FilterFilter
Hook\AttributeAttribute
Hook\Join\FilterJoinFilter
Hook\Join\ConditionJoinCondition
Hook\Join\PlacementPlacement
Hook\WriteWrite
Hook\Filter\TenantTenant
Hook\Attribute\MapMap

Contents

use utopia_query::hook::Hook;
Rust
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.

Also implements From<Tenant>, From<Map>.
use utopia_query::hook::JoinCondition;
Rust
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;
Rust
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.

Rust
use utopia_query::hook::{Attribute, Map};

let map = Map::new([("$id", "_uid")]);
assert_eq!(map.resolve("$id")?, "_uid");
assert_eq!(map.resolve("name")?, "name");
PHP
use Utopia\Query\Hook\Attribute\Map;

$map = new Map(['$id' => '_uid']);
echo $map->resolve('$id'), "\n";  // _uid
echo $map->resolve('name'), "\n"; // name
Also implements From<Map>.
use utopia_query::hook::Tenant;
Rust
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.

Rust
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 (?)");
PHP
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 (?)
Also implements From<Tenant>.
use utopia_query::hook::Placement;
Rust
pub enum Placement {
    On,
    Where,
}

Where a join filter's condition goes.

PHP: Utopia\Query\Hook\Join\Placement.

use utopia_query::hook::Attribute;
Rust
pub trait Attribute: Send + Sync

Maps an attribute name to the column it is stored in.

PHP: Utopia\Query\Hook\Attribute.

Implemented byMap
use utopia_query::hook::Filter;
Rust
pub trait Filter: Send + Sync

Adds a condition to the WHERE of every query on a table (tenancy, permissions).

PHP: Utopia\Query\Hook\Filter.

Implemented byTenant
use utopia_query::hook::JoinFilter;
Rust
pub trait JoinFilter: Send + Sync

Scopes a joined table: a condition for the join, placed in its ON or in the WHERE, or nothing.

PHP: Utopia\Query\Hook\Join\Filter.

Implemented byTenant
use utopia_query::hook::Write;
Rust
pub trait Write: Send + Sync

Decorates rows before they are written and observes writes, for the database layer.

PHP: Utopia\Query\Hook\Write.