From 44df5a86218065924cbaf251e50c812c4a5b7e3f Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 23 May 2022 16:02:50 +0100 Subject: [PATCH] Allow modification of documents from subquery result sets --- lib/src/dbs/channel.rs | 23 +++++++++++++++++++++++ lib/src/dbs/iterate.rs | 23 +++++++++++++++++++++++ lib/src/sql/value/value.rs | 1 + 3 files changed, 47 insertions(+) diff --git a/lib/src/dbs/channel.rs b/lib/src/dbs/channel.rs index 8ce15723..fb8f1da3 100644 --- a/lib/src/dbs/channel.rs +++ b/lib/src/dbs/channel.rs @@ -7,6 +7,7 @@ use crate::key::thing; use crate::sql::array::Array; use crate::sql::id::Id; use crate::sql::model::Model; +use crate::sql::object::Object; use crate::sql::table::Table; use crate::sql::thing::Thing; use crate::sql::value::Value; @@ -26,6 +27,7 @@ impl Value { ) -> Result<(), Error> { if ctx.is_ok() { match self { + Value::Object(v) => v.process(ctx, opt, txn, stm, &chn).await?, Value::Array(v) => v.process(ctx, opt, txn, stm, &chn).await?, Value::Model(v) => v.process(ctx, opt, txn, stm, &chn).await?, Value::Thing(v) => v.process(ctx, opt, txn, stm, &chn).await?, @@ -51,6 +53,7 @@ impl Array { for v in self { if ctx.is_ok() { match v { + Value::Object(v) => v.process(ctx, opt, txn, stm, chn).await?, Value::Array(v) => v.process(ctx, opt, txn, stm, chn).await?, Value::Model(v) => v.process(ctx, opt, txn, stm, chn).await?, Value::Thing(v) => v.process(ctx, opt, txn, stm, chn).await?, @@ -63,6 +66,26 @@ impl Array { } } +impl Object { + #[cfg_attr(feature = "parallel", async_recursion)] + #[cfg_attr(not(feature = "parallel"), async_recursion(?Send))] + pub(crate) async fn process( + self, + ctx: &Context<'_>, + opt: &Options, + txn: &Transaction, + stm: &Statement<'_>, + chn: &Sender<(Option, Value)>, + ) -> Result<(), Error> { + if ctx.is_ok() { + if let Some(Value::Thing(id)) = self.get("id") { + id.clone().process(ctx, opt, txn, stm, chn).await?; + } + } + Ok(()) + } +} + impl Model { pub(crate) async fn process( self, diff --git a/lib/src/dbs/iterate.rs b/lib/src/dbs/iterate.rs index e64c3d6e..e43030c3 100644 --- a/lib/src/dbs/iterate.rs +++ b/lib/src/dbs/iterate.rs @@ -8,6 +8,7 @@ use crate::key::thing; use crate::sql::array::Array; use crate::sql::id::Id; use crate::sql::model::Model; +use crate::sql::object::Object; use crate::sql::table::Table; use crate::sql::thing::Thing; use crate::sql::value::Value; @@ -26,6 +27,7 @@ impl Value { ) -> Result<(), Error> { if ctx.is_ok() { match self { + Value::Object(v) => v.iterate(ctx, opt, txn, stm, ite).await?, Value::Array(v) => v.iterate(ctx, opt, txn, stm, ite).await?, Value::Model(v) => v.iterate(ctx, opt, txn, stm, ite).await?, Value::Thing(v) => v.iterate(ctx, opt, txn, stm, ite).await?, @@ -51,6 +53,7 @@ impl Array { for v in self.into_iter() { if ctx.is_ok() { match v { + Value::Object(v) => v.iterate(ctx, opt, txn, stm, ite).await?, Value::Array(v) => v.iterate(ctx, opt, txn, stm, ite).await?, Value::Model(v) => v.iterate(ctx, opt, txn, stm, ite).await?, Value::Thing(v) => v.iterate(ctx, opt, txn, stm, ite).await?, @@ -63,6 +66,26 @@ impl Array { } } +impl Object { + #[cfg_attr(feature = "parallel", async_recursion)] + #[cfg_attr(not(feature = "parallel"), async_recursion(?Send))] + pub(crate) async fn iterate( + self, + ctx: &Context<'_>, + opt: &Options, + txn: &Transaction, + stm: &Statement<'_>, + ite: &mut Iterator, + ) -> Result<(), Error> { + if ctx.is_ok() { + if let Some(Value::Thing(id)) = self.get("id") { + id.clone().iterate(ctx, opt, txn, stm, ite).await?; + } + } + Ok(()) + } +} + impl Model { pub(crate) async fn iterate( self, diff --git a/lib/src/sql/value/value.rs b/lib/src/sql/value/value.rs index f6d739ee..52110d72 100644 --- a/lib/src/sql/value/value.rs +++ b/lib/src/sql/value/value.rs @@ -1158,6 +1158,7 @@ pub fn select(i: &str) -> IResult<&str, Value> { pub fn what(i: &str) -> IResult<&str, Value> { alt(( + map(subquery, |v| Value::Subquery(Box::new(v))), map(function, |v| Value::Function(Box::new(v))), map(param, Value::Param), map(model, Value::Model),