Skip to content
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

Support USE db #565

Merged
merged 1 commit into from Aug 11, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/ast/mod.rs
Expand Up @@ -1012,6 +1012,10 @@ pub enum Statement {
table_name: ObjectName,
filter: Option<ShowStatementFilter>,
},
/// USE
///
/// Note: This is a MySQL-specific statement.
Use { db_name: Ident },
/// `{ BEGIN [ TRANSACTION | WORK ] | START TRANSACTION } ...`
StartTransaction { modes: Vec<TransactionMode> },
/// `SET TRANSACTION ...`
Expand Down Expand Up @@ -1814,6 +1818,10 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::Use { db_name } => {
write!(f, "USE {}", db_name)?;
Ok(())
}
Statement::StartTransaction { modes } => {
write!(f, "START TRANSACTION")?;
if !modes.is_empty() {
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Expand Up @@ -535,6 +535,7 @@ define_keywords!(
UPDATE,
UPPER,
USAGE,
USE,
USER,
USING,
UUID,
Expand Down
6 changes: 6 additions & 0 deletions src/parser.rs
Expand Up @@ -177,6 +177,7 @@ impl<'a> Parser<'a> {
Keyword::CLOSE => Ok(self.parse_close()?),
Keyword::SET => Ok(self.parse_set()?),
Keyword::SHOW => Ok(self.parse_show()?),
Keyword::USE => Ok(self.parse_use()?),
Keyword::GRANT => Ok(self.parse_grant()?),
Keyword::REVOKE => Ok(self.parse_revoke()?),
Keyword::START => Ok(self.parse_start_transaction()?),
Expand Down Expand Up @@ -3763,6 +3764,11 @@ impl<'a> Parser<'a> {
}
}

pub fn parse_use(&mut self) -> Result<Statement, ParserError> {
let db_name = self.parse_identifier()?;
Ok(Statement::Use { db_name })
}

pub fn parse_table_and_joins(&mut self) -> Result<TableWithJoins, ParserError> {
let relation = self.parse_table_factor()?;
// Note that for keywords to be properly handled here, they need to be
Expand Down
10 changes: 10 additions & 0 deletions tests/sqlparser_mysql.rs
Expand Up @@ -140,6 +140,16 @@ fn parse_show_create() {
}
}

#[test]
fn parse_use() {
assert_eq!(
mysql_and_generic().verified_stmt("USE mydb"),
Statement::Use {
db_name: Ident::new("mydb")
}
);
}

#[test]
fn parse_create_table_auto_increment() {
let sql = "CREATE TABLE foo (bar INT PRIMARY KEY AUTO_INCREMENT)";
Expand Down