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
157
158
159
160
161
162
163
164
165
use async_trait::async_trait;
use crate::error::ToqlError;
use crate::from_row::FromRow;
use crate::key::Key;
use crate::keyed::Keyed;
use crate::page::Page;
use crate::page_counts::PageCounts;
use crate::query::Query;
use crate::toql_api::count::Count;
use crate::toql_api::delete::Delete;
use crate::toql_api::fields::Fields;
use crate::toql_api::insert::Insert;
use crate::toql_api::load::Load;
use crate::toql_api::paths::Paths;
use crate::toql_api::update::Update;
use crate::toql_api::ToqlApi;
use std::borrow::{Borrow, BorrowMut};
use crate::backend::{count::count, delete::delete, insert::insert, load::load, update::update};
use super::row::Row;
use super::MockDb;
#[async_trait]
impl<'a> ToqlApi for MockDb<'a> {
type Row = Row;
type Error = ToqlError;
#[tracing::instrument(skip(self, entity, paths), fields(ty = %<T as crate::table_mapper::mapped::Mapped>::type_name()))]
async fn insert_one<T>(&mut self, entity: &mut T, paths: Paths) -> Result<(), Self::Error>
where
T: Insert,
{
insert::<_, _, T, _, _>(&mut self.backend, &mut [entity], paths).await
}
#[tracing::instrument(skip(self, entities, paths), fields(ty = %<T as crate::table_mapper::mapped::Mapped>::type_name()))]
async fn insert_many<T, Q>(
&mut self,
entities: &mut [Q],
paths: Paths,
) -> Result<(), Self::Error>
where
T: Insert,
Q: BorrowMut<T> + Send,
{
insert(&mut self.backend, entities, paths).await
}
#[tracing::instrument(skip(self, entity, fields), fields(ty = %<T as crate::table_mapper::mapped::Mapped>::type_name()))]
async fn update_one<T>(&mut self, entity: &mut T, fields: Fields) -> Result<(), Self::Error>
where
T: Update + Keyed,
{
update::<_, _, T, _, _>(&mut self.backend, &mut [entity], fields).await
}
#[tracing::instrument(skip(self, entities, fields), fields(ty = %<T as crate::table_mapper::mapped::Mapped>::type_name()))]
async fn update_many<T, Q>(
&mut self,
entities: &mut [Q],
fields: Fields,
) -> Result<(), Self::Error>
where
T: Update + Keyed,
Q: BorrowMut<T> + Send + Sync,
{
update(&mut self.backend, entities, fields).await
}
#[tracing::instrument(skip(self, query), fields(ty = %<T as crate::table_mapper::mapped::Mapped>::type_name()))]
async fn load_one<T, B>(&mut self, query: B) -> Result<T, Self::Error>
where
T: Load<Self::Row, Self::Error>,
B: Borrow<Query<T>> + Send + Sync,
<T as Keyed>::Key: FromRow<Self::Row, Self::Error>,
{
let (mut e, _) = load(&mut self.backend, query, Some(Page::Uncounted(0, 2))).await?;
match e.len() {
0 => Err(ToqlError::NotFound.into()),
1 => Ok(e.pop().unwrap()),
_ => Err(ToqlError::NotUnique.into()),
}
}
#[tracing::instrument(skip(self, query), fields(ty = %<T as crate::table_mapper::mapped::Mapped>::type_name()))]
async fn load_many<T, B>(&mut self, query: B) -> Result<Vec<T>, Self::Error>
where
T: Load<Self::Row, Self::Error>,
B: Borrow<Query<T>> + Send + Sync,
<T as Keyed>::Key: FromRow<Self::Row, Self::Error>,
{
let res = load(&mut self.backend, query, None).await?;
Ok(res.0)
}
#[tracing::instrument(skip(self, query), fields(ty = %<T as crate::table_mapper::mapped::Mapped>::type_name()))]
async fn load_page<T, B>(
&mut self,
query: B,
page: Page,
) -> Result<(Vec<T>, Option<PageCounts>), Self::Error>
where
T: Load<Self::Row, Self::Error>,
B: Borrow<Query<T>> + Send + Sync,
<T as Keyed>::Key: FromRow<Self::Row, Self::Error>,
{
let entities_page = load(&mut self.backend, query, Some(page)).await?;
Ok(entities_page)
}
#[tracing::instrument(skip(self, query), fields(ty = %<T as crate::table_mapper::mapped::Mapped>::type_name()))]
async fn count<T, B>(&mut self, query: B) -> Result<u64, Self::Error>
where
T: Count,
B: Borrow<Query<T>> + Send + Sync,
{
count(&mut self.backend, query).await
}
#[tracing::instrument(skip(self, key), fields(ty = %<<K as Key>::Entity as crate::table_mapper::mapped::Mapped>::type_name()))]
async fn delete_one<K>(&mut self, key: K) -> Result<(), Self::Error>
where
K: Key + Into<Query<<K as Key>::Entity>> + Send,
<K as Key>::Entity: Send + Delete,
{
let query = Query::from(key);
delete(&mut self.backend, query).await?;
Ok(())
}
#[tracing::instrument(skip(self, query), fields(ty = %<T as crate::table_mapper::mapped::Mapped>::type_name()))]
async fn delete_many<T, B>(&mut self, query: B) -> Result<(), Self::Error>
where
T: Delete,
B: Borrow<Query<T>> + Send + Sync,
<Self as ToqlApi>::Error: From<ToqlError>,
{
delete(&mut self.backend, query).await?;
Ok(())
}
}