aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJokler <jokler.contact@gmail.com>2017-11-04 22:46:39 +0100
committerJokler <jokler.contact@gmail.com>2017-12-24 00:40:06 +0100
commit5928afc3bf83661cd3b11130a31a2d97ef135a9e (patch)
tree4d1ad028c3a7130eb98c18ef5b97bea9f52f2e2f /src
parentf3d679da59a64711ef96042668b26dffd1e662d5 (diff)
downloadfrippy-5928afc3bf83661cd3b11130a31a2d97ef135a9e.tar.gz
frippy-5928afc3bf83661cd3b11130a31a2d97ef135a9e.zip
Add MySql as a possible database for the Factoids plugin
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs7
-rw-r--r--src/plugins/factoids/database.rs72
-rw-r--r--src/plugins/factoids/mod.rs16
3 files changed, 82 insertions, 13 deletions
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<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 {{ ... }}")
+ }
+}