Skip to content
Permalink
Browse files

Escape terms in FTS5 queries

Refs simonw/datasette#651

Searches with commas in no longer break search, e.g.:

https://www.niche-museums.com/browse/search?q=york%2C
  • Loading branch information
simonw committed Dec 29, 2019
1 parent 2a9192d commit cd3e70a2814d03e4f1d518da55ee588bc7a6ef72
Showing with 7 additions and 2 deletions.
  1. +1 −1 metadata.json
  2. +6 −1 plugins/sql_functions.py
@@ -5,7 +5,7 @@
"queries": {
"nearby": "select *, haversine(latitude, longitude, cast(:latitude as real), cast(:longitude as real), 'mi') as distance_mi\nfrom museums order by distance_mi",
"feed": "SELECT\n 'tag:niche-museums.com,' || substr(m.created, 0, 11) || ':' || m.id as atom_id,\n m.name as atom_title,\n m.created as atom_updated,\n 'https://www.niche-museums.com/browse/museums/' || m.id as atom_link,\n coalesce(\n '<img src=\"' || m.photo_url || '?w=800&amp;h=400&amp;fit=crop&amp;auto=compress\">',\n ''\n ) || render_markdown(\n m.description || '\n\n' || coalesce(\n (\n select\n group_concat(\n '* [' || json_extract(p.value, '$.title') || '](' || json_extract(p.value, '$.url') || ') ' || json_extract(p.value, '$.author') || ', ' || json_extract(p.value, '$.publication') || ', ' || json_extract(p.value, '$.date'),\n '\n'\n )\n from\n json_each(coalesce(m.press, '[{}]')) as p\n ),\n ''\n )\n ) || coalesce(\n (\n select\n group_concat(\n '<p><img src=\"' || json_extract(ph.value, '$.url') || '?w=400&auto=compress\"></p>',\n ''\n )\n from\n json_each(coalesce(m.photos, '[{}]')) as ph\n ),\n ''\n ) as atom_content_html\nFROM\n museums m\norder by\n m.created desc\nlimit\n 15",
"search": "select\n museums_fts.rank,\n museums.*\nfrom\n museums\n join museums_fts on museums.id = museums_fts.rowid\nwhere\n museums_fts match case\n :q\n when '' then '*'\n else :q\n end\norder by\n museums_fts.rank"
"search": "select\n museums_fts.rank,\n museums.*\nfrom\n museums\n join museums_fts on museums.id = museums_fts.rowid\nwhere\n museums_fts match case\n :q\n when '' then '*'\n else escape_fts_query(:q)\n end\norder by\n museums_fts.rank"
}
}
}
@@ -1,8 +1,13 @@
from datasette import hookimpl
from datasette_render_markdown import render_markdown
import json


def escape_fts_query(query):
bits = query.split()
return ' '.join('"{}"'.format(bit.replace('"', '')) for bit in bits)


@hookimpl
def prepare_connection(conn):
conn.create_function("render_markdown", 1, render_markdown)
conn.create_function("escape_fts_query", 1, escape_fts_query)

0 comments on commit cd3e70a

Please sign in to comment.
You can’t perform that action at this time.