surrealpatch/lib/src/sql/limit.rs

52 lines
1.2 KiB
Rust
Raw Normal View History

2020-06-29 15:36:01 +00:00
use crate::sql::comment::shouldbespace;
2022-01-20 20:26:28 +00:00
use crate::sql::common::take_usize;
2022-01-16 20:31:50 +00:00
use crate::sql::error::IResult;
2020-06-29 15:36:01 +00:00
use nom::bytes::complete::tag_no_case;
use nom::combinator::opt;
use nom::sequence::tuple;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
2022-01-20 20:26:28 +00:00
pub struct Limit(pub usize);
2020-06-29 15:36:01 +00:00
impl fmt::Display for Limit {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "LIMIT {}", self.0)
2020-06-29 15:36:01 +00:00
}
}
pub fn limit(i: &str) -> IResult<&str, Limit> {
let (i, _) = tag_no_case("LIMIT")(i)?;
let (i, _) = opt(tuple((shouldbespace, tag_no_case("BY"))))(i)?;
let (i, _) = shouldbespace(i)?;
2022-01-20 20:26:28 +00:00
let (i, v) = take_usize(i)?;
Ok((i, Limit(v)))
2020-06-29 15:36:01 +00:00
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn limit_statement() {
let sql = "LIMIT 100";
let res = limit(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!(out, Limit(100));
2020-06-29 15:36:01 +00:00
assert_eq!("LIMIT 100", format!("{}", out));
}
#[test]
fn limit_statement_by() {
let sql = "LIMIT BY 100";
let res = limit(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!(out, Limit(100));
2020-06-29 15:36:01 +00:00
assert_eq!("LIMIT 100", format!("{}", out));
}
}