aboutsummaryrefslogtreecommitdiff
path: root/src/db/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db/mod.rs')
-rw-r--r--src/db/mod.rs48
1 files changed, 20 insertions, 28 deletions
diff --git a/src/db/mod.rs b/src/db/mod.rs
index 611526f..180012d 100644
--- a/src/db/mod.rs
+++ b/src/db/mod.rs
@@ -1,6 +1,7 @@
use std::{
convert::AsRef,
env,
+ str::FromStr,
};
use chrono::{
@@ -13,10 +14,14 @@ use diesel::{
prelude::*,
r2d2::{ConnectionManager, ManageConnection},
};
-use postgres::Connection as RawPgConn;
+
+use postgres::Client as RawPgConn;
use r2d2_postgres::{
PostgresConnectionManager as RawPgConnMgr,
- TlsMode,
+ postgres::{
+ NoTls,
+ Config,
+ },
};
use anyhow::anyhow;
@@ -32,8 +37,9 @@ mod models;
lazy_static! {
static ref DB_URL: String = env::var("DATABASE_URL").expect("no database url in environment").into();
+ static ref DB_CONFIG: Config = Config::from_str(&DB_URL).expect("parsing db url as config");
static ref CONN_MGR: ConnectionManager<PgConnection> = ConnectionManager::new(DB_URL.clone());
- static ref RAW_CONN_MGR: RawPgConnMgr = RawPgConnMgr::new(DB_URL.clone(), TlsMode::None).unwrap();
+ static ref RAW_CONN_MGR: RawPgConnMgr<NoTls> = RawPgConnMgr::new(DB_CONFIG.clone(), NoTls);
}
#[inline]
@@ -71,7 +77,7 @@ pub fn find_meme<T: AsRef<str>>(conn: &PgConnection, search: T) -> Result<Meme>
}
pub fn query_meme<T: AsRef<str>>(search: T, user_id: Option<u64>, age_desc: bool) -> Result<Vec<(Meme, Metadata)>> {
- let raw_conn = raw_connection()?;
+ let mut raw_conn = raw_connection()?;
let search = format!("%{}%", search.as_ref());
@@ -168,7 +174,7 @@ pub fn delete_meme<T: AsRef<str>>(conn: &PgConnection, search: T, deleted_by: u6
pub fn rare_meme(conn: &PgConnection, audio: bool) -> Result<Meme> {
use rand::prelude::*;
- let raw_conn = raw_connection()?;
+ let mut raw_conn = raw_connection()?;
let rows = raw_conn.query(r#"
WITH
@@ -377,21 +383,19 @@ pub fn stats(conn: &PgConnection) -> Result<Stats> {
.first(conn)
.map_err(Error::from)?;
- let raw_conn = raw_connection()?;
+ let mut raw_conn = raw_connection()?;
- let rows = raw_conn.query(r#"
+ let row = raw_conn.query_one(r#"
SELECT DATE(time) as dt, COUNT(*) FROM invocation_records
GROUP BY dt
ORDER BY COUNT(*) DESC
LIMIT 1;
"#, &[])?;
- let row = rows.get(0);
-
let most_active_day = to_utc_date(row.get(0));
let most_active_day_count: i64 = row.get(1);
- let rows = raw_conn.query(r#"
+ let row = raw_conn.query_one(r#"
SELECT DATE(time) as dt, COUNT(*) FROM invocation_records
INNER JOIN memes ON invocation_records.meme_id = memes.id
WHERE memes.audio_id IS NOT NULL
@@ -400,12 +404,10 @@ pub fn stats(conn: &PgConnection) -> Result<Stats> {
LIMIT 1;
"#, &[])?;
- let row = rows.get(0);
-
let most_active_audio_day = to_utc_date(row.get(0));
let most_active_audio_day_count: i64 = row.get(1);
- let rows = raw_conn.query(r#"
+ let row = raw_conn.query_one(r#"
SELECT user_id, COUNT(*) FROM invocation_records
WHERE random IS TRUE
GROUP BY user_id
@@ -413,12 +415,10 @@ pub fn stats(conn: &PgConnection) -> Result<Stats> {
LIMIT 1;
"#, &[])?;
- let row = rows.get(0);
-
let most_random_invoker: i64 = row.get(0);
let most_random_invoker_count: i64 = row.get(1);
- let rows = raw_conn.query(r#"
+ let row = raw_conn.query_one(r#"
SELECT user_id, COUNT(*) FROM invocation_records
WHERE random IS FALSE
GROUP BY user_id
@@ -426,12 +426,10 @@ pub fn stats(conn: &PgConnection) -> Result<Stats> {
LIMIT 1;
"#, &[])?;
- let row = rows.get(0);
-
let most_specific_invoker: i64 = row.get(0);
let most_specific_invoker_count: i64 = row.get(1);
- let rows = raw_conn.query(r#"
+ let row = raw_conn.query_one(r#"
SELECT memes.title, COUNT(*) FROM invocation_records
INNER JOIN memes ON meme_id = memes.id
WHERE random IS FALSE
@@ -440,12 +438,10 @@ pub fn stats(conn: &PgConnection) -> Result<Stats> {
LIMIT 1;
"#, &[])?;
- let row = rows.get(0);
-
let most_requested_meme = row.get(0);
let most_requested_meme_count: i64 = row.get(1);
- let rows = raw_conn.query(r#"
+ let row = raw_conn.query_one(r#"
SELECT memes.title, COUNT(*) FROM invocation_records
INNER JOIN memes ON meme_id = memes.id
WHERE random IS TRUE
@@ -454,12 +450,10 @@ pub fn stats(conn: &PgConnection) -> Result<Stats> {
LIMIT 1;
"#, &[])?;
- let row = rows.get(0);
-
let most_random_meme = row.get(0);
let most_random_meme_count: i64 = row.get(1);
- let rows = raw_conn.query(r#"
+ let row = raw_conn.query_one(r#"
SELECT memes.title, COUNT(*) FROM invocation_records
INNER JOIN memes ON meme_id = memes.id
GROUP BY memes.title
@@ -467,8 +461,6 @@ pub fn stats(conn: &PgConnection) -> Result<Stats> {
LIMIT 1;
"#, &[])?;
- let row = rows.get(0);
-
let most_invoked_meme = row.get(0);
let most_invoked_meme_count: i64 = row.get(1);
@@ -512,7 +504,7 @@ pub struct MemerInfo {
}
pub fn memers() -> Result<Vec<MemerInfo>> {
- let raw_conn = raw_connection()?;
+ let mut raw_conn = raw_connection()?;
let rows = raw_conn.query(r#"
WITH random_count AS (