From 0cb42d72831b8bfe597f8c10e93b1bd9e9fdb767 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 20 Jun 2022 12:26:27 +0100 Subject: [PATCH] Add functionality to compute sql value within database context --- lib/src/kvs/ds.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/src/kvs/ds.rs b/lib/src/kvs/ds.rs index 84f64605..7701acbc 100644 --- a/lib/src/kvs/ds.rs +++ b/lib/src/kvs/ds.rs @@ -8,8 +8,11 @@ use crate::dbs::Session; use crate::dbs::Variables; use crate::err::Error; use crate::sql; -use crate::sql::query::Query; +use crate::sql::Query; +use crate::sql::Value; use channel::Sender; +use futures::lock::Mutex; +use std::sync::Arc; /// The underlying datastore instance which stores the dataset. pub struct Datastore { @@ -191,6 +194,40 @@ impl Datastore { exe.execute(ctx, opt, ast).await } + /// Execute a pre-parsed SQL query + pub async fn compute( + &self, + val: Value, + sess: &Session, + vars: Variables, + ) -> Result { + // Start a new transaction + let txn = self.transaction(val.writeable(), false).await?; + // + let txn = Arc::new(Mutex::new(txn)); + // Create a new query options + let mut opt = Options::default(); + // Create a default context + let ctx = Context::default(); + // Start an execution context + let ctx = sess.context(ctx); + // Store the query variables + let ctx = vars.attach(ctx); + // Setup the query options + opt.auth = sess.au.clone(); + opt.ns = sess.ns(); + opt.db = sess.db(); + // Compute the value + let res = val.compute(&ctx, &opt, &txn, None).await?; + // Store any data + match val.writeable() { + true => txn.lock().await.commit().await?, + false => txn.lock().await.cancel().await?, + }; + // Return result + Ok(res) + } + /// Performs a full database export as SQL pub async fn export(&self, ns: String, db: String, chn: Sender>) -> Result<(), Error> { // Start a new transaction