Skip to content

Commit 73f55fe

Browse files
authored
Merge pull request apache#36 from nickolay/strings
Clean up string-related variants in Token and Value
2 parents ff897b9 + 078eb67 commit 73f55fe

File tree

4 files changed

+11
-29
lines changed

4 files changed

+11
-29
lines changed

src/sqlast/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub enum ASTNode {
103103
/// COLUMNS
104104
columns: Vec<String>,
105105
/// VALUES a vector of values to be copied
106-
values: Vec<Value>,
106+
values: Vec<Option<String>>,
107107
},
108108
/// UPDATE
109109
SQLUpdate {
@@ -290,7 +290,7 @@ impl ToString for ASTNode {
290290
"\n{}",
291291
values
292292
.iter()
293-
.map(|v| v.to_string())
293+
.map(|v| v.clone().unwrap_or("\\N".to_string()))
294294
.collect::<Vec<String>>()
295295
.join("\t")
296296
);

src/sqlast/value.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@ use chrono::{offset::FixedOffset, DateTime, NaiveDate, NaiveDateTime, NaiveTime}
22

33
use uuid::Uuid;
44

5-
/// SQL values such as int, double, string timestamp
5+
/// SQL values such as int, double, string, timestamp
66
#[derive(Debug, Clone, PartialEq)]
77
pub enum Value {
88
/// Literal signed long
99
Long(i64),
1010
/// Literal floating point value
1111
Double(f64),
12-
/// Unquoted string
13-
String(String),
1412
/// Uuid value
1513
Uuid(Uuid),
1614
/// 'string value'
1715
SingleQuotedString(String),
18-
/// "string value"
19-
DoubleQuotedString(String),
2016
/// Boolean value true or false,
2117
Boolean(bool),
2218
/// Date value
@@ -36,10 +32,8 @@ impl ToString for Value {
3632
match self {
3733
Value::Long(v) => v.to_string(),
3834
Value::Double(v) => v.to_string(),
39-
Value::String(v) => v.to_string(),
4035
Value::Uuid(v) => v.to_string(),
4136
Value::SingleQuotedString(v) => format!("'{}'", v),
42-
Value::DoubleQuotedString(v) => format!("\"{}\"", v),
4337
Value::Boolean(v) => v.to_string(),
4438
Value::Date(v) => v.to_string(),
4539
Value::Time(v) => v.to_string(),

src/sqlparser.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,7 @@ impl Parser {
157157
}
158158
}
159159
}
160-
Token::Number(_)
161-
| Token::String(_)
162-
| Token::SingleQuotedString(_)
163-
| Token::DoubleQuotedString(_) => {
160+
Token::Number(_) | Token::SingleQuotedString(_) => {
164161
self.prev_token();
165162
self.parse_sql_value()
166163
}
@@ -693,7 +690,7 @@ impl Parser {
693690

694691
/// Parse a tab separated values in
695692
/// COPY payload
696-
fn parse_tsv(&mut self) -> Result<Vec<Value>, ParserError> {
693+
fn parse_tsv(&mut self) -> Result<Vec<Option<String>>, ParserError> {
697694
let values = self.parse_tab_value()?;
698695
Ok(values)
699696
}
@@ -702,17 +699,17 @@ impl Parser {
702699
Ok(ASTNode::SQLValue(self.parse_value()?))
703700
}
704701

705-
fn parse_tab_value(&mut self) -> Result<Vec<Value>, ParserError> {
702+
fn parse_tab_value(&mut self) -> Result<Vec<Option<String>>, ParserError> {
706703
let mut values = vec![];
707704
let mut content = String::from("");
708705
while let Some(t) = self.next_token_no_skip() {
709706
match t {
710707
Token::Whitespace(Whitespace::Tab) => {
711-
values.push(Value::String(content.to_string()));
708+
values.push(Some(content.to_string()));
712709
content.clear();
713710
}
714711
Token::Whitespace(Whitespace::Newline) => {
715-
values.push(Value::String(content.to_string()));
712+
values.push(Some(content.to_string()));
716713
content.clear();
717714
}
718715
Token::Backslash => {
@@ -721,7 +718,7 @@ impl Parser {
721718
}
722719
if let Some(token) = self.next_token() {
723720
if token == Token::Identifier("N".to_string()) {
724-
values.push(Value::Null);
721+
values.push(None);
725722
}
726723
} else {
727724
continue;
@@ -735,6 +732,7 @@ impl Parser {
735732
Ok(values)
736733
}
737734

735+
/// Parse a literal value (numbers, strings, date/time, booleans)
738736
fn parse_value(&mut self) -> Result<Value, ParserError> {
739737
match self.next_token() {
740738
Some(t) => {
@@ -754,14 +752,9 @@ impl Parser {
754752
Ok(n) => Ok(Value::Long(n)),
755753
Err(e) => parser_err!(format!("Could not parse '{}' as i64: {}", n, e)),
756754
},
757-
Token::Identifier(id) => Ok(Value::String(id.to_string())),
758-
Token::String(ref s) => Ok(Value::String(s.to_string())),
759755
Token::SingleQuotedString(ref s) => {
760756
Ok(Value::SingleQuotedString(s.to_string()))
761757
}
762-
Token::DoubleQuotedString(ref s) => {
763-
Ok(Value::DoubleQuotedString(s.to_string()))
764-
}
765758
_ => parser_err!(format!("Unsupported value: {:?}", self.peek_token())),
766759
}
767760
}
@@ -792,9 +785,7 @@ impl Parser {
792785
/// Parse a literal string
793786
pub fn parse_literal_string(&mut self) -> Result<String, ParserError> {
794787
match self.next_token() {
795-
Some(Token::String(ref s)) => Ok(s.clone()),
796788
Some(Token::SingleQuotedString(ref s)) => Ok(s.clone()),
797-
Some(Token::DoubleQuotedString(ref s)) => Ok(s.clone()),
798789
other => parser_err!(format!("Expected literal string, found {:?}", other)),
799790
}
800791
}

src/sqltokenizer.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ pub enum Token {
3232
Keyword(String),
3333
/// Numeric literal
3434
Number(String),
35-
/// String literal
36-
String(String),
35+
/// A character that could not be tokenized
3736
Char(char),
3837
/// Single quoted string: i.e: 'string'
3938
SingleQuotedString(String),
@@ -97,7 +96,6 @@ impl ToString for Token {
9796
Token::Identifier(ref id) => id.to_string(),
9897
Token::Keyword(ref k) => k.to_string(),
9998
Token::Number(ref n) => n.to_string(),
100-
Token::String(ref s) => s.to_string(),
10199
Token::Char(ref c) => c.to_string(),
102100
Token::SingleQuotedString(ref s) => format!("'{}'", s),
103101
Token::DoubleQuotedString(ref s) => format!("\"{}\"", s),
@@ -194,7 +192,6 @@ impl<'a> Tokenizer<'a> {
194192
Token::Identifier(s) => self.col += s.len() as u64,
195193
Token::Keyword(s) => self.col += s.len() as u64,
196194
Token::Number(s) => self.col += s.len() as u64,
197-
Token::String(s) => self.col += s.len() as u64,
198195
Token::SingleQuotedString(s) => self.col += s.len() as u64,
199196
Token::DoubleQuotedString(s) => self.col += s.len() as u64,
200197
_ => self.col += 1,

0 commit comments

Comments
 (0)