Bumbling SQL query improved.
This commit is contained in:
parent
8fac614905
commit
3f5d790d3c
1 changed files with 22 additions and 30 deletions
42
src/main.rs
42
src/main.rs
|
@ -60,7 +60,6 @@ fn get_attribute_file_as(opf: ZipFile) -> Option<String> {
|
||||||
}) if name.local_name == "creator" => {
|
}) if name.local_name == "creator" => {
|
||||||
for attr in attributes {
|
for attr in attributes {
|
||||||
if attr.name.local_name == "file-as" {
|
if attr.name.local_name == "file-as" {
|
||||||
println!("File-as: {}", &attr.value);
|
|
||||||
return Some(attr.value);
|
return Some(attr.value);
|
||||||
}
|
}
|
||||||
if is_epub3 && attr.name.local_name == "id" {
|
if is_epub3 && attr.name.local_name == "id" {
|
||||||
|
@ -120,41 +119,34 @@ fn main() {
|
||||||
{
|
{
|
||||||
// Get book ids from entries where we have something like "firstname lastname" in author
|
// Get book ids from entries where we have something like "firstname lastname" in author
|
||||||
// but no "lastname, firstname" in fistauthor
|
// but no "lastname, firstname" in fistauthor
|
||||||
let mut stmt = tx.prepare("SELECT id FROM books_impl WHERE ext LIKE 'epub' AND author LIKE '% %' AND (firstauthor NOT LIKE '%\\,%' ESCAPE '\\' OR firstauthor LIKE '%&%')").unwrap();
|
|
||||||
let mut rows = stmt.query(NO_PARAMS).unwrap();
|
|
||||||
let mut book_ids: Vec<i32> = Vec::new();
|
|
||||||
while let Some(row) = rows.next().unwrap() {
|
|
||||||
book_ids.push(row.get(0).unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get also book ids from the special case where we have multiple authors (separated by ", " in authors)
|
// Get also book ids from the special case where we have multiple authors (separated by ", " in authors)
|
||||||
// but no ampersand ("&") in firstauthor
|
// but no ampersand ("&") in firstauthor
|
||||||
let mut stmt = tx.prepare("SELECT id FROM books_impl WHERE ext LIKE 'epub' AND author LIKE '%\\, %' ESCAPE '\\' AND firstauthor NOT LIKE '%&%'").unwrap();
|
let mut stmt = tx.prepare(r"
|
||||||
|
SELECT files.book_id, folders.name, files.filename
|
||||||
|
FROM files INNER JOIN folders
|
||||||
|
ON files.folder_id = folders.id
|
||||||
|
WHERE files.book_id IN
|
||||||
|
(
|
||||||
|
SELECT DISTINCT id FROM books_impl
|
||||||
|
WHERE (ext LIKE 'epub' AND author LIKE '% %' AND (firstauthor NOT LIKE '%\,%' ESCAPE '\' OR firstauthor LIKE '%&%'))
|
||||||
|
OR (ext LIKE 'epub' AND author LIKE '%\, %' ESCAPE '\' AND firstauthor NOT LIKE '%&%')
|
||||||
|
)
|
||||||
|
AND files.storageid = 1
|
||||||
|
;").unwrap();
|
||||||
|
|
||||||
let mut rows = stmt.query(NO_PARAMS).unwrap();
|
let mut rows = stmt.query(NO_PARAMS).unwrap();
|
||||||
while let Some(row) = rows.next().unwrap() {
|
|
||||||
book_ids.push(row.get(0).unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
book_ids.sort();
|
|
||||||
book_ids.dedup();
|
|
||||||
|
|
||||||
let mut bookentries = Vec::new();
|
let mut bookentries = Vec::new();
|
||||||
|
|
||||||
for book_id in book_ids {
|
|
||||||
let mut stmt = tx.prepare("SELECT folders.name,files.filename FROM files,folders WHERE files.book_id = :book_id AND files.storageid = 1 AND files.folder_id = folders.id").unwrap();
|
|
||||||
let mut rows = stmt
|
|
||||||
.query_named(named_params! { ":book_id": book_id })
|
|
||||||
.unwrap();
|
|
||||||
while let Some(row) = rows.next().unwrap() {
|
while let Some(row) = rows.next().unwrap() {
|
||||||
let prefix: String = row.get(0).unwrap();
|
let book_id: i32 = row.get(0).unwrap();
|
||||||
let filename: String = row.get(1).unwrap();
|
let prefix: String = row.get(1).unwrap();
|
||||||
|
let filename: String = row.get(2).unwrap();
|
||||||
let filepath = format!("{}/{}", prefix, filename);
|
let filepath = format!("{}/{}", prefix, filename);
|
||||||
bookentries.push(BookEntry {
|
bookentries.push(BookEntry {
|
||||||
id: book_id,
|
id: book_id,
|
||||||
filepath: filepath,
|
filepath,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for entry in bookentries {
|
for entry in bookentries {
|
||||||
let file = File::open(entry.filepath.as_str());
|
let file = File::open(entry.filepath.as_str());
|
||||||
|
|
Loading…
Reference in a new issue