Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,8 @@ pub enum ColumnOption {
CharacterSet(ObjectName),
/// `COLLATE <name>` column option
Collation(ObjectName),
/// `BINARY` column attribute, such as in MySQL's `CHAR(1) BINARY`
Binary,
/// `COMMENT '<text>'` column option
Comment(String),
/// `ON UPDATE <expr>` column option
Expand Down Expand Up @@ -2327,6 +2329,7 @@ impl fmt::Display for ColumnOption {
DialectSpecific(val) => write!(f, "{}", display_separated(val, " ")),
CharacterSet(n) => write!(f, "CHARACTER SET {n}"),
Collation(n) => write!(f, "COLLATE {n}"),
Binary => write!(f, "BINARY"),
Comment(v) => write!(f, "COMMENT '{}'", escape_single_quote_string(v)),
OnUpdate(expr) => write!(f, "ON UPDATE {expr}"),
Generated {
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@ impl Spanned for ColumnOption {
ColumnOption::DialectSpecific(_) => Span::empty(),
ColumnOption::CharacterSet(object_name) => object_name.span(),
ColumnOption::Collation(object_name) => object_name.span(),
ColumnOption::Binary => Span::empty(),
ColumnOption::Comment(_) => Span::empty(),
ColumnOption::OnUpdate(expr) => expr.span(),
ColumnOption::Generated { .. } => Span::empty(),
Expand Down
10 changes: 10 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1914,6 +1914,16 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports the `BINARY` attribute after a
/// character column type.
///
/// For example: ```CREATE TABLE t (c CHAR(1) BINARY);```.
///
/// [MySQL](https://dev.mysql.com/doc/refman/8.0/en/string-type-syntax.html)
fn supports_binary_column_attribute(&self) -> bool {
false
}

/// Returns true if the dialect supports PostgreSQL-style ordering operators:
/// `ORDER BY expr USING <operator>`.
///
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ const RESERVED_FOR_TABLE_ALIAS_MYSQL: &[Keyword] = &[
pub struct MySqlDialect {}

impl Dialect for MySqlDialect {
fn supports_binary_column_attribute(&self) -> bool {
true
}

/// Only select-list aliases may use string quoting, see
/// <https://dev.mysql.com/doc/refman/8.4/en/problems-with-alias.html>
fn supports_string_literal_table_alias(&self, _explicit: bool) -> bool {
Expand Down
4 changes: 4 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10547,6 +10547,10 @@ impl<'a> Parser<'a> {
Ok(Some(ColumnOption::Collation(
self.parse_object_name(false)?,
)))
} else if self.dialect.supports_binary_column_attribute()
&& self.parse_keyword(Keyword::BINARY)
{
Ok(Some(ColumnOption::Binary))
} else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
Ok(Some(ColumnOption::NotNull))
} else if self.parse_keywords(&[Keyword::COMMENT]) {
Expand Down
22 changes: 22 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5372,3 +5372,25 @@ fn test_index_constraint_multiple_index_options() {
ParserError::ParserError("Expected: a data type name, found: 'lookup'".to_string())
);
}

#[test]
fn parse_create_table_column_binary_attribute() {
match mysql().verified_stmt("CREATE TABLE t (c CHAR(1) BINARY)") {
Statement::CreateTable(CreateTable { columns, .. }) => {
assert_eq!(
columns[0].data_type,
DataType::Char(Some(CharacterLength::IntegerLength {
length: 1,
unit: None,
}))
);
assert_eq!(columns[0].options[0].option, ColumnOption::Binary);
}
_ => unreachable!(),
}
mysql().verified_stmt("CREATE TABLE t (c VARCHAR(10) BINARY NOT NULL)");
mysql().verified_stmt("CREATE TABLE t (c TEXT CHARACTER SET utf8mb4 BINARY)");
all_dialects_where(|d| !d.supports_binary_column_attribute())
.parse_sql_statements("CREATE TABLE t (c CHAR(1) BINARY)")
.unwrap_err();
}