Skip to main content

cast_core/
column.rs

1//! Typed column references. Generated by `#[derive(Model)]`.
2//!
3//! The point of `Column<M, T>` is compile-time type safety on the where clause:
4//! `User::query().where_eq(User::columns().email(), 42)` fails because 42 is `i32`,
5//! not `String`.
6
7use std::marker::PhantomData;
8
9use sea_query::DynIden;
10
11#[derive(Debug, Clone, Copy)]
12#[allow(clippy::type_complexity)]
13pub struct Column<M, T> {
14    name: &'static str,
15    _marker: PhantomData<(fn() -> M, fn() -> T)>,
16}
17
18impl<M, T> Column<M, T> {
19    pub const fn new(name: &'static str) -> Self {
20        Self {
21            name,
22            _marker: PhantomData,
23        }
24    }
25
26    pub fn name(&self) -> &'static str {
27        self.name
28    }
29
30    pub fn iden(&self) -> sea_query::Alias {
31        sea_query::Alias::new(self.name)
32    }
33}
34
35/// Untyped column reference for the query builder's internal use.
36#[derive(Debug, Clone)]
37pub struct ColumnRef {
38    pub table: Option<&'static str>,
39    pub name: &'static str,
40}
41
42impl ColumnRef {
43    pub fn new(name: &'static str) -> Self {
44        Self { table: None, name }
45    }
46
47    pub fn qualified(table: &'static str, name: &'static str) -> Self {
48        Self {
49            table: Some(table),
50            name,
51        }
52    }
53
54    pub fn iden(&self) -> DynIden {
55        sea_query::SeaRc::new(sea_query::Alias::new(self.name))
56    }
57}
58
59impl<M, T> From<Column<M, T>> for ColumnRef {
60    fn from(c: Column<M, T>) -> Self {
61        ColumnRef::new(c.name)
62    }
63}