Skip to main content

diesel/collation/
mod.rs

1//! This module contains types that represent SQL collations.
2//!
3//! These types are used as arguments for
4//! [`TextExpressionMethods::collate`](crate::expression_methods::TextExpressionMethods::collate).
5
6use crate::backend::Backend;
7use crate::query_builder::{AstPass, QueryFragment, QueryId};
8use crate::result::QueryResult;
9
10/// Trait to identify a valid collation.
11pub trait Collation: QueryId + Clone + Send + Sync + 'static {}
12
13/// A custom collation.
14///
15/// This type wraps a string that represents a collation name.
16/// It can be used to use a collation that is not supported by Diesel.
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
18pub struct Custom(pub &'static str);
19
20impl Collation for Custom {}
21
22impl<DB: Backend> QueryFragment<DB> for Custom {
23    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
24        out.push_sql(self.0);
25        Ok(())
26    }
27}
28
29impl QueryId for Custom {
30    type QueryId = ();
31    const HAS_STATIC_QUERY_ID: bool = false;
32}
33
34/// The `BINARY` collation.
35///
36/// This collation is binary, case-sensitive, and locale-free.
37/// It is supported by SQLite.
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, QueryId)]
39#[cfg(feature = "__sqlite-shared")]
40pub struct Binary;
41
42#[cfg(feature = "__sqlite-shared")]
43impl Collation for Binary {}
44
45#[cfg(feature = "__sqlite-shared")]
46impl QueryFragment<crate::sqlite::Sqlite> for Binary {
47    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, crate::sqlite::Sqlite>) -> QueryResult<()> {
48        out.push_sql("BINARY");
49        Ok(())
50    }
51}
52
53/// The `C` collation.
54///
55/// This collation is byte-wise, with only ASCII A-Z treated as letters.
56/// It is supported by PostgreSQL.
57#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, QueryId)]
58#[cfg(feature = "postgres_backend")]
59pub struct C;
60
61#[cfg(feature = "postgres_backend")]
62impl Collation for C {}
63
64#[cfg(feature = "postgres_backend")]
65impl QueryFragment<crate::pg::Pg> for C {
66    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, crate::pg::Pg>) -> QueryResult<()> {
67        out.push_sql("\"C\"");
68        Ok(())
69    }
70}
71
72/// The `NOCASE` collation.
73///
74/// This collation is ASCII-only case-insensitive.
75/// It is supported by SQLite.
76#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, QueryId)]
77#[cfg(feature = "__sqlite-shared")]
78pub struct NoCase;
79
80#[cfg(feature = "__sqlite-shared")]
81impl Collation for NoCase {}
82
83#[cfg(feature = "__sqlite-shared")]
84impl QueryFragment<crate::sqlite::Sqlite> for NoCase {
85    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, crate::sqlite::Sqlite>) -> QueryResult<()> {
86        out.push_sql("NOCASE");
87        Ok(())
88    }
89}
90
91/// The `POSIX` collation.
92///
93/// This collation is byte-wise, with only ASCII A-Z treated as letters.
94/// It is supported by PostgreSQL.
95#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, QueryId)]
96#[cfg(feature = "postgres_backend")]
97pub struct Posix;
98
99#[cfg(feature = "postgres_backend")]
100impl Collation for Posix {}
101
102#[cfg(feature = "postgres_backend")]
103impl QueryFragment<crate::pg::Pg> for Posix {
104    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, crate::pg::Pg>) -> QueryResult<()> {
105        out.push_sql("\"POSIX\"");
106        Ok(())
107    }
108}
109
110/// The `RTRIM` collation.
111///
112/// This collation ignores trailing spaces.
113/// It is supported by SQLite.
114#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, QueryId)]
115#[cfg(feature = "__sqlite-shared")]
116pub struct RTrim;
117
118#[cfg(feature = "__sqlite-shared")]
119impl Collation for RTrim {}
120
121#[cfg(feature = "__sqlite-shared")]
122impl QueryFragment<crate::sqlite::Sqlite> for RTrim {
123    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, crate::sqlite::Sqlite>) -> QueryResult<()> {
124        out.push_sql("RTRIM");
125        Ok(())
126    }
127}