Skip to content

Commit

Permalink
Add support for ATTACH DATABASE (apache#989)
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa authored and serprex committed Nov 6, 2023
1 parent 9484336 commit 8acc3b6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,16 @@ pub enum Statement {
name: Ident,
operation: AlterRoleOperation,
},
/// ATTACH DATABASE 'path/to/file' AS alias
/// (SQLite-specific)
AttachDatabase {
/// The name to bind to the newly attached database
schema_name: Ident,
/// An expression that indicates the path to the database file
database_file_name: Expr,
/// true if the syntax is 'ATTACH DATABASE', false if it's just 'ATTACH'
database: bool,
},
/// DROP
Drop {
/// The type of the object to drop: TABLE, VIEW, etc.
Expand Down Expand Up @@ -2157,6 +2167,14 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::AttachDatabase {
schema_name,
database_file_name,
database,
} => {
let keyword = if *database { "DATABASE " } else { "" };
write!(f, "ATTACH {keyword}{database_file_name} AS {schema_name}")
}
Statement::Analyze {
table_name,
partitions,
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ define_keywords!(
ASYMMETRIC,
AT,
ATOMIC,
ATTACH,
AUTHORIZATION,
AUTOINCREMENT,
AUTO_INCREMENT,
Expand Down
13 changes: 13 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ impl<'a> Parser<'a> {
Ok(Statement::Query(Box::new(self.parse_query()?)))
}
Keyword::TRUNCATE => Ok(self.parse_truncate()?),
Keyword::ATTACH => Ok(self.parse_attach_database()?),
Keyword::MSCK => Ok(self.parse_msck()?),
Keyword::CREATE => Ok(self.parse_create()?),
Keyword::CACHE => Ok(self.parse_cache_table()?),
Expand Down Expand Up @@ -547,6 +548,18 @@ impl<'a> Parser<'a> {
})
}

pub fn parse_attach_database(&mut self) -> Result<Statement, ParserError> {
let database = self.parse_keyword(Keyword::DATABASE);
let database_file_name = self.parse_expr()?;
self.expect_keyword(Keyword::AS)?;
let schema_name = self.parse_identifier()?;
Ok(Statement::AttachDatabase {
database,
schema_name,
database_file_name,
})
}

pub fn parse_analyze(&mut self) -> Result<Statement, ParserError> {
self.expect_keyword(Keyword::TABLE)?;
let table_name = self.parse_object_name()?;
Expand Down
18 changes: 18 additions & 0 deletions tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,24 @@ fn parse_create_table_with_strict() {
}
}

#[test]
fn parse_attach_database() {
let sql = "ATTACH DATABASE 'test.db' AS test";
let verified_stmt = sqlite().verified_stmt(sql);
assert_eq!(sql, format!("{}", verified_stmt));
match verified_stmt {
Statement::AttachDatabase {
schema_name,
database_file_name: Expr::Value(Value::SingleQuotedString(literal_name)),
database: true,
} => {
assert_eq!(schema_name.value, "test");
assert_eq!(literal_name, "test.db");
}
_ => unreachable!(),
}
}

fn sqlite() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(SQLiteDialect {})],
Expand Down

0 comments on commit 8acc3b6

Please sign in to comment.