Skip to content

Commit 0b4ea72

Browse files
committed
fix #1
1 parent 2337856 commit 0b4ea72

14 files changed

+612
-433
lines changed

Diff for: src/base/algorithm_type.rs

+29-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use nom::branch::alt;
22
use nom::bytes::complete::{tag, tag_no_case};
3-
use nom::character::complete::multispace0;
3+
use nom::character::complete::{multispace0, multispace1};
44
use nom::combinator::{map, opt};
55
use nom::sequence::tuple;
66
use nom::IResult;
@@ -21,21 +21,31 @@ impl AlgorithmType {
2121
/// algorithm_option:
2222
/// ALGORITHM [=] {DEFAULT | INSTANT | INPLACE | COPY}
2323
pub fn parse(i: &str) -> IResult<&str, AlgorithmType, ParseSQLError<&str>> {
24-
map(
25-
tuple((
26-
tag_no_case("ALGORITHM"),
27-
multispace0,
28-
opt(tag("=")),
29-
multispace0,
30-
alt((
31-
map(tag_no_case("DEFAULT"), |_| AlgorithmType::Default),
32-
map(tag_no_case("INSTANT"), |_| AlgorithmType::Instant),
33-
map(tag_no_case("INPLACE"), |_| AlgorithmType::Inplace),
34-
map(tag_no_case("COPY"), |_| AlgorithmType::Copy),
24+
alt((
25+
map(
26+
tuple((tag_no_case("ALGORITHM"), multispace1, Self::parse_algorithm)),
27+
|(_, _, algorithm)| algorithm,
28+
),
29+
map(
30+
tuple((
31+
tag_no_case("ALGORITHM"),
32+
multispace0,
33+
tag("="),
34+
multispace0,
35+
Self::parse_algorithm,
3536
)),
36-
)),
37-
|x| x.4,
38-
)(i)
37+
|(_, _, _, _, algorithm)| algorithm,
38+
),
39+
))(i)
40+
}
41+
42+
fn parse_algorithm(i: &str) -> IResult<&str, AlgorithmType, ParseSQLError<&str>> {
43+
alt((
44+
map(tag_no_case("DEFAULT"), |_| AlgorithmType::Default),
45+
map(tag_no_case("INSTANT"), |_| AlgorithmType::Instant),
46+
map(tag_no_case("INPLACE"), |_| AlgorithmType::Inplace),
47+
map(tag_no_case("COPY"), |_| AlgorithmType::Copy),
48+
))(i)
3949
}
4050
}
4151

@@ -80,5 +90,9 @@ mod tests {
8090
let res5 = AlgorithmType::parse(str5);
8191
assert!(res5.is_ok());
8292
assert_eq!(res5.unwrap().1, AlgorithmType::Default);
93+
94+
let str6 = "ALGORITHMDEFAULT";
95+
let res6 = AlgorithmType::parse(str6);
96+
assert!(res6.is_err());
8397
}
8498
}

Diff for: src/base/common_parser.rs

+112-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use nom::sequence::{delimited, pair, preceded, terminated, tuple};
1010
use nom::{IResult, InputLength, Parser};
1111

1212
use base::column::Column;
13-
use base::{OrderType, ParseSQLError};
13+
use base::{DefaultOrZeroOrOne, OrderType, ParseSQLError};
1414

1515
/// collection of common used parsers
1616
pub struct CommonParser;
@@ -360,6 +360,117 @@ impl CommonParser {
360360
multispace0,
361361
))(i)
362362
}
363+
364+
/// extract String quoted by `'` or `"`
365+
pub fn parse_quoted_string(i: &str) -> IResult<&str, String, ParseSQLError<&str>> {
366+
alt((
367+
map(delimited(tag("'"), take_until("'"), tag("'")), String::from),
368+
map(
369+
delimited(tag("\""), take_until("\""), tag("\"")),
370+
String::from,
371+
),
372+
))(i)
373+
}
374+
375+
/// extract value from `key [=] 'value'` or `key [=] "value"`
376+
pub fn parse_quoted_string_value_with_key(
377+
i: &str,
378+
key: String,
379+
) -> IResult<&str, String, ParseSQLError<&str>> {
380+
alt((
381+
map(
382+
tuple((
383+
tag_no_case(key.as_str()),
384+
multispace1,
385+
CommonParser::parse_quoted_string,
386+
)),
387+
|(_, _, value)| value,
388+
),
389+
map(
390+
tuple((
391+
tag_no_case(key.as_str()),
392+
multispace0,
393+
tag("="),
394+
multispace0,
395+
CommonParser::parse_quoted_string,
396+
)),
397+
|(_, _, _, _, value)| value,
398+
),
399+
))(i)
400+
}
401+
402+
/// extract value from `key [=] value`
403+
pub fn parse_string_value_with_key(
404+
i: &str,
405+
key: String,
406+
) -> IResult<&str, String, ParseSQLError<&str>> {
407+
alt((
408+
map(
409+
tuple((tag_no_case(key.as_str()), multispace1, Self::sql_identifier)),
410+
|(_, _, value)| String::from(value),
411+
),
412+
map(
413+
tuple((
414+
tag_no_case(key.as_str()),
415+
multispace0,
416+
tag("="),
417+
multispace0,
418+
Self::sql_identifier,
419+
)),
420+
|(_, _, _, _, value)| String::from(value),
421+
),
422+
))(i)
423+
}
424+
425+
/// extract value from `key [=] value`
426+
pub fn parse_digit_value_with_key(
427+
i: &str,
428+
key: String,
429+
) -> IResult<&str, String, ParseSQLError<&str>> {
430+
alt((
431+
map(
432+
tuple((tag_no_case(key.as_str()), multispace1, Self::sql_identifier)),
433+
|(_, _, value)| String::from(value),
434+
),
435+
map(
436+
tuple((
437+
tag_no_case(key.as_str()),
438+
multispace0,
439+
tag("="),
440+
multispace0,
441+
digit1,
442+
)),
443+
|(_, _, _, _, value)| String::from(value),
444+
),
445+
))(i)
446+
}
447+
448+
/// extract value from `key [=] {DEFAULT | 0 | 1}`
449+
pub fn parse_default_value_with_key(
450+
i: &str,
451+
key: String,
452+
) -> IResult<&str, DefaultOrZeroOrOne, ParseSQLError<&str>> {
453+
alt((
454+
map(
455+
tuple((
456+
tag_no_case(key.as_str()),
457+
multispace1,
458+
DefaultOrZeroOrOne::parse,
459+
)),
460+
|(_, _, value)| value,
461+
),
462+
map(
463+
tuple((
464+
tag_no_case(key.as_str()),
465+
multispace0,
466+
tag("="),
467+
multispace0,
468+
DefaultOrZeroOrOne::parse,
469+
)),
470+
|(_, _, _, _, value)| value,
471+
),
472+
))(i)
473+
}
363474
}
364475

365476
#[cfg(test)]

Diff for: src/base/compression_type.rs

+36-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use nom::branch::alt;
22
use nom::bytes::complete::{tag, tag_no_case};
3-
use nom::character::complete::multispace0;
3+
use nom::character::complete::{multispace0, multispace1};
44
use nom::combinator::{map, opt};
55
use nom::sequence::{delimited, tuple};
66
use nom::IResult;
@@ -28,28 +28,42 @@ impl Display for CompressionType {
2828

2929
impl CompressionType {
3030
pub fn parse(i: &str) -> IResult<&str, CompressionType, ParseSQLError<&str>> {
31-
map(
32-
tuple((
33-
tag_no_case("COMPRESSION"),
34-
multispace0,
35-
opt(tag("=")),
36-
multispace0,
37-
alt((
38-
map(
39-
alt((tag_no_case("'ZLIB'"), tag_no_case("\"ZLIB\""))),
40-
|_| CompressionType::ZLIB,
41-
),
42-
map(alt((tag_no_case("'LZ4'"), tag_no_case("\"LZ4\""))), |_| {
43-
CompressionType::LZ4
44-
}),
45-
map(
46-
alt((tag_no_case("'NONE'"), tag_no_case("\"NONE\""))),
47-
|_| CompressionType::NONE,
48-
),
31+
alt((
32+
map(
33+
tuple((
34+
tag_no_case("COMPRESSION"),
35+
multispace1,
36+
Self::parse_compression,
4937
)),
50-
)),
51-
|(_, _, _, _, compression_type)| compression_type,
52-
)(i)
38+
|(_, _, compression_type)| compression_type,
39+
),
40+
map(
41+
tuple((
42+
tag_no_case("COMPRESSION"),
43+
multispace0,
44+
tag("="),
45+
multispace0,
46+
Self::parse_compression,
47+
)),
48+
|(_, _, _, _, compression_type)| compression_type,
49+
),
50+
))(i)
51+
}
52+
53+
fn parse_compression(i: &str) -> IResult<&str, CompressionType, ParseSQLError<&str>> {
54+
alt((
55+
map(
56+
alt((tag_no_case("'ZLIB'"), tag_no_case("\"ZLIB\""))),
57+
|_| CompressionType::ZLIB,
58+
),
59+
map(alt((tag_no_case("'LZ4'"), tag_no_case("\"LZ4\""))), |_| {
60+
CompressionType::LZ4
61+
}),
62+
map(
63+
alt((tag_no_case("'NONE'"), tag_no_case("\"NONE\""))),
64+
|_| CompressionType::NONE,
65+
),
66+
))(i)
5367
}
5468
}
5569

Diff for: src/base/index_option.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,7 @@ impl IndexOption {
140140

141141
/// SECONDARY_ENGINE_ATTRIBUTE [=] value
142142
fn secondary_engine_attribute(i: &str) -> IResult<&str, String, ParseSQLError<&str>> {
143-
map(
144-
tuple((
145-
tag_no_case("SECONDARY_ENGINE_ATTRIBUTE"),
146-
multispace0,
147-
opt(tag("=")),
148-
map(delimited(tag("'"), take_until("'"), tag("'")), |x| {
149-
String::from(x)
150-
}),
151-
multispace0,
152-
)),
153-
|(_, _, _, engine, _)| engine,
154-
)(i)
143+
CommonParser::parse_string_value_with_key(i, "SECONDARY_ENGINE_ATTRIBUTE".to_string())
155144
}
156145
}
157146

Diff for: src/base/insert_method_type.rs

+29-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use nom::branch::alt;
2-
use nom::bytes::complete::tag_no_case;
3-
use nom::character::complete::multispace0;
2+
use nom::bytes::complete::{tag, tag_no_case};
3+
use nom::character::complete::{multispace0, multispace1};
44
use nom::combinator::{map, opt};
55
use nom::sequence::tuple;
6-
use nom::streaming::tag;
76
use nom::IResult;
87
use std::fmt::{Display, Formatter};
98

@@ -29,20 +28,34 @@ impl Display for InsertMethodType {
2928

3029
impl InsertMethodType {
3130
pub fn parse(i: &str) -> IResult<&str, InsertMethodType, ParseSQLError<&str>> {
32-
map(
33-
tuple((
34-
tag_no_case("INSERT_METHOD"),
35-
multispace0,
36-
opt(tag_no_case("=")),
37-
multispace0,
38-
alt((
39-
map(tag_no_case("NO"), |_| InsertMethodType::No),
40-
map(tag_no_case("FIRST"), |_| InsertMethodType::First),
41-
map(tag_no_case("LAST"), |_| InsertMethodType::Last),
31+
alt((
32+
map(
33+
tuple((
34+
tag_no_case("INSERT_METHOD"),
35+
multispace1,
36+
Self::parse_method,
4237
)),
43-
)),
44-
|(_, _, _, _, inert_method_type)| inert_method_type,
45-
)(i)
38+
|(_, _, inert_method_type)| inert_method_type,
39+
),
40+
map(
41+
tuple((
42+
tag_no_case("INSERT_METHOD"),
43+
multispace0,
44+
tag("="),
45+
multispace0,
46+
Self::parse_method,
47+
)),
48+
|(_, _, _, _, inert_method_type)| inert_method_type,
49+
),
50+
))(i)
51+
}
52+
53+
fn parse_method(i: &str) -> IResult<&str, InsertMethodType, ParseSQLError<&str>> {
54+
alt((
55+
map(tag_no_case("NO"), |_| InsertMethodType::No),
56+
map(tag_no_case("FIRST"), |_| InsertMethodType::First),
57+
map(tag_no_case("LAST"), |_| InsertMethodType::Last),
58+
))(i)
4659
}
4760
}
4861

0 commit comments

Comments
 (0)