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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
use std::collections::HashMap;
#[cfg(feature = "mysql")]
use std::sync::Arc;
#[cfg(feature = "mysql")]
use diesel::mysql::MysqlConnection;
#[cfg(feature = "mysql")]
use diesel::prelude::*;
#[cfg(feature = "mysql")]
use failure::ResultExt;
#[cfg(feature = "mysql")]
use r2d2::Pool;
#[cfg(feature = "mysql")]
use r2d2_diesel::ConnectionManager;
use chrono::NaiveDateTime;
use super::error::*;
#[cfg_attr(feature = "mysql", derive(Queryable))]
#[derive(Clone, Debug)]
pub struct Quote {
pub quotee: String,
pub channel: String,
pub idx: i32,
pub content: String,
pub author: String,
pub created: NaiveDateTime,
}
#[cfg_attr(feature = "mysql", derive(Insertable))]
#[cfg_attr(feature = "mysql", table_name = "quotes")]
pub struct NewQuote<'a> {
pub quotee: &'a str,
pub channel: &'a str,
pub idx: i32,
pub content: &'a str,
pub author: &'a str,
pub created: NaiveDateTime,
}
pub trait Database: Send + Sync {
fn insert_quote(&mut self, quote: &NewQuote) -> Result<(), QuoteError>;
fn get_user_quote(&self, quotee: &str, channel: &str, idx: i32) -> Result<Quote, QuoteError>;
fn get_channel_quote(&self, channel: &str, idx: i32) -> Result<Quote, QuoteError>;
fn count_user_quotes(&self, quotee: &str, channel: &str) -> Result<i32, QuoteError>;
fn count_channel_quotes(&self, channel: &str) -> Result<i32, QuoteError>;
}
// HashMap
impl<S: ::std::hash::BuildHasher + Send + Sync> Database
for HashMap<(String, String, i32), Quote, S>
{
fn insert_quote(&mut self, quote: &NewQuote) -> Result<(), QuoteError> {
let quote = Quote {
quotee: quote.quotee.to_owned(),
channel: quote.channel.to_owned(),
idx: quote.idx,
content: quote.content.to_owned(),
author: quote.author.to_owned(),
created: quote.created,
};
let quotee = quote.quotee.clone();
let channel = quote.channel.clone();
match self.insert((quotee, channel, quote.idx), quote) {
None => Ok(()),
Some(_) => Err(ErrorKind::Duplicate)?,
}
}
fn get_user_quote(&self, quotee: &str, channel: &str, idx: i32) -> Result<Quote, QuoteError> {
Ok(self
.get(&(quotee.to_owned(), channel.to_owned(), idx))
.cloned()
.ok_or(ErrorKind::NotFound)?)
}
fn get_channel_quote(&self, channel: &str, idx: i32) -> Result<Quote, QuoteError> {
Ok(self
.iter()
.filter(|&(&(_, ref c, _), _)| c == channel)
.nth(idx as usize - 1)
.ok_or(ErrorKind::NotFound)?
.1
.clone())
}
fn count_user_quotes(&self, quotee: &str, channel: &str) -> Result<i32, QuoteError> {
Ok(self
.iter()
.filter(|&(&(ref n, ref c, _), _)| n == quotee && c == channel)
.count() as i32)
}
fn count_channel_quotes(&self, channel: &str) -> Result<i32, QuoteError> {
Ok(self
.iter()
.filter(|&(&(_, ref c, _), _)| c == channel)
.count() as i32)
}
}
// Diesel automatically defines the quotes module as public.
// We create a schema module to keep it private.
#[cfg(feature = "mysql")]
mod schema {
table! {
quotes (quotee, channel, idx) {
quotee -> Varchar,
channel -> Varchar,
idx -> Integer,
content -> Text,
author -> Varchar,
created -> Timestamp,
}
}
}
#[cfg(feature = "mysql")]
use self::schema::quotes;
#[cfg(feature = "mysql")]
impl Database for Arc<Pool<ConnectionManager<MysqlConnection>>> {
fn insert_quote(&mut self, quote: &NewQuote) -> Result<(), QuoteError> {
let conn = &*self.get().context(ErrorKind::NoConnection)?;
diesel::insert_into(quotes::table)
.values(quote)
.execute(conn)
.context(ErrorKind::MysqlError)?;
Ok(())
}
fn get_user_quote(&self, quotee: &str, channel: &str, idx: i32) -> Result<Quote, QuoteError> {
let conn = &*self.get().context(ErrorKind::NoConnection)?;
Ok(quotes::table
.find((quotee, channel, idx))
.first(conn)
.context(ErrorKind::MysqlError)?)
}
fn get_channel_quote(&self, channel: &str, idx: i32) -> Result<Quote, QuoteError> {
let conn = &*self.get().context(ErrorKind::NoConnection)?;
Ok(quotes::table
.filter(quotes::columns::channel.eq(channel))
.offset(idx as i64 - 1)
.first(conn)
.context(ErrorKind::MysqlError)?)
}
fn count_user_quotes(&self, quotee: &str, channel: &str) -> Result<i32, QuoteError> {
let conn = &*self.get().context(ErrorKind::NoConnection)?;
let count: Result<i64, _> = quotes::table
.filter(quotes::columns::quotee.eq(quotee))
.filter(quotes::columns::channel.eq(channel))
.count()
.get_result(conn);
match count {
Ok(c) => Ok(c as i32),
Err(diesel::NotFound) => Ok(0),
Err(e) => Err(e).context(ErrorKind::MysqlError)?,
}
}
fn count_channel_quotes(&self, channel: &str) -> Result<i32, QuoteError> {
let conn = &*self.get().context(ErrorKind::NoConnection)?;
let count: Result<i64, _> = quotes::table
.filter(quotes::columns::channel.eq(channel))
.count()
.get_result(conn);
match count {
Ok(c) => Ok(c as i32),
Err(diesel::NotFound) => Ok(0),
Err(e) => Err(e).context(ErrorKind::MysqlError)?,
}
}
}
|