mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-10-09 13:40:22 +00:00
Add support for table-level comments (#946)
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
This commit is contained in:
parent
8bbb85356c
commit
83e30677b0
4 changed files with 43 additions and 0 deletions
|
@ -65,6 +65,7 @@ pub struct CreateTableBuilder {
|
||||||
pub like: Option<ObjectName>,
|
pub like: Option<ObjectName>,
|
||||||
pub clone: Option<ObjectName>,
|
pub clone: Option<ObjectName>,
|
||||||
pub engine: Option<String>,
|
pub engine: Option<String>,
|
||||||
|
pub comment: Option<String>,
|
||||||
pub default_charset: Option<String>,
|
pub default_charset: Option<String>,
|
||||||
pub collation: Option<String>,
|
pub collation: Option<String>,
|
||||||
pub on_commit: Option<OnCommit>,
|
pub on_commit: Option<OnCommit>,
|
||||||
|
@ -96,6 +97,7 @@ impl CreateTableBuilder {
|
||||||
like: None,
|
like: None,
|
||||||
clone: None,
|
clone: None,
|
||||||
engine: None,
|
engine: None,
|
||||||
|
comment: None,
|
||||||
default_charset: None,
|
default_charset: None,
|
||||||
collation: None,
|
collation: None,
|
||||||
on_commit: None,
|
on_commit: None,
|
||||||
|
@ -197,6 +199,11 @@ impl CreateTableBuilder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn comment(mut self, comment: Option<String>) -> Self {
|
||||||
|
self.comment = comment;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn default_charset(mut self, default_charset: Option<String>) -> Self {
|
pub fn default_charset(mut self, default_charset: Option<String>) -> Self {
|
||||||
self.default_charset = default_charset;
|
self.default_charset = default_charset;
|
||||||
self
|
self
|
||||||
|
@ -249,6 +256,7 @@ impl CreateTableBuilder {
|
||||||
like: self.like,
|
like: self.like,
|
||||||
clone: self.clone,
|
clone: self.clone,
|
||||||
engine: self.engine,
|
engine: self.engine,
|
||||||
|
comment: self.comment,
|
||||||
default_charset: self.default_charset,
|
default_charset: self.default_charset,
|
||||||
collation: self.collation,
|
collation: self.collation,
|
||||||
on_commit: self.on_commit,
|
on_commit: self.on_commit,
|
||||||
|
@ -287,6 +295,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
|
||||||
like,
|
like,
|
||||||
clone,
|
clone,
|
||||||
engine,
|
engine,
|
||||||
|
comment,
|
||||||
default_charset,
|
default_charset,
|
||||||
collation,
|
collation,
|
||||||
on_commit,
|
on_commit,
|
||||||
|
@ -314,6 +323,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
|
||||||
like,
|
like,
|
||||||
clone,
|
clone,
|
||||||
engine,
|
engine,
|
||||||
|
comment,
|
||||||
default_charset,
|
default_charset,
|
||||||
collation,
|
collation,
|
||||||
on_commit,
|
on_commit,
|
||||||
|
|
|
@ -1319,6 +1319,7 @@ pub enum Statement {
|
||||||
like: Option<ObjectName>,
|
like: Option<ObjectName>,
|
||||||
clone: Option<ObjectName>,
|
clone: Option<ObjectName>,
|
||||||
engine: Option<String>,
|
engine: Option<String>,
|
||||||
|
comment: Option<String>,
|
||||||
default_charset: Option<String>,
|
default_charset: Option<String>,
|
||||||
collation: Option<String>,
|
collation: Option<String>,
|
||||||
on_commit: Option<OnCommit>,
|
on_commit: Option<OnCommit>,
|
||||||
|
@ -2250,6 +2251,7 @@ impl fmt::Display for Statement {
|
||||||
clone,
|
clone,
|
||||||
default_charset,
|
default_charset,
|
||||||
engine,
|
engine,
|
||||||
|
comment,
|
||||||
collation,
|
collation,
|
||||||
on_commit,
|
on_commit,
|
||||||
on_cluster,
|
on_cluster,
|
||||||
|
@ -2401,6 +2403,9 @@ impl fmt::Display for Statement {
|
||||||
if let Some(engine) = engine {
|
if let Some(engine) = engine {
|
||||||
write!(f, " ENGINE={engine}")?;
|
write!(f, " ENGINE={engine}")?;
|
||||||
}
|
}
|
||||||
|
if let Some(comment) = comment {
|
||||||
|
write!(f, " COMMENT '{comment}'")?;
|
||||||
|
}
|
||||||
if let Some(order_by) = order_by {
|
if let Some(order_by) = order_by {
|
||||||
write!(f, " ORDER BY ({})", display_comma_separated(order_by))?;
|
write!(f, " ORDER BY ({})", display_comma_separated(order_by))?;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3503,6 +3503,17 @@ impl<'a> Parser<'a> {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let comment = if self.parse_keyword(Keyword::COMMENT) {
|
||||||
|
let _ = self.consume_token(&Token::Eq);
|
||||||
|
let next_token = self.next_token();
|
||||||
|
match next_token.token {
|
||||||
|
Token::SingleQuotedString(str) => Some(str),
|
||||||
|
_ => self.expected("comment", next_token)?,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
|
let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
|
||||||
if self.consume_token(&Token::LParen) {
|
if self.consume_token(&Token::LParen) {
|
||||||
let columns = if self.peek_token() != Token::RParen {
|
let columns = if self.peek_token() != Token::RParen {
|
||||||
|
@ -3583,6 +3594,7 @@ impl<'a> Parser<'a> {
|
||||||
.like(like)
|
.like(like)
|
||||||
.clone_clause(clone)
|
.clone_clause(clone)
|
||||||
.engine(engine)
|
.engine(engine)
|
||||||
|
.comment(comment)
|
||||||
.order_by(order_by)
|
.order_by(order_by)
|
||||||
.default_charset(default_charset)
|
.default_charset(default_charset)
|
||||||
.collation(collation)
|
.collation(collation)
|
||||||
|
|
|
@ -296,6 +296,22 @@ fn parse_create_table_auto_increment() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_create_table_comment() {
|
||||||
|
let canonical = "CREATE TABLE foo (bar INT) COMMENT 'baz'";
|
||||||
|
let with_equal = "CREATE TABLE foo (bar INT) COMMENT = 'baz'";
|
||||||
|
|
||||||
|
for sql in [canonical, with_equal] {
|
||||||
|
match mysql().one_statement_parses_to(sql, canonical) {
|
||||||
|
Statement::CreateTable { name, comment, .. } => {
|
||||||
|
assert_eq!(name.to_string(), "foo");
|
||||||
|
assert_eq!(comment.expect("Should exist").to_string(), "baz");
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_create_table_set_enum() {
|
fn parse_create_table_set_enum() {
|
||||||
let sql = "CREATE TABLE foo (bar SET('a', 'b'), baz ENUM('a', 'b'))";
|
let sql = "CREATE TABLE foo (bar SET('a', 'b'), baz ENUM('a', 'b'))";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue