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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue