add: better app data queries
This commit is contained in:
parent
9f61d9ce6a
commit
22aea48cc5
12 changed files with 175 additions and 60 deletions
|
@ -12,9 +12,9 @@ impl DataManager {
|
|||
pub(crate) fn get_app_data_from_row(x: &PostgresRow) -> AppData {
|
||||
AppData {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
app: get!(x->2(i64)) as usize,
|
||||
key: get!(x->3(String)),
|
||||
value: get!(x->4(String)),
|
||||
app: get!(x->1(i64)) as usize,
|
||||
key: get!(x->2(String)),
|
||||
value: get!(x->3(String)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,10 +44,7 @@ impl DataManager {
|
|||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Get all app_data by owner.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - the ID of the user to fetch app_data for
|
||||
/// Get all app_data by the given query.
|
||||
pub async fn query_app_data(&self, query: AppDataQuery) -> Result<AppDataQueryResult> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
|
@ -57,12 +54,13 @@ impl DataManager {
|
|||
let query_str = query.to_string().replace(
|
||||
"%q%",
|
||||
&match query.query {
|
||||
AppDataSelectQuery::Like(_, _) => format!("v LIKE $1"),
|
||||
AppDataSelectQuery::KeyIs(_) => format!("k = $1"),
|
||||
AppDataSelectQuery::LikeJson(_, _) => format!("v LIKE $1"),
|
||||
},
|
||||
);
|
||||
|
||||
let res = match query.mode {
|
||||
AppDataSelectMode::One => AppDataQueryResult::One(
|
||||
AppDataSelectMode::One(_) => AppDataQueryResult::One(
|
||||
match query_row!(&conn, &query_str, params![&query.query.to_string()], |x| {
|
||||
Ok(Self::get_app_data_from_row(x))
|
||||
}) {
|
||||
|
@ -70,7 +68,15 @@ impl DataManager {
|
|||
Err(_) => return Err(Error::GeneralNotFound("app_data".to_string())),
|
||||
},
|
||||
),
|
||||
AppDataSelectMode::Many(_, _, _) => AppDataQueryResult::Many(
|
||||
AppDataSelectMode::Many(_, _) => AppDataQueryResult::Many(
|
||||
match query_rows!(&conn, &query_str, params![&query.query.to_string()], |x| {
|
||||
Self::get_app_data_from_row(x)
|
||||
}) {
|
||||
Ok(x) => x,
|
||||
Err(_) => return Err(Error::GeneralNotFound("app_data".to_string())),
|
||||
},
|
||||
),
|
||||
AppDataSelectMode::ManyJson(_, _, _) => AppDataQueryResult::Many(
|
||||
match query_rows!(&conn, &query_str, params![&query.query.to_string()], |x| {
|
||||
Self::get_app_data_from_row(x)
|
||||
}) {
|
||||
|
@ -83,6 +89,35 @@ impl DataManager {
|
|||
Ok(res)
|
||||
}
|
||||
|
||||
/// Delete all app_data matched by the given query.
|
||||
pub async fn query_delete_app_data(&self, query: AppDataQuery) -> Result<()> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let query_str = query
|
||||
.to_string()
|
||||
.replace(
|
||||
"%q%",
|
||||
&match query.query {
|
||||
AppDataSelectQuery::KeyIs(_) => format!("k = $1"),
|
||||
AppDataSelectQuery::LikeJson(_, _) => format!("v LIKE $1"),
|
||||
},
|
||||
)
|
||||
.replace("SELECT * FROM", "SELECT id FROM");
|
||||
|
||||
if let Err(e) = execute!(
|
||||
&conn,
|
||||
&format!("DELETE FROM app_data WHERE id IN ({query_str})"),
|
||||
params![&query.query.to_string()]
|
||||
) {
|
||||
return Err(Error::MiscError(e.to_string()));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
const MAXIMUM_FREE_APP_DATA: usize = 5;
|
||||
const MAXIMUM_DATA_SIZE: usize = 205_000;
|
||||
|
||||
|
@ -101,9 +136,9 @@ impl DataManager {
|
|||
}
|
||||
|
||||
if data.value.len() < 2 {
|
||||
return Err(Error::DataTooShort("key".to_string()));
|
||||
return Err(Error::DataTooShort("value".to_string()));
|
||||
} else if data.value.len() > Self::MAXIMUM_DATA_SIZE {
|
||||
return Err(Error::DataTooLong("key".to_string()));
|
||||
return Err(Error::DataTooLong("value".to_string()));
|
||||
}
|
||||
|
||||
// check number of app_data
|
||||
|
|
|
@ -29,7 +29,7 @@ impl DataManager {
|
|||
}
|
||||
|
||||
auto_method!(get_app_by_id(usize as i64)@get_app_from_row -> "SELECT * FROM apps WHERE id = $1" --name="app" --returns=ThirdPartyApp --cache-key-tmpl="atto.app:{}");
|
||||
auto_method!(get_app_by_api_key(&str)@get_app_from_row -> "SELECT * FROM apps WHERE api_key = $1" --name="app" --returns=ThirdPartyApp --cache-key-tmpl="atto.app:{}");
|
||||
auto_method!(get_app_by_api_key(&str)@get_app_from_row -> "SELECT * FROM apps WHERE api_key = $1" --name="app" --returns=ThirdPartyApp --cache-key-tmpl="atto.app_k:{}");
|
||||
|
||||
/// Get all apps by user.
|
||||
///
|
||||
|
@ -134,7 +134,7 @@ impl DataManager {
|
|||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.0.1.remove(format!("atto.app:{}", id)).await;
|
||||
self.cache_clear_app(&app).await;
|
||||
|
||||
// remove data
|
||||
let res = execute!(
|
||||
|
@ -151,14 +151,21 @@ impl DataManager {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
auto_method!(update_app_title(&str)@get_app_by_id:FinePermission::MANAGE_APPS; -> "UPDATE apps SET title = $1 WHERE id = $2" --cache-key-tmpl="atto.app:{}");
|
||||
auto_method!(update_app_homepage(&str)@get_app_by_id:FinePermission::MANAGE_APPS; -> "UPDATE apps SET homepage = $1 WHERE id = $2" --cache-key-tmpl="atto.app:{}");
|
||||
auto_method!(update_app_redirect(&str)@get_app_by_id:FinePermission::MANAGE_APPS; -> "UPDATE apps SET redirect = $1 WHERE id = $2" --cache-key-tmpl="atto.app:{}");
|
||||
auto_method!(update_app_quota_status(AppQuota) -> "UPDATE apps SET quota_status = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.app:{}");
|
||||
auto_method!(update_app_scopes(Vec<AppScope>)@get_app_by_id:FinePermission::MANAGE_APPS; -> "UPDATE apps SET scopes = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.app:{}");
|
||||
auto_method!(update_app_api_key(&str) -> "UPDATE apps SET api_key = $1 WHERE id = $2" --cache-key-tmpl="atto.app:{}");
|
||||
auto_method!(update_app_data_used(i32) -> "UPDATE apps SET data_used = $1 WHERE id = $2" --cache-key-tmpl="atto.app:{}");
|
||||
pub async fn cache_clear_app(&self, app: &ThirdPartyApp) {
|
||||
self.0.1.remove(format!("atto.app:{}", app.id)).await;
|
||||
self.0.1.remove(format!("atto.app_k:{}", app.api_key)).await;
|
||||
}
|
||||
|
||||
auto_method!(incr_app_grants() -> "UPDATE apps SET grants = grants + 1 WHERE id = $1" --cache-key-tmpl="atto.app:{}" --incr);
|
||||
auto_method!(decr_app_grants()@get_app_by_id -> "UPDATE apps SET grants = grants - 1 WHERE id = $1" --cache-key-tmpl="atto.app:{}" --decr=grants);
|
||||
auto_method!(update_app_title(&str)@get_app_by_id:FinePermission::MANAGE_APPS; -> "UPDATE apps SET title = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_app);
|
||||
auto_method!(update_app_homepage(&str)@get_app_by_id:FinePermission::MANAGE_APPS; -> "UPDATE apps SET homepage = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_app);
|
||||
auto_method!(update_app_redirect(&str)@get_app_by_id:FinePermission::MANAGE_APPS; -> "UPDATE apps SET redirect = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_app);
|
||||
auto_method!(update_app_quota_status(AppQuota)@get_app_by_id -> "UPDATE apps SET quota_status = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_app);
|
||||
auto_method!(update_app_scopes(Vec<AppScope>)@get_app_by_id:FinePermission::MANAGE_APPS; -> "UPDATE apps SET scopes = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_app);
|
||||
auto_method!(update_app_api_key(&str)@get_app_by_id -> "UPDATE apps SET api_key = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_app);
|
||||
|
||||
auto_method!(update_app_data_used(i32)@get_app_by_id -> "UPDATE apps SET data_used = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_app);
|
||||
auto_method!(add_app_data_used(i32)@get_app_by_id -> "UPDATE apps SET data_used = data_used + $1 WHERE id = $2" --cache-key-tmpl=cache_clear_app);
|
||||
|
||||
auto_method!(incr_app_grants()@get_app_by_id -> "UPDATE apps SET grants = grants + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_app --incr);
|
||||
auto_method!(decr_app_grants()@get_app_by_id -> "UPDATE apps SET grants = grants - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_app --decr=grants);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
CREATE TABLE IF NOT EXISTS app_data (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
owner BIGINT NOT NULL,
|
||||
app BIGINT NOT NULL,
|
||||
k TEXT NOT NULL,
|
||||
v TEXT NOT NULL
|
||||
|
|
|
@ -8,5 +8,6 @@ CREATE TABLE IF NOT EXISTS apps (
|
|||
quota_status TEXT NOT NULL,
|
||||
banned INT NOT NULL,
|
||||
grants INT NOT NULL,
|
||||
scopes TEXT NOT NULL
|
||||
scopes TEXT NOT NULL,
|
||||
data_used INT NOT NULL CHECK (data_used >= 0)
|
||||
)
|
||||
|
|
|
@ -146,34 +146,46 @@ impl AppData {
|
|||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub enum AppDataSelectQuery {
|
||||
Like(String, String),
|
||||
KeyIs(String),
|
||||
LikeJson(String, String),
|
||||
}
|
||||
|
||||
impl Display for AppDataSelectQuery {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(&match self {
|
||||
Self::Like(k, v) => format!("%\"{k}\":\"{v}\"%"),
|
||||
Self::KeyIs(k) => k.to_owned(),
|
||||
Self::LikeJson(k, v) => format!("%\"{k}\":\"{v}\"%"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub enum AppDataSelectMode {
|
||||
/// Select a single row.
|
||||
One,
|
||||
/// Select a single row (with offset).
|
||||
One(usize),
|
||||
/// Select multiple rows at once.
|
||||
///
|
||||
/// `(limit, offset)`
|
||||
Many(usize, usize),
|
||||
/// Select multiple rows at once.
|
||||
///
|
||||
/// `(order by top level key, limit, offset)`
|
||||
Many(String, usize, usize),
|
||||
ManyJson(String, usize, usize),
|
||||
}
|
||||
|
||||
impl Display for AppDataSelectMode {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(&match self {
|
||||
Self::One => "LIMIT 1".to_string(),
|
||||
Self::Many(order_by_top_level_key, limit, offset) => {
|
||||
Self::One(offset) => format!("LIMIT 1 OFFSET {offset}"),
|
||||
Self::Many(limit, offset) => {
|
||||
format!(
|
||||
"ORDER BY v::jsonb->>'{order_by_top_level_key}' LIMIT {} OFFSET {offset}",
|
||||
"LIMIT {} OFFSET {offset}",
|
||||
if *limit > 1024 { 1024 } else { *limit }
|
||||
)
|
||||
}
|
||||
Self::ManyJson(order_by_top_level_key, limit, offset) => {
|
||||
format!(
|
||||
"ORDER BY v::jsonb->>'{order_by_top_level_key}' DESC LIMIT {} OFFSET {offset}",
|
||||
if *limit > 1024 { 1024 } else { *limit }
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue