2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2021-03-29 15:43:37 +00:00
|
|
|
use crate::dbs::Iterator;
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::dbs::Level;
|
|
|
|
use crate::dbs::Options;
|
2022-05-13 20:46:56 +00:00
|
|
|
use crate::dbs::Statement;
|
2022-02-15 01:00:30 +00:00
|
|
|
use crate::dbs::Transaction;
|
2021-03-29 15:43:37 +00:00
|
|
|
use crate::err::Error;
|
2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::comment::shouldbespace;
|
|
|
|
use crate::sql::cond::{cond, Cond};
|
|
|
|
use crate::sql::data::{data, Data};
|
2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::output::{output, Output};
|
|
|
|
use crate::sql::timeout::{timeout, Timeout};
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::value::{whats, Value, Values};
|
2022-02-26 00:34:05 +00:00
|
|
|
use derive::Store;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::bytes::complete::tag_no_case;
|
|
|
|
use nom::combinator::opt;
|
|
|
|
use nom::sequence::preceded;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
|
|
|
|
2022-02-26 00:34:05 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize, Store)]
|
2020-06-29 15:36:01 +00:00
|
|
|
pub struct UpdateStatement {
|
2022-01-13 17:36:41 +00:00
|
|
|
pub what: Values,
|
2020-06-29 15:36:01 +00:00
|
|
|
pub data: Option<Data>,
|
|
|
|
pub cond: Option<Cond>,
|
|
|
|
pub output: Option<Output>,
|
|
|
|
pub timeout: Option<Timeout>,
|
2022-02-26 16:53:38 +00:00
|
|
|
pub parallel: bool,
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 08:12:56 +00:00
|
|
|
impl UpdateStatement {
|
2022-05-01 22:25:53 +00:00
|
|
|
pub(crate) async fn compute(
|
2022-05-13 20:46:56 +00:00
|
|
|
&self,
|
2022-05-14 12:35:08 +00:00
|
|
|
ctx: &Context<'_>,
|
2022-02-06 21:06:52 +00:00
|
|
|
opt: &Options,
|
2022-02-15 03:33:16 +00:00
|
|
|
txn: &Transaction,
|
2022-01-13 17:36:41 +00:00
|
|
|
doc: Option<&Value>,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
// Allowed to run?
|
2022-02-06 21:06:52 +00:00
|
|
|
opt.check(Level::No)?;
|
2021-03-29 15:43:37 +00:00
|
|
|
// Create a new iterator
|
2022-05-13 20:46:56 +00:00
|
|
|
let mut i = Iterator::new();
|
2022-01-13 17:36:41 +00:00
|
|
|
// Ensure futures are stored
|
|
|
|
let opt = &opt.futures(false);
|
|
|
|
// Loop over the update targets
|
|
|
|
for w in self.what.0.iter() {
|
2022-02-15 01:00:30 +00:00
|
|
|
let v = w.compute(ctx, opt, txn, doc).await?;
|
2022-02-06 01:14:56 +00:00
|
|
|
match v {
|
|
|
|
Value::Table(_) => i.prepare(v),
|
|
|
|
Value::Thing(_) => i.prepare(v),
|
|
|
|
Value::Model(_) => i.prepare(v),
|
|
|
|
Value::Array(_) => i.prepare(v),
|
2022-01-13 17:36:41 +00:00
|
|
|
v => {
|
2022-03-06 10:58:59 +00:00
|
|
|
return Err(Error::UpdateStatement {
|
2022-05-06 22:09:08 +00:00
|
|
|
value: v.to_string(),
|
2022-01-13 17:36:41 +00:00
|
|
|
})
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2022-05-13 20:46:56 +00:00
|
|
|
// Assign the statement
|
|
|
|
let stm = Statement::from(self);
|
2021-03-29 15:43:37 +00:00
|
|
|
// Output the results
|
2022-05-13 20:46:56 +00:00
|
|
|
i.output(ctx, opt, txn, &stm).await
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 08:12:56 +00:00
|
|
|
impl fmt::Display for UpdateStatement {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "UPDATE {}", self.what)?;
|
|
|
|
if let Some(ref v) = self.data {
|
|
|
|
write!(f, " {}", v)?
|
|
|
|
}
|
|
|
|
if let Some(ref v) = self.cond {
|
|
|
|
write!(f, " {}", v)?
|
|
|
|
}
|
|
|
|
if let Some(ref v) = self.output {
|
|
|
|
write!(f, " {}", v)?
|
|
|
|
}
|
|
|
|
if let Some(ref v) = self.timeout {
|
|
|
|
write!(f, " {}", v)?
|
|
|
|
}
|
2022-02-26 16:53:38 +00:00
|
|
|
if self.parallel {
|
|
|
|
write!(f, " PARALLEL")?
|
|
|
|
}
|
2022-01-14 08:12:56 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
pub fn update(i: &str) -> IResult<&str, UpdateStatement> {
|
|
|
|
let (i, _) = tag_no_case("UPDATE")(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
|
|
|
let (i, what) = whats(i)?;
|
|
|
|
let (i, data) = opt(preceded(shouldbespace, data))(i)?;
|
|
|
|
let (i, cond) = opt(preceded(shouldbespace, cond))(i)?;
|
|
|
|
let (i, output) = opt(preceded(shouldbespace, output))(i)?;
|
|
|
|
let (i, timeout) = opt(preceded(shouldbespace, timeout))(i)?;
|
2022-02-26 16:53:38 +00:00
|
|
|
let (i, parallel) = opt(preceded(shouldbespace, tag_no_case("PARALLEL")))(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
UpdateStatement {
|
|
|
|
what,
|
|
|
|
data,
|
|
|
|
cond,
|
|
|
|
output,
|
|
|
|
timeout,
|
2022-02-26 16:53:38 +00:00
|
|
|
parallel: parallel.is_some(),
|
2020-06-29 15:36:01 +00:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn update_statement() {
|
|
|
|
let sql = "UPDATE test";
|
|
|
|
let res = update(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("UPDATE test", format!("{}", out))
|
|
|
|
}
|
|
|
|
}
|