Add MSSQL dialect and fix up the postgres' identifier rules

The `@@version` test is MS' dialect of SQL, it seems, so test it with
its own dialect.

Update the rules for identifiers in Postresql dialect per documentation,
while we're at it. The current identifier rules in Postgresql dialect
were introduced in this commit - as a copy of generic rules, it seems:
810cd8e6cf (diff-2808df0fba0aed85f9d35c167bd6a5f1L138)
This commit is contained in:
Nickolay Ponomarev 2019-05-03 03:09:44 +03:00
parent 5047f2c02e
commit 304710d59a
8 changed files with 75 additions and 15 deletions

View file

@ -5,7 +5,7 @@ pub struct GenericSqlDialect {}
impl Dialect for GenericSqlDialect {
fn is_identifier_start(&self, ch: char) -> bool {
(ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '@'
(ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_' || ch == '#' || ch == '@'
}
fn is_identifier_part(&self, ch: char) -> bool {
@ -13,6 +13,8 @@ impl Dialect for GenericSqlDialect {
|| (ch >= 'A' && ch <= 'Z')
|| (ch >= '0' && ch <= '9')
|| ch == '@'
|| ch == '$'
|| ch == '#'
|| ch == '_'
}
}

View file

@ -1,12 +1,14 @@
mod ansi_sql;
mod generic_sql;
pub mod keywords;
mod mssql;
mod postgresql;
use std::fmt::Debug;
pub use self::ansi_sql::AnsiSqlDialect;
pub use self::generic_sql::GenericSqlDialect;
pub use self::mssql::MsSqlDialect;
pub use self::postgresql::PostgreSqlDialect;
pub trait Dialect: Debug {

22
src/dialect/mssql.rs Normal file
View file

@ -0,0 +1,22 @@
use crate::dialect::Dialect;
#[derive(Debug)]
pub struct MsSqlDialect {}
impl Dialect for MsSqlDialect {
fn is_identifier_start(&self, ch: char) -> bool {
// See https://docs.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers?view=sql-server-2017#rules-for-regular-identifiers
// We don't support non-latin "letters" currently.
(ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_' || ch == '#' || ch == '@'
}
fn is_identifier_part(&self, ch: char) -> bool {
(ch >= 'a' && ch <= 'z')
|| (ch >= 'A' && ch <= 'Z')
|| (ch >= '0' && ch <= '9')
|| ch == '@'
|| ch == '$'
|| ch == '#'
|| ch == '_'
}
}

View file

@ -5,14 +5,17 @@ pub struct PostgreSqlDialect {}
impl Dialect for PostgreSqlDialect {
fn is_identifier_start(&self, ch: char) -> bool {
(ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '@'
// See https://www.postgresql.org/docs/11/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
// We don't yet support identifiers beginning with "letters with
// diacritical marks and non-Latin letters"
(ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_'
}
fn is_identifier_part(&self, ch: char) -> bool {
(ch >= 'a' && ch <= 'z')
|| (ch >= 'A' && ch <= 'Z')
|| (ch >= '0' && ch <= '9')
|| ch == '@'
|| ch == '$'
|| ch == '_'
}
}

View file

@ -103,6 +103,7 @@ pub fn all_dialects() -> TestedDialects {
dialects: vec![
Box::new(GenericSqlDialect {}),
Box::new(PostgreSqlDialect {}),
Box::new(MsSqlDialect {}),
Box::new(AnsiSqlDialect {}),
],
}