Skip to content

Commit

Permalink
Improve calibredb-candidates
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyanming committed Jul 7, 2024
1 parent 9e0e2f7 commit 2c7ff6a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 43 deletions.
8 changes: 5 additions & 3 deletions calibredb-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,12 @@ Argument CALIBRE-ITEM-LIST is the calibred item list."
(setq display-alist
(cons (list (calibredb-format-item item) item) display-alist)))))

(defun calibredb-candidates (&optional sql &rest properties)
"Generate ebooks candidates alist."
(defun calibredb-candidates (&rest properties)
"Generate ebooks candidates alist.
Argument PROPERTIES is for selecting different sql statement."
(let* ((count (plist-get properties :count))
(distinct (plist-get properties :distinct))
(where (plist-get properties :where))
(sql (format (cond
(count "SELECT COUNT(id) FROM (SELECT * FROM (%s) %s)")
(distinct (concat "SELECT DISTINCT " distinct " FROM (SELECT * FROM (%s) %s)"))
Expand All @@ -664,7 +666,7 @@ Argument CALIBRE-ITEM-LIST is the calibred item list."
(_ " ORDER BY id"))
(when (eq calibredb-order 'desc)
" DESC"))
(if sql (concat " WHERE " sql) "")))
(if where (concat " WHERE " where) "")))
(query-result (calibredb-query sql))
(line-list (if (and (functionp 'sqlite-available-p) (sqlite-available-p))
query-result
Expand Down
79 changes: 40 additions & 39 deletions calibredb-search.el
Original file line number Diff line number Diff line change
Expand Up @@ -863,51 +863,52 @@ When FORCE is non-nil, redraw even when the database hasn't changed."

(defun calibredb-db-select (filter &rest properties)
"Generate ebook candidate alist.
ARGUMENT FILTER is the filter string."
Argument: FILTER is the filter string.
Argument: PROPERTIES is the addiontal parameters."
(let* ((words (split-string filter " "))
(limit (plist-get properties :limit))
(count (plist-get properties :count))
(page (plist-get properties :page)))
(calibredb-candidates
(substring
(mapconcat #'identity
(append
(cond (calibredb-tag-filter-p
(cl-loop for word in words collect
(unless (equal (calibredb-tag-width) 0) (format " OR tag like '%%%s%%' " word)))
)
(calibredb-format-filter-p
(cl-loop for word in words collect
(unless (equal (calibredb-tag-width) 0) (format " OR format like '%%%s%%' " word)))
)
(calibredb-author-filter-p
(cl-loop for word in words collect
(unless (equal (calibredb-tag-width) 0) (format " OR author_sort like '%%%s%%' " word)))
)
(calibredb-date-filter-p
(cl-loop for word in words collect
(unless (equal (calibredb-tag-width) 0) (format " OR last_modified like '%%%s%%' " word)))
:where (substring
(mapconcat #'identity
(append
(cond (calibredb-tag-filter-p
(cl-loop for word in words collect
(unless (equal (calibredb-tag-width) 0) (format " OR tag like '%%%s%%' " word)))
)
(calibredb-format-filter-p
(cl-loop for word in words collect
(unless (equal (calibredb-tag-width) 0) (format " OR format like '%%%s%%' " word)))
)
(calibredb-author-filter-p
(cl-loop for word in words collect
(unless (equal (calibredb-tag-width) 0) (format " OR author_sort like '%%%s%%' " word)))
)
(calibredb-date-filter-p
(cl-loop for word in words collect
(unless (equal (calibredb-tag-width) 0) (format " OR last_modified like '%%%s%%' " word)))
)
(t (cl-loop for word in words collect
(or
(unless (equal calibredb-id-width 0) (format " OR id like '%%%s%%' " word))
(unless (equal (calibredb-title-width) 0) (format " OR title like '%%%s%%' " word))
(unless (equal (calibredb-format-width) 0) (format " OR format like '%%%s%%' " word))
(unless (equal (calibredb-tag-width) 0) (format " OR tag like '%%%s%%' " word))
(unless (equal (calibredb-ids-width) 0) (format " OR ids like '%%%s%%' " word))
(unless (equal (calibredb-author-width) 0) (format " OR author_sort like '%%%s%%' " word))
(unless (equal (calibredb-date-width) 0) (format " OR last_modified like '%%%s%%' " word))
;; Normally, comments are long, it is necessary to trancate the comments to speed up the searching
;; except calibredb-comment-width is -1.
(unless (equal (calibredb-comment-width) 0) (format " text like '%%%s%%' OR " word))))))

`( ,(when limit
(format " LIMIT %s " limit) ) )
`( ,(when page
(format " OFFSET %s " (* (1- page) calibredb-search-page-max-rows))) )

)
(t (cl-loop for word in words collect
(or
(unless (equal calibredb-id-width 0) (format " OR id like '%%%s%%' " word))
(unless (equal (calibredb-title-width) 0) (format " OR title like '%%%s%%' " word))
(unless (equal (calibredb-format-width) 0) (format " OR format like '%%%s%%' " word))
(unless (equal (calibredb-tag-width) 0) (format " OR tag like '%%%s%%' " word))
(unless (equal (calibredb-ids-width) 0) (format " OR ids like '%%%s%%' " word))
(unless (equal (calibredb-author-width) 0) (format " OR author_sort like '%%%s%%' " word))
(unless (equal (calibredb-date-width) 0) (format " OR last_modified like '%%%s%%' " word))
;; Normally, comments are long, it is necessary to trancate the comments to speed up the searching
;; except calibredb-comment-width is -1.
(unless (equal (calibredb-comment-width) 0) (format " text like '%%%s%%' OR " word))))))

`( ,(when limit
(format " LIMIT %s " limit) ) )
`( ,(when page
(format " OFFSET %s " (* (1- page) calibredb-search-page-max-rows))) )

)
" ") 3 -1 )
" ") 3 -1 )
:count count)))


Expand Down
2 changes: 1 addition & 1 deletion calibredb-utils.el
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ With universal ARG \\[universal-argument] use title as initial value."
,(format "Get all %s and return as a list." field)
(seq-uniq
(let (l)
(cl-loop for item in (calibredb-candidates nil :distinct ,(format "%s" field )) do
(cl-loop for item in (calibredb-candidates :distinct ,(format "%s" field )) do
(if (car item) (setq l (append (split-string (car item ) ",") l)) "" )) l))))

(calibredb-all "id")
Expand Down

0 comments on commit 2c7ff6a

Please sign in to comment.