Skip to content

Fix qualified wildcard stringifying #53

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

Merged
merged 5 commits into from
Apr 27, 2019
Merged
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
2 changes: 1 addition & 1 deletion src/sqlast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl ToString for ASTNode {
match self {
ASTNode::SQLIdentifier(s) => s.to_string(),
ASTNode::SQLWildcard => "*".to_string(),
ASTNode::SQLQualifiedWildcard(q) => q.join(".") + "*",
ASTNode::SQLQualifiedWildcard(q) => q.join(".") + ".*",
ASTNode::SQLCompoundIdentifier(s) => s.join("."),
ASTNode::SQLIsNull(ast) => format!("{} IS NULL", ast.as_ref().to_string()),
ASTNode::SQLIsNotNull(ast) => format!("{} IS NOT NULL", ast.as_ref().to_string()),
Expand Down
14 changes: 13 additions & 1 deletion src/sqlast/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl ToString for Value {
Value::Long(v) => v.to_string(),
Value::Double(v) => v.to_string(),
Value::Uuid(v) => v.to_string(),
Value::SingleQuotedString(v) => format!("'{}'", v),
Value::SingleQuotedString(v) => format!("'{}'", escape_single_quote_string(v)),
Value::NationalStringLiteral(v) => format!("N'{}'", v),
Value::Boolean(v) => v.to_string(),
Value::Date(v) => v.to_string(),
Expand All @@ -46,3 +46,15 @@ impl ToString for Value {
}
}
}

fn escape_single_quote_string(s: &str) -> String {
let mut escaped = String::new();
for c in s.chars() {
if c == '\'' {
escaped.push_str("\'\'");
} else {
escaped.push(c);
}
}
escaped
}
8 changes: 7 additions & 1 deletion src/sqltokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,13 @@ impl<'a> Tokenizer<'a> {
match ch {
'\'' => {
chars.next(); // consume
break;
let escaped_quote = chars.peek().map(|c| *c == '\'').unwrap_or(false);
if escaped_quote {
s.push('\'');
chars.next();
} else {
break;
}
}
_ => {
chars.next(); // consume
Expand Down
26 changes: 26 additions & 0 deletions tests/sqlparser_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ fn parse_select_wildcard() {
);
}

#[test]
fn parse_count_wildcard() {
verified_only_select(
"SELECT COUNT(Employee.*) FROM Order JOIN Employee ON Order.employee = Employee.id",
);
}

#[test]
fn parse_column_aliases() {
let sql = "SELECT a.col + 1 AS newname FROM foo AS a";
Expand Down Expand Up @@ -148,6 +155,25 @@ fn parse_projection_nested_type() {
//TODO: add assertions
}

#[test]
fn parse_escaped_single_quote_string_predicate() {
use self::ASTNode::*;
use self::SQLOperator::*;
let sql = "SELECT id, fname, lname FROM customer \
WHERE salary != 'Jim''s salary'";
let ast = verified_only_select(sql);
assert_eq!(
Some(SQLBinaryExpr {
left: Box::new(SQLIdentifier("salary".to_string())),
op: NotEq,
right: Box::new(SQLValue(Value::SingleQuotedString(
"Jim's salary".to_string()
)))
}),
ast.selection,
);
}

#[test]
fn parse_compound_expr_1() {
use self::ASTNode::*;
Expand Down