diff options
Diffstat (limited to 'src/plugins')
| -rw-r--r-- | src/plugins/factoids/database.rs | 72 | ||||
| -rw-r--r-- | src/plugins/factoids/mod.rs | 16 |
2 files changed, 75 insertions, 13 deletions
diff --git a/src/plugins/factoids/database.rs b/src/plugins/factoids/database.rs index 7a6f568..dbf136c 100644 --- a/src/plugins/factoids/database.rs +++ b/src/plugins/factoids/database.rs @@ -1,18 +1,74 @@ -use std::fmt; +#[cfg(feature = "mysql")] +extern crate dotenv; use std::collections::HashMap; -pub trait Database: Send + Sync + fmt::Debug { - fn insert(&mut self, name: String, content: String) -> Option<String>; - fn get(&self, name: &str) -> Option<&str>; +#[cfg(feature = "mysql")] +use diesel::prelude::*; + +#[cfg(feature = "mysql")] +use diesel::mysql::MysqlConnection; + +pub trait Database: Send { + fn insert(&mut self, name: &str, content: &str) -> Option<()>; + fn get(&self, name: &str) -> Option<String>; } impl Database for HashMap<String, String> { - fn insert(&mut self, name: String, content: String) -> Option<String> { - self.insert(name, content) + fn insert(&mut self, name: &str, content: &str) -> Option<()> { + self.insert(String::from(name), String::from(content)).map(|_| ()) + } + + fn get(&self, name: &str) -> Option<String> { + self.get(name).cloned() + } +} + +#[cfg(feature = "mysql")] +#[derive(Queryable)] +struct Factoid { + pub name: String, + pub content: String, +} + +#[cfg(feature = "mysql")] +table! { + factoids (name) { + name -> Varchar, + content -> Varchar, + } +} + +#[cfg(feature = "mysql")] +#[derive(Insertable)] +#[table_name="factoids"] +struct NewFactoid<'a> { + pub name: &'a str, + pub content: &'a str, +} + + +#[cfg(feature = "mysql")] +impl Database for MysqlConnection { + fn insert(&mut self, name: &str, content: &str) -> Option<()> { + let factoid = NewFactoid { + name: name, + content: content, + }; + + ::diesel::insert(&factoid) + .into(factoids::table) + .execute(self) + .ok() + .map(|_| ()) } - fn get(&self, name: &str) -> Option<&str> { - self.get(name).map(String::as_str) + fn get(&self, name: &str) -> Option<String> { + factoids::table + .filter(factoids::columns::name.eq(name)) + .limit(1) + .load::<Factoid>(self) + .ok() + .and_then(|v| v.first().map(|f| f.content.clone())) } } diff --git a/src/plugins/factoids/mod.rs b/src/plugins/factoids/mod.rs index a13bba6..5f9f99a 100644 --- a/src/plugins/factoids/mod.rs +++ b/src/plugins/factoids/mod.rs @@ -1,5 +1,6 @@ extern crate rlua; +use std::fmt; use self::rlua::prelude::*; use irc::client::prelude::*; use irc::error::Error as IrcError; @@ -8,11 +9,11 @@ use std::sync::Mutex; use plugin::*; mod database; -use self::database::*; +use self::database::Database; static LUA_SANDBOX: &'static str = include_str!("sandbox.lua"); -#[derive(PluginName, Debug)] +#[derive(PluginName)] pub struct Factoids<T: Database> { factoids: Mutex<T>, } @@ -40,7 +41,7 @@ impl<T: Database> Factoids<T> { let name = command.tokens.remove(0); try_lock!(self.factoids) - .insert(name, command.tokens.join(" ")); + .insert(&name, &command.tokens.join(" ")); server.send_notice(&command.source, "Successfully added") } @@ -63,7 +64,6 @@ impl<T: Database> Factoids<T> { } fn exec(&self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> { - if command.tokens.len() < 1 { self.invalid_command(server, &command) @@ -88,7 +88,7 @@ impl<T: Database> Factoids<T> { } } } else { - String::from(factoid) + factoid }; server.send_privmsg(&command.target, &value) @@ -170,3 +170,9 @@ impl<T: Database> Plugin for Factoids<T> { } } } + +impl<T: Database> fmt::Debug for Factoids<T> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Factoids {{ ... }}") + } +} |
