Skip to content

Commit 8e91244

Browse files
committed
Add get_all to database provider
Automatically implemented for all provider if the Provider implement a new trait `GetAllCondition`.
1 parent f17312f commit 8e91244

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

internal/mithril-persistence/src/sqlite/condition.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ impl WhereCondition {
137137
}
138138
}
139139

140+
/// Get all condition builder.
141+
///
142+
/// By default, nothing will be filtered out when using this condition. But you
143+
/// can override this behavior by implementing this trait for your type.
144+
pub trait GetAllCondition {
145+
/// Get the condition for a get all query.
146+
fn get_all_condition() -> WhereCondition {
147+
WhereCondition::default()
148+
}
149+
}
150+
140151
#[cfg(test)]
141152
mod tests {
142153
use super::*;
@@ -349,4 +360,15 @@ mod tests {
349360
assert_eq!("a = ?1", &sql);
350361
assert_eq!(1, params.len());
351362
}
363+
364+
#[test]
365+
fn expression_get_all_default() {
366+
impl GetAllCondition for String {}
367+
368+
let expression = String::get_all_condition();
369+
let (sql, params) = expression.expand();
370+
371+
assert_eq!("true".to_string(), sql);
372+
assert!(params.is_empty());
373+
}
352374
}

internal/mithril-persistence/src/sqlite/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ mod projection;
1010
mod provider;
1111
mod source_alias;
1212

13-
pub use condition::WhereCondition;
13+
pub use condition::{GetAllCondition, WhereCondition};
1414
pub use connection_builder::{ConnectionBuilder, ConnectionOptions};
1515
pub use cursor::EntityCursor;
1616
pub use entity::{HydrationError, SqLiteEntity};
1717
pub use projection::{Projection, ProjectionField};
18-
pub use provider::Provider;
18+
pub use provider::{GetAllProvider, Provider};
1919
pub use source_alias::SourceAlias;
2020

2121
use mithril_common::StdResult;

internal/mithril-persistence/src/sqlite/provider.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use crate::sqlite::condition::GetAllCondition;
12
use anyhow::Context;
23
use mithril_common::StdResult;
34

45
use super::{EntityCursor, SqLiteEntity, SqliteConnection, WhereCondition};
56

6-
/// A Provider is able to performe queries on a database and return iterator of a defined entity.
7+
/// A Provider is able to perform queries on a database and return iterator of a defined entity.
78
/// It aims at being easily testable and adaptable.
89
pub trait Provider<'conn> {
910
/// Entity type returned by the result cursor.
@@ -38,6 +39,22 @@ pub trait Provider<'conn> {
3839
fn get_definition(&self, condition: &str) -> String;
3940
}
4041

42+
/// An extension of the [Provider] that can return all the entities of a given type.
43+
pub trait GetAllProvider<'conn, E: SqLiteEntity> {
44+
/// Return all the entities of the given type.
45+
fn get_all(&'conn self) -> StdResult<EntityCursor<'conn, E>>;
46+
}
47+
48+
impl<'conn, P, E> GetAllProvider<'conn, E> for P
49+
where
50+
P: Provider<'conn, Entity = E> + GetAllCondition,
51+
E: SqLiteEntity,
52+
{
53+
fn get_all(&'conn self) -> StdResult<EntityCursor<'conn, E>> {
54+
self.find(P::get_all_condition())
55+
}
56+
}
57+
4158
#[cfg(test)]
4259
mod tests {
4360
use sqlite::{Connection, Value};
@@ -136,6 +153,8 @@ returning {projection}
136153
}
137154
}
138155

156+
impl GetAllCondition for TestEntityProvider<'_> {}
157+
139158
fn init_database() -> SqliteConnection {
140159
let connection = Connection::open_thread_safe(":memory:").unwrap();
141160
connection
@@ -231,6 +250,7 @@ returning {projection}
231250
);
232251
assert!(cursor.next().is_none());
233252
}
253+
234254
#[test]
235255
fn test_upsertion() {
236256
let connection = init_database();
@@ -258,4 +278,30 @@ returning {projection}
258278
);
259279
assert!(cursor.next().is_none());
260280
}
281+
282+
#[test]
283+
pub fn test_blanket_get_all() {
284+
let connection = init_database();
285+
let provider = TestEntityProvider::new(&connection);
286+
let cursor = provider.get_all().unwrap();
287+
let entities: Vec<TestEntity> = cursor.collect();
288+
289+
assert_eq!(
290+
vec![
291+
TestEntity {
292+
text_data: "row 1".to_string(),
293+
real_data: 1.23,
294+
integer_data: -52,
295+
maybe_null: None
296+
},
297+
TestEntity {
298+
text_data: "row 2".to_string(),
299+
real_data: 2.34,
300+
integer_data: 1789,
301+
maybe_null: Some(0)
302+
}
303+
],
304+
entities
305+
);
306+
}
261307
}

0 commit comments

Comments
 (0)