Convert Parent to Runtime for query variable storage

This commit is contained in:
Tobie Morgan Hitchcock 2021-03-31 13:10:13 +01:00
parent 7768e85145
commit 5b58c5fa0e
35 changed files with 106 additions and 79 deletions

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs::response::{Response, Responses};
use crate::dbs::Process;
use crate::dbs::Runtime;
use crate::err::Error;
use crate::sql::query::Query;
use std::time::Instant;
@ -21,7 +21,7 @@ impl Executor {
}
}
pub fn execute(&self, ctx: &Parent, qry: Query) -> Result<Responses, Error> {
pub fn execute(&self, ctx: &Runtime, qry: Query) -> Result<Responses, Error> {
let mut r: Vec<Response> = vec![];
for stm in qry.statements().iter() {

View file

@ -1,5 +1,5 @@
use crate::ctx::Parent;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::err::Error;
use crate::sql::literal::Literal;
@ -9,13 +9,13 @@ impl Iterator {
pub fn new() -> Iterator {
Iterator {}
}
pub fn process_query(&self, ctx: &Parent, exe: &Executor) {}
pub fn process_table(&self, ctx: &Parent, exe: &Executor) {}
pub fn process_thing(&self, ctx: &Parent, exe: &Executor) {}
pub fn process_model(&self, ctx: &Parent, exe: &Executor) {}
pub fn process_array(&self, ctx: &Parent, exe: &Executor) {}
pub fn process_object(&self, ctx: &Parent, exe: &Executor) {}
pub fn output(&self, ctx: &Parent, exe: &Executor) -> Result<Literal, Error> {
pub fn process_query(&self, ctx: &Runtime, exe: &Executor) {}
pub fn process_table(&self, ctx: &Runtime, exe: &Executor) {}
pub fn process_thing(&self, ctx: &Runtime, exe: &Executor) {}
pub fn process_model(&self, ctx: &Runtime, exe: &Executor) {}
pub fn process_array(&self, ctx: &Runtime, exe: &Executor) {}
pub fn process_object(&self, ctx: &Runtime, exe: &Executor) {}
pub fn output(&self, ctx: &Runtime, exe: &Executor) -> Result<Literal, Error> {
Ok(Literal::Null)
}
}

View file

@ -3,8 +3,10 @@ mod executor;
mod iterator;
mod process;
mod response;
mod runtime;
pub use self::dbs::*;
pub use self::executor::*;
pub use self::iterator::*;
pub use self::process::*;
pub use self::runtime::*;

View file

@ -1,5 +1,5 @@
use crate::ctx::Parent;
use crate::dbs::executor::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::literal::Literal;
@ -7,7 +7,7 @@ use crate::sql::literal::Literal;
pub trait Process {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error>;

25
src/dbs/runtime.rs Normal file
View file

@ -0,0 +1,25 @@
/*
* 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<Context>;

View file

@ -1,8 +1,8 @@
use crate::ctx::Context;
use crate::dbs::Runtime;
use crate::err::Error;
use crate::sql::literal::Literal;
pub fn run(ctx: &Context, name: &String, val: Literal) -> Result<Literal, Error> {
pub fn run(ctx: &Runtime, name: &String, val: Literal) -> Result<Literal, Error> {
match name.as_str() {
"bool" => bool(ctx, val),
"int" => int(ctx, val),
@ -16,33 +16,33 @@ pub fn run(ctx: &Context, name: &String, val: Literal) -> Result<Literal, Error>
}
}
pub fn bool(ctx: &Context, val: Literal) -> Result<Literal, Error> {
pub fn bool(ctx: &Runtime, val: Literal) -> Result<Literal, Error> {
match val.as_bool() {
true => Ok(Literal::True),
false => Ok(Literal::False),
}
}
pub fn int(ctx: &Context, val: Literal) -> Result<Literal, Error> {
pub fn int(ctx: &Runtime, val: Literal) -> Result<Literal, Error> {
Ok(Literal::Int(val.as_int()))
}
pub fn float(ctx: &Context, val: Literal) -> Result<Literal, Error> {
pub fn float(ctx: &Runtime, val: Literal) -> Result<Literal, Error> {
Ok(Literal::Float(val.as_float()))
}
pub fn string(ctx: &Context, val: Literal) -> Result<Literal, Error> {
pub fn string(ctx: &Runtime, val: Literal) -> Result<Literal, Error> {
Ok(Literal::Strand(val.as_strand()))
}
pub fn number(ctx: &Context, val: Literal) -> Result<Literal, Error> {
pub fn number(ctx: &Runtime, val: Literal) -> Result<Literal, Error> {
Ok(Literal::Number(val.as_number()))
}
pub fn datetime(ctx: &Context, val: Literal) -> Result<Literal, Error> {
pub fn datetime(ctx: &Runtime, val: Literal) -> Result<Literal, Error> {
Ok(Literal::Datetime(val.as_datetime()))
}
pub fn duration(ctx: &Context, val: Literal) -> Result<Literal, Error> {
pub fn duration(ctx: &Runtime, val: Literal) -> Result<Literal, Error> {
Ok(Literal::Duration(val.as_duration()))
}

View file

@ -1,7 +1,7 @@
use crate::ctx::Parent;
use crate::dbs::Runtime;
use crate::err::Error;
use crate::sql::literal::Literal;
pub fn run(ctx: &Parent, args: Literal) -> Result<Literal, Error> {
pub fn run(ctx: &Runtime, args: Literal) -> Result<Literal, Error> {
todo!()
}

View file

@ -1,4 +1,4 @@
use crate::ctx::Parent;
use crate::dbs::Runtime;
use crate::err::Error;
use crate::sql::literal::Literal;
@ -6,6 +6,6 @@ pub mod cast;
pub mod future;
pub mod operate;
pub fn run(ctx: &Parent, name: &String, args: Vec<Literal>) -> Result<Literal, Error> {
pub fn run(ctx: &Runtime, name: &String, args: Vec<Literal>) -> Result<Literal, Error> {
todo!()
}

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::mightbespace;
@ -36,7 +36,7 @@ impl fmt::Display for Array {
impl dbs::Process for Array {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::fnc;
@ -49,7 +49,7 @@ impl fmt::Display for Expression {
impl dbs::Process for Expression {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::fnc;
@ -48,7 +48,7 @@ impl fmt::Display for Function {
impl dbs::Process for Function {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::common::commas;
@ -76,7 +76,7 @@ impl fmt::Display for Idiom {
impl dbs::Process for Idiom {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::fnc;
@ -349,7 +349,7 @@ impl fmt::Display for Literal {
impl dbs::Process for Literal {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::mightbespace;
@ -45,7 +45,7 @@ impl fmt::Display for Object {
impl dbs::Process for Object {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::common::val_char;
@ -43,7 +43,7 @@ impl fmt::Display for Param {
impl dbs::Process for Param {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::{comment, mightbespace};
@ -110,7 +110,7 @@ impl fmt::Display for Statement {
impl dbs::Process for Statement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,7 +1,7 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Iterator;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::shouldbespace;
@ -46,7 +46,7 @@ impl fmt::Display for CreateStatement {
impl dbs::Process for CreateStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::algorithm::{algorithm, Algorithm};
@ -56,7 +56,7 @@ impl fmt::Display for DefineStatement {
impl dbs::Process for DefineStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,7 +1,7 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Iterator;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::shouldbespace;
@ -47,7 +47,7 @@ impl fmt::Display for DeleteStatement {
impl dbs::Process for DeleteStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::shouldbespace;
@ -42,7 +42,7 @@ impl fmt::Display for IfelseStatement {
impl dbs::Process for IfelseStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::shouldbespace;
@ -34,7 +34,7 @@ impl fmt::Display for InfoStatement {
impl dbs::Process for InfoStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,7 +1,7 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Iterator;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::shouldbespace;
@ -44,7 +44,7 @@ impl fmt::Display for InsertStatement {
impl dbs::Process for InsertStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::shouldbespace;
@ -25,7 +25,7 @@ impl fmt::Display for KillStatement {
impl dbs::Process for KillStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::shouldbespace;
@ -42,7 +42,7 @@ impl fmt::Display for LiveStatement {
impl dbs::Process for LiveStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::mightbespace;
@ -35,7 +35,7 @@ impl fmt::Display for OptionStatement {
impl dbs::Process for OptionStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::shouldbespace;
@ -25,7 +25,7 @@ impl fmt::Display for OutputStatement {
impl dbs::Process for OutputStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,7 +1,7 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Iterator;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::mightbespace;
@ -57,7 +57,7 @@ impl fmt::Display for RelateStatement {
impl dbs::Process for RelateStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::base::{base, Base};
@ -46,7 +46,7 @@ impl fmt::Display for RemoveStatement {
impl dbs::Process for RemoveStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,7 +1,7 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Iterator;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::shouldbespace;
@ -84,7 +84,7 @@ impl fmt::Display for SelectStatement {
impl dbs::Process for SelectStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::mightbespace;
@ -29,7 +29,7 @@ impl fmt::Display for SetStatement {
impl dbs::Process for SetStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,7 +1,7 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Iterator;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::shouldbespace;
@ -52,7 +52,7 @@ impl fmt::Display for UpdateStatement {
impl dbs::Process for UpdateStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,7 +1,7 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Iterator;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::shouldbespace;
@ -44,7 +44,7 @@ impl fmt::Display for UpsertStatement {
impl dbs::Process for UpsertStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::comment::shouldbespace;
@ -36,7 +36,7 @@ impl fmt::Display for UseStatement {
impl dbs::Process for UseStatement {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,7 +1,7 @@
use crate::ctx::Context;
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::expression::{expression, Expression};
@ -61,7 +61,7 @@ impl fmt::Display for Subquery {
impl dbs::Process for Subquery {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {

View file

@ -1,6 +1,6 @@
use crate::ctx::Parent;
use crate::dbs;
use crate::dbs::Executor;
use crate::dbs::Runtime;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::expression::Expression;
@ -44,7 +44,7 @@ impl fmt::Display for Value {
impl dbs::Process for Value {
fn process(
&self,
ctx: &Parent,
ctx: &Runtime,
exe: &Executor,
doc: Option<&Document>,
) -> Result<Literal, Error> {