Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/util/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from concurrent.futures import ProcessPoolExecutor

from lxml import etree, html
from psycopg2 import sql
from psycopg2.extensions import quote_ident
from psycopg2.extras import Json

Expand Down Expand Up @@ -64,19 +65,17 @@ def add_snippet_names_on_html_field(cr, table, column, snippets, regex):
Will search for all the snippets in the fields mentionned (should be html fields) and add
the corresponding data-snippet on them.
"""

def quote(ident):
return quote_ident(ident, cr._cnx)

query = cr.mogrify(
f"""
SELECT id, array((SELECT regexp_matches({quote(column)}, %(regex)s, 'g'))), {column}
FROM {quote(table)}
WHERE {quote(column)} ~ %(regex)s
""",
sql.SQL(
"""
SELECT id, array((SELECT regexp_matches({column}, %(regex)s, 'g'))), {column}
FROM {table}
WHERE {column} ~ %(regex)s
"""
).format(column=sql.Identifier(column), table=sql.Identifier(table)),
{"regex": regex},
).decode()
where = cr.mogrify(f"{quote(column)} ~ %s", [regex]).decode()
where = cr.mogrify(sql.SQL("{column} ~ %s").format(column=sql.Identifier(column)), [regex]).decode()
ids_ranges = determine_chunk_limit_ids(cr, table, [column], where)
for id0, id1 in ids_ranges:
add_snippet_names(cr, table, column, snippets, query + f" AND id BETWEEN {id0} AND {id1}")
Expand Down