Skip to content

Commit

Permalink
Improve the sql with AND
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyanming committed Jul 7, 2024
1 parent 5b24a2e commit a54c552
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 53 deletions.
25 changes: 13 additions & 12 deletions calibredb-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -678,20 +678,21 @@ Argument PROPERTIES is for selecting different sql statement."
(line-list (if (and (functionp 'sqlite-available-p) (sqlite-available-p))
query-result
(split-string (calibredb-chomp query-result) calibredb-sql-newline) )))

;; (message "%s" sql)
(cond (count (caar query-result))
(distinct query-result)
(t (cond ((equal "" query-result) '(""))
((equal nil query-result) '(""))
(t (let (res-list)
(dolist (line line-list)
(if (and (functionp 'sqlite-available-p) (sqlite-available-p))
(push (calibredb-query-to-alist line) res-list)
;; validate if it is right format
(if (string-match-p (concat "^[0-9]\\{1,10\\}" calibredb-sql-separator) line)
;; decode and push to res-list
(push (calibredb-query-to-alist line) res-list))))
(calibredb-getbooklist res-list))))))))
(t (cond ((equal "" query-result) '())
((equal nil query-result) '())
((numberp query-result) '())
(t (let (res-list)
(dolist (line line-list)
(if (and (functionp 'sqlite-available-p) (sqlite-available-p))
(push (calibredb-query-to-alist line) res-list)
;; validate if it is right format
(if (string-match-p (concat "^[0-9]\\{1,10\\}" calibredb-sql-separator) line)
;; decode and push to res-list
(push (calibredb-query-to-alist line) res-list))))
(calibredb-getbooklist res-list))))))))

(defun calibredb-candidate(id)
"Generate one ebook candidate alist.
Expand Down
111 changes: 72 additions & 39 deletions calibredb-search.el
Original file line number Diff line number Diff line change
Expand Up @@ -871,45 +871,78 @@ Argument: PROPERTIES is the addiontal parameters."
(count (plist-get properties :count))
(page (plist-get properties :page)))
(calibredb-candidates
: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))) )

)
" ") 3 -1 )
:where (concat
(cond (calibredb-tag-filter-p
(mapconcat
(lambda (word)
(mapconcat 'identity
(delq nil
(list
(unless (equal (calibredb-tag-width) 0) (format "tag like '%%%s%%' " word))))
" OR "))
words
" AND ")
)
(calibredb-format-filter-p
(mapconcat
(lambda (word)
(mapconcat 'identity
(delq nil
(list
(unless (equal (calibredb-tag-width) 0) (format "format like '%%%s%%' " word))))
" OR "))
words
" AND ")
)
(calibredb-author-filter-p
(mapconcat
(lambda (word)
(mapconcat 'identity
(delq nil
(list
(unless (equal (calibredb-tag-width) 0) (format "author_sort like '%%%s%%' " word))))
" OR "))
words
" AND ")
)
(calibredb-date-filter-p
(mapconcat
(lambda (word)
(mapconcat 'identity
(delq nil
(list
(unless (equal (calibredb-tag-width) 0) (format "last_modified like '%%%s%%' " word))))
" OR "))
words
" AND ")
)
(t
(mapconcat
(lambda (word)
(format "(%s)"
(mapconcat 'identity
(delq nil
(list
(unless (equal calibredb-id-width 0) (format "id like '%%%s%%'" word))
(unless (equal (calibredb-title-width) 0) (format "title like '%%%s%%'" word))
(unless (equal (calibredb-format-width) 0) (format "format like '%%%s%%'" word))
(unless (equal (calibredb-tag-width) 0) (format "tag like '%%%s%%'" word))
(unless (equal (calibredb-ids-width) 0) (format "ids like '%%%s%%'" word))
(unless (equal (calibredb-author-width) 0) (format "author_sort like '%%%s%%'" word))
(unless (equal (calibredb-date-width) 0) (format "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%%'" word))))
" OR ")))
words
" AND ")))

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

)
:count count)))


Expand Down
4 changes: 2 additions & 2 deletions calibredb-utils.el
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ With universal ARG \\[universal-argument] use title as initial value."
(if (car item) (setq l (append (split-string (car item ) ",") l)) "" )) l))))

(calibredb-all "id")
(calibredb-all "author-sort")
(calibredb-all "author_sort")
(calibredb-all "path")
(calibredb-all "name")
(calibredb-all "format")
Expand Down Expand Up @@ -1086,7 +1086,7 @@ With universal ARG \\[universal-argument] use title as initial value."
(defun calibredb-filter-by-author-sort ()
"Filter results by author-sort."
(interactive)
(let ((author (completing-read "Select author: " (calibredb-all-author-sort))))
(let ((author (completing-read "Select author: " (calibredb-all-author_sort))))
(setq calibredb-tag-filter-p nil)
(setq calibredb-favorite-filter-p nil)
(setq calibredb-author-filter-p t)
Expand Down

0 comments on commit a54c552

Please sign in to comment.