paginated authors
This commit is contained in:
parent
352d4e0a7a
commit
ead3672570
12 changed files with 175 additions and 23 deletions
|
@ -34,7 +34,7 @@ impl Calibre {
|
|||
&self,
|
||||
limit: u64,
|
||||
cursor: Option<&str>,
|
||||
sort_order: SortOrder,
|
||||
sort_order: &SortOrder,
|
||||
) -> Result<Vec<Author>, DataStoreError> {
|
||||
let conn = self.pool.get()?;
|
||||
Author::multiple(&conn, limit, cursor, sort_order)
|
||||
|
@ -80,6 +80,16 @@ impl Calibre {
|
|||
let conn = self.pool.get()?;
|
||||
Series::book_series(&conn, id)
|
||||
}
|
||||
|
||||
pub fn has_previous_authors(&self, author_sort: &str) -> Result<bool, DataStoreError> {
|
||||
let conn = self.pool.get()?;
|
||||
Author::has_previous_authors(&conn, author_sort)
|
||||
}
|
||||
|
||||
pub fn has_more_authors(&self, author_sort: &str) -> Result<bool, DataStoreError> {
|
||||
let conn = self.pool.get()?;
|
||||
Author::has_more_authors(&conn, author_sort)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -100,7 +110,7 @@ mod tests {
|
|||
#[test]
|
||||
fn authors() {
|
||||
let c = init_calibre();
|
||||
let authors = c.authors(10, None, SortOrder::ASC).unwrap();
|
||||
let authors = c.authors(10, None, &SortOrder::ASC).unwrap();
|
||||
assert_eq!(authors.len(), 3);
|
||||
}
|
||||
|
||||
|
@ -114,24 +124,24 @@ mod tests {
|
|||
#[test]
|
||||
fn pagination() {
|
||||
let c = init_calibre();
|
||||
let authors = c.authors(1, None, SortOrder::ASC).unwrap();
|
||||
let authors = c.authors(1, None, &SortOrder::ASC).unwrap();
|
||||
assert_eq!(authors.len(), 1);
|
||||
assert_eq!(authors[0].name, "Kevin R. Grazier");
|
||||
|
||||
let authors = c
|
||||
.authors(1, Some(&authors[0].sort), SortOrder::ASC)
|
||||
.authors(1, Some(&authors[0].sort), &SortOrder::ASC)
|
||||
.unwrap();
|
||||
assert_eq!(authors.len(), 1);
|
||||
assert_eq!(authors[0].name, "Terry Pratchett");
|
||||
|
||||
let authors = c
|
||||
.authors(1, Some(&authors[0].sort), SortOrder::ASC)
|
||||
.authors(1, Some(&authors[0].sort), &SortOrder::ASC)
|
||||
.unwrap();
|
||||
assert_eq!(authors.len(), 1);
|
||||
assert_eq!(authors[0].name, "Edward Noyes Westcott");
|
||||
|
||||
let authors = c
|
||||
.authors(1, Some(&authors[0].sort), SortOrder::DESC)
|
||||
.authors(1, Some(&authors[0].sort), &SortOrder::DESC)
|
||||
.unwrap();
|
||||
assert_eq!(authors.len(), 1);
|
||||
assert_eq!(authors[0].name, "Terry Pratchett");
|
||||
|
|
|
@ -26,9 +26,9 @@ impl Author {
|
|||
conn: &Connection,
|
||||
limit: u64,
|
||||
cursor: Option<&str>,
|
||||
sort_order: SortOrder,
|
||||
sort_order: &SortOrder,
|
||||
) -> Result<Vec<Self>, DataStoreError> {
|
||||
let pagination = Pagination::new("sort", cursor, limit, sort_order);
|
||||
let pagination = Pagination::new("sort", cursor, limit, *sort_order);
|
||||
pagination.paginate(
|
||||
conn,
|
||||
"SELECT id, name, sort FROM authors",
|
||||
|
@ -46,4 +46,25 @@ impl Author {
|
|||
let params = named_params! { ":id": id };
|
||||
Ok(stmt.query_row(params, Self::from_row)?)
|
||||
}
|
||||
|
||||
pub fn has_previous_authors(
|
||||
conn: &Connection,
|
||||
sort_name: &str,
|
||||
) -> Result<bool, DataStoreError> {
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT Count(1) FROM authors WHERE sort < (:sort_name) ORDER BY sort DESC")?;
|
||||
let params = named_params! { ":sort_name": sort_name };
|
||||
let count: u64 = stmt.query_row(params, |x| x.get(0))?;
|
||||
|
||||
Ok(count > 0)
|
||||
}
|
||||
|
||||
pub fn has_more_authors(conn: &Connection, sort_name: &str) -> Result<bool, DataStoreError> {
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT Count(1) FROM authors WHERE sort > (:sort_name) ORDER BY sort ASC")?;
|
||||
let params = named_params! { ":sort_name": sort_name };
|
||||
let count: u64 = stmt.query_row(params, |x| x.get(0))?;
|
||||
|
||||
Ok(count > 0)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
use rusqlite::{Connection, Row, ToSql};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::error::DataStoreError;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Deserialize, Serialize)]
|
||||
pub enum SortOrder {
|
||||
ASC,
|
||||
DESC,
|
||||
|
@ -53,10 +54,17 @@ impl<'a> Pagination<'a> {
|
|||
};
|
||||
|
||||
let sort_col = self.sort_col;
|
||||
// otherwise paginated statements with join will fail
|
||||
let sort_col_wrapped = if let Some(index) = sort_col.find('.') {
|
||||
let right_part = &sort_col[index..];
|
||||
"t".to_owned() + right_part
|
||||
} else {
|
||||
sort_col.to_owned()
|
||||
};
|
||||
let sort_order = &self.sort_order;
|
||||
// DANGER: vulnerable to SQL injection if statement or sort_col variable is influenced by user input
|
||||
let mut stmt = conn.prepare(&format!(
|
||||
"{statement} {where_sql} {sort_col} {comparison} (:cursor) ORDER BY {sort_col} {sort_order:?} LIMIT (:limit)"
|
||||
"SELECT * FROM ({statement} {where_sql} {sort_col} {comparison} (:cursor) ORDER BY {sort_col} {sort_order:?} LIMIT (:limit)) AS t ORDER BY {sort_col_wrapped} ASC"
|
||||
))?;
|
||||
let params = [
|
||||
&[
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue