diff --git a/lib/src/ctx/context.rs b/lib/src/ctx/context.rs index dca575c0..4049f24e 100644 --- a/lib/src/ctx/context.rs +++ b/lib/src/ctx/context.rs @@ -1,32 +1,45 @@ use crate::ctx::canceller::Canceller; use crate::ctx::reason::Reason; -use std::any::Any; +use crate::sql::value::Value; +use std::borrow::Cow; use std::collections::HashMap; use std::fmt; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::time::{Duration, Instant}; -pub struct Context { +impl<'a> From for Cow<'a, Value> { + fn from(v: Value) -> Cow<'a, Value> { + Cow::Owned(v) + } +} + +impl<'a> From<&'a Value> for Cow<'a, Value> { + fn from(v: &'a Value) -> Cow<'a, Value> { + Cow::Borrowed(v) + } +} + +pub struct Context<'a> { // An optional parent context. - parent: Option>, + parent: Option<&'a Context<'a>>, // An optional deadline. deadline: Option, // Wether or not this context is cancelled. cancelled: Arc, // A collection of read only values stored in this context. - values: Option>>, + values: Option>>, } -impl Default for Context { +impl<'a> Default for Context<'a> { fn default() -> Self { Context::background() } } -impl Context { +impl<'a> Context<'a> { // Create an empty background context. - pub fn background() -> Context { + pub fn background() -> Self { Context { values: None, parent: None, @@ -36,24 +49,15 @@ impl Context { } // Create a new child from a frozen context. - pub fn new(parent: &Arc) -> Context { + pub fn new(parent: &'a Context) -> Self { Context { values: None, - parent: Some(Arc::clone(parent)), + parent: Some(parent), deadline: parent.deadline, cancelled: Arc::new(AtomicBool::new(false)), } } - // Freeze the context so it can be used to create child contexts. The - // parent context can no longer be modified once it has been frozen. - pub fn freeze(mut self) -> Arc { - if let Some(ref mut values) = self.values { - values.shrink_to_fit(); - } - Arc::new(self) - } - // Add cancelation to the context. The value that is returned will cancel // the context and it's children once called. pub fn add_cancel(&mut self) -> Canceller { @@ -80,10 +84,10 @@ impl Context { // with the same key. pub fn add_value(&mut self, key: String, value: V) where - V: Any + Send + Sync + Sized, + V: Into>, { if let Some(ref mut values) = self.values { - values.insert(key, Box::new(value)); + values.insert(key, value.into()); } else { self.values = Some(HashMap::new()); self.add_value(key, value); @@ -104,7 +108,7 @@ impl Context { // TODO: see if we can relax the ordering. _ if self.cancelled.load(Ordering::SeqCst) => Some(Reason::Canceled), _ => match self.parent { - Some(ref parent_ctx) => parent_ctx.done(), + Some(ctx) => ctx.done(), _ => None, }, } @@ -146,24 +150,27 @@ impl Context { // Get a value from the context. If no value is stored under the // provided key, then this will return None. - pub fn value(&self, key: &str) -> Option<&V> - where - V: Any + Send + Sync + Sized, - { - if let Some(ref values) = self.values { - if let Some(value) = values.get(key) { - let value: &dyn Any = &**value; - return value.downcast_ref::(); - } - } - match self.parent { - Some(ref parent) => parent.value(key), - _ => None, + pub fn value(&self, key: &str) -> Option<&Value> { + match &self.values { + Some(v) => match v.get(key) { + Some(v) => match v { + Cow::Borrowed(v) => Some(*v), + Cow::Owned(v) => Some(v), + }, + None => match self.parent { + Some(p) => p.value(key), + _ => None, + }, + }, + None => match self.parent { + Some(p) => p.value(key), + _ => None, + }, } } } -impl fmt::Debug for Context { +impl<'a> fmt::Debug for Context<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Context") .field("parent", &self.parent) diff --git a/lib/src/dbs/channel.rs b/lib/src/dbs/channel.rs index 17f640de..d1923278 100644 --- a/lib/src/dbs/channel.rs +++ b/lib/src/dbs/channel.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::err::Error; @@ -16,7 +16,7 @@ use channel::Sender; impl Value { pub(crate) async fn channel( self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, @@ -39,7 +39,7 @@ impl Array { #[async_recursion] pub(crate) async fn process( self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, @@ -63,7 +63,7 @@ impl Array { impl Model { pub(crate) async fn process( self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, @@ -100,7 +100,7 @@ impl Model { impl Thing { pub(crate) async fn process( self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, _stm: &Statement<'_>, @@ -122,7 +122,7 @@ impl Thing { impl Table { pub(crate) async fn process( self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, _stm: &Statement<'_>, diff --git a/lib/src/dbs/executor.rs b/lib/src/dbs/executor.rs index a297f7d6..87c35373 100644 --- a/lib/src/dbs/executor.rs +++ b/lib/src/dbs/executor.rs @@ -3,7 +3,6 @@ use crate::dbs::response::Response; use crate::dbs::Auth; use crate::dbs::Level; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::kvs::Datastore; @@ -117,7 +116,7 @@ impl<'a> Executor<'a> { pub async fn execute( &mut self, - mut ctx: Runtime, + mut ctx: Context<'_>, mut opt: Options, qry: Query, ) -> Result, Error> { @@ -213,10 +212,7 @@ impl<'a> Executor<'a> { // Process the statement match stm.compute(&ctx, &opt, &self.txn(), None).await { Ok(val) => { - let mut new = Context::new(&ctx); - let key = stm.name.to_owned(); - new.add_value(key, val); - ctx = new.freeze(); + ctx.add_value(stm.name.to_owned(), val); } _ => break, } @@ -240,7 +236,6 @@ impl<'a> Executor<'a> { // Set statement timeout let mut ctx = Context::new(&ctx); ctx.add_timeout(timeout); - let ctx = ctx.freeze(); // Process the statement let res = stm.compute(&ctx, &opt, &self.txn(), None).await; // Catch statement timeout diff --git a/lib/src/dbs/iterate.rs b/lib/src/dbs/iterate.rs index 10cc0608..e64c3d6e 100644 --- a/lib/src/dbs/iterate.rs +++ b/lib/src/dbs/iterate.rs @@ -1,6 +1,6 @@ +use crate::ctx::Context; use crate::dbs::Iterator; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::err::Error; @@ -18,7 +18,7 @@ impl Value { #[cfg_attr(not(feature = "parallel"), async_recursion(?Send))] pub(crate) async fn iterate( self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, @@ -42,7 +42,7 @@ impl Array { #[cfg_attr(not(feature = "parallel"), async_recursion(?Send))] pub(crate) async fn iterate( self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, @@ -66,7 +66,7 @@ impl Array { impl Model { pub(crate) async fn iterate( self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, @@ -103,7 +103,7 @@ impl Model { impl Thing { pub(crate) async fn iterate( self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, @@ -125,7 +125,7 @@ impl Thing { impl Table { pub(crate) async fn iterate( self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, diff --git a/lib/src/dbs/iterator.rs b/lib/src/dbs/iterator.rs index ce7c9570..286cbd88 100644 --- a/lib/src/dbs/iterator.rs +++ b/lib/src/dbs/iterator.rs @@ -2,7 +2,6 @@ use crate::cnf::MAX_CONCURRENT_TASKS; use crate::ctx::Canceller; use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -54,7 +53,7 @@ impl Iterator { // Process the records and output pub async fn output( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, @@ -64,7 +63,6 @@ impl Iterator { // Enable context override let mut ctx = Context::new(ctx); self.run = ctx.add_cancel(); - let ctx = ctx.freeze(); // Process prepared values self.iterate(&ctx, opt, txn, stm).await?; // Return any document errors @@ -90,7 +88,7 @@ impl Iterator { #[inline] async fn output_split( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, @@ -134,7 +132,7 @@ impl Iterator { #[inline] async fn output_group( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, @@ -217,7 +215,7 @@ impl Iterator { #[inline] async fn output_order( &mut self, - _ctx: &Runtime, + _ctx: &Context<'_>, _opt: &Options, _txn: &Transaction, stm: &Statement<'_>, @@ -256,7 +254,7 @@ impl Iterator { #[inline] async fn output_start( &mut self, - _ctx: &Runtime, + _ctx: &Context<'_>, _opt: &Options, _txn: &Transaction, stm: &Statement<'_>, @@ -270,7 +268,7 @@ impl Iterator { #[inline] async fn output_limit( &mut self, - _ctx: &Runtime, + _ctx: &Context<'_>, _opt: &Options, _txn: &Transaction, stm: &Statement<'_>, @@ -284,7 +282,7 @@ impl Iterator { #[inline] async fn output_fetch( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, @@ -323,7 +321,7 @@ impl Iterator { #[cfg(not(feature = "parallel"))] async fn iterate( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, @@ -339,7 +337,7 @@ impl Iterator { #[cfg(feature = "parallel")] async fn iterate( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, @@ -412,7 +410,7 @@ impl Iterator { // Process a new record Thing and Value pub async fn process( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, diff --git a/lib/src/dbs/mod.rs b/lib/src/dbs/mod.rs index acc1196f..8f94231c 100644 --- a/lib/src/dbs/mod.rs +++ b/lib/src/dbs/mod.rs @@ -4,7 +4,6 @@ mod iterate; mod iterator; mod options; mod response; -mod runtime; mod session; mod statement; mod transaction; @@ -15,7 +14,6 @@ pub use self::executor::*; pub use self::iterator::*; pub use self::options::*; pub use self::response::*; -pub use self::runtime::*; pub use self::session::*; pub use self::statement::*; pub use self::transaction::*; diff --git a/lib/src/dbs/runtime.rs b/lib/src/dbs/runtime.rs deleted file mode 100644 index 5e2248d1..00000000 --- a/lib/src/dbs/runtime.rs +++ /dev/null @@ -1,25 +0,0 @@ -/* - * A Runtime is passed around when processing a set of query - * statements. The Runtime contains any saved parameters and - * variables set in the SQL, or any pre-defined paramaters which - * are determined by the authentication / session / environment. - * Embedded queries, and subqueries will create their own Runtime - * based off of the parent Runtime, and set their own variables - * accordingly. Predetermined variables include: - * - * $ENV = "surrealdb.com"; - * - * $auth.AL = "KV" / "NS" / "DB" / "SC"; - * $auth.NS = ""; - * $auth.DB = ""; - * - * $session.id = ""; - * $session.ip = ""; - * $session.origin = "app.surrealdb.com"; - * - */ - -use crate::ctx::Context; -use std::sync::Arc; - -pub type Runtime = Arc; diff --git a/lib/src/dbs/session.rs b/lib/src/dbs/session.rs index cc15941f..7c119387 100644 --- a/lib/src/dbs/session.rs +++ b/lib/src/dbs/session.rs @@ -34,7 +34,7 @@ impl Session { self.db.to_owned().map(Arc::new) } // Convert a session into a runtime - pub(crate) fn context(&self, mut ctx: Context) -> Context { + pub(crate) fn context<'a>(&self, mut ctx: Context<'a>) -> Context<'a> { // Add session value let key = String::from("session"); let val: Value = self.into(); diff --git a/lib/src/dbs/test.rs b/lib/src/dbs/test.rs index 7b4f89f8..ff602139 100644 --- a/lib/src/dbs/test.rs +++ b/lib/src/dbs/test.rs @@ -1,13 +1,12 @@ use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::kvs::Datastore; use futures::lock::Mutex; use std::sync::Arc; -pub async fn mock<'a>() -> (Runtime, Options, Transaction) { - let ctx = Context::default().freeze(); +pub async fn mock<'a>() -> (Context<'a>, Options, Transaction) { + let ctx = Context::default(); let opt = Options::default(); let kvs = Datastore::new("memory").await.unwrap(); let txn = kvs.transaction(true, false).await.unwrap(); diff --git a/lib/src/doc/admit.rs b/lib/src/doc/admit.rs index 9664094a..3208e61f 100644 --- a/lib/src/doc/admit.rs +++ b/lib/src/doc/admit.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -8,7 +8,7 @@ use crate::err::Error; impl<'a> Document<'a> { pub async fn admit( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, _opt: &Options, _txn: &Transaction, stm: &Statement<'_>, diff --git a/lib/src/doc/allow.rs b/lib/src/doc/allow.rs index b438a595..fa3320d0 100644 --- a/lib/src/doc/allow.rs +++ b/lib/src/doc/allow.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -9,7 +9,7 @@ use crate::sql::permission::Permission; impl<'a> Document<'a> { pub async fn allow( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, diff --git a/lib/src/doc/check.rs b/lib/src/doc/check.rs index 033ad654..619d885c 100644 --- a/lib/src/doc/check.rs +++ b/lib/src/doc/check.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -8,7 +8,7 @@ use crate::err::Error; impl<'a> Document<'a> { pub async fn check( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, diff --git a/lib/src/doc/compute.rs b/lib/src/doc/compute.rs index f8ce492b..d237b7b2 100644 --- a/lib/src/doc/compute.rs +++ b/lib/src/doc/compute.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -10,7 +10,7 @@ use channel::Sender; impl<'a> Document<'a> { pub(crate) async fn compute( - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, diff --git a/lib/src/doc/create.rs b/lib/src/doc/create.rs index 0a7c13a0..28c5dc69 100644 --- a/lib/src/doc/create.rs +++ b/lib/src/doc/create.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -9,7 +9,7 @@ use crate::sql::value::Value; impl<'a> Document<'a> { pub async fn create( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, diff --git a/lib/src/doc/delete.rs b/lib/src/doc/delete.rs index 4f2671d1..f41c3e4b 100644 --- a/lib/src/doc/delete.rs +++ b/lib/src/doc/delete.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -9,7 +9,7 @@ use crate::sql::value::Value; impl<'a> Document<'a> { pub async fn delete( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, diff --git a/lib/src/doc/empty.rs b/lib/src/doc/empty.rs index 716fc326..86732785 100644 --- a/lib/src/doc/empty.rs +++ b/lib/src/doc/empty.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -8,7 +8,7 @@ use crate::err::Error; impl<'a> Document<'a> { pub async fn empty( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, _opt: &Options, _txn: &Transaction, _stm: &Statement<'_>, diff --git a/lib/src/doc/erase.rs b/lib/src/doc/erase.rs index 404f3769..54419c0e 100644 --- a/lib/src/doc/erase.rs +++ b/lib/src/doc/erase.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -8,7 +8,7 @@ use crate::err::Error; impl<'a> Document<'a> { pub async fn erase( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, _stm: &Statement<'_>, diff --git a/lib/src/doc/event.rs b/lib/src/doc/event.rs index 0c4fd0cf..6c5286fc 100644 --- a/lib/src/doc/event.rs +++ b/lib/src/doc/event.rs @@ -1,6 +1,5 @@ use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -11,7 +10,7 @@ use std::ops::Deref; impl<'a> Document<'a> { pub async fn event( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, _stm: &Statement<'_>, @@ -37,10 +36,9 @@ impl<'a> Document<'a> { // Configure the context let mut ctx = Context::new(ctx); ctx.add_value("event".into(), met); - ctx.add_value("value".into(), self.current.deref().clone()); - ctx.add_value("after".into(), self.current.deref().clone()); - ctx.add_value("before".into(), self.initial.deref().clone()); - let ctx = ctx.freeze(); + ctx.add_value("value".into(), self.current.deref()); + ctx.add_value("after".into(), self.current.deref()); + ctx.add_value("before".into(), self.initial.deref()); // Ensure event queries run let opt = &opt.perms(false); // Process conditional clause diff --git a/lib/src/doc/exist.rs b/lib/src/doc/exist.rs index 57fe5222..de3140e8 100644 --- a/lib/src/doc/exist.rs +++ b/lib/src/doc/exist.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -8,7 +8,7 @@ use crate::err::Error; impl<'a> Document<'a> { pub async fn exist( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, _opt: &Options, _txn: &Transaction, _stm: &Statement<'_>, diff --git a/lib/src/doc/field.rs b/lib/src/doc/field.rs index fa833599..f8455377 100644 --- a/lib/src/doc/field.rs +++ b/lib/src/doc/field.rs @@ -1,6 +1,5 @@ use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -11,7 +10,7 @@ use crate::sql::value::Value; impl<'a> Document<'a> { pub async fn field( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, _stm: &Statement<'_>, @@ -28,10 +27,9 @@ impl<'a> Document<'a> { if let Some(expr) = &fd.value { // Configure the context let mut ctx = Context::new(ctx); - ctx.add_value("value".into(), val.clone()); - ctx.add_value("after".into(), val.clone()); - ctx.add_value("before".into(), old.clone()); - let ctx = ctx.freeze(); + ctx.add_value("value".into(), &val); + ctx.add_value("after".into(), &val); + ctx.add_value("before".into(), &old); // Process the VALUE clause val = expr.compute(&ctx, opt, txn, Some(&self.current)).await?; } @@ -43,10 +41,9 @@ impl<'a> Document<'a> { if let Some(expr) = &fd.assert { // Configure the context let mut ctx = Context::new(ctx); - ctx.add_value("value".into(), val.clone()); - ctx.add_value("after".into(), val.clone()); - ctx.add_value("before".into(), old.clone()); - let ctx = ctx.freeze(); + ctx.add_value("value".into(), &val); + ctx.add_value("after".into(), &val); + ctx.add_value("before".into(), &old); // Process the ASSERT clause if !expr.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() { return Err(Error::FieldValue { @@ -73,10 +70,9 @@ impl<'a> Document<'a> { Permission::Specific(e) => { // Configure the context let mut ctx = Context::new(ctx); - ctx.add_value("value".into(), val.clone()); - ctx.add_value("after".into(), val.clone()); - ctx.add_value("before".into(), old.clone()); - let ctx = ctx.freeze(); + ctx.add_value("value".into(), &val); + ctx.add_value("after".into(), &val); + ctx.add_value("before".into(), &old); // Process the PERMISSION clause if !e.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() { val = old diff --git a/lib/src/doc/index.rs b/lib/src/doc/index.rs index 0c9268ab..c24ef993 100644 --- a/lib/src/doc/index.rs +++ b/lib/src/doc/index.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -9,7 +9,7 @@ use crate::sql::array::Array; impl<'a> Document<'a> { pub async fn index( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, _stm: &Statement<'_>, diff --git a/lib/src/doc/insert.rs b/lib/src/doc/insert.rs index a9b10b68..d02677a8 100644 --- a/lib/src/doc/insert.rs +++ b/lib/src/doc/insert.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -9,7 +9,7 @@ use crate::sql::value::Value; impl<'a> Document<'a> { pub async fn insert( &mut self, - _ctx: &Runtime, + _ctx: &Context<'_>, _opt: &Options, _txn: &Transaction, _stm: &Statement<'_>, diff --git a/lib/src/doc/lives.rs b/lib/src/doc/lives.rs index b947c19a..8020c399 100644 --- a/lib/src/doc/lives.rs +++ b/lib/src/doc/lives.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -8,7 +8,7 @@ use crate::err::Error; impl<'a> Document<'a> { pub async fn lives( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, _opt: &Options, _txn: &Transaction, _stm: &Statement<'_>, diff --git a/lib/src/doc/merge.rs b/lib/src/doc/merge.rs index c2062294..1450d09e 100644 --- a/lib/src/doc/merge.rs +++ b/lib/src/doc/merge.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -11,7 +11,7 @@ use crate::sql::value::Value; impl<'a> Document<'a> { pub async fn merge( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, diff --git a/lib/src/doc/pluck.rs b/lib/src/doc/pluck.rs index 4a43a55e..76117237 100644 --- a/lib/src/doc/pluck.rs +++ b/lib/src/doc/pluck.rs @@ -1,6 +1,5 @@ use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -14,7 +13,7 @@ use crate::sql::value::Value; impl<'a> Document<'a> { pub async fn pluck( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, @@ -127,8 +126,7 @@ impl<'a> Document<'a> { let val = self.current.pick(k); // Configure the context let mut ctx = Context::new(ctx); - ctx.add_value("value".into(), val); - let ctx = ctx.freeze(); + ctx.add_value("value".into(), &val); // Process the PERMISSION clause if !e.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() { out.del(&ctx, opt, txn, k).await? diff --git a/lib/src/doc/purge.rs b/lib/src/doc/purge.rs index b27a675c..9a716615 100644 --- a/lib/src/doc/purge.rs +++ b/lib/src/doc/purge.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -8,7 +8,7 @@ use crate::err::Error; impl<'a> Document<'a> { pub async fn purge( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _stm: &Statement<'_>, diff --git a/lib/src/doc/relate.rs b/lib/src/doc/relate.rs index 57488cd1..ebcacbcb 100644 --- a/lib/src/doc/relate.rs +++ b/lib/src/doc/relate.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -9,7 +9,7 @@ use crate::sql::value::Value; impl<'a> Document<'a> { pub async fn relate( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, diff --git a/lib/src/doc/select.rs b/lib/src/doc/select.rs index afde8a2f..f07c9ee3 100644 --- a/lib/src/doc/select.rs +++ b/lib/src/doc/select.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -9,7 +9,7 @@ use crate::sql::value::Value; impl<'a> Document<'a> { pub async fn select( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, diff --git a/lib/src/doc/store.rs b/lib/src/doc/store.rs index 5199be89..6a63fe8f 100644 --- a/lib/src/doc/store.rs +++ b/lib/src/doc/store.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -8,7 +8,7 @@ use crate::err::Error; impl<'a> Document<'a> { pub async fn store( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _stm: &Statement<'_>, diff --git a/lib/src/doc/table.rs b/lib/src/doc/table.rs index 59588d7d..a4bd941a 100644 --- a/lib/src/doc/table.rs +++ b/lib/src/doc/table.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -8,7 +8,7 @@ use crate::err::Error; impl<'a> Document<'a> { pub async fn table( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, _opt: &Options, _txn: &Transaction, _stm: &Statement<'_>, diff --git a/lib/src/doc/update.rs b/lib/src/doc/update.rs index 6c710953..480471a7 100644 --- a/lib/src/doc/update.rs +++ b/lib/src/doc/update.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; @@ -9,7 +9,7 @@ use crate::sql::value::Value; impl<'a> Document<'a> { pub async fn update( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, stm: &Statement<'_>, diff --git a/lib/src/fnc/args.rs b/lib/src/fnc/args.rs index 46cd2bd0..ba32a4e5 100644 --- a/lib/src/fnc/args.rs +++ b/lib/src/fnc/args.rs @@ -1,4 +1,4 @@ -use crate::dbs::Runtime; +use crate::ctx::Context; use crate::err::Error; use crate::sql::value::Value; @@ -15,11 +15,11 @@ pub enum Args { } pub fn check( - ctx: &Runtime, + ctx: &Context, name: &str, args: Vec, size: Args, - func: fn(&Runtime, Vec) -> Result, + func: fn(&Context, Vec) -> Result, ) -> Result { match size { Args::None => match args.len() { diff --git a/lib/src/fnc/array.rs b/lib/src/fnc/array.rs index 375a276b..7d9e479f 100644 --- a/lib/src/fnc/array.rs +++ b/lib/src/fnc/array.rs @@ -1,4 +1,4 @@ -use crate::dbs::Runtime; +use crate::ctx::Context; use crate::err::Error; use crate::sql::array::Combine; use crate::sql::array::Concat; @@ -8,7 +8,7 @@ use crate::sql::array::Union; use crate::sql::array::Uniq; use crate::sql::value::Value; -pub fn concat(_: &Runtime, mut args: Vec) -> Result { +pub fn concat(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => match args.remove(0) { Value::Array(w) => Ok(v.concat(w).into()), @@ -18,7 +18,7 @@ pub fn concat(_: &Runtime, mut args: Vec) -> Result { } } -pub fn combine(_: &Runtime, mut args: Vec) -> Result { +pub fn combine(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => match args.remove(0) { Value::Array(w) => Ok(v.combine(w).into()), @@ -28,7 +28,7 @@ pub fn combine(_: &Runtime, mut args: Vec) -> Result { } } -pub fn difference(_: &Runtime, mut args: Vec) -> Result { +pub fn difference(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => match args.remove(0) { Value::Array(w) => Ok(v.difference(w).into()), @@ -38,14 +38,14 @@ pub fn difference(_: &Runtime, mut args: Vec) -> Result { } } -pub fn distinct(_: &Runtime, mut args: Vec) -> Result { +pub fn distinct(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => Ok(v.uniq().into()), _ => Ok(Value::None), } } -pub fn intersect(_: &Runtime, mut args: Vec) -> Result { +pub fn intersect(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => match args.remove(0) { Value::Array(w) => Ok(v.intersect(w).into()), @@ -55,14 +55,14 @@ pub fn intersect(_: &Runtime, mut args: Vec) -> Result { } } -pub fn len(_: &Runtime, mut args: Vec) -> Result { +pub fn len(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => Ok(v.len().into()), _ => Ok(Value::None), } } -pub fn union(_: &Runtime, mut args: Vec) -> Result { +pub fn union(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => match args.remove(0) { Value::Array(w) => Ok(v.union(w).into()), diff --git a/lib/src/fnc/cast.rs b/lib/src/fnc/cast.rs index d1d92aa9..4a3b8b11 100644 --- a/lib/src/fnc/cast.rs +++ b/lib/src/fnc/cast.rs @@ -1,9 +1,9 @@ -use crate::dbs::Runtime; +use crate::ctx::Context; use crate::err::Error; use crate::sql::number::Number; use crate::sql::value::Value; -pub fn run(ctx: &Runtime, name: &str, val: Value) -> Result { +pub fn run(ctx: &Context, name: &str, val: Value) -> Result { match name { "bool" => bool(ctx, val), "int" => int(ctx, val), @@ -17,56 +17,56 @@ pub fn run(ctx: &Runtime, name: &str, val: Value) -> Result { } } -pub fn bool(_: &Runtime, val: Value) -> Result { +pub fn bool(_: &Context, val: Value) -> Result { match val.is_truthy() { true => Ok(Value::True), false => Ok(Value::False), } } -pub fn int(_: &Runtime, val: Value) -> Result { +pub fn int(_: &Context, val: Value) -> Result { match val { Value::Number(Number::Int(_)) => Ok(val), _ => Ok(Value::Number(Number::Int(val.as_int()))), } } -pub fn float(_: &Runtime, val: Value) -> Result { +pub fn float(_: &Context, val: Value) -> Result { match val { Value::Number(Number::Float(_)) => Ok(val), _ => Ok(Value::Number(Number::Float(val.as_float()))), } } -pub fn number(_: &Runtime, val: Value) -> Result { +pub fn number(_: &Context, val: Value) -> Result { match val { Value::Number(Number::Decimal(_)) => Ok(val), _ => Ok(Value::Number(Number::Decimal(val.as_decimal()))), } } -pub fn decimal(_: &Runtime, val: Value) -> Result { +pub fn decimal(_: &Context, val: Value) -> Result { match val { Value::Number(Number::Decimal(_)) => Ok(val), _ => Ok(Value::Number(Number::Decimal(val.as_decimal()))), } } -pub fn string(_: &Runtime, val: Value) -> Result { +pub fn string(_: &Context, val: Value) -> Result { match val { Value::Strand(_) => Ok(val), _ => Ok(Value::Strand(val.as_strand())), } } -pub fn datetime(_: &Runtime, val: Value) -> Result { +pub fn datetime(_: &Context, val: Value) -> Result { match val { Value::Datetime(_) => Ok(val), _ => Ok(Value::Datetime(val.as_datetime())), } } -pub fn duration(_: &Runtime, val: Value) -> Result { +pub fn duration(_: &Context, val: Value) -> Result { match val { Value::Duration(_) => Ok(val), _ => Ok(Value::Duration(val.as_duration())), diff --git a/lib/src/fnc/count.rs b/lib/src/fnc/count.rs index 6b4d9f98..e3a597af 100644 --- a/lib/src/fnc/count.rs +++ b/lib/src/fnc/count.rs @@ -1,8 +1,8 @@ -use crate::dbs::Runtime; +use crate::ctx::Context; use crate::err::Error; use crate::sql::value::Value; -pub fn count(_: &Runtime, mut args: Vec) -> Result { +pub fn count(_: &Context, mut args: Vec) -> Result { match args.len() { 1 => match args.remove(0) { Value::Array(v) => Ok(v.iter().filter(|v| v.is_truthy()).count().into()), diff --git a/lib/src/fnc/crypto.rs b/lib/src/fnc/crypto.rs index ed40dd3f..b6a1a14e 100644 --- a/lib/src/fnc/crypto.rs +++ b/lib/src/fnc/crypto.rs @@ -1,4 +1,4 @@ -use crate::dbs::Runtime; +use crate::ctx::Context; use crate::err::Error; use crate::sql::value::Value; use md5::Digest; @@ -7,7 +7,7 @@ use sha1::Sha1; use sha2::Sha256; use sha2::Sha512; -pub fn md5(_: &Runtime, mut args: Vec) -> Result { +pub fn md5(_: &Context, mut args: Vec) -> Result { let mut hasher = Md5::new(); hasher.update(args.remove(0).as_string().as_str()); let val = hasher.finalize(); @@ -15,7 +15,7 @@ pub fn md5(_: &Runtime, mut args: Vec) -> Result { Ok(val.into()) } -pub fn sha1(_: &Runtime, mut args: Vec) -> Result { +pub fn sha1(_: &Context, mut args: Vec) -> Result { let mut hasher = Sha1::new(); hasher.update(args.remove(0).as_string().as_str()); let val = hasher.finalize(); @@ -23,7 +23,7 @@ pub fn sha1(_: &Runtime, mut args: Vec) -> Result { Ok(val.into()) } -pub fn sha256(_: &Runtime, mut args: Vec) -> Result { +pub fn sha256(_: &Context, mut args: Vec) -> Result { let mut hasher = Sha256::new(); hasher.update(args.remove(0).as_string().as_str()); let val = hasher.finalize(); @@ -31,7 +31,7 @@ pub fn sha256(_: &Runtime, mut args: Vec) -> Result { Ok(val.into()) } -pub fn sha512(_: &Runtime, mut args: Vec) -> Result { +pub fn sha512(_: &Context, mut args: Vec) -> Result { let mut hasher = Sha512::new(); hasher.update(args.remove(0).as_string().as_str()); let val = hasher.finalize(); @@ -41,7 +41,7 @@ pub fn sha512(_: &Runtime, mut args: Vec) -> Result { pub mod argon2 { - use crate::dbs::Runtime; + use crate::ctx::Context; use crate::err::Error; use crate::sql::value::Value; use argon2::{ @@ -50,7 +50,7 @@ pub mod argon2 { }; use rand::rngs::OsRng; - pub fn cmp(_: &Runtime, mut args: Vec) -> Result { + pub fn cmp(_: &Context, mut args: Vec) -> Result { let algo = Argon2::default(); let hash = args.remove(0).as_string(); let pass = args.remove(0).as_string(); @@ -58,7 +58,7 @@ pub mod argon2 { Ok(algo.verify_password(pass.as_ref(), &test).is_ok().into()) } - pub fn gen(_: &Runtime, mut args: Vec) -> Result { + pub fn gen(_: &Context, mut args: Vec) -> Result { let algo = Argon2::default(); let pass = args.remove(0).as_string(); let salt = SaltString::generate(&mut OsRng); @@ -69,7 +69,7 @@ pub mod argon2 { pub mod pbkdf2 { - use crate::dbs::Runtime; + use crate::ctx::Context; use crate::err::Error; use crate::sql::value::Value; use pbkdf2::{ @@ -78,14 +78,14 @@ pub mod pbkdf2 { }; use rand::rngs::OsRng; - pub fn cmp(_: &Runtime, mut args: Vec) -> Result { + pub fn cmp(_: &Context, mut args: Vec) -> Result { let hash = args.remove(0).as_string(); let pass = args.remove(0).as_string(); let test = PasswordHash::new(&hash).unwrap(); Ok(Pbkdf2.verify_password(pass.as_ref(), &test).is_ok().into()) } - pub fn gen(_: &Runtime, mut args: Vec) -> Result { + pub fn gen(_: &Context, mut args: Vec) -> Result { let pass = args.remove(0).as_string(); let salt = SaltString::generate(&mut OsRng); let hash = Pbkdf2.hash_password(pass.as_ref(), salt.as_ref()).unwrap().to_string(); @@ -95,7 +95,7 @@ pub mod pbkdf2 { pub mod scrypt { - use crate::dbs::Runtime; + use crate::ctx::Context; use crate::err::Error; use crate::sql::value::Value; use rand::rngs::OsRng; @@ -104,14 +104,14 @@ pub mod scrypt { Scrypt, }; - pub fn cmp(_: &Runtime, mut args: Vec) -> Result { + pub fn cmp(_: &Context, mut args: Vec) -> Result { let hash = args.remove(0).as_string(); let pass = args.remove(0).as_string(); let test = PasswordHash::new(&hash).unwrap(); Ok(Scrypt.verify_password(pass.as_ref(), &test).is_ok().into()) } - pub fn gen(_: &Runtime, mut args: Vec) -> Result { + pub fn gen(_: &Context, mut args: Vec) -> Result { let pass = args.remove(0).as_string(); let salt = SaltString::generate(&mut OsRng); let hash = Scrypt.hash_password(pass.as_ref(), salt.as_ref()).unwrap().to_string(); diff --git a/lib/src/fnc/future.rs b/lib/src/fnc/future.rs index 7f44fd38..74116707 100644 --- a/lib/src/fnc/future.rs +++ b/lib/src/fnc/future.rs @@ -1,7 +1,7 @@ -use crate::dbs::Runtime; +use crate::ctx::Context; use crate::err::Error; use crate::sql::value::Value; -pub fn run(_: &Runtime, expr: Value) -> Result { +pub fn run(_: &Context, expr: Value) -> Result { Ok(expr) } diff --git a/lib/src/fnc/geo.rs b/lib/src/fnc/geo.rs index c929f67e..ea56ecef 100644 --- a/lib/src/fnc/geo.rs +++ b/lib/src/fnc/geo.rs @@ -1,4 +1,4 @@ -use crate::dbs::Runtime; +use crate::ctx::Context; use crate::err::Error; use crate::sql::geometry::Geometry; use crate::sql::value::Value; @@ -7,7 +7,7 @@ use geo::algorithm::bearing::Bearing; use geo::algorithm::centroid::Centroid; use geo::algorithm::haversine_distance::HaversineDistance; -pub fn area(_: &Runtime, mut args: Vec) -> Result { +pub fn area(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Geometry(v) => match v { Geometry::Point(v) => Ok(v.signed_area().into()), @@ -24,7 +24,7 @@ pub fn area(_: &Runtime, mut args: Vec) -> Result { } } -pub fn bearing(_: &Runtime, mut args: Vec) -> Result { +pub fn bearing(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Geometry(Geometry::Point(v)) => match args.remove(0) { Value::Geometry(Geometry::Point(w)) => Ok(v.bearing(w).into()), @@ -34,7 +34,7 @@ pub fn bearing(_: &Runtime, mut args: Vec) -> Result { } } -pub fn centroid(_: &Runtime, mut args: Vec) -> Result { +pub fn centroid(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Geometry(v) => match v { Geometry::Point(v) => Ok(v.centroid().into()), @@ -69,7 +69,7 @@ pub fn centroid(_: &Runtime, mut args: Vec) -> Result { } } -pub fn distance(_: &Runtime, mut args: Vec) -> Result { +pub fn distance(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Geometry(Geometry::Point(v)) => match args.remove(0) { Value::Geometry(Geometry::Point(w)) => Ok(v.haversine_distance(&w).into()), @@ -81,13 +81,13 @@ pub fn distance(_: &Runtime, mut args: Vec) -> Result { pub mod hash { - use crate::dbs::Runtime; + use crate::ctx::Context; use crate::err::Error; use crate::fnc::util::geo; use crate::sql::geometry::Geometry; use crate::sql::value::Value; - pub fn encode(_: &Runtime, mut args: Vec) -> Result { + pub fn encode(_: &Context, mut args: Vec) -> Result { match args.len() { 2 => match args.remove(0) { Value::Geometry(Geometry::Point(v)) => match args.remove(0).as_int() { @@ -107,7 +107,7 @@ pub mod hash { } } - pub fn decode(_: &Runtime, mut args: Vec) -> Result { + pub fn decode(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Strand(v) => Ok(geo::decode(v).into()), _ => Ok(Value::None), diff --git a/lib/src/fnc/http.rs b/lib/src/fnc/http.rs index bb2454ff..9e540657 100644 --- a/lib/src/fnc/http.rs +++ b/lib/src/fnc/http.rs @@ -1,8 +1,8 @@ -use crate::dbs::Runtime; +use crate::ctx::Context; use crate::err::Error; use crate::sql::value::Value; -pub async fn head(_ctx: &Runtime, args: Vec) -> Result { +pub async fn head(_ctx: &Context<'_>, args: Vec) -> Result { match args.len() { 1 | 2 => todo!(), _ => Err(Error::InvalidArguments { @@ -12,7 +12,7 @@ pub async fn head(_ctx: &Runtime, args: Vec) -> Result { } } -pub async fn get(_ctx: &Runtime, args: Vec) -> Result { +pub async fn get(_ctx: &Context<'_>, args: Vec) -> Result { match args.len() { 1 | 2 => todo!(), _ => Err(Error::InvalidArguments { @@ -22,7 +22,7 @@ pub async fn get(_ctx: &Runtime, args: Vec) -> Result { } } -pub async fn put(_ctx: &Runtime, args: Vec) -> Result { +pub async fn put(_ctx: &Context<'_>, args: Vec) -> Result { match args.len() { 1 | 2 | 3 => todo!(), _ => Err(Error::InvalidArguments { @@ -32,7 +32,7 @@ pub async fn put(_ctx: &Runtime, args: Vec) -> Result { } } -pub async fn post(_ctx: &Runtime, args: Vec) -> Result { +pub async fn post(_ctx: &Context<'_>, args: Vec) -> Result { match args.len() { 1 | 2 | 3 => todo!(), _ => Err(Error::InvalidArguments { @@ -42,7 +42,7 @@ pub async fn post(_ctx: &Runtime, args: Vec) -> Result { } } -pub async fn patch(_ctx: &Runtime, args: Vec) -> Result { +pub async fn patch(_ctx: &Context<'_>, args: Vec) -> Result { match args.len() { 1 | 2 | 3 => todo!(), _ => Err(Error::InvalidArguments { @@ -52,7 +52,7 @@ pub async fn patch(_ctx: &Runtime, args: Vec) -> Result { } } -pub async fn delete(_ctx: &Runtime, args: Vec) -> Result { +pub async fn delete(_ctx: &Context<'_>, args: Vec) -> Result { match args.len() { 1 | 2 => todo!(), _ => Err(Error::InvalidArguments { diff --git a/lib/src/fnc/is.rs b/lib/src/fnc/is.rs index 9ff13ecd..8fc927b8 100644 --- a/lib/src/fnc/is.rs +++ b/lib/src/fnc/is.rs @@ -1,4 +1,4 @@ -use crate::dbs::Runtime; +use crate::ctx::Context; use crate::err::Error; use crate::sql::value::Value; use once_cell::sync::Lazy; @@ -13,23 +13,23 @@ use std::char; #[rustfmt::skip] static LATITUDE_RE: Lazy = Lazy::new(|| Regex::new("^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$").unwrap()); #[rustfmt::skip] static LONGITUDE_RE: Lazy = Lazy::new(|| Regex::new("^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$").unwrap()); -pub fn alphanum(_: &Runtime, mut args: Vec) -> Result { +pub fn alphanum(_: &Context, mut args: Vec) -> Result { Ok(args.remove(0).as_string().chars().all(char::is_alphanumeric).into()) } -pub fn alpha(_: &Runtime, mut args: Vec) -> Result { +pub fn alpha(_: &Context, mut args: Vec) -> Result { Ok(args.remove(0).as_string().chars().all(char::is_alphabetic).into()) } -pub fn ascii(_: &Runtime, mut args: Vec) -> Result { +pub fn ascii(_: &Context, mut args: Vec) -> Result { Ok(args.remove(0).as_string().chars().all(|x| char::is_ascii(&x)).into()) } -pub fn domain(_: &Runtime, mut args: Vec) -> Result { +pub fn domain(_: &Context, mut args: Vec) -> Result { Ok(DOMAIN_RE.is_match(args.remove(0).as_string().as_str()).into()) } -pub fn email(_: &Runtime, mut args: Vec) -> Result { +pub fn email(_: &Context, mut args: Vec) -> Result { // Convert to a String let val = args.remove(0).as_string(); // Convert to a &str @@ -56,26 +56,26 @@ pub fn email(_: &Runtime, mut args: Vec) -> Result { Ok(Value::True) } -pub fn hexadecimal(_: &Runtime, mut args: Vec) -> Result { +pub fn hexadecimal(_: &Context, mut args: Vec) -> Result { Ok(args.remove(0).as_string().chars().all(|x| char::is_ascii_hexdigit(&x)).into()) } -pub fn latitude(_: &Runtime, mut args: Vec) -> Result { +pub fn latitude(_: &Context, mut args: Vec) -> Result { Ok(LATITUDE_RE.is_match(args.remove(0).as_string().as_str()).into()) } -pub fn longitude(_: &Runtime, mut args: Vec) -> Result { +pub fn longitude(_: &Context, mut args: Vec) -> Result { Ok(LONGITUDE_RE.is_match(args.remove(0).as_string().as_str()).into()) } -pub fn numeric(_: &Runtime, mut args: Vec) -> Result { +pub fn numeric(_: &Context, mut args: Vec) -> Result { Ok(args.remove(0).as_string().chars().all(char::is_numeric).into()) } -pub fn semver(_: &Runtime, mut args: Vec) -> Result { +pub fn semver(_: &Context, mut args: Vec) -> Result { Ok(SEMVER_RE.is_match(args.remove(0).as_string().as_str()).into()) } -pub fn uuid(_: &Runtime, mut args: Vec) -> Result { +pub fn uuid(_: &Context, mut args: Vec) -> Result { Ok(UUID_RE.is_match(args.remove(0).as_string().as_str()).into()) } diff --git a/lib/src/fnc/math.rs b/lib/src/fnc/math.rs index d6903b6c..99ffd02e 100644 --- a/lib/src/fnc/math.rs +++ b/lib/src/fnc/math.rs @@ -1,4 +1,4 @@ -use crate::dbs::Runtime; +use crate::ctx::Context; use crate::err::Error; use crate::fnc::util::math::bottom::Bottom; use crate::fnc::util::math::deviation::Deviation; @@ -16,11 +16,11 @@ use crate::fnc::util::math::variance::Variance; use crate::sql::number::Number; use crate::sql::value::Value; -pub fn abs(_: &Runtime, mut args: Vec) -> Result { +pub fn abs(_: &Context, mut args: Vec) -> Result { Ok(args.remove(0).as_number().abs().into()) } -pub fn bottom(_: &Runtime, mut args: Vec) -> Result { +pub fn bottom(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => { let c = args.remove(0).as_int(); @@ -30,11 +30,11 @@ pub fn bottom(_: &Runtime, mut args: Vec) -> Result { } } -pub fn ceil(_: &Runtime, mut args: Vec) -> Result { +pub fn ceil(_: &Context, mut args: Vec) -> Result { Ok(args.remove(0).as_number().ceil().into()) } -pub fn fixed(_: &Runtime, mut args: Vec) -> Result { +pub fn fixed(_: &Context, mut args: Vec) -> Result { let v = args.remove(0); match args.remove(0).as_int() { p if p > 0 => Ok(v.as_number().fixed(p as usize).into()), @@ -45,18 +45,18 @@ pub fn fixed(_: &Runtime, mut args: Vec) -> Result { } } -pub fn floor(_: &Runtime, mut args: Vec) -> Result { +pub fn floor(_: &Context, mut args: Vec) -> Result { Ok(args.remove(0).as_number().floor().into()) } -pub fn interquartile(_: &Runtime, mut args: Vec) -> Result { +pub fn interquartile(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => Ok(v.as_numbers().interquartile().into()), _ => Ok(Value::None), } } -pub fn max(_: &Runtime, mut args: Vec) -> Result { +pub fn max(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => match v.as_numbers().into_iter().max() { Some(v) => Ok(v.into()), @@ -66,28 +66,28 @@ pub fn max(_: &Runtime, mut args: Vec) -> Result { } } -pub fn mean(_: &Runtime, mut args: Vec) -> Result { +pub fn mean(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => Ok(v.as_numbers().mean().into()), _ => Ok(Value::None), } } -pub fn median(_: &Runtime, mut args: Vec) -> Result { +pub fn median(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => Ok(v.as_numbers().median().into()), _ => Ok(Value::None), } } -pub fn midhinge(_: &Runtime, mut args: Vec) -> Result { +pub fn midhinge(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => Ok(v.as_numbers().midhinge().into()), _ => Ok(Value::None), } } -pub fn min(_: &Runtime, mut args: Vec) -> Result { +pub fn min(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => match v.as_numbers().into_iter().min() { Some(v) => Ok(v.into()), @@ -97,64 +97,64 @@ pub fn min(_: &Runtime, mut args: Vec) -> Result { } } -pub fn mode(_: &Runtime, mut args: Vec) -> Result { +pub fn mode(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => Ok(v.as_numbers().mode().into()), _ => Ok(Value::None), } } -pub fn nearestrank(_: &Runtime, mut args: Vec) -> Result { +pub fn nearestrank(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => Ok(v.as_numbers().nearestrank(args.remove(0).as_number()).into()), _ => Ok(Value::None), } } -pub fn percentile(_: &Runtime, mut args: Vec) -> Result { +pub fn percentile(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => Ok(v.as_numbers().percentile(args.remove(0).as_number()).into()), _ => Ok(Value::None), } } -pub fn product(_: &Runtime, mut args: Vec) -> Result { +pub fn product(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => Ok(v.as_numbers().into_iter().product::().into()), _ => Ok(Value::None), } } -pub fn round(_: &Runtime, mut args: Vec) -> Result { +pub fn round(_: &Context, mut args: Vec) -> Result { Ok(args.remove(0).as_number().round().into()) } -pub fn spread(_: &Runtime, mut args: Vec) -> Result { +pub fn spread(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => Ok(v.as_numbers().spread().into()), _ => Ok(Value::None), } } -pub fn sqrt(_: &Runtime, mut args: Vec) -> Result { +pub fn sqrt(_: &Context, mut args: Vec) -> Result { Ok(args.remove(0).as_number().sqrt().into()) } -pub fn stddev(_: &Runtime, mut args: Vec) -> Result { +pub fn stddev(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => Ok(v.as_numbers().deviation().into()), _ => Ok(Value::None), } } -pub fn sum(_: &Runtime, mut args: Vec) -> Result { +pub fn sum(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => Ok(v.as_numbers().into_iter().sum::().into()), _ => Ok(Value::None), } } -pub fn top(_: &Runtime, mut args: Vec) -> Result { +pub fn top(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => { let c = args.remove(0).as_int(); @@ -164,14 +164,14 @@ pub fn top(_: &Runtime, mut args: Vec) -> Result { } } -pub fn trimean(_: &Runtime, mut args: Vec) -> Result { +pub fn trimean(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => Ok(v.as_numbers().trimean().into()), _ => Ok(Value::None), } } -pub fn variance(_: &Runtime, mut args: Vec) -> Result { +pub fn variance(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Array(v) => Ok(v.as_numbers().variance().into()), _ => Ok(Value::None), diff --git a/lib/src/fnc/mod.rs b/lib/src/fnc/mod.rs index f087cf75..f55e7367 100644 --- a/lib/src/fnc/mod.rs +++ b/lib/src/fnc/mod.rs @@ -1,4 +1,4 @@ -use crate::dbs::Runtime; +use crate::ctx::Context; use crate::err::Error; use crate::fnc::args::Args; use crate::sql::value::Value; @@ -22,7 +22,7 @@ pub mod time; pub mod r#type; pub mod util; -pub async fn run(ctx: &Runtime, name: &str, args: Vec) -> Result { +pub async fn run(ctx: &Context<'_>, name: &str, args: Vec) -> Result { match name { // "array::combine" => args::check(ctx, name, args, Args::Two, array::combine), diff --git a/lib/src/fnc/parse.rs b/lib/src/fnc/parse.rs index fa1bb06e..1e7f13aa 100644 --- a/lib/src/fnc/parse.rs +++ b/lib/src/fnc/parse.rs @@ -1,6 +1,6 @@ pub mod email { - use crate::dbs::Runtime; + use crate::ctx::Context; use crate::err::Error; use crate::sql::value::Value; use once_cell::sync::Lazy; @@ -9,7 +9,7 @@ pub mod email { #[rustfmt::skip] static USER_RE: Lazy = Lazy::new(|| Regex::new(r"^(?i)[a-z0-9.!#$%&'*+/=?^_`{|}~-]+\z").unwrap()); #[rustfmt::skip] static HOST_RE: Lazy = Lazy::new(|| Regex::new(r"(?i)^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$",).unwrap()); - pub fn domain(_: &Runtime, mut args: Vec) -> Result { + pub fn domain(_: &Context, mut args: Vec) -> Result { // Convert to a String let val = args.remove(0).as_string(); // Check if value is empty @@ -34,7 +34,7 @@ pub mod email { Ok(parts[0].into()) } - pub fn user(_: &Runtime, mut args: Vec) -> Result { + pub fn user(_: &Context, mut args: Vec) -> Result { // Convert to a String let val = args.remove(0).as_string(); // Check if value is empty @@ -62,12 +62,12 @@ pub mod email { pub mod url { - use crate::dbs::Runtime; + use crate::ctx::Context; use crate::err::Error; use crate::sql::value::Value; use url::Url; - pub fn domain(_: &Runtime, mut args: Vec) -> Result { + pub fn domain(_: &Context, mut args: Vec) -> Result { // Convert to a String let val = args.remove(0).as_string(); // Parse the URL @@ -80,7 +80,7 @@ pub mod url { } } - pub fn fragment(_: &Runtime, mut args: Vec) -> Result { + pub fn fragment(_: &Context, mut args: Vec) -> Result { // Convert to a String let val = args.remove(0).as_string(); // Parse the URL @@ -93,7 +93,7 @@ pub mod url { } } - pub fn host(_: &Runtime, mut args: Vec) -> Result { + pub fn host(_: &Context, mut args: Vec) -> Result { // Convert to a String let val = args.remove(0).as_string(); // Parse the URL @@ -106,7 +106,7 @@ pub mod url { } } - pub fn path(_: &Runtime, mut args: Vec) -> Result { + pub fn path(_: &Context, mut args: Vec) -> Result { // Convert to a String let val = args.remove(0).as_string(); // Parse the URL @@ -116,7 +116,7 @@ pub mod url { } } - pub fn port(_: &Runtime, mut args: Vec) -> Result { + pub fn port(_: &Context, mut args: Vec) -> Result { // Convert to a String let val = args.remove(0).as_string(); // Parse the URL @@ -129,7 +129,7 @@ pub mod url { } } - pub fn query(_: &Runtime, mut args: Vec) -> Result { + pub fn query(_: &Context, mut args: Vec) -> Result { // Convert to a String let val = args.remove(0).as_string(); // Parse the URL diff --git a/lib/src/fnc/rand.rs b/lib/src/fnc/rand.rs index 01ec8e69..930f2181 100644 --- a/lib/src/fnc/rand.rs +++ b/lib/src/fnc/rand.rs @@ -1,5 +1,5 @@ use crate::cnf::ID_CHARS; -use crate::dbs::Runtime; +use crate::ctx::Context; use crate::err::Error; use crate::sql::datetime::Datetime; use crate::sql::value::Value; @@ -8,15 +8,15 @@ use rand::distributions::Alphanumeric; use rand::Rng; use uuid::Uuid; -pub fn rand(_: &Runtime, _: Vec) -> Result { +pub fn rand(_: &Context, _: Vec) -> Result { Ok(rand::random::().into()) } -pub fn bool(_: &Runtime, _: Vec) -> Result { +pub fn bool(_: &Context, _: Vec) -> Result { Ok(rand::random::().into()) } -pub fn r#enum(_: &Runtime, mut args: Vec) -> Result { +pub fn r#enum(_: &Context, mut args: Vec) -> Result { match args.len() { 0 => Ok(Value::None), 1 => match args.remove(0) { @@ -36,7 +36,7 @@ pub fn r#enum(_: &Runtime, mut args: Vec) -> Result { } } -pub fn float(_: &Runtime, mut args: Vec) -> Result { +pub fn float(_: &Context, mut args: Vec) -> Result { match args.len() { 2 => { let min = args.remove(0).as_float(); @@ -50,7 +50,7 @@ pub fn float(_: &Runtime, mut args: Vec) -> Result { } } -pub fn guid(_: &Runtime, mut args: Vec) -> Result { +pub fn guid(_: &Context, mut args: Vec) -> Result { match args.len() { 1 => { let len = args.remove(0).as_int() as usize; @@ -61,7 +61,7 @@ pub fn guid(_: &Runtime, mut args: Vec) -> Result { } } -pub fn int(_: &Runtime, mut args: Vec) -> Result { +pub fn int(_: &Context, mut args: Vec) -> Result { match args.len() { 2 => { let min = args.remove(0).as_int(); @@ -75,7 +75,7 @@ pub fn int(_: &Runtime, mut args: Vec) -> Result { } } -pub fn string(_: &Runtime, mut args: Vec) -> Result { +pub fn string(_: &Context, mut args: Vec) -> Result { match args.len() { 2 => match args.remove(0).as_int() { min if min >= 0 => match args.remove(0).as_int() { @@ -123,7 +123,7 @@ pub fn string(_: &Runtime, mut args: Vec) -> Result { } } -pub fn time(_: &Runtime, mut args: Vec) -> Result { +pub fn time(_: &Context, mut args: Vec) -> Result { match args.len() { 2 => { let min = args.remove(0).as_int(); @@ -146,6 +146,6 @@ pub fn time(_: &Runtime, mut args: Vec) -> Result { } } -pub fn uuid(_: &Runtime, _: Vec) -> Result { +pub fn uuid(_: &Context, _: Vec) -> Result { Ok(Uuid::new_v4().to_string().into()) } diff --git a/lib/src/fnc/script.rs b/lib/src/fnc/script.rs index 6acc7737..a4db9d35 100644 --- a/lib/src/fnc/script.rs +++ b/lib/src/fnc/script.rs @@ -1,9 +1,9 @@ -use crate::dbs::Runtime; +use crate::ctx::Context; use crate::err::Error; use crate::sql::script::Script; use crate::sql::value::Value; -pub fn run(_ctx: &Runtime, _expr: Script) -> Result { +pub fn run(_ctx: &Context, _expr: Script) -> Result { Err(Error::InvalidScript { message: String::from("Embedded functions are not yet supported."), }) diff --git a/lib/src/fnc/string.rs b/lib/src/fnc/string.rs index 5e4957b8..7b86345c 100644 --- a/lib/src/fnc/string.rs +++ b/lib/src/fnc/string.rs @@ -1,53 +1,53 @@ -use crate::dbs::Runtime; +use crate::ctx::Context; use crate::err::Error; use crate::sql::value::Value; use slug::slugify; -pub fn concat(_: &Runtime, args: Vec) -> Result { +pub fn concat(_: &Context, args: Vec) -> Result { Ok(args.into_iter().map(|x| x.as_string()).collect::>().concat().into()) } -pub fn ends_with(_: &Runtime, mut args: Vec) -> Result { +pub fn ends_with(_: &Context, mut args: Vec) -> Result { let val = args.remove(0).as_string(); let chr = args.remove(0).as_string(); Ok(val.ends_with(&chr).into()) } -pub fn join(_: &Runtime, mut args: Vec) -> Result { +pub fn join(_: &Context, mut args: Vec) -> Result { let chr = args.remove(0).as_string(); let val = args.into_iter().map(|x| x.as_string()); let val = val.collect::>().join(&chr); Ok(val.into()) } -pub fn length(_: &Runtime, mut args: Vec) -> Result { +pub fn length(_: &Context, mut args: Vec) -> Result { let val = args.remove(0).as_string(); let num = val.chars().count() as i64; Ok(num.into()) } -pub fn lowercase(_: &Runtime, mut args: Vec) -> Result { +pub fn lowercase(_: &Context, mut args: Vec) -> Result { Ok(args.remove(0).as_string().to_lowercase().into()) } -pub fn repeat(_: &Runtime, mut args: Vec) -> Result { +pub fn repeat(_: &Context, mut args: Vec) -> Result { let val = args.remove(0).as_string(); let num = args.remove(0).as_int() as usize; Ok(val.repeat(num).into()) } -pub fn replace(_: &Runtime, mut args: Vec) -> Result { +pub fn replace(_: &Context, mut args: Vec) -> Result { let val = args.remove(0).as_string(); let old = args.remove(0).as_string(); let new = args.remove(0).as_string(); Ok(val.replace(&old, &new).into()) } -pub fn reverse(_: &Runtime, mut args: Vec) -> Result { +pub fn reverse(_: &Context, mut args: Vec) -> Result { Ok(args.remove(0).as_string().chars().rev().collect::().into()) } -pub fn slice(_: &Runtime, mut args: Vec) -> Result { +pub fn slice(_: &Context, mut args: Vec) -> Result { let val = args.remove(0).as_string(); let beg = args.remove(0).as_int() as usize; let lim = args.remove(0).as_int() as usize; @@ -55,31 +55,31 @@ pub fn slice(_: &Runtime, mut args: Vec) -> Result { Ok(val.into()) } -pub fn slug(_: &Runtime, mut args: Vec) -> Result { +pub fn slug(_: &Context, mut args: Vec) -> Result { Ok(slugify(&args.remove(0).as_string()).into()) } -pub fn split(_: &Runtime, mut args: Vec) -> Result { +pub fn split(_: &Context, mut args: Vec) -> Result { let val = args.remove(0).as_string(); let chr = args.remove(0).as_string(); let val = val.split(&chr).collect::>(); Ok(val.into()) } -pub fn starts_with(_: &Runtime, mut args: Vec) -> Result { +pub fn starts_with(_: &Context, mut args: Vec) -> Result { let val = args.remove(0).as_string(); let chr = args.remove(0).as_string(); Ok(val.starts_with(&chr).into()) } -pub fn trim(_: &Runtime, mut args: Vec) -> Result { +pub fn trim(_: &Context, mut args: Vec) -> Result { Ok(args.remove(0).as_string().trim().into()) } -pub fn uppercase(_: &Runtime, mut args: Vec) -> Result { +pub fn uppercase(_: &Context, mut args: Vec) -> Result { Ok(args.remove(0).as_string().to_uppercase().into()) } -pub fn words(_: &Runtime, mut args: Vec) -> Result { +pub fn words(_: &Context, mut args: Vec) -> Result { Ok(args.remove(0).as_string().split(' ').collect::>().into()) } diff --git a/lib/src/fnc/time.rs b/lib/src/fnc/time.rs index 0ede57f9..4a0a34e7 100644 --- a/lib/src/fnc/time.rs +++ b/lib/src/fnc/time.rs @@ -1,4 +1,4 @@ -use crate::dbs::Runtime; +use crate::ctx::Context; use crate::err::Error; use crate::sql::datetime::Datetime; use crate::sql::value::Value; @@ -8,7 +8,7 @@ use chrono::DurationRound; use chrono::Timelike; use chrono::Utc; -pub fn day(_: &Runtime, mut args: Vec) -> Result { +pub fn day(_: &Context, mut args: Vec) -> Result { match args.len() { 0 => Ok(Utc::now().day().into()), _ => match args.remove(0) { @@ -18,7 +18,7 @@ pub fn day(_: &Runtime, mut args: Vec) -> Result { } } -pub fn floor(_: &Runtime, mut args: Vec) -> Result { +pub fn floor(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Datetime(v) => match args.remove(0) { Value::Duration(w) => match chrono::Duration::from_std(*w) { @@ -34,7 +34,7 @@ pub fn floor(_: &Runtime, mut args: Vec) -> Result { } } -pub fn group(_: &Runtime, mut args: Vec) -> Result { +pub fn group(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Datetime(v) => match args.remove(0) { Value::Strand(g) => match g.as_str() { @@ -67,7 +67,7 @@ pub fn group(_: &Runtime, mut args: Vec) -> Result { } } -pub fn hour(_: &Runtime, mut args: Vec) -> Result { +pub fn hour(_: &Context, mut args: Vec) -> Result { match args.len() { 0 => Ok(Utc::now().hour().into()), _ => match args.remove(0) { @@ -77,7 +77,7 @@ pub fn hour(_: &Runtime, mut args: Vec) -> Result { } } -pub fn mins(_: &Runtime, mut args: Vec) -> Result { +pub fn mins(_: &Context, mut args: Vec) -> Result { match args.len() { 0 => Ok(Utc::now().minute().into()), _ => match args.remove(0) { @@ -87,7 +87,7 @@ pub fn mins(_: &Runtime, mut args: Vec) -> Result { } } -pub fn month(_: &Runtime, mut args: Vec) -> Result { +pub fn month(_: &Context, mut args: Vec) -> Result { match args.len() { 0 => Ok(Utc::now().day().into()), _ => match args.remove(0) { @@ -97,7 +97,7 @@ pub fn month(_: &Runtime, mut args: Vec) -> Result { } } -pub fn nano(_: &Runtime, mut args: Vec) -> Result { +pub fn nano(_: &Context, mut args: Vec) -> Result { match args.len() { 0 => Ok(Utc::now().timestamp_nanos().into()), _ => match args.remove(0) { @@ -107,11 +107,11 @@ pub fn nano(_: &Runtime, mut args: Vec) -> Result { } } -pub fn now(_: &Runtime, _: Vec) -> Result { +pub fn now(_: &Context, _: Vec) -> Result { Ok(Datetime::default().into()) } -pub fn round(_: &Runtime, mut args: Vec) -> Result { +pub fn round(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Datetime(v) => match args.remove(0) { Value::Duration(w) => match chrono::Duration::from_std(*w) { @@ -127,7 +127,7 @@ pub fn round(_: &Runtime, mut args: Vec) -> Result { } } -pub fn secs(_: &Runtime, mut args: Vec) -> Result { +pub fn secs(_: &Context, mut args: Vec) -> Result { match args.len() { 0 => Ok(Utc::now().second().into()), _ => match args.remove(0) { @@ -137,7 +137,7 @@ pub fn secs(_: &Runtime, mut args: Vec) -> Result { } } -pub fn unix(_: &Runtime, mut args: Vec) -> Result { +pub fn unix(_: &Context, mut args: Vec) -> Result { match args.len() { 0 => Ok(Utc::now().timestamp().into()), _ => match args.remove(0) { @@ -147,7 +147,7 @@ pub fn unix(_: &Runtime, mut args: Vec) -> Result { } } -pub fn wday(_: &Runtime, mut args: Vec) -> Result { +pub fn wday(_: &Context, mut args: Vec) -> Result { match args.len() { 0 => Ok(Utc::now().weekday().number_from_monday().into()), _ => match args.remove(0) { @@ -157,7 +157,7 @@ pub fn wday(_: &Runtime, mut args: Vec) -> Result { } } -pub fn week(_: &Runtime, mut args: Vec) -> Result { +pub fn week(_: &Context, mut args: Vec) -> Result { match args.len() { 0 => Ok(Utc::now().iso_week().week().into()), _ => match args.remove(0) { @@ -167,7 +167,7 @@ pub fn week(_: &Runtime, mut args: Vec) -> Result { } } -pub fn yday(_: &Runtime, mut args: Vec) -> Result { +pub fn yday(_: &Context, mut args: Vec) -> Result { match args.len() { 0 => Ok(Utc::now().ordinal().into()), _ => match args.remove(0) { @@ -177,7 +177,7 @@ pub fn yday(_: &Runtime, mut args: Vec) -> Result { } } -pub fn year(_: &Runtime, mut args: Vec) -> Result { +pub fn year(_: &Context, mut args: Vec) -> Result { match args.len() { 0 => Ok(Utc::now().year().into()), _ => match args.remove(0) { diff --git a/lib/src/fnc/type.rs b/lib/src/fnc/type.rs index dc2611f1..e1795853 100644 --- a/lib/src/fnc/type.rs +++ b/lib/src/fnc/type.rs @@ -1,4 +1,4 @@ -use crate::dbs::Runtime; +use crate::ctx::Context; use crate::err::Error; use crate::sql::geometry::Geometry; use crate::sql::number::Number; @@ -6,14 +6,14 @@ use crate::sql::table::Table; use crate::sql::thing::Thing; use crate::sql::value::Value; -pub fn bool(_: &Runtime, mut args: Vec) -> Result { +pub fn bool(_: &Context, mut args: Vec) -> Result { match args.remove(0).is_truthy() { true => Ok(Value::True), false => Ok(Value::False), } } -pub fn datetime(_: &Runtime, mut args: Vec) -> Result { +pub fn datetime(_: &Context, mut args: Vec) -> Result { let val = args.remove(0); match val { Value::Datetime(_) => Ok(val), @@ -21,7 +21,7 @@ pub fn datetime(_: &Runtime, mut args: Vec) -> Result { } } -pub fn decimal(_: &Runtime, mut args: Vec) -> Result { +pub fn decimal(_: &Context, mut args: Vec) -> Result { let val = args.remove(0); match val { Value::Number(Number::Decimal(_)) => Ok(val), @@ -29,7 +29,7 @@ pub fn decimal(_: &Runtime, mut args: Vec) -> Result { } } -pub fn duration(_: &Runtime, mut args: Vec) -> Result { +pub fn duration(_: &Context, mut args: Vec) -> Result { let val = args.remove(0); match val { Value::Duration(_) => Ok(val), @@ -37,7 +37,7 @@ pub fn duration(_: &Runtime, mut args: Vec) -> Result { } } -pub fn float(_: &Runtime, mut args: Vec) -> Result { +pub fn float(_: &Context, mut args: Vec) -> Result { let val = args.remove(0); match val { Value::Number(Number::Float(_)) => Ok(val), @@ -45,7 +45,7 @@ pub fn float(_: &Runtime, mut args: Vec) -> Result { } } -pub fn int(_: &Runtime, mut args: Vec) -> Result { +pub fn int(_: &Context, mut args: Vec) -> Result { let val = args.remove(0); match val { Value::Number(Number::Int(_)) => Ok(val), @@ -53,7 +53,7 @@ pub fn int(_: &Runtime, mut args: Vec) -> Result { } } -pub fn number(_: &Runtime, mut args: Vec) -> Result { +pub fn number(_: &Context, mut args: Vec) -> Result { let val = args.remove(0); match val { Value::Number(_) => Ok(val), @@ -61,7 +61,7 @@ pub fn number(_: &Runtime, mut args: Vec) -> Result { } } -pub fn point(_: &Runtime, mut args: Vec) -> Result { +pub fn point(_: &Context, mut args: Vec) -> Result { match args.len() { 2 => { let x = args.remove(0); @@ -77,14 +77,14 @@ pub fn point(_: &Runtime, mut args: Vec) -> Result { } } -pub fn regex(_: &Runtime, mut args: Vec) -> Result { +pub fn regex(_: &Context, mut args: Vec) -> Result { match args.remove(0) { Value::Strand(v) => Ok(Value::Regex(v.as_str().into())), _ => Ok(Value::None), } } -pub fn string(_: &Runtime, mut args: Vec) -> Result { +pub fn string(_: &Context, mut args: Vec) -> Result { let val = args.remove(0); match val { Value::Strand(_) => Ok(val), @@ -92,11 +92,11 @@ pub fn string(_: &Runtime, mut args: Vec) -> Result { } } -pub fn table(_: &Runtime, mut args: Vec) -> Result { +pub fn table(_: &Context, mut args: Vec) -> Result { Ok(Value::Table(Table(args.remove(0).as_string()))) } -pub fn thing(_: &Runtime, mut args: Vec) -> Result { +pub fn thing(_: &Context, mut args: Vec) -> Result { let tb = args.remove(0); match args.remove(0) { Value::Thing(id) => Ok(Value::Thing(Thing { diff --git a/lib/src/kvs/ds.rs b/lib/src/kvs/ds.rs index fc2ec984..a06038a1 100644 --- a/lib/src/kvs/ds.rs +++ b/lib/src/kvs/ds.rs @@ -163,8 +163,6 @@ impl Datastore { let ctx = vars.attach(ctx); // Parse the SQL query text let ast = sql::parse(txt)?; - // Freeze the context - let ctx = ctx.freeze(); // Process all statements opt.auth = sess.au.clone(); opt.ns = sess.ns(); @@ -189,8 +187,6 @@ impl Datastore { let ctx = sess.context(ctx); // Store the query variables let ctx = vars.attach(ctx); - // Freeze the context - let ctx = ctx.freeze(); // Process all statements opt.auth = sess.au.clone(); opt.ns = sess.ns(); diff --git a/lib/src/sql/array.rs b/lib/src/sql/array.rs index e5c4a384..56366a33 100644 --- a/lib/src/sql/array.rs +++ b/lib/src/sql/array.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::comment::mightbespace; @@ -132,7 +132,7 @@ impl Array { impl Array { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, diff --git a/lib/src/sql/expression.rs b/lib/src/sql/expression.rs index 45498c71..7bb75104 100644 --- a/lib/src/sql/expression.rs +++ b/lib/src/sql/expression.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::fnc; @@ -59,7 +59,7 @@ impl Expression { impl Expression { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, diff --git a/lib/src/sql/function.rs b/lib/src/sql/function.rs index ed5813e4..1c1cb592 100644 --- a/lib/src/sql/function.rs +++ b/lib/src/sql/function.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::fnc; @@ -104,7 +104,7 @@ impl Function { impl Function { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, diff --git a/lib/src/sql/idiom.rs b/lib/src/sql/idiom.rs index 763f6a65..5eef0fbd 100644 --- a/lib/src/sql/idiom.rs +++ b/lib/src/sql/idiom.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::common::commas; @@ -72,7 +72,7 @@ impl Idiom { impl Idiom { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, diff --git a/lib/src/sql/object.rs b/lib/src/sql/object.rs index 347c98cf..48360197 100644 --- a/lib/src/sql/object.rs +++ b/lib/src/sql/object.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::comment::mightbespace; @@ -100,7 +100,7 @@ impl Object { impl Object { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, diff --git a/lib/src/sql/param.rs b/lib/src/sql/param.rs index cd1bb2e9..9d61e9da 100644 --- a/lib/src/sql/param.rs +++ b/lib/src/sql/param.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::error::IResult; @@ -33,7 +33,7 @@ impl Deref for Param { impl Param { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, @@ -55,7 +55,7 @@ impl Param { // The base document does not exist None => Ok(Value::None), }, - _ => match ctx.value::(v) { + _ => match ctx.value(v) { // The base variable exists Some(v) => { // Get the path parts diff --git a/lib/src/sql/statement.rs b/lib/src/sql/statement.rs index 8f53685e..044a6d9c 100644 --- a/lib/src/sql/statement.rs +++ b/lib/src/sql/statement.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::comment::{comment, mightbespace}; @@ -97,7 +97,7 @@ impl Statement { impl Statement { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, diff --git a/lib/src/sql/statements/create.rs b/lib/src/sql/statements/create.rs index 7fb95dbf..43a1cadd 100644 --- a/lib/src/sql/statements/create.rs +++ b/lib/src/sql/statements/create.rs @@ -1,7 +1,7 @@ +use crate::ctx::Context; use crate::dbs::Iterator; use crate::dbs::Level; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::err::Error; @@ -30,7 +30,7 @@ pub struct CreateStatement { impl CreateStatement { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, diff --git a/lib/src/sql/statements/define.rs b/lib/src/sql/statements/define.rs index 13f9ddd7..fc77e72e 100644 --- a/lib/src/sql/statements/define.rs +++ b/lib/src/sql/statements/define.rs @@ -1,6 +1,6 @@ +use crate::ctx::Context; use crate::dbs::Level; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::algorithm::{algorithm, Algorithm}; @@ -48,7 +48,7 @@ pub enum DefineStatement { impl DefineStatement { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, @@ -109,7 +109,7 @@ pub struct DefineNamespaceStatement { impl DefineNamespaceStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, @@ -156,7 +156,7 @@ pub struct DefineDatabaseStatement { impl DefineDatabaseStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, @@ -211,7 +211,7 @@ pub struct DefineLoginStatement { impl DefineLoginStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, @@ -330,7 +330,7 @@ pub struct DefineTokenStatement { impl DefineTokenStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, @@ -426,7 +426,7 @@ pub struct DefineScopeStatement { impl DefineScopeStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, @@ -562,7 +562,7 @@ pub struct DefineTableStatement { impl DefineTableStatement { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, @@ -727,7 +727,7 @@ pub struct DefineEventStatement { impl DefineEventStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, @@ -806,7 +806,7 @@ pub struct DefineFieldStatement { impl DefineFieldStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, @@ -944,7 +944,7 @@ pub struct DefineIndexStatement { impl DefineIndexStatement { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, diff --git a/lib/src/sql/statements/delete.rs b/lib/src/sql/statements/delete.rs index 8daba695..38474c99 100644 --- a/lib/src/sql/statements/delete.rs +++ b/lib/src/sql/statements/delete.rs @@ -1,7 +1,7 @@ +use crate::ctx::Context; use crate::dbs::Iterator; use crate::dbs::Level; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::err::Error; @@ -31,7 +31,7 @@ pub struct DeleteStatement { impl DeleteStatement { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, diff --git a/lib/src/sql/statements/ifelse.rs b/lib/src/sql/statements/ifelse.rs index 3973b9d2..3860ebd5 100644 --- a/lib/src/sql/statements/ifelse.rs +++ b/lib/src/sql/statements/ifelse.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::comment::shouldbespace; @@ -21,7 +21,7 @@ pub struct IfelseStatement { impl IfelseStatement { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, diff --git a/lib/src/sql/statements/info.rs b/lib/src/sql/statements/info.rs index 95ea8585..da9b0e23 100644 --- a/lib/src/sql/statements/info.rs +++ b/lib/src/sql/statements/info.rs @@ -1,6 +1,6 @@ +use crate::ctx::Context; use crate::dbs::Level; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::comment::shouldbespace; @@ -26,7 +26,7 @@ pub enum InfoStatement { impl InfoStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, diff --git a/lib/src/sql/statements/insert.rs b/lib/src/sql/statements/insert.rs index 659a8f28..23caedcd 100644 --- a/lib/src/sql/statements/insert.rs +++ b/lib/src/sql/statements/insert.rs @@ -1,7 +1,7 @@ +use crate::ctx::Context; use crate::dbs::Iterator; use crate::dbs::Level; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::err::Error; @@ -34,7 +34,7 @@ pub struct InsertStatement { impl InsertStatement { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, diff --git a/lib/src/sql/statements/kill.rs b/lib/src/sql/statements/kill.rs index 833285eb..a7ec22c5 100644 --- a/lib/src/sql/statements/kill.rs +++ b/lib/src/sql/statements/kill.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::comment::shouldbespace; @@ -19,7 +19,7 @@ pub struct KillStatement { impl KillStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, _opt: &Options, _txn: &Transaction, _doc: Option<&Value>, diff --git a/lib/src/sql/statements/live.rs b/lib/src/sql/statements/live.rs index 7091a952..75679076 100644 --- a/lib/src/sql/statements/live.rs +++ b/lib/src/sql/statements/live.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::comment::shouldbespace; @@ -27,7 +27,7 @@ pub struct LiveStatement { impl LiveStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, _opt: &Options, _txn: &Transaction, _doc: Option<&Value>, diff --git a/lib/src/sql/statements/output.rs b/lib/src/sql/statements/output.rs index 20f6052b..6f5f46a7 100644 --- a/lib/src/sql/statements/output.rs +++ b/lib/src/sql/statements/output.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::comment::shouldbespace; @@ -18,7 +18,7 @@ pub struct OutputStatement { impl OutputStatement { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, diff --git a/lib/src/sql/statements/relate.rs b/lib/src/sql/statements/relate.rs index 05af3dbb..ee3aa66c 100644 --- a/lib/src/sql/statements/relate.rs +++ b/lib/src/sql/statements/relate.rs @@ -1,7 +1,7 @@ +use crate::ctx::Context; use crate::dbs::Iterator; use crate::dbs::Level; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::err::Error; @@ -37,7 +37,7 @@ pub struct RelateStatement { impl RelateStatement { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, diff --git a/lib/src/sql/statements/remove.rs b/lib/src/sql/statements/remove.rs index 9d091c43..a5ceb8da 100644 --- a/lib/src/sql/statements/remove.rs +++ b/lib/src/sql/statements/remove.rs @@ -1,6 +1,6 @@ +use crate::ctx::Context; use crate::dbs::Level; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::base::{base, Base}; @@ -32,7 +32,7 @@ pub enum RemoveStatement { impl RemoveStatement { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, @@ -93,7 +93,7 @@ pub struct RemoveNamespaceStatement { impl RemoveNamespaceStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, @@ -147,7 +147,7 @@ pub struct RemoveDatabaseStatement { impl RemoveDatabaseStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, @@ -202,7 +202,7 @@ pub struct RemoveLoginStatement { impl RemoveLoginStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, @@ -277,7 +277,7 @@ pub struct RemoveTokenStatement { impl RemoveTokenStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, @@ -351,7 +351,7 @@ pub struct RemoveScopeStatement { impl RemoveScopeStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, @@ -402,7 +402,7 @@ pub struct RemoveTableStatement { impl RemoveTableStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, @@ -457,7 +457,7 @@ pub struct RemoveEventStatement { impl RemoveEventStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, @@ -515,7 +515,7 @@ pub struct RemoveFieldStatement { impl RemoveFieldStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, @@ -573,7 +573,7 @@ pub struct RemoveIndexStatement { impl RemoveIndexStatement { pub(crate) async fn compute( &self, - _ctx: &Runtime, + _ctx: &Context<'_>, opt: &Options, txn: &Transaction, _doc: Option<&Value>, diff --git a/lib/src/sql/statements/select.rs b/lib/src/sql/statements/select.rs index 90291f91..d4ca8fdb 100644 --- a/lib/src/sql/statements/select.rs +++ b/lib/src/sql/statements/select.rs @@ -1,7 +1,7 @@ +use crate::ctx::Context; use crate::dbs::Iterator; use crate::dbs::Level; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::err::Error; @@ -59,7 +59,7 @@ impl SelectStatement { impl SelectStatement { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, diff --git a/lib/src/sql/statements/set.rs b/lib/src/sql/statements/set.rs index 50a2011c..9958b40b 100644 --- a/lib/src/sql/statements/set.rs +++ b/lib/src/sql/statements/set.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::comment::mightbespace; @@ -23,7 +23,7 @@ pub struct SetStatement { impl SetStatement { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, diff --git a/lib/src/sql/statements/update.rs b/lib/src/sql/statements/update.rs index d12004d8..a5ed9ebe 100644 --- a/lib/src/sql/statements/update.rs +++ b/lib/src/sql/statements/update.rs @@ -1,7 +1,7 @@ +use crate::ctx::Context; use crate::dbs::Iterator; use crate::dbs::Level; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Statement; use crate::dbs::Transaction; use crate::err::Error; @@ -32,7 +32,7 @@ pub struct UpdateStatement { impl UpdateStatement { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, diff --git a/lib/src/sql/subquery.rs b/lib/src/sql/subquery.rs index 6c7c3656..6ba5da0f 100644 --- a/lib/src/sql/subquery.rs +++ b/lib/src/sql/subquery.rs @@ -1,6 +1,5 @@ use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::error::IResult; @@ -41,7 +40,7 @@ impl PartialOrd for Subquery { impl Subquery { pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&Value>, @@ -55,12 +54,9 @@ impl Subquery { // Duplicate context let mut ctx = Context::new(ctx); // Add parent document - if doc.is_some() { - let doc = doc.unwrap().clone(); + if let Some(doc) = doc { ctx.add_value("parent".into(), doc); } - // Prepare context - let ctx = ctx.freeze(); // Process subquery let res = v.compute(&ctx, &opt, txn, doc).await?; // Process result @@ -81,12 +77,9 @@ impl Subquery { // Duplicate context let mut ctx = Context::new(ctx); // Add parent document - if doc.is_some() { - let doc = doc.unwrap().clone(); + if let Some(doc) = doc { ctx.add_value("parent".into(), doc); } - // Prepare context - let ctx = ctx.freeze(); // Process subquery match v.compute(&ctx, &opt, txn, doc).await? { Value::Array(mut v) => match v.len() { @@ -102,12 +95,9 @@ impl Subquery { // Duplicate context let mut ctx = Context::new(ctx); // Add parent document - if doc.is_some() { - let doc = doc.unwrap().clone(); + if let Some(doc) = doc { ctx.add_value("parent".into(), doc); } - // Prepare context - let ctx = ctx.freeze(); // Process subquery match v.compute(&ctx, &opt, txn, doc).await? { Value::Array(mut v) => match v.len() { @@ -123,12 +113,9 @@ impl Subquery { // Duplicate context let mut ctx = Context::new(ctx); // Add parent document - if doc.is_some() { - let doc = doc.unwrap().clone(); + if let Some(doc) = doc { ctx.add_value("parent".into(), doc); } - // Prepare context - let ctx = ctx.freeze(); // Process subquery match v.compute(&ctx, &opt, txn, doc).await? { Value::Array(mut v) => match v.len() { @@ -144,12 +131,9 @@ impl Subquery { // Duplicate context let mut ctx = Context::new(ctx); // Add parent document - if doc.is_some() { - let doc = doc.unwrap().clone(); + if let Some(doc) = doc { ctx.add_value("parent".into(), doc); } - // Prepare context - let ctx = ctx.freeze(); // Process subquery match v.compute(&ctx, &opt, txn, doc).await? { Value::Array(mut v) => match v.len() { @@ -165,12 +149,9 @@ impl Subquery { // Duplicate context let mut ctx = Context::new(ctx); // Add parent document - if doc.is_some() { - let doc = doc.unwrap().clone(); + if let Some(doc) = doc { ctx.add_value("parent".into(), doc); } - // Prepare context - let ctx = ctx.freeze(); // Process subquery match v.compute(&ctx, &opt, txn, doc).await? { Value::Array(mut v) => match v.len() { diff --git a/lib/src/sql/value/array.rs b/lib/src/sql/value/array.rs index 7f3e3a6d..b6e35fcb 100644 --- a/lib/src/sql/value/array.rs +++ b/lib/src/sql/value/array.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::array::Array; @@ -9,7 +9,7 @@ use crate::sql::value::Value; impl Value { pub async fn array( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, path: &[Part], diff --git a/lib/src/sql/value/clear.rs b/lib/src/sql/value/clear.rs index b01c737b..311c8130 100644 --- a/lib/src/sql/value/clear.rs +++ b/lib/src/sql/value/clear.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::value::Value; @@ -7,7 +7,7 @@ use crate::sql::value::Value; impl Value { pub async fn clear( &mut self, - _ctx: &Runtime, + _ctx: &Context<'_>, _opt: &Options, _txn: &Transaction, ) -> Result<(), Error> { diff --git a/lib/src/sql/value/decrement.rs b/lib/src/sql/value/decrement.rs index c5745742..8d622313 100644 --- a/lib/src/sql/value/decrement.rs +++ b/lib/src/sql/value/decrement.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::number::Number; @@ -9,7 +9,7 @@ use crate::sql::value::Value; impl Value { pub async fn decrement( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, path: &[Part], diff --git a/lib/src/sql/value/def.rs b/lib/src/sql/value/def.rs index 19d1d4bb..8e8c111e 100644 --- a/lib/src/sql/value/def.rs +++ b/lib/src/sql/value/def.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::part::Part; @@ -12,7 +12,7 @@ static RID: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("id")]); impl Value { pub async fn def( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, val: &Thing, diff --git a/lib/src/sql/value/del.rs b/lib/src/sql/value/del.rs index 45e0edab..8965ec27 100644 --- a/lib/src/sql/value/del.rs +++ b/lib/src/sql/value/del.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::array::Abolish; @@ -15,7 +15,7 @@ impl Value { #[cfg_attr(not(feature = "parallel"), async_recursion(?Send))] pub async fn del( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, path: &[Part], diff --git a/lib/src/sql/value/get.rs b/lib/src/sql/value/get.rs index 34124c27..c203b5f2 100644 --- a/lib/src/sql/value/get.rs +++ b/lib/src/sql/value/get.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::field::{Field, Fields}; @@ -16,7 +16,7 @@ impl Value { #[cfg_attr(not(feature = "parallel"), async_recursion(?Send))] pub async fn get( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, path: &[Part], diff --git a/lib/src/sql/value/increment.rs b/lib/src/sql/value/increment.rs index b5a9f291..079e4137 100644 --- a/lib/src/sql/value/increment.rs +++ b/lib/src/sql/value/increment.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::number::Number; @@ -9,7 +9,7 @@ use crate::sql::value::Value; impl Value { pub async fn increment( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, path: &[Part], diff --git a/lib/src/sql/value/merge.rs b/lib/src/sql/value/merge.rs index 48655c5b..fbb7af88 100644 --- a/lib/src/sql/value/merge.rs +++ b/lib/src/sql/value/merge.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::part::Part; @@ -8,7 +8,7 @@ use crate::sql::value::Value; impl Value { pub async fn merge( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, val: &Value, diff --git a/lib/src/sql/value/object.rs b/lib/src/sql/value/object.rs index 5c18c78a..97d8cfd0 100644 --- a/lib/src/sql/value/object.rs +++ b/lib/src/sql/value/object.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::object::Object; @@ -9,7 +9,7 @@ use crate::sql::value::Value; impl Value { pub async fn object( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, path: &[Part], diff --git a/lib/src/sql/value/patch.rs b/lib/src/sql/value/patch.rs index 69a867ba..a43e9ed3 100644 --- a/lib/src/sql/value/patch.rs +++ b/lib/src/sql/value/patch.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::array::Array; @@ -9,7 +9,7 @@ use crate::sql::value::Value; impl Value { pub async fn patch( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, val: &Array, diff --git a/lib/src/sql/value/replace.rs b/lib/src/sql/value/replace.rs index 24da70f2..ba2fb2eb 100644 --- a/lib/src/sql/value/replace.rs +++ b/lib/src/sql/value/replace.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::value::Value; @@ -7,7 +7,7 @@ use crate::sql::value::Value; impl Value { pub async fn replace( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, val: &Value, diff --git a/lib/src/sql/value/set.rs b/lib/src/sql/value/set.rs index d821c6d5..ff0c3953 100644 --- a/lib/src/sql/value/set.rs +++ b/lib/src/sql/value/set.rs @@ -1,5 +1,5 @@ +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::part::Next; @@ -13,7 +13,7 @@ impl Value { #[cfg_attr(not(feature = "parallel"), async_recursion(?Send))] pub async fn set( &mut self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, path: &[Part], diff --git a/lib/src/sql/value/value.rs b/lib/src/sql/value/value.rs index a9ddac0d..57fe55d0 100644 --- a/lib/src/sql/value/value.rs +++ b/lib/src/sql/value/value.rs @@ -1,7 +1,7 @@ #![allow(clippy::derive_ord_xor_partial_ord)] +use crate::ctx::Context; use crate::dbs::Options; -use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; use crate::sql::array::{array, Array}; @@ -971,7 +971,7 @@ impl Value { #[cfg_attr(not(feature = "parallel"), async_recursion(?Send))] pub(crate) async fn compute( &self, - ctx: &Runtime, + ctx: &Context<'_>, opt: &Options, txn: &Transaction, doc: Option<&'async_recursion Value>,