mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add API to process each result when returning it
This commit is contained in:
parent
a398c0ece2
commit
9ae2074669
@ -987,9 +987,10 @@ class DB:
|
|||||||
return self.fts.dirty_book(book_id, *fmts)
|
return self.fts.dirty_book(book_id, *fmts)
|
||||||
|
|
||||||
def fts_search(self,
|
def fts_search(self,
|
||||||
fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids, return_text,
|
fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids, return_text, process_each_result
|
||||||
):
|
):
|
||||||
yield from self.fts.search(fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids, return_text,)
|
yield from self.fts.search(
|
||||||
|
fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids, return_text, process_each_result)
|
||||||
|
|
||||||
def shutdown_fts(self):
|
def shutdown_fts(self):
|
||||||
if self.fts_enabled:
|
if self.fts_enabled:
|
||||||
|
@ -633,6 +633,7 @@ class Cache:
|
|||||||
restrict_to_book_ids=None,
|
restrict_to_book_ids=None,
|
||||||
return_text=True,
|
return_text=True,
|
||||||
result_type=tuple,
|
result_type=tuple,
|
||||||
|
process_each_result=None,
|
||||||
):
|
):
|
||||||
return result_type(self.backend.fts_search(
|
return result_type(self.backend.fts_search(
|
||||||
fts_engine_query,
|
fts_engine_query,
|
||||||
@ -642,6 +643,7 @@ class Cache:
|
|||||||
snippet_size=snippet_size,
|
snippet_size=snippet_size,
|
||||||
return_text=return_text,
|
return_text=return_text,
|
||||||
restrict_to_book_ids=restrict_to_book_ids,
|
restrict_to_book_ids=restrict_to_book_ids,
|
||||||
|
process_each_result=process_each_result,
|
||||||
))
|
))
|
||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
|
@ -150,7 +150,7 @@ class FTS:
|
|||||||
|
|
||||||
def search(self,
|
def search(self,
|
||||||
fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids,
|
fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids,
|
||||||
return_text=True,
|
return_text=True, process_each_result=None
|
||||||
):
|
):
|
||||||
if restrict_to_book_ids is not None and not restrict_to_book_ids:
|
if restrict_to_book_ids is not None and not restrict_to_book_ids:
|
||||||
return
|
return
|
||||||
@ -184,12 +184,15 @@ class FTS:
|
|||||||
query += f'; DROP TABLE temp.{temp_table_name}'
|
query += f'; DROP TABLE temp.{temp_table_name}'
|
||||||
try:
|
try:
|
||||||
for record in conn.execute(query, tuple(data)):
|
for record in conn.execute(query, tuple(data)):
|
||||||
ret = yield {
|
result = {
|
||||||
'id': record[0],
|
'id': record[0],
|
||||||
'book_id': record[1],
|
'book_id': record[1],
|
||||||
'format': record[2],
|
'format': record[2],
|
||||||
'text': record[3] if return_text else '',
|
'text': record[3] if return_text else '',
|
||||||
}
|
}
|
||||||
|
if process_each_result is not None:
|
||||||
|
result = process_each_result(result)
|
||||||
|
ret = yield result
|
||||||
if ret is True:
|
if ret is True:
|
||||||
break
|
break
|
||||||
except apsw.SQLError as e:
|
except apsw.SQLError as e:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user