2022-10-17 01:57:53 +00:00
|
|
|
use crate::ctx::Context;
|
2023-07-06 14:57:42 +00:00
|
|
|
use crate::dbs::{Options, Transaction};
|
|
|
|
use crate::doc::CursorDoc;
|
2022-10-17 01:57:53 +00:00
|
|
|
use crate::err::Error;
|
2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::comment::shouldbespace;
|
2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2023-04-25 10:13:04 +00:00
|
|
|
use crate::sql::number::Number;
|
2022-10-17 01:57:53 +00:00
|
|
|
use crate::sql::value::{value, Value};
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::bytes::complete::tag_no_case;
|
2023-08-29 10:50:00 +00:00
|
|
|
use nom::combinator::{cut, opt};
|
|
|
|
use nom::sequence::terminated;
|
2023-08-17 18:03:46 +00:00
|
|
|
use revision::revisioned;
|
2020-06-29 15:36:01 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
|
|
|
|
2022-10-27 12:23:24 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
|
2023-08-17 18:03:46 +00:00
|
|
|
#[revisioned(revision = 1)]
|
2022-10-17 01:57:53 +00:00
|
|
|
pub struct Start(pub Value);
|
|
|
|
|
|
|
|
impl Start {
|
2023-07-06 14:57:42 +00:00
|
|
|
pub(crate) async fn process(
|
|
|
|
&self,
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
opt: &Options,
|
|
|
|
txn: &Transaction,
|
|
|
|
doc: Option<&CursorDoc<'_>>,
|
|
|
|
) -> Result<usize, Error> {
|
|
|
|
match self.0.compute(ctx, opt, txn, doc).await {
|
2023-04-25 10:13:04 +00:00
|
|
|
// This is a valid starting number
|
|
|
|
Ok(Value::Number(Number::Int(v))) if v >= 0 => Ok(v as usize),
|
|
|
|
// An invalid value was specified
|
2022-10-17 01:57:53 +00:00
|
|
|
Ok(v) => Err(Error::InvalidStart {
|
|
|
|
value: v.as_string(),
|
|
|
|
}),
|
2023-04-25 10:13:04 +00:00
|
|
|
// A different error occured
|
2022-10-17 01:57:53 +00:00
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
|
|
|
|
impl fmt::Display for Start {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2022-01-13 17:36:41 +00:00
|
|
|
write!(f, "START {}", self.0)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn start(i: &str) -> IResult<&str, Start> {
|
|
|
|
let (i, _) = tag_no_case("START")(i)?;
|
|
|
|
let (i, _) = shouldbespace(i)?;
|
2023-08-29 10:50:00 +00:00
|
|
|
cut(|i| {
|
|
|
|
let (i, _) = opt(terminated(tag_no_case("AT"), shouldbespace))(i)?;
|
|
|
|
let (i, v) = value(i)?;
|
|
|
|
Ok((i, Start(v)))
|
|
|
|
})(i)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn start_statement() {
|
|
|
|
let sql = "START 100";
|
|
|
|
let res = start(sql);
|
|
|
|
let out = res.unwrap().1;
|
2022-10-17 01:57:53 +00:00
|
|
|
assert_eq!(out, Start(Value::from(100)));
|
2020-06-29 15:36:01 +00:00
|
|
|
assert_eq!("START 100", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn start_statement_at() {
|
|
|
|
let sql = "START AT 100";
|
|
|
|
let res = start(sql);
|
|
|
|
let out = res.unwrap().1;
|
2022-10-17 01:57:53 +00:00
|
|
|
assert_eq!(out, Start(Value::from(100)));
|
2020-06-29 15:36:01 +00:00
|
|
|
assert_eq!("START 100", format!("{}", out));
|
|
|
|
}
|
|
|
|
}
|