use crate::ctx::Context;
use crate::dbs::Options;
use crate::dbs::Statement;
use crate::dbs::Workable;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::paths::EDGE;
use crate::sql::paths::IN;
use crate::sql::paths::OUT;
use crate::sql::value::Value;
use crate::sql::Dir;
use crate::sql::Relation;
use crate::sql::TableType;
impl Document {
pub(super) async fn store_edges_data(
&mut self,
ctx: &Context,
opt: &Options,
_stm: &Statement<'_>,
) -> Result<(), Error> {
let tb = self.tb(ctx, opt).await?;
if tb.drop {
return Ok(());
}
if let Workable::Relate(l, r, _) = &self.extras {
let ns = opt.ns()?;
let db = opt.db()?;
let rid = self.id()?;
let txn = ctx.tx();
let mut txn = txn.lock().await;
if matches!(
tb.kind,
TableType::Relation(Relation {
enforced: true,
..
})
) {
let key = crate::key::thing::new(ns, db, &l.tb, &l.id);
if !txn.exists(key, None).await? {
return Err(Error::IdNotFound {
value: l.to_string(),
});
}
let key = crate::key::thing::new(ns, db, &r.tb, &r.id);
if !txn.exists(key, None).await? {
return Err(Error::IdNotFound {
value: r.to_string(),
});
}
}
let (ref o, ref i) = (Dir::Out, Dir::In);
let key = crate::key::graph::new(ns, db, &l.tb, &l.id, o, &rid);
txn.set(key, vec![], opt.version).await?;
let key = crate::key::graph::new(ns, db, &rid.tb, &rid.id, i, l);
txn.set(key, vec![], opt.version).await?;
let key = crate::key::graph::new(ns, db, &rid.tb, &rid.id, o, r);
txn.set(key, vec![], opt.version).await?;
let key = crate::key::graph::new(ns, db, &r.tb, &r.id, i, &rid);
txn.set(key, vec![], opt.version).await?;
self.current.doc.to_mut().put(&*EDGE, Value::Bool(true));
self.current.doc.to_mut().put(&*IN, l.clone().into());
self.current.doc.to_mut().put(&*OUT, r.clone().into());
}
Ok(())
}
}