Add functionality to compute sql value within database context

This commit is contained in:
Tobie Morgan Hitchcock 2022-06-20 12:26:27 +01:00
parent b4e2082196
commit 0cb42d7283

View file

@ -8,8 +8,11 @@ use crate::dbs::Session;
use crate::dbs::Variables; use crate::dbs::Variables;
use crate::err::Error; use crate::err::Error;
use crate::sql; use crate::sql;
use crate::sql::query::Query; use crate::sql::Query;
use crate::sql::Value;
use channel::Sender; use channel::Sender;
use futures::lock::Mutex;
use std::sync::Arc;
/// The underlying datastore instance which stores the dataset. /// The underlying datastore instance which stores the dataset.
pub struct Datastore { pub struct Datastore {
@ -191,6 +194,40 @@ impl Datastore {
exe.execute(ctx, opt, ast).await exe.execute(ctx, opt, ast).await
} }
/// Execute a pre-parsed SQL query
pub async fn compute(
&self,
val: Value,
sess: &Session,
vars: Variables,
) -> Result<Value, Error> {
// 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 /// Performs a full database export as SQL
pub async fn export(&self, ns: String, db: String, chn: Sender<Vec<u8>>) -> Result<(), Error> { pub async fn export(&self, ns: String, db: String, chn: Sender<Vec<u8>>) -> Result<(), Error> {
// Start a new transaction // Start a new transaction