Quick hack for search order options

This commit is contained in:
Simon
2018-06-07 11:22:35 -04:00
parent 221a16697b
commit 06d3a09e11
3 changed files with 28 additions and 7 deletions

View File

@@ -30,6 +30,13 @@ class File:
class Database:
SORT_ORDERS = {
"score": "ORDER BY rank",
"size_asc": "ORDER BY size ASC",
"size_dsc": "ORDER BY size DESC",
"none": ""
}
def __init__(self, db_path):
self.db_path = db_path
@@ -200,18 +207,21 @@ class Database:
return stats
def search(self, q, limit: int = 25, offset: int = 0):
def search(self, q, limit: int = 25, offset: int = 0, sort_order="score"):
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
try:
order_by = Database.SORT_ORDERS.get(sort_order, "")
print(order_by)
cursor.execute("SELECT size, Website.url, WebsitePath.path, File.name, Website.id FROM File_index "
"INNER JOIN File ON File.id = File_index.rowid "
"INNER JOIN WebsitePath ON File.path_id = WebsitePath.id "
"INNER JOIN Website ON website_id = Website.id "
"WHERE File_index MATCH ? "
"ORDER BY rank LIMIT ? OFFSET ?", (q, limit, offset * limit))
"WHERE File_index MATCH ? " +
order_by + " LIMIT ? OFFSET ?",
(q, limit, offset * limit))
except sqlite3.OperationalError as e:
raise InvalidQueryException(str(e))