proper error handling
This commit is contained in:
parent
687c33829f
commit
ac7b0e7e88
15 changed files with 245 additions and 25 deletions
64
calibre-db/src/data/series.rs
Normal file
64
calibre-db/src/data/series.rs
Normal file
|
@ -0,0 +1,64 @@
|
|||
use rusqlite::{named_params, Connection, Row};
|
||||
use serde::Serialize;
|
||||
|
||||
use super::{
|
||||
error::DataStoreError,
|
||||
pagination::{Pagination, SortOrder},
|
||||
};
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Series {
|
||||
pub id: u64,
|
||||
pub name: String,
|
||||
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)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn multiple(
|
||||
conn: &Connection,
|
||||
limit: u64,
|
||||
cursor: Option<&str>,
|
||||
sort_order: SortOrder,
|
||||
) -> Result<Vec<Self>, DataStoreError> {
|
||||
let pagination = Pagination::new("sort", cursor, limit, sort_order);
|
||||
pagination.paginate(
|
||||
conn,
|
||||
"SELECT id, title, sort, path FROM series",
|
||||
&[],
|
||||
Self::from_row,
|
||||
)
|
||||
}
|
||||
|
||||
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)),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue