memory (sqla): Add filter for keyword:

A `keyword` filter can now be used to retrieve messages matching given keyword.

rel 458
This commit is contained in:
Goffi 2025-06-04 17:52:18 +02:00
commit 660dd6d538

View file

@ -28,7 +28,7 @@ from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union
from alembic import config as al_config, script as al_script
from alembic.runtime import migration as al_migration
from sqlalchemy import and_, delete, event, func, or_, update
from sqlalchemy import and_, delete, event, exists, func, or_, update
from sqlalchemy import Integer, literal_column, text
from sqlalchemy.dialects.sqlite import insert
from sqlalchemy.engine import Connection, Engine
@ -667,6 +667,14 @@ class Storage:
stmt = stmt.where(History.origin_id == filters["origin_id"])
if "thread_id" in filters:
stmt = stmt.where(Thread.thread_id == filters["thread_id"])
if "keyword" in filters:
keyword = filters["keyword"]
# FIXME: "json_each" works with SQLite, but when PostGreSQL support will
# be added, "json_array_elements" may have to be used with it.
subquery = func.json_each(History.extra["keywords"]).table_valued("value")
stmt = stmt.where(
exists().where(subquery.c.value == keyword)
)
if limit is not None:
stmt = stmt.limit(limit)