diff --git a/src/dbs/iterator.rs b/src/dbs/iterator.rs index 46c19115..9f4dccb0 100644 --- a/src/dbs/iterator.rs +++ b/src/dbs/iterator.rs @@ -178,7 +178,7 @@ impl<'a> Iterator<'a> { return; } // Setup a new document - let mut doc = Document::new(thg, val); + let mut doc = Document::new(thg, &val); // Process the document let res = match self.stmt { diff --git a/src/doc/admit.rs b/src/doc/admit.rs index 662655ea..e8edd9bb 100644 --- a/src/doc/admit.rs +++ b/src/doc/admit.rs @@ -5,7 +5,7 @@ use crate::dbs::Statement; use crate::doc::Document; use crate::err::Error; -impl Document { +impl<'a> Document<'a> { pub async fn admit( &self, _ctx: &Runtime, @@ -17,19 +17,19 @@ impl Document { Some(_) => Ok(()), None => match stm { Statement::Create(_) => Err(Error::CreateStatementError { - value: self.initial.clone(), + value: (*self.initial).clone(), }), Statement::Update(_) => Err(Error::UpdateStatementError { - value: self.initial.clone(), + value: (*self.initial).clone(), }), Statement::Relate(_) => Err(Error::RelateStatementError { - value: self.initial.clone(), + value: (*self.initial).clone(), }), Statement::Delete(_) => Err(Error::DeleteStatementError { - value: self.initial.clone(), + value: (*self.initial).clone(), }), Statement::Insert(_) => Err(Error::InsertStatementError { - value: self.initial.clone(), + value: (*self.initial).clone(), }), _ => unreachable!(), }, diff --git a/src/doc/allow.rs b/src/doc/allow.rs index ab262fdf..c29efad3 100644 --- a/src/doc/allow.rs +++ b/src/doc/allow.rs @@ -5,7 +5,7 @@ use crate::dbs::Statement; use crate::doc::Document; use crate::err::Error; -impl Document { +impl<'a> Document<'a> { pub async fn allow( &self, _ctx: &Runtime, diff --git a/src/doc/check.rs b/src/doc/check.rs index a38c5907..9277fc5f 100644 --- a/src/doc/check.rs +++ b/src/doc/check.rs @@ -5,7 +5,7 @@ use crate::dbs::Statement; use crate::doc::Document; use crate::err::Error; -impl Document { +impl<'a> Document<'a> { pub async fn check( &self, ctx: &Runtime, diff --git a/src/doc/compute.rs b/src/doc/compute.rs index 2877468e..50a1135e 100644 --- a/src/doc/compute.rs +++ b/src/doc/compute.rs @@ -6,7 +6,7 @@ use crate::doc::Document; use crate::err::Error; use crate::sql::value::Value; -impl Document { +impl<'a> Document<'a> { pub async fn compute( &mut self, ctx: &Runtime, diff --git a/src/doc/create.rs b/src/doc/create.rs index 809f1394..cc805d41 100644 --- a/src/doc/create.rs +++ b/src/doc/create.rs @@ -6,7 +6,7 @@ use crate::doc::Document; use crate::err::Error; use crate::sql::value::Value; -impl Document { +impl<'a> Document<'a> { pub async fn create( &mut self, ctx: &Runtime, diff --git a/src/doc/delete.rs b/src/doc/delete.rs index dd9da198..f8fd4897 100644 --- a/src/doc/delete.rs +++ b/src/doc/delete.rs @@ -6,7 +6,7 @@ use crate::doc::Document; use crate::err::Error; use crate::sql::value::Value; -impl Document { +impl<'a> Document<'a> { pub async fn delete( &mut self, ctx: &Runtime, diff --git a/src/doc/document.rs b/src/doc/document.rs index aa414863..cdc16014 100644 --- a/src/doc/document.rs +++ b/src/doc/document.rs @@ -1,19 +1,20 @@ use crate::sql::thing::Thing; use crate::sql::value::Value; +use std::borrow::Cow; #[derive(Clone, Debug, Default, Eq, PartialEq)] -pub struct Document { +pub struct Document<'a> { pub(super) id: Option, - pub(super) current: Value, - pub(super) initial: Value, + pub(super) current: Cow<'a, Value>, + pub(super) initial: Cow<'a, Value>, } -impl Document { - pub fn new(id: Option, val: Value) -> Document { +impl<'a> Document<'a> { + pub fn new(id: Option, val: &'a Value) -> Self { Document { id, - current: val.clone(), - initial: val, + current: Cow::Borrowed(val), + initial: Cow::Borrowed(val), } } } diff --git a/src/doc/empty.rs b/src/doc/empty.rs index e7ef4c72..d517f5b9 100644 --- a/src/doc/empty.rs +++ b/src/doc/empty.rs @@ -4,9 +4,8 @@ use crate::dbs::Runtime; use crate::dbs::Statement; use crate::doc::Document; use crate::err::Error; -use crate::sql::value::Value; -impl Document { +impl<'a> Document<'a> { pub async fn empty( &self, _ctx: &Runtime, @@ -14,7 +13,7 @@ impl Document { _exe: &Executor<'_>, _stm: &Statement<'_>, ) -> Result<(), Error> { - match self.id.is_some() && self.current == Value::None { + match self.id.is_some() && self.current.is_none() { true => Err(Error::IgnoreError), false => Ok(()), } diff --git a/src/doc/erase.rs b/src/doc/erase.rs index 1ccd3cb9..ac71c304 100644 --- a/src/doc/erase.rs +++ b/src/doc/erase.rs @@ -5,7 +5,7 @@ use crate::dbs::Statement; use crate::doc::Document; use crate::err::Error; -impl Document { +impl<'a> Document<'a> { pub async fn erase( &mut self, ctx: &Runtime, @@ -13,6 +13,6 @@ impl Document { exe: &Executor<'_>, _stm: &Statement<'_>, ) -> Result<(), Error> { - self.current.clear(ctx, opt, exe).await + self.current.to_mut().clear(ctx, opt, exe).await } } diff --git a/src/doc/event.rs b/src/doc/event.rs index 1226e2e5..bce6c10e 100644 --- a/src/doc/event.rs +++ b/src/doc/event.rs @@ -5,7 +5,7 @@ use crate::dbs::Statement; use crate::doc::Document; use crate::err::Error; -impl Document { +impl<'a> Document<'a> { pub async fn event( &self, _ctx: &Runtime, diff --git a/src/doc/index.rs b/src/doc/index.rs index 3e6a9b3a..34ef0fec 100644 --- a/src/doc/index.rs +++ b/src/doc/index.rs @@ -5,7 +5,7 @@ use crate::dbs::Statement; use crate::doc::Document; use crate::err::Error; -impl Document { +impl<'a> Document<'a> { pub async fn index( &self, _ctx: &Runtime, diff --git a/src/doc/insert.rs b/src/doc/insert.rs index dd34dfdf..d2c68b46 100644 --- a/src/doc/insert.rs +++ b/src/doc/insert.rs @@ -6,7 +6,7 @@ use crate::doc::Document; use crate::err::Error; use crate::sql::value::Value; -impl Document { +impl<'a> Document<'a> { pub async fn insert( &mut self, _ctx: &Runtime, diff --git a/src/doc/lives.rs b/src/doc/lives.rs index 83529341..f5e3d6fc 100644 --- a/src/doc/lives.rs +++ b/src/doc/lives.rs @@ -5,7 +5,7 @@ use crate::dbs::Statement; use crate::doc::Document; use crate::err::Error; -impl Document { +impl<'a> Document<'a> { pub async fn lives( &self, _ctx: &Runtime, diff --git a/src/doc/merge.rs b/src/doc/merge.rs index 5cc6feb5..df3b8e68 100644 --- a/src/doc/merge.rs +++ b/src/doc/merge.rs @@ -8,7 +8,7 @@ use crate::sql::data::Data; use crate::sql::operator::Operator; use crate::sql::value::Value; -impl Document { +impl<'a> Document<'a> { pub async fn merge( &mut self, ctx: &Runtime, @@ -25,7 +25,7 @@ impl Document { _ => unreachable!(), }; // Set default field values - self.current.def(ctx, opt, exe, id).await?; + self.current.to_mut().def(ctx, opt, exe, id).await?; // Check for a data clause match data { // The statement has a data clause @@ -35,26 +35,36 @@ impl Document { let v = x.2.compute(ctx, opt, exe, Some(&self.current)).await?; match x.1 { Operator::Equal => match v { - Value::Void => self.current.del(ctx, opt, exe, &x.0).await?, - _ => self.current.set(ctx, opt, exe, &x.0, v).await?, + Value::Void => { + self.current.to_mut().del(ctx, opt, exe, &x.0).await? + } + _ => self.current.to_mut().set(ctx, opt, exe, &x.0, v).await?, }, - Operator::Inc => self.current.increment(ctx, opt, exe, &x.0, v).await?, - Operator::Dec => self.current.decrement(ctx, opt, exe, &x.0, v).await?, + Operator::Inc => { + self.current.to_mut().increment(ctx, opt, exe, &x.0, v).await? + } + Operator::Dec => { + self.current.to_mut().decrement(ctx, opt, exe, &x.0, v).await? + } _ => unreachable!(), } } } - Data::PatchExpression(v) => self.current.patch(ctx, opt, exe, v).await?, - Data::MergeExpression(v) => self.current.merge(ctx, opt, exe, v).await?, - Data::ReplaceExpression(v) => self.current.replace(ctx, opt, exe, v).await?, - Data::ContentExpression(v) => self.current.replace(ctx, opt, exe, v).await?, + Data::PatchExpression(v) => self.current.to_mut().patch(ctx, opt, exe, v).await?, + Data::MergeExpression(v) => self.current.to_mut().merge(ctx, opt, exe, v).await?, + Data::ReplaceExpression(v) => { + self.current.to_mut().replace(ctx, opt, exe, v).await? + } + Data::ContentExpression(v) => { + self.current.to_mut().replace(ctx, opt, exe, v).await? + } _ => unreachable!(), }, // No data clause has been set None => (), }; // Set default field values - self.current.def(ctx, opt, exe, id).await?; + self.current.to_mut().def(ctx, opt, exe, id).await?; // Set ASSERT and VALUE clauses // todo!(); // Delete non-defined FIELDs diff --git a/src/doc/pluck.rs b/src/doc/pluck.rs index e5301f2e..c562e617 100644 --- a/src/doc/pluck.rs +++ b/src/doc/pluck.rs @@ -9,7 +9,7 @@ use crate::sql::idiom::Idiom; use crate::sql::output::Output; use crate::sql::value::Value; -impl Document { +impl<'a> Document<'a> { pub async fn pluck( &self, ctx: &Runtime, diff --git a/src/doc/relate.rs b/src/doc/relate.rs index 90070c2e..7de84c83 100644 --- a/src/doc/relate.rs +++ b/src/doc/relate.rs @@ -6,7 +6,7 @@ use crate::doc::Document; use crate::err::Error; use crate::sql::value::Value; -impl Document { +impl<'a> Document<'a> { pub async fn relate( &mut self, ctx: &Runtime, diff --git a/src/doc/select.rs b/src/doc/select.rs index eec7bd05..39e93bcc 100644 --- a/src/doc/select.rs +++ b/src/doc/select.rs @@ -6,7 +6,7 @@ use crate::doc::Document; use crate::err::Error; use crate::sql::value::Value; -impl Document { +impl<'a> Document<'a> { pub async fn select( &self, ctx: &Runtime, diff --git a/src/doc/store.rs b/src/doc/store.rs index 752bfa0d..06eb5872 100644 --- a/src/doc/store.rs +++ b/src/doc/store.rs @@ -5,7 +5,7 @@ use crate::dbs::Statement; use crate::doc::Document; use crate::err::Error; -impl Document { +impl<'a> Document<'a> { pub async fn store( &self, _ctx: &Runtime, diff --git a/src/doc/table.rs b/src/doc/table.rs index d031e84f..d436c5c9 100644 --- a/src/doc/table.rs +++ b/src/doc/table.rs @@ -5,7 +5,7 @@ use crate::dbs::Statement; use crate::doc::Document; use crate::err::Error; -impl Document { +impl<'a> Document<'a> { pub async fn table( &self, _ctx: &Runtime, diff --git a/src/doc/update.rs b/src/doc/update.rs index 156f5430..5764c0c0 100644 --- a/src/doc/update.rs +++ b/src/doc/update.rs @@ -6,7 +6,7 @@ use crate::doc::Document; use crate::err::Error; use crate::sql::value::Value; -impl Document { +impl<'a> Document<'a> { pub async fn update( &mut self, ctx: &Runtime,