Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft for Repository #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tbd-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ authors = ["Katharina Fey <[email protected]>"]
[dependencies]

# Remove this again soon
rusqlite = "0.13.0"
sqlite = "0.23.9"
futures = "0.1"
8 changes: 4 additions & 4 deletions tbd-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Shared code and data mapper

extern crate rusqlite;
extern crate sqlite;
extern crate futures;

mod mapping;
mod db_tool;
pub mod repository;

pub use mapping::test_this;
//pub use mapping::test_this;
24 changes: 24 additions & 0 deletions tbd-core/src/repository.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use futures::{Future, Stream};

pub type Error = String;

pub trait Queryable<Model> {
fn query(&self, q: Query) -> Box<Stream<Item = Model, Error = String>>;
}

pub enum Query {
All,
}

pub trait KVRespository {
type Error;

fn get<M: 'static>(pk: i64) -> Box<Future<Item = M, Error = Self::Error>>;
}

pub trait Repository {
fn query<M: 'static>(&self, q: Query) -> Box<Stream<Item = M, Error = Error>>
where
Self: Queryable<M>;
}

39 changes: 39 additions & 0 deletions tbd-core/tests/sqlite_repos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
extern crate futures;
extern crate sqlite;
extern crate tbd_core;

mod sqlite_repository;

use sqlite_repository::*;

use tbd_core::repository::{Query, Repository};

use futures::{Future, Stream};

#[test]
fn test() {
let connection = sqlite::open(":memory:").unwrap();

connection
.execute(
"
CREATE TABLE users (name TEXT, age INTEGER);
INSERT INTO users (name, age) VALUES ('Alice', 42);
INSERT INTO users (name, age) VALUES ('Bob', 69);
",
)
.unwrap();

let repo = SqliteRepository {
connection: connection,
};
let result = repo.query::<User>(Query::All);

result
.for_each(|u| {
println!("{:?}", u);
Ok(())
})
.wait()
.unwrap();
}
46 changes: 46 additions & 0 deletions tbd-core/tests/sqlite_repository/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use futures;
use futures::Stream;
use sqlite;
use sqlite::Value;
use tbd_core::repository::*;

pub struct SqliteRepository {
pub connection: sqlite::Connection,
}

impl Repository for SqliteRepository {
fn query<M: 'static>(&self, q: Query) -> Box<Stream<Item = M, Error = String>>
where
Self: Queryable<M>,
{
<Self as Queryable<M>>::query(self, q)
}
}

#[derive(Debug)]
pub struct User {
name: String,
age: i8,
}

impl Queryable<User> for SqliteRepository {
fn query(&self, _q: Query) -> Box<Stream<Item = User, Error = Error>> {
let mut cursor = self.connection
.prepare("SELECT * FROM users WHERE age > ?")
.unwrap()
.cursor();

cursor.bind(&[Value::Integer(50)]).unwrap();

let mut results = Vec::new();

while let Some(row) = cursor.next().unwrap() {
results.push(User {
name: row[0].as_string().unwrap().into(),
age: row[1].as_integer().unwrap() as i8,
});
}

Box::new(futures::stream::iter_ok(results.into_iter()))
}
}