taitan_orm/dto/
count.rs

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
use sqlx::mysql::MySqlRow;
use sqlx::postgres::PgRow;
use sqlx::sqlite::SqliteRow;
use sqlx::{Error, MySql, Postgres, Row, Sqlite};
use taitan_orm_trait::{SelectedEntity, Selection};

#[derive(Clone, Debug, Default)]
pub struct EmptySelection {}

impl Selection for EmptySelection {
    fn get_table_name(&self) -> &'static str {
        todo!()
    }

    fn get_selected_fields(&self) -> Vec<String> {
        todo!()
    }


    fn full_fields() -> Self {
        todo!()
    }
}

#[derive(Clone, Debug, Default)]
pub struct CountResult {
    pub count: u64,
}

impl SelectedEntity<Sqlite> for CountResult {
    type Selection = EmptySelection;

    fn from_row(_selection: &Self::Selection, row: SqliteRow) -> Result<Self, Error>
    where
        Self: Sized,
    {
        // TODO: 有可能使用try_get(0)更好
        // let count: i64 = row.try_get("count")?;
        let count: i64 = row.try_get(0)?;
        Ok(Self {
            count: count as u64,
        })
    }

    fn from_row_full(row: SqliteRow) -> Result<Self, Error>
    where
        Self: Sized,
    {
        // let count: i64 = row.try_get("count")?;
        let count: i64 = row.try_get(0)?;
        Ok(Self {
            count: count as u64,
        })
    }
}

impl SelectedEntity<MySql> for CountResult {
    type Selection = EmptySelection;

    fn from_row(_selection: &Self::Selection, row: MySqlRow) -> Result<Self, Error>
    where
        Self: Sized,
    {
        // let count: i64 = row.try_get("count")?;
        let count: i64 = row.try_get(0)?;
        Ok(Self {
            count: count as u64,
        })
    }
    fn from_row_full(row: MySqlRow) -> Result<Self, Error> {
        // let count: i64 = row.try_get("count")?;
        let count: i64 = row.try_get(0)?;
        Ok(Self {
            count: count as u64,
        })
    }
}

impl SelectedEntity<Postgres> for CountResult {
    type Selection = EmptySelection;

    fn from_row(_selection: &Self::Selection, row: PgRow) -> Result<Self, Error>
    where
        Self: Sized,
    {
        // let count: i64 = row.try_get("count")?;
        let count: i64 = row.try_get(0)?;
        Ok(Self {
            count: count as u64,
        })
    }
    fn from_row_full(row: PgRow) -> Result<Self, Error> {
        // let count: i64 = row.try_get("count")?;
        let count: i64 = row.try_get(0)?;
        Ok(Self {
            count: count as u64,
        })
    }
}