summaryrefslogtreecommitdiffstats
path: root/src/db.rs
blob: b088f0a371622ab1bc0cce2021f03b73bd2a6f5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#![allow(non_camel_case_types)]

use diesel::backend::Backend;
use diesel::expression::{
    IsContainedInGroupBy, ValidGrouping, is_aggregate, is_contained_in_group_by,
};
use diesel::internal::table_macro::{FromClause, SelectStatement};
use diesel::prelude::*;
use diesel::query_builder::{AsQuery, AstPass, QueryFragment, QueryId};
use diesel::query_source::{AppearsInFromClause, Once, Table as TableTrait};
use diesel::sql_types::{Text, Uuid};

pub use self::columns::*;

pub mod columns {
    use super::*;

    macro_rules! col {
        ($col:ident, $sql_type:ty) => {
            pub struct $col;

            impl Expression for $col {
                type SqlType = $sql_type;
            }

            impl<'a, QS> AppearsOnTable<QS> for $col where
                QS: AppearsInFromClause<super::Table<'a>, Count = Once>
            {
            }

            impl<'a> SelectableExpression<super::Table<'a>> for $col {}

            impl ValidGrouping<()> for $col {
                type IsAggregate = is_aggregate::No;
            }

            impl<GB> ValidGrouping<GB> for $col
            where
                GB: IsContainedInGroupBy<$col, Output = is_contained_in_group_by::Yes>,
            {
                type IsAggregate = is_aggregate::Yes;
            }

            impl<'a> Column for $col {
                type Table = Table<'static>;

                const NAME: &'static str = stringify!($col);
            }

            impl<DB> QueryFragment<DB> for $col
            where
                DB: Backend,
            {
                fn walk_ast<'b>(&'b self, mut pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
                    pass.push_identifier(<$col as Column>::NAME)?;
                    Ok(())
                }
            }

            impl QueryId for $col {
                type QueryId = $col;

                const HAS_STATIC_QUERY_ID: bool = true;
            }
        };
    }

    col!(id, Uuid);
    col!(username, Text);
    col!(token, Text);
}

//pub const all_columns: <Table<'static> as TableTrait>::AllColumns = (id, username, token);

pub type SqlType = (Uuid, Text, Text);

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct Table<'a> {
    name: &'a str,
    schema: Option<&'a str>,
}

impl<'a> Table<'a> {
    pub fn new(name: &'a str, schema: Option<&'a str>) -> Self {
        Self { name, schema }
    }
}

impl QuerySource for Table<'_> {
    type FromClause = Self;
    type DefaultSelection = <Self as TableTrait>::AllColumns;

    fn from_clause(&self) -> Self::FromClause {
        self.clone()
    }

    fn default_selection(&self) -> Self::DefaultSelection {
        <Self as TableTrait>::all_columns()
    }
}

impl AsQuery for Table<'_> {
    type SqlType = SqlType;
    type Query = SelectStatement<FromClause<Self>>;

    fn as_query(self) -> Self::Query {
        SelectStatement::simple(self)
    }
}

impl TableTrait for Table<'_>
where
    Self: QuerySource + AsQuery,
{
    type PrimaryKey = id;
    type AllColumns = (id, username, token);

    fn primary_key(&self) -> Self::PrimaryKey {
        id
    }

    fn all_columns() -> Self::AllColumns {
        (id, username, token)
    }
}

impl<DB> QueryFragment<DB> for Table<'_>
where
    DB: Backend,
{
    fn walk_ast<'b>(&'b self, mut pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
        if let Some(ref schema) = self.schema {
            pass.push_identifier(schema)?;
            pass.push_sql(".");
        }
        pass.push_identifier(self.name)?;
        Ok(())
    }
}

impl QueryId for Table<'_> {
    type QueryId = ();

    const HAS_STATIC_QUERY_ID: bool = false;
}

impl AppearsInFromClause<Table<'_>> for Table<'_> {
    type Count = Once;
}