|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +// you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +#![warn(clippy::all)] |
| 14 | + |
| 15 | +//! Test SQL syntax specific to MySQL. The parser based on the generic dialect |
| 16 | +//! is also tested (on the inputs it can handle). |
| 17 | +
|
| 18 | +use sqlparser::ast::*; |
| 19 | +use sqlparser::dialect::{GenericDialect, MySqlDialect}; |
| 20 | +use sqlparser::test_utils::*; |
| 21 | + |
| 22 | +#[test] |
| 23 | +fn parse_identifiers() { |
| 24 | + mysql().verified_stmt("SELECT $a$, àà"); |
| 25 | +} |
| 26 | + |
| 27 | +#[test] |
| 28 | +fn parse_show_columns() { |
| 29 | + let table_name = ObjectName(vec!["mytable".to_string()]); |
| 30 | + assert_eq!( |
| 31 | + mysql_and_generic().verified_stmt("SHOW COLUMNS FROM mytable"), |
| 32 | + Statement::ShowColumns { |
| 33 | + extended: false, |
| 34 | + full: false, |
| 35 | + table_name: table_name.clone(), |
| 36 | + filter: None, |
| 37 | + } |
| 38 | + ); |
| 39 | + assert_eq!( |
| 40 | + mysql_and_generic().verified_stmt("SHOW COLUMNS FROM mydb.mytable"), |
| 41 | + Statement::ShowColumns { |
| 42 | + extended: false, |
| 43 | + full: false, |
| 44 | + table_name: ObjectName(vec!["mydb".to_string(), "mytable".to_string()]), |
| 45 | + filter: None, |
| 46 | + } |
| 47 | + ); |
| 48 | + assert_eq!( |
| 49 | + mysql_and_generic().verified_stmt("SHOW EXTENDED COLUMNS FROM mytable"), |
| 50 | + Statement::ShowColumns { |
| 51 | + extended: true, |
| 52 | + full: false, |
| 53 | + table_name: table_name.clone(), |
| 54 | + filter: None, |
| 55 | + } |
| 56 | + ); |
| 57 | + assert_eq!( |
| 58 | + mysql_and_generic().verified_stmt("SHOW FULL COLUMNS FROM mytable"), |
| 59 | + Statement::ShowColumns { |
| 60 | + extended: false, |
| 61 | + full: true, |
| 62 | + table_name: table_name.clone(), |
| 63 | + filter: None, |
| 64 | + } |
| 65 | + ); |
| 66 | + assert_eq!( |
| 67 | + mysql_and_generic().verified_stmt("SHOW COLUMNS FROM mytable LIKE 'pattern'"), |
| 68 | + Statement::ShowColumns { |
| 69 | + extended: false, |
| 70 | + full: false, |
| 71 | + table_name: table_name.clone(), |
| 72 | + filter: Some(ShowStatementFilter::Like("pattern".into())), |
| 73 | + } |
| 74 | + ); |
| 75 | + assert_eq!( |
| 76 | + mysql_and_generic().verified_stmt("SHOW COLUMNS FROM mytable WHERE 1 = 2"), |
| 77 | + Statement::ShowColumns { |
| 78 | + extended: false, |
| 79 | + full: false, |
| 80 | + table_name: table_name.clone(), |
| 81 | + filter: Some(ShowStatementFilter::Where( |
| 82 | + mysql_and_generic().verified_expr("1 = 2") |
| 83 | + )), |
| 84 | + } |
| 85 | + ); |
| 86 | + mysql_and_generic() |
| 87 | + .one_statement_parses_to("SHOW FIELDS FROM mytable", "SHOW COLUMNS FROM mytable"); |
| 88 | + mysql_and_generic() |
| 89 | + .one_statement_parses_to("SHOW COLUMNS IN mytable", "SHOW COLUMNS FROM mytable"); |
| 90 | + mysql_and_generic() |
| 91 | + .one_statement_parses_to("SHOW FIELDS IN mytable", "SHOW COLUMNS FROM mytable"); |
| 92 | + |
| 93 | + // unhandled things are truly unhandled |
| 94 | + match mysql_and_generic().parse_sql_statements("SHOW COLUMNS FROM mytable FROM mydb") { |
| 95 | + Err(_) => {} |
| 96 | + Ok(val) => panic!("unexpected successful parse: {:?}", val), |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +fn mysql() -> TestedDialects { |
| 101 | + TestedDialects { |
| 102 | + dialects: vec![Box::new(MySqlDialect {})], |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +fn mysql_and_generic() -> TestedDialects { |
| 107 | + TestedDialects { |
| 108 | + dialects: vec![Box::new(MySqlDialect {}), Box::new(GenericDialect {})], |
| 109 | + } |
| 110 | +} |
0 commit comments