add: move database drivers to oiseau
This commit is contained in:
parent
40fce4bc77
commit
81036e3733
57 changed files with 638 additions and 1106 deletions
|
@ -1,16 +1,12 @@
|
|||
use crate::{
|
||||
database::drivers::common,
|
||||
execute,
|
||||
model::{Error, Result},
|
||||
cache::Cache,
|
||||
};
|
||||
use super::DataManager;
|
||||
use crate::model::{Error, Result};
|
||||
use super::{DataManager, drivers::common};
|
||||
use oiseau::{cache::Cache, execute};
|
||||
|
||||
pub const NAME_REGEX: &str = r"[^\w_\-\.,!]+";
|
||||
|
||||
impl DataManager {
|
||||
pub async fn init(&self) -> Result<()> {
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -39,10 +35,12 @@ impl DataManager {
|
|||
execute!(&conn, common::CREATE_TABLE_POLLS).unwrap();
|
||||
execute!(&conn, common::CREATE_TABLE_POLLVOTES).unwrap();
|
||||
|
||||
self.2
|
||||
self.0
|
||||
.1
|
||||
.set("atto.active_connections:users".to_string(), "0".to_string())
|
||||
.await;
|
||||
self.2
|
||||
self.0
|
||||
.1
|
||||
.set("atto.active_connections:chats".to_string(), "0".to_string())
|
||||
.await;
|
||||
|
||||
|
@ -54,7 +52,7 @@ impl DataManager {
|
|||
macro_rules! auto_method {
|
||||
($name:ident()@$select_fn:ident -> $query:literal --name=$name_:literal --returns=$returns_:tt) => {
|
||||
pub async fn $name(&self, id: usize) -> Result<$returns_> {
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -73,18 +71,18 @@ macro_rules! auto_method {
|
|||
|
||||
($name:ident()@$select_fn:ident -> $query:literal --name=$name_:literal --returns=$returns_:tt --cache-key-tmpl=$cache_key_tmpl:literal) => {
|
||||
pub async fn $name(&self, id: usize) -> Result<$returns_> {
|
||||
if let Some(cached) = self.2.get(format!($cache_key_tmpl, id)).await {
|
||||
if let Some(cached) = self.0.1.get(format!($cache_key_tmpl, id)).await {
|
||||
if let Ok(c) = serde_json::from_str(&cached) {
|
||||
return Ok(c);
|
||||
}
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_row!(&conn, $query, &[&(id as i64)], |x| {
|
||||
let res = oiseau::query_row!(&conn, $query, &[&(id as i64)], |x| {
|
||||
Ok(Self::$select_fn(x))
|
||||
});
|
||||
|
||||
|
@ -93,7 +91,8 @@ macro_rules! auto_method {
|
|||
}
|
||||
|
||||
let x = res.unwrap();
|
||||
self.2
|
||||
self.0
|
||||
.1
|
||||
.set(
|
||||
format!($cache_key_tmpl, id),
|
||||
serde_json::to_string(&x).unwrap(),
|
||||
|
@ -106,7 +105,7 @@ macro_rules! auto_method {
|
|||
|
||||
($name:ident($selector_t:ty)@$select_fn:ident -> $query:literal --name=$name_:literal --returns=$returns_:tt) => {
|
||||
pub async fn $name(&self, selector: $selector_t) -> Result<$returns_> {
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -125,11 +124,11 @@ macro_rules! auto_method {
|
|||
pub async fn $name(&self, selector: $selector_t) -> Result<$returns_> {
|
||||
let selector = selector.to_string().to_lowercase();
|
||||
|
||||
if let Some(cached) = self.2.get(format!($cache_key_tmpl, selector)).await {
|
||||
if let Some(cached) = self.0.1.get(format!($cache_key_tmpl, selector)).await {
|
||||
return Ok(serde_json::from_str(&cached).unwrap());
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -143,7 +142,8 @@ macro_rules! auto_method {
|
|||
}
|
||||
|
||||
let x = res.unwrap();
|
||||
self.2
|
||||
self.0
|
||||
.1
|
||||
.set(
|
||||
format!($cache_key_tmpl, selector),
|
||||
serde_json::to_string(&x).unwrap(),
|
||||
|
@ -157,19 +157,20 @@ macro_rules! auto_method {
|
|||
($name:ident($selector_t:ty as i64)@$select_fn:ident -> $query:literal --name=$name_:literal --returns=$returns_:tt --cache-key-tmpl=$cache_key_tmpl:literal) => {
|
||||
pub async fn $name(&self, selector: $selector_t) -> Result<$returns_> {
|
||||
if let Some(cached) = self
|
||||
.2
|
||||
.0
|
||||
.1
|
||||
.get(format!($cache_key_tmpl, selector.to_string()))
|
||||
.await
|
||||
{
|
||||
return Ok(serde_json::from_str(&cached).unwrap());
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_row!(&conn, $query, &[&(selector as i64)], |x| {
|
||||
let res = oiseau::query_row!(&conn, $query, &[&(selector as i64)], |x| {
|
||||
Ok(Self::$select_fn(x))
|
||||
});
|
||||
|
||||
|
@ -178,7 +179,8 @@ macro_rules! auto_method {
|
|||
}
|
||||
|
||||
let x = res.unwrap();
|
||||
self.2
|
||||
self.0
|
||||
.1
|
||||
.set(
|
||||
format!($cache_key_tmpl, selector),
|
||||
serde_json::to_string(&x).unwrap(),
|
||||
|
@ -204,7 +206,7 @@ macro_rules! auto_method {
|
|||
}
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -234,7 +236,7 @@ macro_rules! auto_method {
|
|||
}
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -245,7 +247,7 @@ macro_rules! auto_method {
|
|||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.2.remove(format!($cache_key_tmpl, id)).await;
|
||||
self.0.1.remove(format!($cache_key_tmpl, id)).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -267,7 +269,7 @@ macro_rules! auto_method {
|
|||
}
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -298,7 +300,7 @@ macro_rules! auto_method {
|
|||
}
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -309,7 +311,7 @@ macro_rules! auto_method {
|
|||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.2.remove(format!($cache_key_tmpl, id)).await;
|
||||
self.0.1.remove(format!($cache_key_tmpl, id)).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -331,7 +333,7 @@ macro_rules! auto_method {
|
|||
}
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -366,7 +368,7 @@ macro_rules! auto_method {
|
|||
}
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -381,7 +383,7 @@ macro_rules! auto_method {
|
|||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.2.remove(format!($cache_key_tmpl, id)).await;
|
||||
self.0.1.remove(format!($cache_key_tmpl, id)).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -389,7 +391,7 @@ macro_rules! auto_method {
|
|||
|
||||
($name:ident($x:ty) -> $query:literal) => {
|
||||
pub async fn $name(&self, id: usize, x: $x) -> Result<()> {
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -406,7 +408,7 @@ macro_rules! auto_method {
|
|||
|
||||
($name:ident($x:ty) -> $query:literal --cache-key-tmpl=$cache_key_tmpl:literal) => {
|
||||
pub async fn $name(&self, id: usize, x: $x) -> Result<()> {
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -417,7 +419,7 @@ macro_rules! auto_method {
|
|||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.2.remove(format!($cache_key_tmpl, id)).await;
|
||||
self.0.1.remove(format!($cache_key_tmpl, id)).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -425,7 +427,7 @@ macro_rules! auto_method {
|
|||
|
||||
($name:ident($x:ty) -> $query:literal --serde) => {
|
||||
pub async fn $name(&self, id: usize, x: $x) -> Result<()> {
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -446,7 +448,7 @@ macro_rules! auto_method {
|
|||
|
||||
($name:ident($x:ty) -> $query:literal --serde --cache-key-tmpl=$cache_key_tmpl:literal) => {
|
||||
pub async fn $name(&self, id: usize, x: $x) -> Result<()> {
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -461,7 +463,7 @@ macro_rules! auto_method {
|
|||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.2.remove(format!($cache_key_tmpl, id)).await;
|
||||
self.0.1.remove(format!($cache_key_tmpl, id)).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -469,7 +471,7 @@ macro_rules! auto_method {
|
|||
|
||||
($name:ident() -> $query:literal --cache-key-tmpl=$cache_key_tmpl:literal --incr) => {
|
||||
pub async fn $name(&self, id: usize) -> Result<()> {
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -480,7 +482,7 @@ macro_rules! auto_method {
|
|||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.2.remove(format!($cache_key_tmpl, id)).await;
|
||||
self.0.1.remove(format!($cache_key_tmpl, id)).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -488,7 +490,7 @@ macro_rules! auto_method {
|
|||
|
||||
($name:ident() -> $query:literal --cache-key-tmpl=$cache_key_tmpl:literal --decr) => {
|
||||
pub async fn $name(&self, id: usize) -> Result<()> {
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -499,7 +501,7 @@ macro_rules! auto_method {
|
|||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.2.remove(format!($cache_key_tmpl, id)).await;
|
||||
self.0.1.remove(format!($cache_key_tmpl, id)).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -521,7 +523,7 @@ macro_rules! auto_method {
|
|||
}
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -554,7 +556,7 @@ macro_rules! auto_method {
|
|||
}
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -575,7 +577,7 @@ macro_rules! auto_method {
|
|||
pub async fn $name(&self, id: usize, x: $x) -> Result<()> {
|
||||
let y = self.$select_fn(id).await?;
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -596,7 +598,7 @@ macro_rules! auto_method {
|
|||
pub async fn $name(&self, id: usize, x: $x) -> Result<()> {
|
||||
let y = self.$select_fn(id).await?;
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -633,7 +635,7 @@ macro_rules! auto_method {
|
|||
}
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -658,7 +660,7 @@ macro_rules! auto_method {
|
|||
pub async fn $name(&self, id: usize) -> Result<()> {
|
||||
let y = self.$select_fn(id).await?;
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -683,7 +685,7 @@ macro_rules! auto_method {
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue