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
use crate::prelude::*;
use std::marker::{PhantomData, Sync};
use tokio_postgres::Error;
#[crate::async_trait::async_trait]
pub trait Query {
type Output;
async fn execute(&self, client: &tokio_postgres::Client) -> Result<Self::Output, Error>;
}
pub struct Select<T: ToTable + ?Sized> {
_marker: PhantomData<T>,
limit: Option<usize>,
}
impl<T: ToTable + Sync> Select<T> {
pub fn new() -> Select<T> {
Select {
_marker: PhantomData,
limit: None,
}
}
pub fn limit(mut self, limit: usize) -> Select<T> {
self.limit = Some(limit);
self
}
}
#[crate::async_trait::async_trait]
impl<T: ToTable + Sync> Query for Select<T> {
type Output = Vec<T>;
async fn execute(&self, client: &tokio_postgres::Client) -> Result<Self::Output, Error> {
let query = format!(
"SELECT * FROM {}{};",
T::table_name(),
if let Some(limit) = self.limit {
format!(" LIMIT {}", limit)
} else {
String::new()
}
);
Ok(client
.query(&query as &str, &[])
.await?
.into_iter()
.map(<T as ToTable>::from_row)
.collect::<Vec<_>>())
}
}
macro_rules! make_string_query {
($i: ident) => {
pub struct $i(pub Vec<String>);
impl $i {
pub fn single(s: String) -> $i {
$i(vec![s])
}
}
#[crate::async_trait::async_trait]
impl Query for $i {
type Output = ();
async fn execute(
&self,
client: &tokio_postgres::Client,
) -> Result<Self::Output, Error> {
for query in &self.0 {
client.query(query as &str, &[]).await?;
}
Ok(())
}
}
};
}
make_string_query!(CreateTable);
make_string_query!(DropTable);
make_string_query!(CreateType);
make_string_query!(DropType);