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
150
151
152
153
154
155
156
use scylla::frame::value::ValueList;
use scylla::query::Query;
use scylla::{Bytes, IntoTypedRows};
use scylla::{CachingSession, QueryResult};

use crate::errors::CharybdisError;
use crate::iterator::CharybdisModelIterator;
use crate::model::BaseModel;
use crate::stream::CharybdisModelStream;

pub trait Find: BaseModel {
    async fn find(
        session: &CachingSession,
        query: &'static str,
        values: impl ValueList,
    ) -> Result<CharybdisModelStream<Self>, CharybdisError>;

    async fn find_first(
        session: &CachingSession,
        query: &'static str,
        values: impl ValueList,
    ) -> Result<Self, CharybdisError>;

    async fn find_paged(
        session: &CachingSession,
        query: &'static str,
        values: impl ValueList,
        page_size: Option<Bytes>,
    ) -> Result<(CharybdisModelIterator<Self>, Option<Bytes>), CharybdisError>;

    async fn find_by_primary_key_value(session: &CachingSession, value: impl ValueList)
        -> Result<Self, CharybdisError>;

    async fn find_by_partition_key_value(
        session: &CachingSession,
        value: impl ValueList,
    ) -> Result<CharybdisModelStream<Self>, CharybdisError>;

    async fn find_first_by_partition_key_value(
        session: &CachingSession,
        value: impl ValueList,
    ) -> Result<Self, CharybdisError>;

    async fn find_by_primary_key(&self, session: &CachingSession) -> Result<Self, CharybdisError>;

    async fn find_by_partition_key(
        &self,
        session: &CachingSession,
    ) -> Result<CharybdisModelStream<Self>, CharybdisError>;
}

impl<T: BaseModel> Find for T {
    // find iter
    async fn find(
        session: &CachingSession,
        query: &'static str,
        values: impl ValueList,
    ) -> Result<CharybdisModelStream<Self>, CharybdisError> {
        let query = Query::new(query);

        let rows = session.execute_iter(query, values).await?.into_typed();

        Ok(CharybdisModelStream::from(rows))
    }

    async fn find_first(
        session: &CachingSession,
        query: &'static str,
        values: impl ValueList,
    ) -> Result<Self, CharybdisError> {
        let result: QueryResult = session.execute(query, values).await?;
        let typed_row: Self = result.first_row_typed()?;

        Ok(typed_row)
    }

    async fn find_paged(
        session: &CachingSession,
        query: &'static str,
        values: impl ValueList,
        paging_state: Option<Bytes>,
    ) -> Result<(CharybdisModelIterator<Self>, Option<Bytes>), CharybdisError> {
        let res = session.execute_paged(query, values, paging_state).await?;
        let paging_state = res.paging_state.clone();
        let rows = res.rows()?;
        let typed_rows = CharybdisModelIterator::from(rows.into_typed());

        Ok((typed_rows, paging_state))
    }

    async fn find_by_primary_key_value(
        session: &CachingSession,
        value: impl ValueList,
    ) -> Result<Self, CharybdisError> {
        let result: QueryResult = session.execute(Self::FIND_BY_PRIMARY_KEY_QUERY, value).await?;

        let res = result.first_row_typed()?;

        Ok(res)
    }

    async fn find_by_partition_key_value(
        session: &CachingSession,
        value: impl ValueList,
    ) -> Result<CharybdisModelStream<Self>, CharybdisError> {
        let rows = session
            .execute_iter(Self::FIND_BY_PARTITION_KEY_QUERY, value)
            .await?
            .into_typed::<Self>();

        Ok(CharybdisModelStream::from(rows))
    }

    async fn find_first_by_partition_key_value(
        session: &CachingSession,
        value: impl ValueList,
    ) -> Result<Self, CharybdisError> {
        let result: QueryResult = session.execute(Self::FIND_BY_PARTITION_KEY_QUERY, value).await?;

        let res = result.first_row_typed()?;

        Ok(res)
    }

    /// Preferred way to find by partition key, as keys will be in correct order
    async fn find_by_primary_key(&self, session: &CachingSession) -> Result<Self, CharybdisError> {
        let primary_key_values = self
            .primary_key_values()
            .map_err(|e| CharybdisError::SerializeValuesError(e, Self::DB_MODEL_NAME.to_string()))?;

        let result: QueryResult = session
            .execute(Self::FIND_BY_PRIMARY_KEY_QUERY, &primary_key_values)
            .await?;

        let res = result.first_row_typed()?;

        Ok(res)
    }

    /// Preferred way to find by partition key, as keys will be in correct order
    async fn find_by_partition_key(
        &self,
        session: &CachingSession,
    ) -> Result<CharybdisModelStream<Self>, CharybdisError> {
        let partition_key_values = self
            .partition_key_values()
            .map_err(|e| CharybdisError::SerializeValuesError(e, Self::DB_MODEL_NAME.to_string()))?;

        let rows = session
            .execute_iter(Self::FIND_BY_PARTITION_KEY_QUERY, partition_key_values)
            .await?
            .into_typed::<Self>();

        Ok(CharybdisModelStream::from(rows))
    }
}