Use usize for START and LIMIT clauses

This commit is contained in:
Tobie Morgan Hitchcock 2022-01-20 20:26:28 +00:00
parent f8848e36f5
commit e153c791f3
5 changed files with 20 additions and 10 deletions

View file

@ -208,11 +208,11 @@ impl<'a> Iterator<'a> {
if let Some(l) = self.limit { if let Some(l) = self.limit {
if let Some(s) = self.start { if let Some(s) = self.start {
if self.results.len() as u64 == l.0 + s.0 { if self.results.len() == l.0 + s.0 {
self.ok = false self.ok = false
} }
} else { } else {
if self.results.len() as u64 == l.0 { if self.results.len() == l.0 {
self.ok = false self.ok = false
} }
} }

View file

@ -41,6 +41,11 @@ pub fn to_u64(s: &str) -> u64 {
str::FromStr::from_str(s).unwrap() str::FromStr::from_str(s).unwrap()
} }
#[inline]
pub fn to_usize(s: &str) -> usize {
str::FromStr::from_str(s).unwrap()
}
#[inline] #[inline]
pub fn val_char(chr: char) -> bool { pub fn val_char(chr: char) -> bool {
is_alphanumeric(chr as u8) || chr == '_' as char is_alphanumeric(chr as u8) || chr == '_' as char
@ -66,6 +71,11 @@ pub fn take_u64(i: &str) -> IResult<&str, u64> {
Ok((i, v)) Ok((i, v))
} }
pub fn take_usize(i: &str) -> IResult<&str, usize> {
let (i, v) = map(take_while(is_digit), to_usize)(i)?;
Ok((i, v))
}
pub fn take_digits(i: &str, n: usize) -> IResult<&str, u32> { pub fn take_digits(i: &str, n: usize) -> IResult<&str, u32> {
let (i, v) = map(take_while_m_n(n, n, is_digit), to_u32)(i)?; let (i, v) = map(take_while_m_n(n, n, is_digit), to_u32)(i)?;
Ok((i, v)) Ok((i, v))

View file

@ -1,5 +1,5 @@
use crate::sql::comment::shouldbespace; use crate::sql::comment::shouldbespace;
use crate::sql::common::take_u64; use crate::sql::common::take_usize;
use crate::sql::error::IResult; use crate::sql::error::IResult;
use nom::bytes::complete::tag_no_case; use nom::bytes::complete::tag_no_case;
use nom::combinator::opt; use nom::combinator::opt;
@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] #[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Limit(pub u64); pub struct Limit(pub usize);
impl fmt::Display for Limit { impl fmt::Display for Limit {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -20,7 +20,7 @@ pub fn limit(i: &str) -> IResult<&str, Limit> {
let (i, _) = tag_no_case("LIMIT")(i)?; let (i, _) = tag_no_case("LIMIT")(i)?;
let (i, _) = opt(tuple((shouldbespace, tag_no_case("BY"))))(i)?; let (i, _) = opt(tuple((shouldbespace, tag_no_case("BY"))))(i)?;
let (i, _) = shouldbespace(i)?; let (i, _) = shouldbespace(i)?;
let (i, v) = take_u64(i)?; let (i, v) = take_usize(i)?;
Ok((i, Limit(v))) Ok((i, Limit(v)))
} }

View file

@ -1,5 +1,5 @@
use crate::sql::comment::shouldbespace; use crate::sql::comment::shouldbespace;
use crate::sql::common::take_u64; use crate::sql::common::take_usize;
use crate::sql::error::IResult; use crate::sql::error::IResult;
use nom::bytes::complete::tag_no_case; use nom::bytes::complete::tag_no_case;
use nom::combinator::opt; use nom::combinator::opt;
@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct Start(pub u64); pub struct Start(pub usize);
impl fmt::Display for Start { impl fmt::Display for Start {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -20,7 +20,7 @@ pub fn start(i: &str) -> IResult<&str, Start> {
let (i, _) = tag_no_case("START")(i)?; let (i, _) = tag_no_case("START")(i)?;
let (i, _) = opt(tuple((shouldbespace, tag_no_case("AT"))))(i)?; let (i, _) = opt(tuple((shouldbespace, tag_no_case("AT"))))(i)?;
let (i, _) = shouldbespace(i)?; let (i, _) = shouldbespace(i)?;
let (i, v) = take_u64(i)?; let (i, v) = take_usize(i)?;
Ok((i, Start(v))) Ok((i, Start(v)))
} }

View file

@ -48,13 +48,13 @@ pub struct SelectStatement {
} }
impl SelectStatement { impl SelectStatement {
pub fn limit(&self) -> u64 { pub fn limit(&self) -> usize {
match self.limit { match self.limit {
Some(Limit(v)) => v, Some(Limit(v)) => v,
None => 0, None => 0,
} }
} }
pub fn start(&self) -> u64 { pub fn start(&self) -> usize {
match self.start { match self.start {
Some(Start(v)) => v, Some(Start(v)) => v,
None => 0, None => 0,