Skip to content

Commit 082dc65

Browse files
committed
feat: Support USE db
1 parent 978758f commit 082dc65

4 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/ast/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,10 @@ pub enum Statement {
894894
///
895895
/// Note: this is a MySQL-specific statement.
896896
ShowCollation { filter: Option<ShowStatementFilter> },
897+
/// USE
898+
///
899+
/// Note: this is a MySQL-specific statement.
900+
Use { db_name: Ident },
897901
/// `{ BEGIN [ TRANSACTION | WORK ] | START TRANSACTION } ...`
898902
StartTransaction { modes: Vec<TransactionMode> },
899903
/// `SET TRANSACTION ...`
@@ -1613,6 +1617,10 @@ impl fmt::Display for Statement {
16131617
}
16141618
Ok(())
16151619
}
1620+
Statement::Use { db_name } => {
1621+
write!(f, "USE {}", db_name)?;
1622+
Ok(())
1623+
}
16161624
Statement::StartTransaction { modes } => {
16171625
write!(f, "START TRANSACTION")?;
16181626
if !modes.is_empty() {

src/keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ define_keywords!(
520520
UPDATE,
521521
UPPER,
522522
USAGE,
523+
USE,
523524
USER,
524525
USING,
525526
UUID,

src/parser.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ impl<'a> Parser<'a> {
175175
Keyword::SHOW => Ok(self.parse_show()?),
176176
Keyword::GRANT => Ok(self.parse_grant()?),
177177
Keyword::REVOKE => Ok(self.parse_revoke()?),
178+
Keyword::USE => Ok(self.parse_use()?),
178179
Keyword::START => Ok(self.parse_start_transaction()?),
179180
// `BEGIN` is a nonstandard but common alias for the
180181
// standard `START TRANSACTION` statement. It is supported
@@ -3385,6 +3386,11 @@ impl<'a> Parser<'a> {
33853386
}
33863387
}
33873388

3389+
pub fn parse_use(&mut self) -> Result<Statement, ParserError> {
3390+
let db_name = self.parse_identifier()?;
3391+
Ok(Statement::Use { db_name })
3392+
}
3393+
33883394
pub fn parse_table_and_joins(&mut self) -> Result<TableWithJoins, ParserError> {
33893395
let relation = self.parse_table_factor()?;
33903396

tests/sqlparser_mysql.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ fn parse_show_collation() {
184184
);
185185
}
186186

187+
#[test]
188+
fn parse_use() {
189+
assert_eq!(
190+
mysql_and_generic().verified_stmt("USE database_name"),
191+
Statement::Use {
192+
db_name: Ident::new("database_name")
193+
}
194+
);
195+
}
196+
187197
#[test]
188198
fn parse_show_create() {
189199
let obj_name = ObjectName(vec![Ident::new("myident")]);

0 commit comments

Comments
 (0)