Skip to content

feat: Support SHOW COLUMNS FROM tbl FROM db #8

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 2 commits into from
Jan 26, 2022
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
21 changes: 17 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2740,14 +2740,27 @@ impl<'a> Parser<'a> {
self.expect_one_of_keywords(&[Keyword::COLUMNS, Keyword::FIELDS])?;
self.expect_one_of_keywords(&[Keyword::FROM, Keyword::IN])?;
let table_name = self.parse_object_name()?;
// MySQL also supports FROM <database> here. In other words, MySQL
// allows both FROM <table> FROM <database> and FROM <database>.<table>,
// while we only support the latter for now.
let double_from = self.parse_one_of_keywords(&[Keyword::FROM, Keyword::IN]);
let db_name = if double_from.is_some() {
Some(self.parse_object_name()?)
} else {
None
};
let object_name = match db_name {
Some(db_name) => ObjectName(
db_name
.0
.into_iter()
.chain(table_name.0.into_iter())
.collect(),
),
None => table_name,
};
let filter = self.parse_show_statement_filter()?;
Ok(Statement::ShowColumns {
extended,
full,
table_name,
table_name: object_name,
filter,
})
}
Expand Down
10 changes: 4 additions & 6 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,10 @@ fn parse_show_columns() {
.one_statement_parses_to("SHOW COLUMNS IN mytable", "SHOW COLUMNS FROM mytable");
mysql_and_generic()
.one_statement_parses_to("SHOW FIELDS IN mytable", "SHOW COLUMNS FROM mytable");

// unhandled things are truly unhandled
match mysql_and_generic().parse_sql_statements("SHOW COLUMNS FROM mytable FROM mydb") {
Err(_) => {}
Ok(val) => panic!("unexpected successful parse: {:?}", val),
}
mysql_and_generic().one_statement_parses_to(
"SHOW COLUMNS FROM mytable FROM mydb",
"SHOW COLUMNS FROM mydb.mytable",
);
}

#[test]
Expand Down