#![deny(missing_docs)]
use std::marker::PhantomData;
use type_bridge_contract::codec::from_canonical_json;
use type_bridge_contract::id::{AttributeId, RoleId, TypeId};
use type_bridge_contract::migration_assertion::{BindingId, ValueComparator};
use type_bridge_contract::query_plan::v3::{
ArithOp, Builtin, Expr as PlanExpr, PlanBuilder as CoreBuilder, QueryPlanV3,
};
use type_bridge_contract::query_plan::{InputColumnId, QueryOperand};
use type_bridge_contract::schema::OwnsFactId;
use type_bridge_contract::value::{CanonicalValue, ValueTypeTag};
use crate::__codegen::{
CanonicalDouble, Date, DateTime, DateTimeTz, Decimal, Duration, FieldToken, Model,
NominalUpcast, QueryValued, RelationModel, RolePlayerBinding, RoleToken, RoleTokenCompatible,
ThingModel, ValidationError, ValidationPath,
};
use crate::Result;
use crate::error::{Error, ModelValidationPhase};
use crate::query::{Exact, SelectionMode, Subtypes};
use crate::schema::{Schema, SchemaPackage};
fn identity_error(code: &'static str) -> Error {
Error::model_validation(
ModelValidationPhase::Input,
code,
vec![],
"generated token identity is not canonical",
None,
)
}
fn literal_error(error: ValidationError) -> Error {
Error::model_validation(
ModelValidationPhase::Input,
"plan_literal_invalid",
vec![],
"a plan literal is not a canonical value",
Some(Box::new(error)),
)
}
fn type_id<M: Model>() -> Result<TypeId> {
from_canonical_json(M::TYPE_ID_JSON.as_bytes())
.map_err(|_| identity_error("invalid_model_identity"))
}
fn owns_id(owns_id_json: &str) -> Result<OwnsFactId> {
let invalid = || identity_error("invalid_field_identity");
let value: serde_json::Value = serde_json::from_str(owns_id_json).map_err(|_| invalid())?;
let owner: TypeId = value
.get("owner")
.cloned()
.and_then(|owner| serde_json::from_value(owner).ok())
.ok_or_else(invalid)?;
let attribute = value
.get("attribute")
.and_then(serde_json::Value::as_str)
.and_then(|attribute| AttributeId::new(attribute).ok())
.ok_or_else(invalid)?;
OwnsFactId::new(owner, attribute).map_err(|_| invalid())
}
fn role_id(role_id_json: &str) -> Result<RoleId> {
from_canonical_json(role_id_json.as_bytes())
.map_err(|_| identity_error("invalid_role_identity"))
}
fn canonical<T: QueryValued>(value: &T) -> std::result::Result<CanonicalValue, ValidationError> {
value
.into_encoded_scalar()
.to_canonical_value(&ValidationPath::root())
}
mod sealed {
pub trait Sealed {
const VALUE_TYPE: super::ValueTypeTag;
}
}
pub trait PlanScalar: sealed::Sealed {}
macro_rules! plan_scalar {
($($domain:ty => $tag:ident),* $(,)?) => {$(
impl sealed::Sealed for $domain {
const VALUE_TYPE: ValueTypeTag = ValueTypeTag::$tag;
}
impl PlanScalar for $domain {}
)*};
}
plan_scalar!(
String => String,
i64 => Long,
CanonicalDouble => Double,
bool => Boolean,
Decimal => Decimal,
Date => Date,
DateTime => DateTime,
DateTimeTz => DateTimeTz,
Duration => Duration,
);
pub struct Thing<M: ThingModel, Mode: SelectionMode = Exact> {
binding: BindingId,
marker: PhantomData<fn() -> (M, Mode)>,
}
impl<M: ThingModel, Mode: SelectionMode> Clone for Thing<M, Mode> {
fn clone(&self) -> Self {
*self
}
}
impl<M: ThingModel, Mode: SelectionMode> Copy for Thing<M, Mode> {}
impl<M: ThingModel, Mode: SelectionMode> std::fmt::Debug for Thing<M, Mode> {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_tuple("Thing")
.field(&self.binding.get())
.finish()
}
}
#[derive(Clone, Copy, Debug)]
pub struct Value {
binding: BindingId,
}
pub struct Input<V> {
column: InputColumnId,
marker: PhantomData<fn() -> V>,
}
impl<V> Clone for Input<V> {
fn clone(&self) -> Self {
*self
}
}
impl<V> Copy for Input<V> {}
impl<V> std::fmt::Debug for Input<V> {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_tuple("Input")
.field(&self.column.get())
.finish()
}
}
impl<V: QueryValued> Input<V> {
#[must_use]
pub fn value(self, value: V) -> InputValue {
InputValue {
column: self.column,
value: canonical(&value).map(Some),
}
}
}
impl<V> Input<V> {
#[must_use]
pub fn absent(self) -> InputValue {
InputValue {
column: self.column,
value: Ok(None),
}
}
}
#[derive(Clone, Debug)]
pub struct InputValue {
column: InputColumnId,
value: std::result::Result<Option<CanonicalValue>, ValidationError>,
}
#[derive(Clone, Debug)]
pub struct Operand(std::result::Result<QueryOperand, ValidationError>);
impl From<Value> for Operand {
fn from(value: Value) -> Self {
Self(Ok(QueryOperand::Binding {
binding: value.binding,
}))
}
}
impl<V> From<Input<V>> for Operand {
fn from(input: Input<V>) -> Self {
Self(Ok(QueryOperand::Input {
column: input.column,
}))
}
}
impl<T: QueryValued> From<T> for Operand {
fn from(value: T) -> Self {
Self(canonical(&value).map(|value| QueryOperand::Literal { value }))
}
}
#[derive(Clone, Debug)]
pub struct Condition {
left: Operand,
comparator: ValueComparator,
right: Operand,
}
macro_rules! comparisons {
($($name:ident => $comparator:ident, $doc:literal;)*) => {$(
#[doc = $doc]
#[must_use]
pub fn $name(self, other: impl Into<Operand>) -> Condition {
Condition {
left: self.into(),
comparator: ValueComparator::$comparator,
right: other.into(),
}
}
)*};
}
impl Value {
comparisons! {
eq => Equal, "Require this value to equal `other`.";
ne => NotEqual, "Require this value to differ from `other`.";
lt => Less, "Require this value to be less than `other`.";
le => LessOrEqual, "Require this value to be at most `other`.";
gt => Greater, "Require this value to be greater than `other`.";
ge => GreaterOrEqual, "Require this value to be at least `other`.";
}
}
#[derive(Clone, Debug)]
pub struct Expr(std::result::Result<PlanExpr, ValidationError>);
impl<T: Into<Operand>> From<T> for Expr {
fn from(operand: T) -> Self {
Self(operand.into().0.map(PlanExpr::Operand))
}
}
impl Expr {
fn arith(op: ArithOp, lhs: Self, rhs: Self) -> Self {
Self(lhs.0.and_then(|lhs| {
rhs.0.map(|rhs| PlanExpr::Arith {
op,
lhs: Box::new(lhs),
rhs: Box::new(rhs),
})
}))
}
fn call(func: Builtin, args: Vec<Self>) -> Self {
Self(
args.into_iter()
.map(|arg| arg.0)
.collect::<std::result::Result<Vec<_>, _>>()
.map(|args| PlanExpr::Call { func, args }),
)
}
#[must_use]
pub fn pow(base: impl Into<Self>, exponent: impl Into<Self>) -> Self {
Self::arith(ArithOp::Pow, base.into(), exponent.into())
}
#[must_use]
pub fn abs(value: impl Into<Self>) -> Self {
Self::call(Builtin::Abs, vec![value.into()])
}
#[must_use]
pub fn ceil(value: impl Into<Self>) -> Self {
Self::call(Builtin::Ceil, vec![value.into()])
}
#[must_use]
pub fn floor(value: impl Into<Self>) -> Self {
Self::call(Builtin::Floor, vec![value.into()])
}
#[must_use]
pub fn round(value: impl Into<Self>) -> Self {
Self::call(Builtin::Round, vec![value.into()])
}
#[must_use]
pub fn min(left: impl Into<Self>, right: impl Into<Self>) -> Self {
Self::call(Builtin::Min, vec![left.into(), right.into()])
}
#[must_use]
pub fn max(left: impl Into<Self>, right: impl Into<Self>) -> Self {
Self::call(Builtin::Max, vec![left.into(), right.into()])
}
#[must_use]
pub fn length(value: impl Into<Self>) -> Self {
Self::call(Builtin::Length, vec![value.into()])
}
}
macro_rules! arithmetic {
($($trait:ident :: $method:ident => $op:ident;)*) => {$(
impl<R: Into<Expr>> std::ops::$trait<R> for Expr {
type Output = Expr;
fn $method(self, rhs: R) -> Expr {
Expr::arith(ArithOp::$op, self, rhs.into())
}
}
impl<R: Into<Expr>> std::ops::$trait<R> for Value {
type Output = Expr;
fn $method(self, rhs: R) -> Expr {
Expr::arith(ArithOp::$op, self.into(), rhs.into())
}
}
impl<V, R: Into<Expr>> std::ops::$trait<R> for Input<V> {
type Output = Expr;
fn $method(self, rhs: R) -> Expr {
Expr::arith(ArithOp::$op, self.into(), rhs.into())
}
}
)*};
}
arithmetic! {
Add::add => Add;
Sub::sub => Sub;
Mul::mul => Mul;
Div::div => Div;
Rem::rem => Mod;
}
#[derive(Clone, Copy, Debug)]
pub struct Column(BindingId);
impl<M: ThingModel, Mode: SelectionMode> From<Thing<M, Mode>> for Column {
fn from(thing: Thing<M, Mode>) -> Self {
Self(thing.binding)
}
}
impl From<Value> for Column {
fn from(value: Value) -> Self {
Self(value.binding)
}
}
pub struct Plan<S: Schema> {
plan: QueryPlanV3,
marker: PhantomData<fn() -> S>,
}
impl<S: Schema> Clone for Plan<S> {
fn clone(&self) -> Self {
Self {
plan: self.plan.clone(),
marker: PhantomData,
}
}
}
impl<S: Schema> std::fmt::Debug for Plan<S> {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("Plan")
.field("writes", &self.plan.writes())
.finish_non_exhaustive()
}
}
impl<S: Schema> Plan<S> {
#[must_use]
pub fn builder() -> PlanBuilder<S> {
PlanBuilder::new()
}
pub fn canonical_bytes(&self) -> Result<Vec<u8>> {
self.plan
.canonical_bytes()
.map_err(Error::from_contract_diagnostic)
}
#[must_use]
pub fn writes(&self) -> bool {
self.plan.writes()
}
pub(crate) fn inner(&self) -> &QueryPlanV3 {
&self.plan
}
}
pub struct PlanBuilder<S: Schema> {
core: CoreBuilder,
error: Option<Error>,
marker: PhantomData<fn() -> S>,
}
impl<S: Schema> Default for PlanBuilder<S> {
fn default() -> Self {
Self::new()
}
}
impl<S: Schema> std::fmt::Debug for PlanBuilder<S> {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("PlanBuilder")
.finish_non_exhaustive()
}
}
impl<S: Schema> PlanBuilder<S> {
#[must_use]
pub fn new() -> Self {
Self {
core: CoreBuilder::new(),
error: None,
marker: PhantomData,
}
}
fn check<T>(&mut self, result: Result<T>) -> Option<T> {
match result {
Ok(value) => Some(value),
Err(error) => {
self.error.get_or_insert(error);
None
}
}
}
fn operand(&mut self, operand: impl Into<Operand>) -> QueryOperand {
let operand = operand.into().0.map_err(literal_error);
self.check(operand).unwrap_or(QueryOperand::Literal {
value: CanonicalValue::Boolean(false),
})
}
fn thing<M: ThingModel<Schema = S>, Mode: SelectionMode>(
&mut self,
binding: impl FnOnce(&mut CoreBuilder, TypeId) -> BindingId,
) -> Thing<M, Mode> {
let binding = match self.check(type_id::<M>()) {
Some(type_id) => binding(&mut self.core, type_id),
None => BindingId::new(0).expect("zero is a valid binding"),
};
Thing {
binding,
marker: PhantomData,
}
}
pub fn exact<M: ThingModel<Schema = S>>(&mut self) -> Thing<M, Exact> {
self.thing(CoreBuilder::exact)
}
pub fn subtypes<M: ThingModel<Schema = S>>(&mut self) -> Thing<M, Subtypes> {
self.thing(CoreBuilder::subtypes)
}
pub fn field<M, Mode, Owner, V>(
&mut self,
thing: Thing<M, Mode>,
token: FieldToken<Owner, V>,
) -> Value
where
M: ThingModel<Schema = S> + NominalUpcast<Owner>,
Mode: SelectionMode,
Owner: Model<Schema = S>,
{
let binding = match self.check(owns_id(token.owns_id_json())) {
Some(field) => self.core.field(thing.binding, field.attribute().clone()),
None => BindingId::new(0).expect("zero is a valid binding"),
};
Value { binding }
}
pub fn input<V>(&mut self, name: &str) -> Input<V>
where
V: QueryValued,
V::Domain: PlanScalar,
{
let column = self
.core
.input(name, <V::Domain as sealed::Sealed>::VALUE_TYPE, false);
Input {
column,
marker: PhantomData,
}
}
pub fn optional_input<V>(&mut self, name: &str) -> Input<V>
where
V: QueryValued,
V::Domain: PlanScalar,
{
let column = self
.core
.input(name, <V::Domain as sealed::Sealed>::VALUE_TYPE, true);
Input {
column,
marker: PhantomData,
}
}
pub fn where_(&mut self, condition: Condition) -> &mut Self {
let left = self.operand(condition.left);
let right = self.operand(condition.right);
self.core.compare(left, condition.comparator, right);
self
}
pub fn links<R, RMode, Owner, Players, P, PMode>(
&mut self,
relation: Thing<R, RMode>,
role: RoleToken<Owner, Players>,
player: Thing<P, PMode>,
) -> &mut Self
where
R: ThingModel<Schema = S> + RoleTokenCompatible<Owner, Players>,
RMode: SelectionMode,
Owner: RelationModel<Schema = S>,
Players: RolePlayerBinding<P, PMode>,
P: ThingModel<Schema = S>,
PMode: SelectionMode,
{
if let Some(role) = self.check(role_id(role.role_id_json())) {
self.core.links(relation.binding, role, player.binding);
}
self
}
pub fn let_(&mut self, name: &str, expr: impl Into<Expr>) -> Value {
let expr = expr.into().0.map_err(literal_error);
let expr = self
.check(expr)
.unwrap_or(PlanExpr::Operand(QueryOperand::Literal {
value: CanonicalValue::Boolean(false),
}));
Value {
binding: self.core.let_(name, expr),
}
}
pub fn update<M, Mode, Owner, V>(
&mut self,
thing: Thing<M, Mode>,
token: FieldToken<Owner, V>,
value: impl Into<Operand>,
) -> &mut Self
where
M: ThingModel<Schema = S> + NominalUpcast<Owner>,
Mode: SelectionMode,
Owner: Model<Schema = S>,
{
let operand = self.operand(value);
if let Some(field) = self.check(owns_id(token.owns_id_json())) {
self.core.update(thing.binding, field, operand);
}
self
}
pub fn update_player<R, RMode, Owner, Players, P, PMode>(
&mut self,
relation: Thing<R, RMode>,
role: RoleToken<Owner, Players>,
player: Thing<P, PMode>,
) -> &mut Self
where
R: ThingModel<Schema = S> + RoleTokenCompatible<Owner, Players>,
RMode: SelectionMode,
Owner: RelationModel<Schema = S>,
Players: RolePlayerBinding<P, PMode>,
P: ThingModel<Schema = S>,
PMode: SelectionMode,
{
if let Some(role) = self.check(role_id(role.role_id_json())) {
self.core
.update_player(relation.binding, role, player.binding);
}
self
}
pub fn insert<M: ThingModel<Schema = S>>(&mut self) -> NewThing<'_, S, M> {
let thing = self.thing(CoreBuilder::insert);
NewThing {
builder: self,
thing,
}
}
pub fn put<M: ThingModel<Schema = S>>(&mut self) -> NewThing<'_, S, M> {
let thing = self.thing(CoreBuilder::put);
NewThing {
builder: self,
thing,
}
}
pub fn delete<M: ThingModel<Schema = S>, Mode: SelectionMode>(
&mut self,
thing: Thing<M, Mode>,
) -> &mut Self {
self.core.delete(thing.binding);
self
}
pub fn delete_field<M, Mode, Owner, V>(
&mut self,
thing: Thing<M, Mode>,
token: FieldToken<Owner, V>,
) -> &mut Self
where
M: ThingModel<Schema = S> + NominalUpcast<Owner>,
Mode: SelectionMode,
Owner: Model<Schema = S>,
{
if let Some(field) = self.check(owns_id(token.owns_id_json())) {
self.core.delete_field(thing.binding, field);
}
self
}
pub fn delete_player<R, RMode, Owner, Players, P, PMode>(
&mut self,
relation: Thing<R, RMode>,
role: RoleToken<Owner, Players>,
player: Thing<P, PMode>,
) -> &mut Self
where
R: ThingModel<Schema = S> + RoleTokenCompatible<Owner, Players>,
RMode: SelectionMode,
Owner: RelationModel<Schema = S>,
Players: RolePlayerBinding<P, PMode>,
P: ThingModel<Schema = S>,
PMode: SelectionMode,
{
if let Some(role) = self.check(role_id(role.role_id_json())) {
self.core
.delete_player(relation.binding, role, player.binding);
}
self
}
pub fn row(&mut self, values: impl IntoIterator<Item = InputValue>) -> &mut Self {
let mut row: Vec<Option<CanonicalValue>> = Vec::new();
let mut seen = std::collections::BTreeSet::new();
for value in values {
let index = usize::from(value.column.get());
if !seen.insert(index) {
self.check::<()>(Err(Error::model_validation(
ModelValidationPhase::Input,
"plan_input_duplicate",
vec![],
"an input row gives one column twice",
None,
)));
return self;
}
let Some(value) = self.check(value.value.map_err(literal_error)) else {
return self;
};
if row.len() <= index {
row.resize(index + 1, None);
}
row[index] = value;
}
self.core.row(row);
self
}
pub fn returning(&mut self, columns: impl IntoIterator<Item = impl Into<Column>>) -> &mut Self {
self.core
.returning(columns.into_iter().map(|column| column.into().0).collect());
self
}
pub fn count(&mut self) -> &mut Self {
self.core.count();
self
}
pub fn exists(&mut self) -> &mut Self {
self.core.exists();
self
}
pub fn build(self, schema: &SchemaPackage<S>) -> Result<Plan<S>> {
if let Some(error) = self.error {
return Err(error);
}
let authority = schema.plan_authority()?;
let plan = self
.core
.build(
authority.declared_schema(),
authority.managed_state().managed_semantic_schema().clone(),
)
.map_err(Error::from_contract_diagnostic)?;
Ok(Plan {
plan,
marker: PhantomData,
})
}
}
pub struct NewThing<'a, S: Schema, M: ThingModel<Schema = S>> {
builder: &'a mut PlanBuilder<S>,
thing: Thing<M, Exact>,
}
impl<S: Schema, M: ThingModel<Schema = S>> std::fmt::Debug for NewThing<'_, S, M> {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_tuple("NewThing")
.field(&self.thing)
.finish()
}
}
impl<S: Schema, M: ThingModel<Schema = S>> NewThing<'_, S, M> {
pub fn field<Owner, V>(self, token: FieldToken<Owner, V>, value: impl Into<Operand>) -> Self
where
M: NominalUpcast<Owner>,
Owner: Model<Schema = S>,
{
let operand = self.builder.operand(value);
if let Some(field) = self.builder.check(owns_id(token.owns_id_json())) {
self.builder
.core
.thing_field(self.thing.binding, field, operand);
}
self
}
pub fn player<Owner, Players, P, PMode>(
self,
role: RoleToken<Owner, Players>,
player: Thing<P, PMode>,
) -> Self
where
M: RoleTokenCompatible<Owner, Players>,
Owner: RelationModel<Schema = S>,
Players: RolePlayerBinding<P, PMode>,
P: ThingModel<Schema = S>,
PMode: SelectionMode,
{
if let Some(role) = self.builder.check(role_id(role.role_id_json())) {
self.builder
.core
.thing_player(self.thing.binding, role, player.binding);
}
self
}
#[must_use]
pub fn thing(self) -> Thing<M, Exact> {
self.thing
}
}
#[cfg(test)]
mod tests;