add: better app data queries

This commit is contained in:
trisua 2025-07-18 00:14:52 -04:00
parent 9f61d9ce6a
commit 22aea48cc5
12 changed files with 175 additions and 60 deletions

View file

@ -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 }
)
}