Skip to main content

squawk_syntax/
identifier.rs

1use crate::quote::normalize_identifier;
2
3/// Postgres Identifiers are case insensitive unless they're quoted.
4///
5/// This type handles the casing rules for us to make comparisions easier.
6#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7pub struct Identifier(String);
8
9impl Identifier {
10    // TODO: we need to handle more advanced identifiers like:
11    // U&"d!0061t!+000061" UESCAPE '!'
12    pub fn new(s: &str) -> Self {
13        let normalized = normalize_identifier(s);
14        Identifier(normalized)
15    }
16}
17
18#[cfg(test)]
19mod test {
20    use crate::identifier::Identifier;
21
22    #[test]
23    fn case_folds_correctly() {
24        // https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
25        // For example, the identifiers FOO, foo, and "foo" are considered the
26        // same by PostgreSQL, but "Foo" and "FOO" are different from these
27        // three and each other.
28        assert_eq!(Identifier::new("FOO"), Identifier::new("foo"));
29        assert_eq!(Identifier::new(r#""foo""#), Identifier::new("foo"));
30        assert_eq!(Identifier::new(r#""foo""#), Identifier::new("FOO"));
31    }
32}