sea_query/types/
mod.rs

1//! Base types used throughout sea-query.
2
3use crate::{Values, expr::*, query::*};
4use std::fmt::Debug;
5
6#[cfg(feature = "backend-postgres")]
7use crate::extension::postgres::PgBinOper;
8#[cfg(feature = "backend-sqlite")]
9use crate::extension::sqlite::SqliteBinOper;
10
11// Intentionally not `pub`, so that we're free to experiment with the internal structure.
12mod iden;
13
14pub use iden::*;
15
16/// A reference counted pointer: either [`Rc`][std::rc::Rc] or [`Arc`][std::sync::Arc],
17/// depending on the feature flags.
18///
19/// [`Arc`][std::sync::Arc] is used when `thread-safe` feature is activated.
20#[cfg(not(feature = "thread-safe"))]
21pub type RcOrArc<T> = std::rc::Rc<T>;
22/// A reference counted pointer: either [`Rc`][std::rc::Rc] or [`Arc`][std::sync::Arc],
23/// depending on the feature flags.
24///
25/// [`Arc`][std::sync::Arc] is used when `thread-safe` feature is activated.
26#[cfg(feature = "thread-safe")]
27pub type RcOrArc<T> = std::sync::Arc<T>;
28
29/// A legacy namespace for compatibility.
30///
31/// It's needed, so that most existing [`SeaRc::new`][SeaRc::new] calls keep working.
32///
33/// This used to be an actual type
34/// (a reference-counted pointer with special impls for `dyn Iden` contents).
35/// It's not needed anymore.
36#[derive(Debug)]
37pub struct SeaRc;
38
39impl SeaRc {
40    /// A legacy method, kept for compatibility.
41    ///
42    /// Nowadays, instead of wrapping an `Iden` object,
43    /// it eagerly "renders" it into a string and then drops the object.
44    ///
45    /// Note that most `Iden`s are statically known
46    /// and their representations aren't actually "rendered" and allocated at runtime.
47    #[allow(clippy::new_ret_no_self)]
48    pub fn new<I>(i: I) -> DynIden
49    where
50        I: Iden,
51    {
52        DynIden(i.quoted())
53    }
54
55    pub fn clone(iden: &DynIden) -> DynIden {
56        iden.clone()
57    }
58}
59
60/// Unary operators.
61#[derive(Debug, Clone, Copy, PartialEq, Eq)]
62#[non_exhaustive]
63pub enum UnOper {
64    Not,
65}
66
67/// Binary operators.
68///
69/// If something is not supported here, you can use [`BinOper::Custom`].
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71#[non_exhaustive]
72pub enum BinOper {
73    And,
74    Or,
75    Like,
76    NotLike,
77    Is,
78    IsNot,
79    In,
80    NotIn,
81    Between,
82    NotBetween,
83    Equal,
84    NotEqual,
85    SmallerThan,
86    GreaterThan,
87    SmallerThanOrEqual,
88    GreaterThanOrEqual,
89    Add,
90    Sub,
91    Mul,
92    Div,
93    Mod,
94    BitAnd,
95    BitOr,
96    LShift,
97    RShift,
98    As,
99    Escape,
100    Custom(&'static str),
101    #[cfg(feature = "backend-postgres")]
102    PgOperator(PgBinOper),
103    #[cfg(feature = "backend-sqlite")]
104    SqliteOperator(SqliteBinOper),
105}
106
107/// Logical chain operator: conjunction or disjunction.
108#[derive(Debug, Clone, PartialEq)]
109pub enum LogicalChainOper {
110    And(Expr),
111    Or(Expr),
112}
113
114/// Join types
115#[derive(Debug, Clone, Copy, PartialEq, Eq)]
116pub enum JoinType {
117    Join,
118    CrossJoin,
119    InnerJoin,
120    LeftJoin,
121    RightJoin,
122    FullOuterJoin,
123    StraightJoin,
124}
125
126/// Nulls order
127#[derive(Debug, Clone, Copy, PartialEq, Eq)]
128pub enum NullOrdering {
129    First,
130    Last,
131}
132
133/// Order expression
134#[derive(Debug, Clone, PartialEq)]
135pub struct OrderExpr {
136    pub(crate) expr: Expr,
137    pub(crate) order: Order,
138    pub(crate) nulls: Option<NullOrdering>,
139}
140
141/// Join on types
142#[derive(Debug, Clone, PartialEq)]
143#[non_exhaustive]
144pub enum JoinOn {
145    Condition(Box<ConditionHolder>),
146    Columns(Vec<Expr>),
147}
148
149/// Ordering options
150#[derive(Debug, Clone, PartialEq)]
151#[non_exhaustive]
152pub enum Order {
153    Asc,
154    Desc,
155    Field(Values),
156}
157
158/// Known SQL keywords that can be used as expressions.
159///
160/// If something is not supported here, you can use [`Keyword::Custom`].
161#[derive(Debug, Clone, PartialEq)]
162#[non_exhaustive]
163pub enum Keyword {
164    Null,
165    CurrentDate,
166    CurrentTime,
167    CurrentTimestamp,
168    Default,
169    Custom(DynIden),
170}
171
172/// Like Expression
173#[derive(Debug, Clone)]
174pub struct LikeExpr {
175    pub(crate) pattern: String,
176    pub(crate) escape: Option<char>,
177}
178
179impl LikeExpr {
180    pub fn new<T>(pattern: T) -> Self
181    where
182        T: Into<String>,
183    {
184        Self {
185            pattern: pattern.into(),
186            escape: None,
187        }
188    }
189
190    #[deprecated(since = "0.29.0", note = "Please use the [`LikeExpr::new`] method")]
191    pub fn str<T>(pattern: T) -> Self
192    where
193        T: Into<String>,
194    {
195        Self {
196            pattern: pattern.into(),
197            escape: None,
198        }
199    }
200
201    pub fn escape(self, c: char) -> Self {
202        Self {
203            pattern: self.pattern,
204            escape: Some(c),
205        }
206    }
207}
208
209pub trait IntoLikeExpr: Into<LikeExpr> {
210    fn into_like_expr(self) -> LikeExpr;
211}
212
213impl<T> IntoLikeExpr for T
214where
215    T: Into<LikeExpr>,
216{
217    fn into_like_expr(self) -> LikeExpr {
218        self.into()
219    }
220}
221
222impl<T> From<T> for LikeExpr
223where
224    T: Into<String>,
225{
226    fn from(value: T) -> Self {
227        LikeExpr::new(value)
228    }
229}
230
231/// SubQuery operators
232#[derive(Debug, Copy, Clone, PartialEq)]
233#[non_exhaustive]
234pub enum SubQueryOper {
235    Exists,
236    Any,
237    Some,
238    All,
239}