Skip to content
This repository was archived by the owner on Dec 25, 2019. It is now read-only.

Commit 27c4248

Browse files
beneschJLDLaughlin
authored andcommitted
Use ShowStatementFilter in ShowObjects (#20)
Make ShowObjects and ShowColumns consistent in their use of ShowStatementFilter, which supports both a shorthand LIKE expression as well as a more general SQL expression. Also rustfmt.
1 parent b55f4c5 commit 27c4248

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

src/ast/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ pub enum Statement {
622622
/// ```
623623
ShowObjects {
624624
object_type: ObjectType,
625-
like: Option<String>,
625+
filter: Option<ShowStatementFilter>,
626626
},
627627
/// `SHOW COLUMNS`
628628
///
@@ -883,7 +883,10 @@ impl fmt::Display for Statement {
883883
write!(f, "{} = {}", variable, value)
884884
}
885885
Statement::ShowVariable { variable } => write!(f, "SHOW {}", variable),
886-
Statement::ShowObjects { object_type, like } => {
886+
Statement::ShowObjects {
887+
object_type,
888+
filter,
889+
} => {
887890
use ObjectType::*;
888891
write!(
889892
f,
@@ -896,8 +899,8 @@ impl fmt::Display for Statement {
896899
Index => unreachable!(),
897900
}
898901
)?;
899-
if let Some(like) = like {
900-
write!(f, " LIKE '{}'", value::escape_single_quote_string(like))?;
902+
if let Some(filter) = filter {
903+
write!(f, " {}", filter)?;
901904
}
902905
Ok(())
903906
}

src/ast/visit_macro.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,9 @@ macro_rules! make_visitor {
505505
visit_show_variable(self, variable)
506506
}
507507

508-
fn visit_show_objects(&mut self, _object_type: ObjectType, _like: &Option<String>) {}
508+
fn visit_show_objects(&mut self, object_type: ObjectType, filter: Option<&'ast $($mut)* ShowStatementFilter>) {
509+
visit_show_objects(self, object_type, filter)
510+
}
509511

510512
fn visit_show_columns(
511513
&mut self,
@@ -654,7 +656,9 @@ macro_rules! make_visitor {
654656
value,
655657
} => visitor.visit_set_variable(*local, variable, value),
656658
Statement::ShowVariable { variable } => visitor.visit_show_variable(variable),
657-
Statement::ShowObjects { object_type, like } => visitor.visit_show_objects(*object_type, like),
659+
Statement::ShowObjects { object_type, filter } => {
660+
visitor.visit_show_objects(*object_type, filter.as_auto_ref())
661+
}
658662
Statement::ShowColumns {
659663
extended,
660664
full,
@@ -1536,6 +1540,17 @@ macro_rules! make_visitor {
15361540
visitor.visit_ident(variable);
15371541
}
15381542

1543+
pub fn visit_show_objects<'ast, V: $name<'ast> + ?Sized>(
1544+
visitor: &mut V,
1545+
object_type: ObjectType,
1546+
filter: Option<&'ast $($mut)* ShowStatementFilter>
1547+
) {
1548+
visitor.visit_object_type(object_type);
1549+
if let Some(filter) = filter {
1550+
visitor.visit_show_statement_filter(filter);
1551+
}
1552+
}
1553+
15391554
pub fn visit_show_columns<'ast, V: $name<'ast> + ?Sized>(
15401555
visitor: &mut V,
15411556
_extended: bool,

src/parser.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,7 @@ impl Parser {
15481548
"TIMESTAMP" => {
15491549
if self.parse_keyword("WITH") {
15501550
self.expect_keywords(&["TIME", "ZONE"])?;
1551-
return Ok(DataType::TimestampTz)
1551+
return Ok(DataType::TimestampTz);
15521552
} else if self.parse_keyword("WITHOUT") {
15531553
self.expect_keywords(&["TIME", "ZONE"])?;
15541554
}
@@ -1557,7 +1557,7 @@ impl Parser {
15571557
"TIME" => {
15581558
if self.parse_keyword("WITH") {
15591559
self.expect_keywords(&["TIME", "ZONE"])?;
1560-
return Ok(DataType::TimeTz)
1560+
return Ok(DataType::TimeTz);
15611561
} else if self.parse_keyword("WITHOUT") {
15621562
self.expect_keywords(&["TIME", "ZONE"])?;
15631563
}
@@ -1940,7 +1940,7 @@ impl Parser {
19401940
val
19411941
),
19421942
},
1943-
like: self.parse_like_filter()?,
1943+
filter: self.parse_show_statement_filter()?,
19441944
})
19451945
} else if self.parse_keywords(vec!["CREATE", "VIEW"]) {
19461946
Ok(Statement::ShowCreateView {

tests/sqlparser_common.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,12 +1914,27 @@ fn parse_show_objects() {
19141914
verified_stmt(&sql),
19151915
Statement::ShowObjects {
19161916
object_type: *ot,
1917-
like: None
1917+
filter: None
19181918
}
19191919
)
19201920
}
19211921
}
19221922

1923+
#[test]
1924+
fn parse_show_objects_with_like_regex() {
1925+
let sql = "SHOW TABLES LIKE '%foo%'";
1926+
match verified_stmt(sql) {
1927+
Statement::ShowObjects {
1928+
object_type,
1929+
filter,
1930+
} => {
1931+
assert_eq!(filter.unwrap(), ShowStatementFilter::Like("%foo%".into()));
1932+
assert_eq!(ObjectType::Table, object_type);
1933+
}
1934+
_ => panic!("invalid SHOW OBJECTS statement"),
1935+
}
1936+
}
1937+
19231938
#[test]
19241939
fn parse_show_create_view() {
19251940
assert_eq!(
@@ -2747,18 +2762,6 @@ fn parse_create_sources_with_like_regex() {
27472762
}
27482763
}
27492764

2750-
#[test]
2751-
fn parse_show_objects_with_like_regex() {
2752-
let sql = "SHOW TABLES LIKE '%foo%'";
2753-
match verified_stmt(sql) {
2754-
Statement::ShowObjects { object_type, like } => {
2755-
assert_eq!(like.unwrap(), "%foo%");
2756-
assert_eq!(ObjectType::Table, object_type);
2757-
}
2758-
_ => panic!("invalid SHOW OBJECTS statement"),
2759-
}
2760-
}
2761-
27622765
#[test]
27632766
fn parse_create_sink() {
27642767
let sql = "CREATE SINK foo FROM bar INTO 'baz' WITH (name = 'val')";

0 commit comments

Comments
 (0)