Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/parse_select_prepare.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
extern crate sqlparser;

use sqlparser::dialect::PostgreSqlDialect;
use sqlparser::sqlparser::*;

fn main() {

let sql = "INSERT INTO users \
(id, type) \
VALUES \
($1, $2)";
let dialect = PostgreSqlDialect {};

let ast = Parser::parse_sql(&dialect, sql.to_string()).unwrap();

println!("AST: {:?}", ast);
}
4 changes: 4 additions & 0 deletions src/dialect/ansi_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ impl Dialect for AnsiSqlDialect {
|| (ch >= '0' && ch <= '9')
|| ch == '_'
}

fn is_identifier_start_prepare(&self, ch: char) -> bool {
ch == '$'
}
}
4 changes: 4 additions & 0 deletions src/dialect/generic_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ impl Dialect for GenericSqlDialect {
|| ch == '@'
|| ch == '_'
}

fn is_identifier_start_prepare(&self, ch: char) -> bool {
ch == '$'
}
}
3 changes: 3 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ pub trait Dialect {
fn is_identifier_start(&self, ch: char) -> bool;
/// Determine if a character is a valid unquoted identifier character
fn is_identifier_part(&self, ch: char) -> bool;
/// Determine whether the character is a valid starting character for the identifier
/// without quotes in the prepared query
fn is_identifier_start_prepare(&self, ch: char) -> bool;
}
4 changes: 4 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ impl Dialect for PostgreSqlDialect {
|| ch == '@'
|| ch == '_'
}

fn is_identifier_start_prepare(&self, ch: char) -> bool {
ch == '$'
}
}
4 changes: 4 additions & 0 deletions src/sqlparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ impl Parser {
self.expect_token(&Token::RParen)?;
Ok(expr)
}
Token::Prepare(p)
=> {
Ok(ASTNode::SQLValue(Value::SingleQuotedString(p.to_string())))
}
_ => parser_err!(format!(
"Did not expect {:?} at the beginning of an expression",
t
Expand Down
21 changes: 20 additions & 1 deletion src/sqltokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ pub enum Token {
LBrace,
/// Right brace `}`
RBrace,
/// Prepare literal `$`
Prepare(String),
}

impl ToString for Token {
Expand Down Expand Up @@ -122,6 +124,7 @@ impl ToString for Token {
Token::Ampersand => "&".to_string(),
Token::LBrace => "{".to_string(),
Token::RBrace => "}".to_string(),
Token::Prepare(ref p) => p.to_string(),
}
}
}
Expand Down Expand Up @@ -206,6 +209,17 @@ impl ToString for Whitespace {
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct Prepare(String);

impl ToString for Prepare {
fn to_string(&self) -> String {
match self {
Prepare(s) => format!("{}", s),
}
}
}

/// Tokenizer error
#[derive(Debug, PartialEq)]
pub struct TokenizerError(String);
Expand Down Expand Up @@ -241,7 +255,7 @@ impl<'a> Tokenizer<'a> {
self.line += 1;
self.col = 1;
}

Token::Prepare(p) => self.col += p.len() as u64,
Token::Whitespace(Whitespace::Tab) => self.col += 4,
Token::SQLWord(w) if w.quote_style == None => self.col += w.value.len() as u64,
Token::SQLWord(w) if w.quote_style != None => self.col += w.value.len() as u64 + 2,
Expand Down Expand Up @@ -293,6 +307,11 @@ impl<'a> Tokenizer<'a> {
let s = self.tokenize_word(ch, chars);
Ok(Some(Token::make_word(&s, None)))
}
ch if self.dialect.is_identifier_start_prepare(ch) => {
chars.next(); // consume the first char
let s = self.tokenize_word(ch, chars);
Ok(Some(Token::Prepare(s)))
}
// string
'\'' => {
let s = self.tokenize_single_quoted_string(chars);
Expand Down