From 5928afc3bf83661cd3b11130a31a2d97ef135a9e Mon Sep 17 00:00:00 2001 From: Jokler Date: Sat, 4 Nov 2017 22:46:39 +0100 Subject: Add MySql as a possible database for the Factoids plugin --- src/lib.rs | 7 ++++ src/plugins/factoids/database.rs | 72 +++++++++++++++++++++++++++++++++++----- src/plugins/factoids/mod.rs | 16 ++++++--- 3 files changed, 82 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 5d15802..1f5514e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,6 +31,13 @@ //! Frippy uses the [log](https://docs.rs/log) crate so you can log events //! which might be of interest. +#[cfg(feature = "mysql")] +#[macro_use] +extern crate diesel; +#[cfg(feature = "mysql")] +#[macro_use] +extern crate diesel_codegen; + #[macro_use] extern crate log; #[macro_use] 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; - 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; } impl Database for HashMap { - fn insert(&mut self, name: String, content: String) -> Option { - 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 { + 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 { + factoids::table + .filter(factoids::columns::name.eq(name)) + .limit(1) + .load::(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 { factoids: Mutex, } @@ -40,7 +41,7 @@ impl Factoids { 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 Factoids { } 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 Factoids { } } } else { - String::from(factoid) + factoid }; server.send_privmsg(&command.target, &value) @@ -170,3 +170,9 @@ impl Plugin for Factoids { } } } + +impl fmt::Debug for Factoids { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Factoids {{ ... }}") + } +} -- cgit v1.2.3-70-g09d2