rust-diesel

Injecting a Diesel connection into an Iron middleware

让人想犯罪 __ 提交于 2019-12-04 05:21:23
问题 In writing my tests, I'd like to be able to inject a connection into the request so that I can wrap the entire test case in a transaction (even if there is more than one request in the test case). I've attempted to do this using a BeforeMiddleware which I can link in my test cases to insert a connection, as such: pub type DatabaseConnection = PooledConnection<ConnectionManager<PgConnection>>; pub struct DatabaseOverride { conn: DatabaseConnection, } impl BeforeMiddleware for DatabaseOverride

Injecting a Diesel connection into an Iron middleware

不羁的心 提交于 2019-12-02 06:15:33
In writing my tests, I'd like to be able to inject a connection into the request so that I can wrap the entire test case in a transaction (even if there is more than one request in the test case). I've attempted to do this using a BeforeMiddleware which I can link in my test cases to insert a connection, as such: pub type DatabaseConnection = PooledConnection<ConnectionManager<PgConnection>>; pub struct DatabaseOverride { conn: DatabaseConnection, } impl BeforeMiddleware for DatabaseOverride { fn before(&self, req: &mut Request) -> IronResult<()> { req.extensions_mut().entry::<DatabaseOverride

Creating Diesel.rs queries with a dynamic number of .and()'s

谁说我不能喝 提交于 2019-12-01 04:28:08
While playing with Diesel, I got stuck writing a function which takes an vector of String s as input and does the following: Combine all String s to a large query run the query on the Connection process the result return a Vec If I construct the query in one step, it works just fine: fn get_books(authors: Vec<String>, connection: SqliteConnection) { use schema::ebook::dsl::*; let inner = author .like(format!("%{}%", authors[0])) .and(author.like(format!("%{}%", authors[1]))) .and(author.like(format!("%{}%", authors[2]))); ebook .filter(inner) .load::<Ebook>(&connection) .expect("Error loading

How do I implement Queryable and Insertable for custom field types in Diesel?

為{幸葍}努か 提交于 2019-11-30 14:51:28
问题 I have an SQL table that I want to work with through Diesel: CREATE TABLE records ( id BIGSERIAL PRIMARY KEY, record_type SMALLINT NOT NULL, value DECIMAL(10, 10) NOT NULL ) This table generates the following schema: table! { records (id) { id -> Int8, record_type -> Int2, value -> Numeric, } } Diesel exports decimals as bigdecimal::BigDecimal , but I'd like to work with decimal::d128 instead. I also want to map record_type to an enum, so I declare my model like this: use decimal::d128; pub

How do I implement Queryable and Insertable for custom field types in Diesel?

旧巷老猫 提交于 2019-11-30 12:38:06
I have an SQL table that I want to work with through Diesel: CREATE TABLE records ( id BIGSERIAL PRIMARY KEY, record_type SMALLINT NOT NULL, value DECIMAL(10, 10) NOT NULL ) This table generates the following schema: table! { records (id) { id -> Int8, record_type -> Int2, value -> Numeric, } } Diesel exports decimals as bigdecimal::BigDecimal , but I'd like to work with decimal::d128 instead. I also want to map record_type to an enum, so I declare my model like this: use decimal::d128; pub enum RecordType { A, B, } pub struct Record { pub id: i64, pub record_type: RecordType, pub value: d128,

How do I combine multiple functions using Diesel into one through abstraction?

ぐ巨炮叔叔 提交于 2019-11-29 18:12:30
I have the following two functions: pub fn get_most_recent_eth_entry(conn: &SqliteConnection) -> Result<i32, Error> { let res = types::ethereum::table .order(types::ethereum::time.desc()) .limit(1) .load::<types::ETHRecord>(&*conn); match res { Ok(x) => { if x.len() > 0 { Ok(x.get(0).unwrap().time) } else { Ok(0) } } Err(err) => Err(format_err!("Error here! {:?}", err)), } } pub fn get_most_recent_btc_entry(conn: &SqliteConnection) -> Result<i32, Error> { let res = types::bitcoin::table .order(types::bitcoin::time.desc()) .limit(1) .load::<types::BTCRecord>(&*conn); match res { Ok(x) => { if x

How do I combine multiple functions using Diesel into one through abstraction?

∥☆過路亽.° 提交于 2019-11-28 11:46:13
问题 I have the following two functions: pub fn get_most_recent_eth_entry(conn: &SqliteConnection) -> Result<i32, Error> { let res = types::ethereum::table .order(types::ethereum::time.desc()) .limit(1) .load::<types::ETHRecord>(&*conn); match res { Ok(x) => { if x.len() > 0 { Ok(x.get(0).unwrap().time) } else { Ok(0) } } Err(err) => Err(format_err!("Error here! {:?}", err)), } } pub fn get_most_recent_btc_entry(conn: &SqliteConnection) -> Result<i32, Error> { let res = types::bitcoin::table