Support MySQL SHOW COLUMNS statement

Co-authored-by: Nikhil Benesch <nikhil.benesch@gmail.com>
This commit is contained in:
Brandon W Maister 2019-07-31 11:21:12 -04:00 committed by Nikhil Benesch
parent 35a20091ea
commit f64928e994
No known key found for this signature in database
GPG key ID: FCF98542083C5A69
7 changed files with 248 additions and 3 deletions

110
tests/sqlparser_mysql.rs Normal file
View file

@ -0,0 +1,110 @@
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![warn(clippy::all)]
//! Test SQL syntax specific to MySQL. The parser based on the generic dialect
//! is also tested (on the inputs it can handle).
use sqlparser::ast::*;
use sqlparser::dialect::{GenericDialect, MySqlDialect};
use sqlparser::test_utils::*;
#[test]
fn parse_identifiers() {
mysql().verified_stmt("SELECT $a$, àà");
}
#[test]
fn parse_show_columns() {
let table_name = ObjectName(vec!["mytable".to_string()]);
assert_eq!(
mysql_and_generic().verified_stmt("SHOW COLUMNS FROM mytable"),
Statement::ShowColumns {
extended: false,
full: false,
table_name: table_name.clone(),
filter: None,
}
);
assert_eq!(
mysql_and_generic().verified_stmt("SHOW COLUMNS FROM mydb.mytable"),
Statement::ShowColumns {
extended: false,
full: false,
table_name: ObjectName(vec!["mydb".to_string(), "mytable".to_string()]),
filter: None,
}
);
assert_eq!(
mysql_and_generic().verified_stmt("SHOW EXTENDED COLUMNS FROM mytable"),
Statement::ShowColumns {
extended: true,
full: false,
table_name: table_name.clone(),
filter: None,
}
);
assert_eq!(
mysql_and_generic().verified_stmt("SHOW FULL COLUMNS FROM mytable"),
Statement::ShowColumns {
extended: false,
full: true,
table_name: table_name.clone(),
filter: None,
}
);
assert_eq!(
mysql_and_generic().verified_stmt("SHOW COLUMNS FROM mytable LIKE 'pattern'"),
Statement::ShowColumns {
extended: false,
full: false,
table_name: table_name.clone(),
filter: Some(ShowStatementFilter::Like("pattern".into())),
}
);
assert_eq!(
mysql_and_generic().verified_stmt("SHOW COLUMNS FROM mytable WHERE 1 = 2"),
Statement::ShowColumns {
extended: false,
full: false,
table_name: table_name.clone(),
filter: Some(ShowStatementFilter::Where(
mysql_and_generic().verified_expr("1 = 2")
)),
}
);
mysql_and_generic()
.one_statement_parses_to("SHOW FIELDS FROM mytable", "SHOW COLUMNS FROM mytable");
mysql_and_generic()
.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),
}
}
fn mysql() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(MySqlDialect {})],
}
}
fn mysql_and_generic() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(MySqlDialect {}), Box::new(GenericDialect {})],
}
}