/
main.rs
65 lines (56 loc) · 1.46 KB
/
main.rs
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
#[macro_use]
extern crate diesel;
use diesel::{
expression::BoxableExpression,
pg::Pg,
query_dsl::{QueryDsl, RunQueryDsl},
sql_types::Bool,
ExpressionMethods,
};
table! {
#[allow(unused_imports)]
use diesel::sql_types::*;
connection(id) {
id -> BigInt,
tunnel_id -> BigInt,
}
}
table! {
#[allow(unused_imports)]
use diesel::sql_types::*;
tunnel (id) {
id -> BigInt,
name -> Text,
}
}
#[derive(Debug, Associations, Identifiable, Queryable)]
#[table_name = "connection"]
#[primary_key(id)]
#[belongs_to(Tunnel)]
pub struct Connection {
pub id: i64,
pub tunnel_id: i64,
}
#[derive(Queryable, Identifiable, Clone, Debug, PartialEq, Eq)]
#[table_name = "tunnel"]
pub struct Tunnel {
pub id: i64,
pub name: String,
}
joinable!(connection -> tunnel(tunnel_id));
allow_tables_to_appear_in_same_query!(connection, tunnel);
fn _filter(
conn: &diesel::PgConnection,
) -> Result<Vec<(Tunnel, Option<Connection>)>, Box<dyn std::error::Error>> {
let mut query = tunnel::table.left_join(connection::table).into_boxed();
for filter in filters() {
query = query.filter(filter);
}
Ok(query.get_results(conn)?)
}
fn filters() -> Vec<Box<dyn BoxableExpression<tunnel::table, Pg, SqlType = Bool>>> {
let mut wheres: Vec<Box<dyn BoxableExpression<tunnel::table, Pg, SqlType = Bool>>> = Vec::new();
wheres.push(Box::new(tunnel::name.eq("adam")));
wheres
}
fn main() {}