Add support for table-level comments (#946)

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
This commit is contained in:
ehoeve 2023-08-17 12:44:55 +02:00 committed by GitHub
parent 8bbb85356c
commit 83e30677b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 0 deletions

View file

@ -65,6 +65,7 @@ pub struct CreateTableBuilder {
pub like: Option<ObjectName>,
pub clone: Option<ObjectName>,
pub engine: Option<String>,
pub comment: Option<String>,
pub default_charset: Option<String>,
pub collation: Option<String>,
pub on_commit: Option<OnCommit>,
@ -96,6 +97,7 @@ impl CreateTableBuilder {
like: None,
clone: None,
engine: None,
comment: None,
default_charset: None,
collation: None,
on_commit: None,
@ -197,6 +199,11 @@ impl CreateTableBuilder {
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 {
self.default_charset = default_charset;
self
@ -249,6 +256,7 @@ impl CreateTableBuilder {
like: self.like,
clone: self.clone,
engine: self.engine,
comment: self.comment,
default_charset: self.default_charset,
collation: self.collation,
on_commit: self.on_commit,
@ -287,6 +295,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
like,
clone,
engine,
comment,
default_charset,
collation,
on_commit,
@ -314,6 +323,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
like,
clone,
engine,
comment,
default_charset,
collation,
on_commit,

View file

@ -1319,6 +1319,7 @@ pub enum Statement {
like: Option<ObjectName>,
clone: Option<ObjectName>,
engine: Option<String>,
comment: Option<String>,
default_charset: Option<String>,
collation: Option<String>,
on_commit: Option<OnCommit>,
@ -2250,6 +2251,7 @@ impl fmt::Display for Statement {
clone,
default_charset,
engine,
comment,
collation,
on_commit,
on_cluster,
@ -2401,6 +2403,9 @@ impl fmt::Display for Statement {
if let Some(engine) = engine {
write!(f, " ENGINE={engine}")?;
}
if let Some(comment) = comment {
write!(f, " COMMENT '{comment}'")?;
}
if let Some(order_by) = order_by {
write!(f, " ORDER BY ({})", display_comma_separated(order_by))?;
}

View file

@ -3503,6 +3503,17 @@ impl<'a> Parser<'a> {
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]) {
if self.consume_token(&Token::LParen) {
let columns = if self.peek_token() != Token::RParen {
@ -3583,6 +3594,7 @@ impl<'a> Parser<'a> {
.like(like)
.clone_clause(clone)
.engine(engine)
.comment(comment)
.order_by(order_by)
.default_charset(default_charset)
.collation(collation)

View file

@ -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]
fn parse_create_table_set_enum() {
let sql = "CREATE TABLE foo (bar SET('a', 'b'), baz ENUM('a', 'b'))";