Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support placeholders ($ or ?) in LIMIT clause #494

Merged
merged 5 commits into from May 25, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/parser.rs
Expand Up @@ -2563,6 +2563,7 @@ impl<'a> Parser<'a> {
pub fn parse_number_value(&mut self) -> Result<Value, ParserError> {
match self.parse_value()? {
v @ Value::Number(_, _) => Ok(v),
v @ Value::Placeholder(_) => Ok(v),
_ => {
self.prev_token();
self.expected("literal number", self.peek_token())
Expand Down Expand Up @@ -4299,7 +4300,7 @@ impl Word {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::all_dialects;
use crate::test_utils::{all_dialects, TestedDialects};

#[test]
fn test_prev_index() {
Expand All @@ -4321,4 +4322,40 @@ mod tests {
parser.prev_token();
});
}

#[test]
fn test_parse_limit() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like these tests 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

let sql = "SELECT * FROM user LIMIT 1";
all_dialects().run_parser_method(sql, |parser| {
let ast = parser.parse_query().unwrap();
assert_eq!(ast.to_string(), sql.to_string());
});

let sql = "SELECT * FROM user LIMIT $1 OFFSET $2";
let dialects = TestedDialects {
dialects: vec![
Box::new(PostgreSqlDialect {}),
Box::new(ClickHouseDialect {}),
Box::new(GenericDialect {}),
Box::new(MsSqlDialect {}),
Box::new(SnowflakeDialect {}),
],
};

dialects.run_parser_method(sql, |parser| {
let ast = parser.parse_query().unwrap();
assert_eq!(ast.to_string(), sql.to_string());
});

let sql = "SELECT * FROM user LIMIT ? OFFSET ?";
let dialects = TestedDialects {
dialects: vec![
Box::new(MySqlDialect {}),
],
};
dialects.run_parser_method(sql, |parser| {
let ast = parser.parse_query().unwrap();
assert_eq!(ast.to_string(), sql.to_string());
});
}
}