Add support of NVARCHAR data type (#462)

* Add support of nvarchar data type

* Change the format type with capitals

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>

* Add Test

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
This commit is contained in:
yuval-illumex 2022-04-21 00:25:50 +03:00 committed by GitHub
parent d035784bdf
commit 278345d21a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 0 deletions

View file

@ -29,6 +29,8 @@ pub enum DataType {
Char(Option<u64>),
/// Variable-length character type e.g. VARCHAR(10)
Varchar(Option<u64>),
/// Variable-length character type e.g. NVARCHAR(10)
Nvarchar(Option<u64>),
/// Uuid type
Uuid,
/// Large character object e.g. CLOB(1000)
@ -98,6 +100,9 @@ impl fmt::Display for DataType {
DataType::Varchar(size) => {
format_type_with_optional_length(f, "CHARACTER VARYING", size, false)
}
DataType::Nvarchar(size) => {
format_type_with_optional_length(f, "NVARCHAR", size, false)
}
DataType::Uuid => write!(f, "UUID"),
DataType::Clob(size) => write!(f, "CLOB({})", size),
DataType::Binary(size) => write!(f, "BINARY({})", size),

View file

@ -336,6 +336,7 @@ define_keywords!(
NULLIF,
NULLS,
NUMERIC,
NVARCHAR,
OBJECT,
OCCURRENCES_REGEX,
OCTET_LENGTH,

View file

@ -2569,6 +2569,7 @@ impl<'a> Parser<'a> {
}
}
Keyword::VARCHAR => Ok(DataType::Varchar(self.parse_optional_precision()?)),
Keyword::NVARCHAR => Ok(DataType::Nvarchar(self.parse_optional_precision()?)),
Keyword::CHAR | Keyword::CHARACTER => {
if self.parse_keyword(Keyword::VARYING) {
Ok(DataType::Varchar(self.parse_optional_precision()?))

View file

@ -1365,6 +1365,16 @@ fn parse_cast() {
"SELECT CAST(id AS DECIMAL) FROM customer",
"SELECT CAST(id AS NUMERIC) FROM customer",
);
let sql = "SELECT CAST(id AS NVARCHAR(50)) FROM customer";
let select = verified_only_select(sql);
assert_eq!(
&Expr::Cast {
expr: Box::new(Expr::Identifier(Ident::new("id"))),
data_type: DataType::Nvarchar(Some(50))
},
expr_from_projection(only(&select.projection))
);
}
#[test]