Skip to content

Commit e9ea5ef

Browse files
RCmercitiensonqin
authored andcommitted
Fix sqlite FTS quote parsing
1 parent 1ec64cd commit e9ea5ef

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/main/frontend/worker/search.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ DROP TRIGGER IF EXISTS blocks_au;
342342
(and (re-find #"[^\w\s]" q)
343343
(or (not (some #(string/includes? match-input %) ["AND" "OR" "NOT"]))
344344
(string/includes? q "/"))) ; punctuations
345-
(str "\"" match-input "\"*")
345+
(str "\"" (string/replace match-input "\"" "\"\"") "\"*")
346346
(not= q match-input)
347347
(string/replace match-input "," "")
348348
:else

src/test/frontend/worker/search_test.cljs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,23 @@
209209
(is (some? result))
210210
(is (empty? result)))))
211211

212+
(deftest search-blocks-escapes-quotes-for-fts
213+
(testing "user quote characters are escaped before SQLite FTS receives them"
214+
(let [fts-binds (atom [])
215+
db #js {:exec (fn [opts]
216+
(let [sql (aget opts "sql")
217+
bind (js->clj (aget opts "bind"))]
218+
(when (string/includes? sql "title match ?")
219+
(swap! fts-binds conj (first bind)))
220+
#js []))}]
221+
(with-redefs [search/combine-results (fn [_db results] results)
222+
search/search-result->block-result
223+
(fn [_conn _q _code-class _option result]
224+
result)]
225+
(is (empty? (search/search-blocks (atom :large-db) db "\"" {:limit 10})))
226+
(is (empty? (search/search-blocks (atom :large-db) db "foo \"bar" {:limit 10})))
227+
(is (= ["\"\"\"\"*" "\"foo \"\"bar\"*"] @fts-binds))))))
228+
212229
(deftest search-blocks-large-graph-benchmark-regression
213230
(testing "cmd-k and autocomplete queries must not scan the full Datascript graph while typing"
214231
(let [calls (atom [])

0 commit comments

Comments
 (0)