Mercurial > libervia-backend
comparison libervia/backend/memory/sqla.py @ 4373:5e48ae998976
memory (sqla): Add filter for keyword:
A `keyword` filter can now be used to retrieve messages matching given keyword.
rel 458
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 04 Jun 2025 17:52:18 +0200 |
parents | 2bdf0c16d852 |
children | 7ac28a270b7f |
comparison
equal
deleted
inserted
replaced
4372:ec9583cac6c2 | 4373:5e48ae998976 |
---|---|
26 import time | 26 import time |
27 from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union | 27 from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union |
28 | 28 |
29 from alembic import config as al_config, script as al_script | 29 from alembic import config as al_config, script as al_script |
30 from alembic.runtime import migration as al_migration | 30 from alembic.runtime import migration as al_migration |
31 from sqlalchemy import and_, delete, event, func, or_, update | 31 from sqlalchemy import and_, delete, event, exists, func, or_, update |
32 from sqlalchemy import Integer, literal_column, text | 32 from sqlalchemy import Integer, literal_column, text |
33 from sqlalchemy.dialects.sqlite import insert | 33 from sqlalchemy.dialects.sqlite import insert |
34 from sqlalchemy.engine import Connection, Engine | 34 from sqlalchemy.engine import Connection, Engine |
35 from sqlalchemy.exc import IntegrityError, NoResultFound | 35 from sqlalchemy.exc import IntegrityError, NoResultFound |
36 from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, create_async_engine | 36 from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, create_async_engine |
665 stmt = stmt.where(History.uid == filters["id"]) | 665 stmt = stmt.where(History.uid == filters["id"]) |
666 if "origin_id" in filters: | 666 if "origin_id" in filters: |
667 stmt = stmt.where(History.origin_id == filters["origin_id"]) | 667 stmt = stmt.where(History.origin_id == filters["origin_id"]) |
668 if "thread_id" in filters: | 668 if "thread_id" in filters: |
669 stmt = stmt.where(Thread.thread_id == filters["thread_id"]) | 669 stmt = stmt.where(Thread.thread_id == filters["thread_id"]) |
670 if "keyword" in filters: | |
671 keyword = filters["keyword"] | |
672 # FIXME: "json_each" works with SQLite, but when PostGreSQL support will | |
673 # be added, "json_array_elements" may have to be used with it. | |
674 subquery = func.json_each(History.extra["keywords"]).table_valued("value") | |
675 stmt = stmt.where( | |
676 exists().where(subquery.c.value == keyword) | |
677 ) | |
670 | 678 |
671 if limit is not None: | 679 if limit is not None: |
672 stmt = stmt.limit(limit) | 680 stmt = stmt.limit(limit) |
673 | 681 |
674 async with self.session() as session: | 682 async with self.session() as session: |