rocket 5.0 & diesel 2.1
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#![allow(clippy::all)]
|
||||
// #![allow(clippy::all)]
|
||||
|
||||
use crate::cache::*;
|
||||
use crate::db_conn::{Conn, Db};
|
||||
@@ -6,7 +6,6 @@ use crate::random_hasher::random_string;
|
||||
use crate::rds_conn::RdsConn;
|
||||
use crate::schema::*;
|
||||
use chrono::{offset::Utc, DateTime};
|
||||
use diesel::dsl::any;
|
||||
use diesel::sql_types::*;
|
||||
use diesel::{
|
||||
insert_into, BoolExpressionMethods, ExpressionMethods, QueryDsl, QueryResult, RunQueryDsl,
|
||||
@@ -17,7 +16,7 @@ use rocket::serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::collections::HashMap;
|
||||
|
||||
no_arg_sql_function!(RANDOM, (), "Represents the sql RANDOM() function");
|
||||
sql_function!(fn random());
|
||||
sql_function!(fn floor(x: Float) -> Int4);
|
||||
sql_function!(fn float4(x: Int4) -> Float);
|
||||
|
||||
@@ -40,7 +39,7 @@ macro_rules! _get_multi {
|
||||
// eq(any()) is only for postgres
|
||||
db.run(move |c| {
|
||||
$table::table
|
||||
.filter($table::id.eq(any(ids)))
|
||||
.filter($table::id.eq_any(ids))
|
||||
.filter($table::is_deleted.eq(false))
|
||||
.load(with_log!(c))
|
||||
})
|
||||
@@ -84,11 +83,11 @@ macro_rules! base_query {
|
||||
};
|
||||
}
|
||||
|
||||
// TODO: log sql query
|
||||
macro_rules! with_log {
|
||||
($c: expr) => {{
|
||||
use crate::libs::diesel_logger::LoggingConnection;
|
||||
&LoggingConnection::new($c)
|
||||
}};
|
||||
($c: expr) => {
|
||||
$c
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Queryable, Insertable, Serialize, Deserialize, Debug)]
|
||||
@@ -137,7 +136,7 @@ pub struct User {
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "posts"]
|
||||
#[diesel(table_name = posts)]
|
||||
pub struct NewPost {
|
||||
pub content: String,
|
||||
pub cw: String,
|
||||
@@ -154,8 +153,8 @@ impl Post {
|
||||
|
||||
_get_multi!(posts);
|
||||
|
||||
pub async fn get_multi(db: &Db, rconn: &RdsConn, ids: &Vec<i32>) -> QueryResult<Vec<Self>> {
|
||||
let mut cacher = PostCache::init(&rconn);
|
||||
pub async fn get_multi(db: &Db, rconn: &RdsConn, ids: &[i32]) -> QueryResult<Vec<Self>> {
|
||||
let mut cacher = PostCache::init(rconn);
|
||||
let mut cached_posts = cacher.gets(ids).await;
|
||||
let mut id2po = HashMap::<i32, &mut Option<Post>>::new();
|
||||
|
||||
@@ -166,7 +165,7 @@ impl Post {
|
||||
.zip(cached_posts.iter_mut())
|
||||
.filter_map(|(pid, p)| match p {
|
||||
None => {
|
||||
id2po.insert(pid.clone(), p);
|
||||
id2po.insert(*pid, p);
|
||||
Some(pid)
|
||||
}
|
||||
_ => None,
|
||||
@@ -194,12 +193,12 @@ impl Post {
|
||||
|
||||
pub async fn get(db: &Db, rconn: &RdsConn, id: i32) -> QueryResult<Self> {
|
||||
// 注意即使is_deleted也应该缓存和返回
|
||||
let mut cacher = PostCache::init(&rconn);
|
||||
let mut cacher = PostCache::init(rconn);
|
||||
if let Some(p) = cacher.get(&id).await {
|
||||
Ok(p)
|
||||
} else {
|
||||
let p = Self::_get(db, id).await?;
|
||||
cacher.sets(&vec![&p]).await;
|
||||
cacher.sets(&[&p]).await;
|
||||
Ok(p)
|
||||
}
|
||||
}
|
||||
@@ -227,10 +226,10 @@ impl Post {
|
||||
start: i64,
|
||||
limit: i64,
|
||||
) -> QueryResult<Vec<Self>> {
|
||||
let mut cacher = PostListCache::init(room_id, order_mode, &rconn);
|
||||
let mut cacher = PostListCache::init(room_id, order_mode, rconn);
|
||||
if cacher.need_fill().await {
|
||||
let pids =
|
||||
Self::_get_ids_by_page(db, room_id, order_mode.clone(), 0, cacher.i64_minlen())
|
||||
Self::_get_ids_by_page(db, room_id, order_mode, 0, cacher.i64_minlen())
|
||||
.await?;
|
||||
let ps = Self::get_multi(db, rconn, &pids).await?;
|
||||
cacher.fill(&ps).await;
|
||||
@@ -268,7 +267,7 @@ impl Post {
|
||||
0 => query.order(posts::id.desc()),
|
||||
1 => query.order(posts::last_comment_time.desc()),
|
||||
2 => query.order(posts::hot_score.desc()),
|
||||
3 => query.order(RANDOM),
|
||||
3 => query.order(random()),
|
||||
4 => query.order(posts::n_attentions.desc()),
|
||||
_ => panic!("Wrong order mode!"),
|
||||
};
|
||||
@@ -287,7 +286,7 @@ impl Post {
|
||||
start: i64,
|
||||
limit: i64,
|
||||
) -> QueryResult<Vec<Self>> {
|
||||
let search_text2 = search_text.replace("%", "\\%");
|
||||
let search_text2 = search_text.replace('%', "\\%");
|
||||
let pids = db
|
||||
.run(move |c| {
|
||||
let pat;
|
||||
@@ -314,7 +313,7 @@ impl Post {
|
||||
)
|
||||
}
|
||||
1 => {
|
||||
pat = format!("%{}%", search_text2.replace(" ", "%"));
|
||||
pat = format!("%{}%", search_text2.replace(' ', "%"));
|
||||
query
|
||||
.filter(
|
||||
posts::content.like(&pat).or(comments::content
|
||||
@@ -351,7 +350,7 @@ impl Post {
|
||||
}
|
||||
|
||||
pub async fn set_instance_cache(&self, rconn: &RdsConn) {
|
||||
PostCache::init(rconn).sets(&vec![self]).await;
|
||||
PostCache::init(rconn).sets(&[self]).await;
|
||||
}
|
||||
pub async fn refresh_cache(&self, rconn: &RdsConn, is_new: bool) {
|
||||
join!(
|
||||
@@ -409,7 +408,7 @@ impl User {
|
||||
_ => token,
|
||||
};
|
||||
// dbg!(token);
|
||||
let mut cacher = UserCache::init(token, &rconn);
|
||||
let mut cacher = UserCache::init(token, rconn);
|
||||
if let Some(u) = cacher.get().await {
|
||||
Some(u)
|
||||
} else {
|
||||
@@ -465,7 +464,7 @@ impl User {
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "comments"]
|
||||
#[diesel(table_name = comments)]
|
||||
pub struct NewComment {
|
||||
pub content: String,
|
||||
pub author_hash: String,
|
||||
|
||||
Reference in New Issue
Block a user