little-hesinde/calibre-db/src/data/series.rs

91 lines
3 KiB
Rust
Raw Normal View History

2024-05-10 14:25:18 +02:00
//! Series data.
2024-05-06 09:09:40 +02:00
use rusqlite::{named_params, Connection, Row};
use serde::Serialize;
use super::{
error::DataStoreError,
pagination::{Pagination, SortOrder},
};
2024-05-10 14:25:18 +02:00
/// Series in calibre.
2024-05-09 11:18:47 +02:00
#[derive(Debug, Clone, Serialize)]
2024-05-06 09:09:40 +02:00
pub struct Series {
2024-05-10 14:25:18 +02:00
/// Id in database.
2024-05-06 09:09:40 +02:00
pub id: u64,
2024-05-10 14:25:18 +02:00
/// Series name.
2024-05-06 09:09:40 +02:00
pub name: String,
2024-05-10 14:25:18 +02:00
/// Series name for sorting.
2024-05-06 09:09:40 +02:00
pub sort: String,
}
impl Series {
fn from_row(row: &Row<'_>) -> Result<Self, rusqlite::Error> {
Ok(Self {
id: row.get(0)?,
name: row.get(1)?,
sort: row.get(2)?,
})
}
2024-05-10 14:25:18 +02:00
/// Fetch series data from calibre, starting at `cursor`, fetching up to an amount of `limit` and
/// ordering by `sort_order`.
2024-05-06 09:09:40 +02:00
pub fn multiple(
conn: &Connection,
limit: u64,
cursor: Option<&str>,
2024-05-06 16:25:15 +02:00
sort_order: &SortOrder,
2024-05-06 09:09:40 +02:00
) -> Result<Vec<Self>, DataStoreError> {
2024-05-06 16:25:15 +02:00
let pagination = Pagination::new("sort", cursor, limit, *sort_order);
2024-05-06 09:09:40 +02:00
pagination.paginate(
conn,
2024-05-06 16:25:15 +02:00
"SELECT id, name, sort FROM series",
2024-05-06 09:09:40 +02:00
&[],
Self::from_row,
)
}
2024-05-10 14:25:18 +02:00
/// Fetch a single series with id `id`.
2024-05-06 16:25:15 +02:00
pub fn scalar_series(conn: &Connection, id: u64) -> Result<Self, DataStoreError> {
let mut stmt = conn.prepare("SELECT id, name, sort FROM series WHERE id = (:id)")?;
let params = named_params! { ":id": id };
Ok(stmt.query_row(params, Self::from_row)?)
}
2024-05-10 14:25:18 +02:00
/// Get the series a book with id `id` is in, as well as the book's position within the series.
2024-05-06 09:09:40 +02:00
pub fn book_series(
conn: &Connection,
book_id: u64,
) -> Result<Option<(Self, f64)>, DataStoreError> {
let mut stmt = conn.prepare(
"SELECT series.id, series.name, series.sort, books.series_index FROM series \
INNER JOIN books_series_link ON series.id = books_series_link.series \
INNER JOIN books ON books.id = books_series_link.book \
WHERE books_series_link.book = (:id)",
)?;
let params = named_params! { ":id": book_id };
let from_row = |row: &Row<'_>| {
let series = Self::from_row(row)?;
let series_idx = row.get(3)?;
Ok((series, series_idx))
};
match stmt.query_row(params, from_row) {
Ok(series) => Ok(Some(series)),
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
Err(e) => Err(DataStoreError::SqliteError(e)),
}
}
2024-05-06 16:25:15 +02:00
2024-05-10 14:25:18 +02:00
/// Check if there are more series before the specified cursor.
2024-05-06 16:25:15 +02:00
pub fn has_previous_series(conn: &Connection, sort_name: &str) -> Result<bool, DataStoreError> {
Pagination::has_prev_or_more(conn, "series", sort_name, &SortOrder::DESC)
}
2024-05-10 14:25:18 +02:00
/// Check if there are more series after the specified cursor.
2024-05-06 16:25:15 +02:00
pub fn has_more_series(conn: &Connection, sort_name: &str) -> Result<bool, DataStoreError> {
Pagination::has_prev_or_more(conn, "series", sort_name, &SortOrder::ASC)
}
2024-05-06 09:09:40 +02:00
}