Mercurial > libervia-backend
annotate sat/memory/sqla.py @ 3664:9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
`searchPubsubItems` is a high level method to handle Full-Text Search queries on Pubsub
cache.
rel 361
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 08 Sep 2021 17:58:48 +0200 |
parents | 257135d5c5c2 |
children | 72b0e4053ab0 |
rev | line source |
---|---|
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # Libervia: an XMPP client |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
19 import sys |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 import time |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
21 import asyncio |
3595
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
22 from datetime import datetime |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
23 from asyncio.subprocess import PIPE |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
24 from pathlib import Path |
3664
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
25 from typing import Union, Dict, List, Tuple, Iterable, Any, Callable, Optional |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
26 from sqlalchemy.ext.asyncio import AsyncSession, AsyncEngine, create_async_engine |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from sqlalchemy.exc import IntegrityError, NoResultFound |
3595
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
28 from sqlalchemy.orm import ( |
3664
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
29 sessionmaker, subqueryload, joinedload, selectinload, contains_eager |
3595
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
30 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
31 from sqlalchemy.orm.decl_api import DeclarativeMeta |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 from sqlalchemy.future import select |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
33 from sqlalchemy.engine import Engine, Connection |
3595
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
34 from sqlalchemy import update, delete, and_, or_, event, func |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
35 from sqlalchemy.sql.functions import coalesce, sum as sum_, now, count |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 from sqlalchemy.dialects.sqlite import insert |
3664
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
37 from sqlalchemy import text, literal_column, Integer |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
38 from alembic import script as al_script, config as al_config |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
39 from alembic.runtime import migration as al_migration |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 from twisted.internet import defer |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 from twisted.words.protocols.jabber import jid |
3595
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
42 from twisted.words.xish import domish |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 from sat.core.i18n import _ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 from sat.core import exceptions |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 from sat.core.log import getLogger |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 from sat.core.constants import Const as C |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 from sat.core.core_types import SatXMPPEntity |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 from sat.tools.utils import aio |
3595
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
49 from sat.tools.common import uri |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
50 from sat.memory import migration |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
51 from sat.memory import sqla_config |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 from sat.memory.sqla_mapping import ( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 NOT_IN_EXTRA, |
3595
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
54 SyncState, |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 Base, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 Profile, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 Component, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 History, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 Message, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 Subject, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 Thread, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 ParamGen, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 ParamInd, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 PrivateGen, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 PrivateInd, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 PrivateGenBin, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 PrivateIndBin, |
3595
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
68 File, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
69 PubsubNode, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
70 PubsubItem, |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 log = getLogger(__name__) |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
75 migration_path = Path(migration.__file__).parent |
3664
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
76 #: mapping of Libervia search query operators to SQLAlchemy method name |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
77 OP_MAP = { |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
78 "==": "__eq__", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
79 "eq": "__eq__", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
80 "!=": "__ne__", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
81 "ne": "__ne__", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
82 ">": "__gt__", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
83 "gt": "__gt__", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
84 "<": "__le__", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
85 "le": "__le__", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
86 "between": "between", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
87 "in": "in_", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
88 "not_in": "not_in", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
89 "overlap": "in_", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
90 "ioverlap": "in_", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
91 "disjoint": "in_", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
92 "idisjoint": "in_", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
93 "like": "like", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
94 "ilike": "ilike", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
95 "not_like": "notlike", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
96 "not_ilike": "notilike", |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
97 } |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 @event.listens_for(Engine, "connect") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 def set_sqlite_pragma(dbapi_connection, connection_record): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 cursor = dbapi_connection.cursor() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 cursor.execute("PRAGMA foreign_keys=ON") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 cursor.close() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 class Storage: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
109 def __init__(self): |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 self.initialized = defer.Deferred() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 # we keep cache for the profiles (key: profile name, value: profile id) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 # profile id to name |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 self.profiles: Dict[int, str] = {} |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 # profile id to component entry point |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 self.components: Dict[int, str] = {} |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
117 async def migrateApply(self, *args: str, log_output: bool = False) -> None: |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
118 """Do a migration command |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
119 |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
120 Commands are applied by running Alembic in a subprocess. |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
121 Arguments are alembic executables commands |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
122 |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
123 @param log_output: manage stdout and stderr: |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
124 - if False, stdout and stderr are buffered, and logged only in case of error |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
125 - if True, stdout and stderr will be logged during the command execution |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
126 @raise exceptions.DatabaseError: something went wrong while running the |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
127 process |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
128 """ |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
129 stdout, stderr = 2 * (None,) if log_output else 2 * (PIPE,) |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
130 proc = await asyncio.create_subprocess_exec( |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
131 sys.executable, "-m", "alembic", *args, |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
132 stdout=stdout, stderr=stderr, cwd=migration_path |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
133 ) |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
134 log_out, log_err = await proc.communicate() |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
135 if proc.returncode != 0: |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
136 msg = _( |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
137 "Can't {operation} database (exit code {exit_code})" |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
138 ).format( |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
139 operation=args[0], |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
140 exit_code=proc.returncode |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
141 ) |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
142 if log_out or log_err: |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
143 msg += f":\nstdout: {log_out.decode()}\nstderr: {log_err.decode()}" |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
144 log.error(msg) |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
145 |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
146 raise exceptions.DatabaseError(msg) |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
147 |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
148 async def createDB(self, engine: AsyncEngine, db_config: dict) -> None: |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
149 """Create a new database |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
150 |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
151 The database is generated from SQLAlchemy model, then stamped by Alembic |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
152 """ |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
153 # the dir may not exist if it's not the XDG recommended one |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
154 db_config["path"].parent.mkdir(0o700, True, True) |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
155 async with engine.begin() as conn: |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
156 await conn.run_sync(Base.metadata.create_all) |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
157 |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
158 log.debug("stamping the database") |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
159 await self.migrateApply("stamp", "head") |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
160 log.debug("stamping done") |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
161 |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
162 def _checkDBIsUpToDate(self, conn: Connection) -> bool: |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
163 al_ini_path = migration_path / "alembic.ini" |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
164 al_cfg = al_config.Config(al_ini_path) |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
165 directory = al_script.ScriptDirectory.from_config(al_cfg) |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
166 context = al_migration.MigrationContext.configure(conn) |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
167 return set(context.get_current_heads()) == set(directory.get_heads()) |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
168 |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
169 async def checkAndUpdateDB(self, engine: AsyncEngine, db_config: dict) -> None: |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
170 """Check that database is up-to-date, and update if necessary""" |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
171 async with engine.connect() as conn: |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
172 up_to_date = await conn.run_sync(self._checkDBIsUpToDate) |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
173 if up_to_date: |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
174 log.debug("Database is up-to-date") |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
175 else: |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
176 log.info("Database needs to be updated") |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
177 log.info("updating…") |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
178 await self.migrateApply("upgrade", "head", log_output=True) |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
179 log.info("Database is now up-to-date") |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
180 |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 @aio |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
182 async def initialise(self) -> None: |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 log.info(_("Connecting database")) |
3583
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
184 |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
185 db_config = sqla_config.getDbConfig() |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 engine = create_async_engine( |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
187 db_config["url"], |
3595
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
188 future=True, |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 ) |
3583
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
190 |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
191 new_base = not db_config["path"].exists() |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 if new_base: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 log.info(_("The database is new, creating the tables")) |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
194 await self.createDB(engine, db_config) |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
195 else: |
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
196 await self.checkAndUpdateDB(engine, db_config) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 |
3583
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
198 self.session = sessionmaker( |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
199 engine, expire_on_commit=False, class_=AsyncSession |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
200 ) |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
201 |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 result = await session.execute(select(Profile)) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 for p in result.scalars(): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 self.profiles[p.name] = p.id |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 result = await session.execute(select(Component)) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 for c in result.scalars(): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 self.components[c.profile_id] = c.entry_point |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 self.initialized.callback(None) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 |
3596
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
212 ## Generic |
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
213 |
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
214 @aio |
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
215 async def add(self, db_obj: DeclarativeMeta) -> None: |
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
216 """Add an object to database""" |
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
217 async with self.session() as session: |
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
218 async with session.begin(): |
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
219 session.add(db_obj) |
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
220 |
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
221 @aio |
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
222 async def delete(self, db_obj: DeclarativeMeta) -> None: |
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
223 """Delete an object from database""" |
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
224 async with self.session() as session: |
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
225 async with session.begin(): |
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
226 await session.delete(db_obj) |
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
227 await session.commit() |
2d97c695af05
core (memory/sqla): generic methods to add and delete ORM objects directly
Goffi <goffi@goffi.org>
parents:
3595
diff
changeset
|
228 |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 ## Profiles |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 def getProfilesList(self) -> List[str]: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 """"Return list of all registered profiles""" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 return list(self.profiles.keys()) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 def hasProfile(self, profile_name: str) -> bool: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 """return True if profile_name exists |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 @param profile_name: name of the profile to check |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 return profile_name in self.profiles |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 def profileIsComponent(self, profile_name: str) -> bool: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 try: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
244 return self.profiles[profile_name] in self.components |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 except KeyError: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 raise exceptions.NotFound("the requested profile doesn't exists") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
247 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 def getEntryPoint(self, profile_name: str) -> str: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 try: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 return self.components[self.profiles[profile_name]] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 except KeyError: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 raise exceptions.NotFound("the requested profile doesn't exists or is not a component") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
253 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
254 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
255 async def createProfile(self, name: str, component_ep: Optional[str] = None) -> None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
256 """Create a new profile |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
257 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
258 @param name: name of the profile |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
259 @param component: if not None, must point to a component entry point |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
260 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
261 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
262 profile = Profile(name=name) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
263 async with session.begin(): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
264 session.add(profile) |
3638
257135d5c5c2
core (memory/sqla): fix key/value inversion in self.profiles
Goffi <goffi@goffi.org>
parents:
3621
diff
changeset
|
265 self.profiles[profile.name] = profile.id |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
266 if component_ep is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
267 async with session.begin(): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
268 component = Component(profile=profile, entry_point=component_ep) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
269 session.add(component) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
270 self.components[profile.id] = component_ep |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
271 return profile |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
272 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
273 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
274 async def deleteProfile(self, name: str) -> None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
275 """Delete profile |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
276 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
277 @param name: name of the profile |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
278 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
279 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
280 result = await session.execute(select(Profile).where(Profile.name == name)) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
281 profile = result.scalar() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
282 await session.delete(profile) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
283 await session.commit() |
3638
257135d5c5c2
core (memory/sqla): fix key/value inversion in self.profiles
Goffi <goffi@goffi.org>
parents:
3621
diff
changeset
|
284 del self.profiles[profile.name] |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
285 if profile.id in self.components: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
286 del self.components[profile.id] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
287 log.info(_("Profile {name!r} deleted").format(name = name)) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
288 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
289 ## Params |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
290 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
291 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
292 async def loadGenParams(self, params_gen: dict) -> None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
293 """Load general parameters |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
294 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
295 @param params_gen: dictionary to fill |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
296 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
297 log.debug(_("loading general parameters from database")) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
298 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
299 result = await session.execute(select(ParamGen)) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
300 for p in result.scalars(): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
301 params_gen[(p.category, p.name)] = p.value |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
302 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
303 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
304 async def loadIndParams(self, params_ind: dict, profile: str) -> None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
305 """Load individual parameters |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
306 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
307 @param params_ind: dictionary to fill |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
308 @param profile: a profile which *must* exist |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
309 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
310 log.debug(_("loading individual parameters from database")) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
311 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
312 result = await session.execute( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
313 select(ParamInd).where(ParamInd.profile_id == self.profiles[profile]) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
314 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
315 for p in result.scalars(): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
316 params_ind[(p.category, p.name)] = p.value |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
317 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
318 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
319 async def getIndParam(self, category: str, name: str, profile: str) -> Optional[str]: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
320 """Ask database for the value of one specific individual parameter |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
321 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
322 @param category: category of the parameter |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
323 @param name: name of the parameter |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
324 @param profile: %(doc_profile)s |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
325 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
326 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
327 result = await session.execute( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
328 select(ParamInd.value) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
329 .filter_by( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
330 category=category, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
331 name=name, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
332 profile_id=self.profiles[profile] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
333 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
334 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
335 return result.scalar_one_or_none() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
336 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
337 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
338 async def getIndParamValues(self, category: str, name: str) -> Dict[str, str]: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
339 """Ask database for the individual values of a parameter for all profiles |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
340 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
341 @param category: category of the parameter |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
342 @param name: name of the parameter |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
343 @return dict: profile => value map |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
344 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
345 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
346 result = await session.execute( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
347 select(ParamInd) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
348 .filter_by( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
349 category=category, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
350 name=name |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
351 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
352 .options(subqueryload(ParamInd.profile)) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
353 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
354 return {param.profile.name: param.value for param in result.scalars()} |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
355 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
356 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
357 async def setGenParam(self, category: str, name: str, value: Optional[str]) -> None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
358 """Save the general parameters in database |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
359 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
360 @param category: category of the parameter |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
361 @param name: name of the parameter |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
362 @param value: value to set |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
363 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
364 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
365 stmt = insert(ParamGen).values( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
366 category=category, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
367 name=name, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
368 value=value |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
369 ).on_conflict_do_update( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
370 index_elements=(ParamGen.category, ParamGen.name), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
371 set_={ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
372 ParamGen.value: value |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
373 } |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
374 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
375 await session.execute(stmt) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
376 await session.commit() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
377 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
378 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
379 async def setIndParam( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
380 self, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
381 category:str, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
382 name: str, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
383 value: Optional[str], |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
384 profile: str |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
385 ) -> None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
386 """Save the individual parameters in database |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
387 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
388 @param category: category of the parameter |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
389 @param name: name of the parameter |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
390 @param value: value to set |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
391 @param profile: a profile which *must* exist |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
392 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
393 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
394 stmt = insert(ParamInd).values( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
395 category=category, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
396 name=name, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
397 profile_id=self.profiles[profile], |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
398 value=value |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
399 ).on_conflict_do_update( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
400 index_elements=(ParamInd.category, ParamInd.name, ParamInd.profile_id), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
401 set_={ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
402 ParamInd.value: value |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
403 } |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
404 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
405 await session.execute(stmt) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
406 await session.commit() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
407 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
408 def _jid_filter(self, jid_: jid.JID, dest: bool = False): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
409 """Generate condition to filter on a JID, using relevant columns |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
410 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
411 @param dest: True if it's the destinee JID, otherwise it's the source one |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
412 @param jid_: JID to filter by |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
413 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
414 if jid_.resource: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
415 if dest: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
416 return and_( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
417 History.dest == jid_.userhost(), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
418 History.dest_res == jid_.resource |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
419 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
420 else: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
421 return and_( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
422 History.source == jid_.userhost(), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
423 History.source_res == jid_.resource |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
424 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
425 else: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
426 if dest: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
427 return History.dest == jid_.userhost() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
428 else: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
429 return History.source == jid_.userhost() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
430 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
431 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
432 async def historyGet( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
433 self, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
434 from_jid: Optional[jid.JID], |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
435 to_jid: Optional[jid.JID], |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
436 limit: Optional[int] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
437 between: bool = True, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
438 filters: Optional[Dict[str, str]] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
439 profile: Optional[str] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
440 ) -> List[Tuple[ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
441 str, int, str, str, Dict[str, str], Dict[str, str], str, str, str] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
442 ]: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
443 """Retrieve messages in history |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
444 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
445 @param from_jid: source JID (full, or bare for catchall) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
446 @param to_jid: dest JID (full, or bare for catchall) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
447 @param limit: maximum number of messages to get: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
448 - 0 for no message (returns the empty list) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
449 - None for unlimited |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
450 @param between: confound source and dest (ignore the direction) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
451 @param filters: pattern to filter the history results |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
452 @return: list of messages as in [messageNew], minus the profile which is already |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
453 known. |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
454 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
455 # we have to set a default value to profile because it's last argument |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
456 # and thus follow other keyword arguments with default values |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
457 # but None should not be used for it |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
458 assert profile is not None |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
459 if limit == 0: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
460 return [] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
461 if filters is None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
462 filters = {} |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
463 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
464 stmt = ( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
465 select(History) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
466 .filter_by( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
467 profile_id=self.profiles[profile] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
468 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
469 .outerjoin(History.messages) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
470 .outerjoin(History.subjects) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
471 .outerjoin(History.thread) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
472 .options( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
473 contains_eager(History.messages), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
474 contains_eager(History.subjects), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
475 contains_eager(History.thread), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
476 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
477 .order_by( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
478 # timestamp may be identical for 2 close messages (specially when delay is |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
479 # used) that's why we order ties by received_timestamp. We'll reverse the |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
480 # order when returning the result. We use DESC here so LIMIT keep the last |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
481 # messages |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
482 History.timestamp.desc(), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
483 History.received_timestamp.desc() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
484 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
485 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
486 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
487 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
488 if not from_jid and not to_jid: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
489 # no jid specified, we want all one2one communications |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
490 pass |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
491 elif between: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
492 if not from_jid or not to_jid: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
493 # we only have one jid specified, we check all messages |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
494 # from or to this jid |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
495 jid_ = from_jid or to_jid |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
496 stmt = stmt.where( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
497 or_( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
498 self._jid_filter(jid_), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
499 self._jid_filter(jid_, dest=True) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
500 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
501 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
502 else: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
503 # we have 2 jids specified, we check all communications between |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
504 # those 2 jids |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
505 stmt = stmt.where( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
506 or_( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
507 and_( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
508 self._jid_filter(from_jid), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
509 self._jid_filter(to_jid, dest=True), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
510 ), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
511 and_( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
512 self._jid_filter(to_jid), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
513 self._jid_filter(from_jid, dest=True), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
514 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
515 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
516 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
517 else: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
518 # we want one communication in specific direction (from somebody or |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
519 # to somebody). |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
520 if from_jid is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
521 stmt = stmt.where(self._jid_filter(from_jid)) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
522 if to_jid is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
523 stmt = stmt.where(self._jid_filter(to_jid, dest=True)) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
524 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
525 if filters: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
526 if 'timestamp_start' in filters: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
527 stmt = stmt.where(History.timestamp >= float(filters['timestamp_start'])) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
528 if 'before_uid' in filters: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
529 # orignially this query was using SQLITE's rowid. This has been changed |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
530 # to use coalesce(received_timestamp, timestamp) to be SQL engine independant |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
531 stmt = stmt.where( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
532 coalesce( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
533 History.received_timestamp, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
534 History.timestamp |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
535 ) < ( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
536 select(coalesce(History.received_timestamp, History.timestamp)) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
537 .filter_by(uid=filters["before_uid"]) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
538 ).scalar_subquery() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
539 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
540 if 'body' in filters: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
541 # TODO: use REGEXP (function to be defined) instead of GLOB: https://www.sqlite.org/lang_expr.html |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
542 stmt = stmt.where(Message.message.like(f"%{filters['body']}%")) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
543 if 'search' in filters: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
544 search_term = f"%{filters['search']}%" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
545 stmt = stmt.where(or_( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
546 Message.message.like(search_term), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
547 History.source_res.like(search_term) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
548 )) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
549 if 'types' in filters: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
550 types = filters['types'].split() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
551 stmt = stmt.where(History.type.in_(types)) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
552 if 'not_types' in filters: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
553 types = filters['not_types'].split() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
554 stmt = stmt.where(History.type.not_in(types)) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
555 if 'last_stanza_id' in filters: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
556 # this request get the last message with a "stanza_id" that we |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
557 # have in history. This is mainly used to retrieve messages sent |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
558 # while we were offline, using MAM (XEP-0313). |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
559 if (filters['last_stanza_id'] is not True |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
560 or limit != 1): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
561 raise ValueError("Unexpected values for last_stanza_id filter") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
562 stmt = stmt.where(History.stanza_id.is_not(None)) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
563 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
564 if limit is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
565 stmt = stmt.limit(limit) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
566 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
567 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
568 result = await session.execute(stmt) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
569 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
570 result = result.scalars().unique().all() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
571 result.reverse() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
572 return [h.as_tuple() for h in result] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
573 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
574 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
575 async def addToHistory(self, data: dict, profile: str) -> None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
576 """Store a new message in history |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
577 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
578 @param data: message data as build by SatMessageProtocol.onMessage |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
579 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
580 extra = {k: v for k, v in data["extra"].items() if k not in NOT_IN_EXTRA} |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
581 messages = [Message(message=mess, language=lang) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
582 for lang, mess in data["message"].items()] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
583 subjects = [Subject(subject=mess, language=lang) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
584 for lang, mess in data["subject"].items()] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
585 if "thread" in data["extra"]: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
586 thread = Thread(thread_id=data["extra"]["thread"], |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
587 parent_id=data["extra"].get["thread_parent"]) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
588 else: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
589 thread = None |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
590 try: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
591 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
592 async with session.begin(): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
593 session.add(History( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
594 uid=data["uid"], |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
595 stanza_id=data["extra"].get("stanza_id"), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
596 update_uid=data["extra"].get("update_uid"), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
597 profile_id=self.profiles[profile], |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
598 source_jid=data["from"], |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
599 dest_jid=data["to"], |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
600 timestamp=data["timestamp"], |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
601 received_timestamp=data.get("received_timestamp"), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
602 type=data["type"], |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
603 extra=extra, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
604 messages=messages, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
605 subjects=subjects, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
606 thread=thread, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
607 )) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
608 except IntegrityError as e: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
609 if "unique" in str(e.orig).lower(): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
610 log.debug( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
611 f"message {data['uid']!r} is already in history, not storing it again" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
612 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
613 else: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
614 log.error(f"Can't store message {data['uid']!r} in history: {e}") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
615 except Exception as e: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
616 log.critical( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
617 f"Can't store message, unexpected exception (uid: {data['uid']}): {e}" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
618 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
619 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
620 ## Private values |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
621 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
622 def _getPrivateClass(self, binary, profile): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
623 """Get ORM class to use for private values""" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
624 if profile is None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
625 return PrivateGenBin if binary else PrivateGen |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
626 else: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
627 return PrivateIndBin if binary else PrivateInd |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
628 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
629 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
630 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
631 async def getPrivates( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
632 self, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
633 namespace:str, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
634 keys: Optional[Iterable[str]] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
635 binary: bool = False, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
636 profile: Optional[str] = None |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
637 ) -> Dict[str, Any]: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
638 """Get private value(s) from databases |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
639 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
640 @param namespace: namespace of the values |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
641 @param keys: keys of the values to get None to get all keys/values |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
642 @param binary: True to deserialise binary values |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
643 @param profile: profile to use for individual values |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
644 None to use general values |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
645 @return: gotten keys/values |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
646 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
647 if keys is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
648 keys = list(keys) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
649 log.debug( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
650 f"getting {'general' if profile is None else 'individual'}" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
651 f"{' binary' if binary else ''} private values from database for namespace " |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
652 f"{namespace}{f' with keys {keys!r}' if keys is not None else ''}" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
653 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
654 cls = self._getPrivateClass(binary, profile) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
655 stmt = select(cls).filter_by(namespace=namespace) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
656 if keys: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
657 stmt = stmt.where(cls.key.in_(list(keys))) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
658 if profile is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
659 stmt = stmt.filter_by(profile_id=self.profiles[profile]) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
660 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
661 result = await session.execute(stmt) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
662 return {p.key: p.value for p in result.scalars()} |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
663 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
664 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
665 async def setPrivateValue( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
666 self, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
667 namespace: str, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
668 key:str, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
669 value: Any, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
670 binary: bool = False, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
671 profile: Optional[str] = None |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
672 ) -> None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
673 """Set a private value in database |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
674 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
675 @param namespace: namespace of the values |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
676 @param key: key of the value to set |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
677 @param value: value to set |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
678 @param binary: True if it's a binary values |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
679 binary values need to be serialised, used for everything but strings |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
680 @param profile: profile to use for individual value |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
681 if None, it's a general value |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
682 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
683 cls = self._getPrivateClass(binary, profile) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
684 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
685 values = { |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
686 "namespace": namespace, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
687 "key": key, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
688 "value": value |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
689 } |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
690 index_elements = [cls.namespace, cls.key] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
691 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
692 if profile is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
693 values["profile_id"] = self.profiles[profile] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
694 index_elements.append(cls.profile_id) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
695 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
696 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
697 await session.execute( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
698 insert(cls).values(**values).on_conflict_do_update( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
699 index_elements=index_elements, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
700 set_={ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
701 cls.value: value |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
702 } |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
703 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
704 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
705 await session.commit() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
706 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
707 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
708 async def delPrivateValue( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
709 self, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
710 namespace: str, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
711 key: str, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
712 binary: bool = False, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
713 profile: Optional[str] = None |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
714 ) -> None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
715 """Delete private value from database |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
716 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
717 @param category: category of the privateeter |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
718 @param key: key of the private value |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
719 @param binary: True if it's a binary values |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
720 @param profile: profile to use for individual value |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
721 if None, it's a general value |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
722 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
723 cls = self._getPrivateClass(binary, profile) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
724 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
725 stmt = delete(cls).filter_by(namespace=namespace, key=key) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
726 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
727 if profile is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
728 stmt = stmt.filter_by(profile_id=self.profiles[profile]) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
729 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
730 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
731 await session.execute(stmt) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
732 await session.commit() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
733 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
734 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
735 async def delPrivateNamespace( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
736 self, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
737 namespace: str, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
738 binary: bool = False, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
739 profile: Optional[str] = None |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
740 ) -> None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
741 """Delete all data from a private namespace |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
742 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
743 Be really cautious when you use this method, as all data with given namespace are |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
744 removed. |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
745 Params are the same as for delPrivateValue |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
746 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
747 cls = self._getPrivateClass(binary, profile) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
748 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
749 stmt = delete(cls).filter_by(namespace=namespace) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
750 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
751 if profile is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
752 stmt = stmt.filter_by(profile_id=self.profiles[profile]) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
753 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
754 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
755 await session.execute(stmt) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
756 await session.commit() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
757 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
758 ## Files |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
759 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
760 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
761 async def getFiles( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
762 self, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
763 client: Optional[SatXMPPEntity], |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
764 file_id: Optional[str] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
765 version: Optional[str] = '', |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
766 parent: Optional[str] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
767 type_: Optional[str] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
768 file_hash: Optional[str] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
769 hash_algo: Optional[str] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
770 name: Optional[str] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
771 namespace: Optional[str] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
772 mime_type: Optional[str] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
773 public_id: Optional[str] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
774 owner: Optional[jid.JID] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
775 access: Optional[dict] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
776 projection: Optional[List[str]] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
777 unique: bool = False |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
778 ) -> List[dict]: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
779 """Retrieve files with with given filters |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
780 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
781 @param file_id: id of the file |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
782 None to ignore |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
783 @param version: version of the file |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
784 None to ignore |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
785 empty string to look for current version |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
786 @param parent: id of the directory containing the files |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
787 None to ignore |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
788 empty string to look for root files/directories |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
789 @param projection: name of columns to retrieve |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
790 None to retrieve all |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
791 @param unique: if True will remove duplicates |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
792 other params are the same as for [setFile] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
793 @return: files corresponding to filters |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
794 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
795 if projection is None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
796 projection = [ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
797 'id', 'version', 'parent', 'type', 'file_hash', 'hash_algo', 'name', |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
798 'size', 'namespace', 'media_type', 'media_subtype', 'public_id', |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
799 'created', 'modified', 'owner', 'access', 'extra' |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
800 ] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
801 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
802 stmt = select(*[getattr(File, f) for f in projection]) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
803 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
804 if unique: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
805 stmt = stmt.distinct() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
806 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
807 if client is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
808 stmt = stmt.filter_by(profile_id=self.profiles[client.profile]) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
809 else: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
810 if public_id is None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
811 raise exceptions.InternalError( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
812 "client can only be omitted when public_id is set" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
813 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
814 if file_id is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
815 stmt = stmt.filter_by(id=file_id) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
816 if version is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
817 stmt = stmt.filter_by(version=version) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
818 if parent is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
819 stmt = stmt.filter_by(parent=parent) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
820 if type_ is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
821 stmt = stmt.filter_by(type=type_) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
822 if file_hash is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
823 stmt = stmt.filter_by(file_hash=file_hash) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
824 if hash_algo is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
825 stmt = stmt.filter_by(hash_algo=hash_algo) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
826 if name is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
827 stmt = stmt.filter_by(name=name) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
828 if namespace is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
829 stmt = stmt.filter_by(namespace=namespace) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
830 if mime_type is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
831 if '/' in mime_type: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
832 media_type, media_subtype = mime_type.split("/", 1) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
833 stmt = stmt.filter_by(media_type=media_type, media_subtype=media_subtype) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
834 else: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
835 stmt = stmt.filter_by(media_type=mime_type) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
836 if public_id is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
837 stmt = stmt.filter_by(public_id=public_id) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
838 if owner is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
839 stmt = stmt.filter_by(owner=owner) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
840 if access is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
841 raise NotImplementedError('Access check is not implemented yet') |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
842 # a JSON comparison is needed here |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
843 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
844 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
845 result = await session.execute(stmt) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
846 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
847 return [dict(r) for r in result] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
848 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
849 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
850 async def setFile( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
851 self, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
852 client: SatXMPPEntity, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
853 name: str, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
854 file_id: str, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
855 version: str = "", |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
856 parent: str = "", |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
857 type_: str = C.FILE_TYPE_FILE, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
858 file_hash: Optional[str] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
859 hash_algo: Optional[str] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
860 size: int = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
861 namespace: Optional[str] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
862 mime_type: Optional[str] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
863 public_id: Optional[str] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
864 created: Optional[float] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
865 modified: Optional[float] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
866 owner: Optional[jid.JID] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
867 access: Optional[dict] = None, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
868 extra: Optional[dict] = None |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
869 ) -> None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
870 """Set a file metadata |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
871 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
872 @param client: client owning the file |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
873 @param name: name of the file (must not contain "/") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
874 @param file_id: unique id of the file |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
875 @param version: version of this file |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
876 @param parent: id of the directory containing this file |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
877 Empty string if it is a root file/directory |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
878 @param type_: one of: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
879 - file |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
880 - directory |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
881 @param file_hash: unique hash of the payload |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
882 @param hash_algo: algorithm used for hashing the file (usually sha-256) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
883 @param size: size in bytes |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
884 @param namespace: identifier (human readable is better) to group files |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
885 for instance, namespace could be used to group files in a specific photo album |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
886 @param mime_type: media type of the file, or None if not known/guessed |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
887 @param public_id: ID used to server the file publicly via HTTP |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
888 @param created: UNIX time of creation |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
889 @param modified: UNIX time of last modification, or None to use created date |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
890 @param owner: jid of the owner of the file (mainly useful for component) |
3621
9e1a993ad1bf
core (memory/sqla): minor docstring line lenght fix
Goffi <goffi@goffi.org>
parents:
3596
diff
changeset
|
891 @param access: serialisable dictionary with access rules. See [memory.memory] for |
9e1a993ad1bf
core (memory/sqla): minor docstring line lenght fix
Goffi <goffi@goffi.org>
parents:
3596
diff
changeset
|
892 details |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
893 @param extra: serialisable dictionary of any extra data |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
894 will be encoded to json in database |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
895 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
896 if mime_type is None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
897 media_type = media_subtype = None |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
898 elif '/' in mime_type: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
899 media_type, media_subtype = mime_type.split('/', 1) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
900 else: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
901 media_type, media_subtype = mime_type, None |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
902 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
903 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
904 async with session.begin(): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
905 session.add(File( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
906 id=file_id, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
907 version=version.strip(), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
908 parent=parent, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
909 type=type_, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
910 file_hash=file_hash, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
911 hash_algo=hash_algo, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
912 name=name, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
913 size=size, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
914 namespace=namespace, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
915 media_type=media_type, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
916 media_subtype=media_subtype, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
917 public_id=public_id, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
918 created=time.time() if created is None else created, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
919 modified=modified, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
920 owner=owner, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
921 access=access, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
922 extra=extra, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
923 profile_id=self.profiles[client.profile] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
924 )) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
925 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
926 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
927 async def fileGetUsedSpace(self, client: SatXMPPEntity, owner: jid.JID) -> int: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
928 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
929 result = await session.execute( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
930 select(sum_(File.size)).filter_by( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
931 owner=owner, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
932 type=C.FILE_TYPE_FILE, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
933 profile_id=self.profiles[client.profile] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
934 )) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
935 return result.scalar_one_or_none() or 0 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
936 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
937 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
938 async def fileDelete(self, file_id: str) -> None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
939 """Delete file metadata from the database |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
940 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
941 @param file_id: id of the file to delete |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
942 NOTE: file itself must still be removed, this method only handle metadata in |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
943 database |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
944 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
945 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
946 await session.execute(delete(File).filter_by(id=file_id)) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
947 await session.commit() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
948 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
949 @aio |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
950 async def fileUpdate( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
951 self, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
952 file_id: str, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
953 column: str, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
954 update_cb: Callable[[dict], None] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
955 ) -> None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
956 """Update a column value using a method to avoid race conditions |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
957 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
958 the older value will be retrieved from database, then update_cb will be applied to |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
959 update it, and file will be updated checking that older value has not been changed |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
960 meanwhile by an other user. If it has changed, it tries again a couple of times |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
961 before failing |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
962 @param column: column name (only "access" or "extra" are allowed) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
963 @param update_cb: method to update the value of the colum |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
964 the method will take older value as argument, and must update it in place |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
965 update_cb must not care about serialization, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
966 it get the deserialized data (i.e. a Python object) directly |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
967 @raise exceptions.NotFound: there is not file with this id |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
968 """ |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
969 if column not in ('access', 'extra'): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
970 raise exceptions.InternalError('bad column name') |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
971 orm_col = getattr(File, column) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
972 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
973 for i in range(5): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
974 async with self.session() as session: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
975 try: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
976 value = (await session.execute( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
977 select(orm_col).filter_by(id=file_id) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
978 )).scalar_one() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
979 except NoResultFound: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
980 raise exceptions.NotFound |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
981 update_cb(value) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
982 stmt = update(orm_col).filter_by(id=file_id) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
983 if not value: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
984 # because JsonDefaultDict convert NULL to an empty dict, we have to |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
985 # test both for empty dict and None when we have and empty dict |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
986 stmt = stmt.where((orm_col == None) | (orm_col == value)) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
987 else: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
988 stmt = stmt.where(orm_col == value) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
989 result = await session.execute(stmt) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
990 await session.commit() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
991 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
992 if result.rowcount == 1: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
993 break |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
994 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
995 log.warning( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
996 _("table not updated, probably due to race condition, trying again " |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
997 "({tries})").format(tries=i+1) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
998 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
999 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1000 else: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1001 raise exceptions.DatabaseError( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1002 _("Can't update file {file_id} due to race condition") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1003 .format(file_id=file_id) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1004 ) |
3595
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1005 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1006 @aio |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1007 async def getPubsubNode( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1008 self, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1009 client: SatXMPPEntity, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1010 service: jid.JID, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1011 name: str, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1012 with_items: bool = False, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1013 ) -> Optional[PubsubNode]: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1014 """ |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1015 """ |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1016 async with self.session() as session: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1017 stmt = ( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1018 select(PubsubNode) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1019 .filter_by( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1020 service=service, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1021 name=name, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1022 profile_id=self.profiles[client.profile], |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1023 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1024 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1025 if with_items: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1026 stmt = stmt.options( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1027 joinedload(PubsubNode.items) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1028 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1029 result = await session.execute(stmt) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1030 return result.unique().scalar_one_or_none() |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1031 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1032 @aio |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1033 async def setPubsubNode( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1034 self, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1035 client: SatXMPPEntity, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1036 service: jid.JID, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1037 name: str, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1038 analyser: Optional[str] = None, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1039 type_: Optional[str] = None, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1040 subtype: Optional[str] = None, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1041 ) -> PubsubNode: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1042 node = PubsubNode( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1043 profile_id=self.profiles[client.profile], |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1044 service=service, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1045 name=name, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1046 subscribed=False, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1047 analyser=analyser, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1048 type_=type_, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1049 subtype=subtype, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1050 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1051 async with self.session() as session: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1052 async with session.begin(): |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1053 session.add(node) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1054 return node |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1055 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1056 @aio |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1057 async def updatePubsubNodeSyncState( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1058 self, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1059 node: PubsubNode, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1060 state: SyncState |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1061 ) -> None: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1062 async with self.session() as session: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1063 async with session.begin(): |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1064 await session.execute( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1065 update(PubsubNode) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1066 .filter_by(id=node.id) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1067 .values( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1068 sync_state=state, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1069 sync_state_updated=time.time(), |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1070 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1071 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1072 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1073 @aio |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1074 async def deletePubsubNode( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1075 self, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1076 profiles: Optional[List[str]], |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1077 services: Optional[List[jid.JID]], |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1078 names: Optional[List[str]] |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1079 ) -> None: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1080 """Delete items cached for a node |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1081 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1082 @param profiles: profile names from which nodes must be deleted. |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1083 None to remove nodes from ALL profiles |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1084 @param services: JIDs of pubsub services from which nodes must be deleted. |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1085 None to remove nodes from ALL services |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1086 @param names: names of nodes which must be deleted. |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1087 None to remove ALL nodes whatever is their names |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1088 """ |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1089 stmt = delete(PubsubNode) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1090 if profiles is not None: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1091 stmt = stmt.where( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1092 PubsubNode.profile.in_( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1093 [self.profiles[p] for p in profiles] |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1094 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1095 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1096 if services is not None: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1097 stmt = stmt.where(PubsubNode.service.in_(services)) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1098 if names is not None: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1099 stmt = stmt.where(PubsubNode.name.in_(names)) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1100 async with self.session() as session: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1101 await session.execute(stmt) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1102 await session.commit() |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1103 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1104 @aio |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1105 async def cachePubsubItems( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1106 self, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1107 client: SatXMPPEntity, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1108 node: PubsubNode, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1109 items: List[domish.Element], |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1110 parsed_items: Optional[List[dict]] = None, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1111 ) -> None: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1112 """Add items to database, using an upsert taking care of "updated" field""" |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1113 if parsed_items is not None and len(items) != len(parsed_items): |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1114 raise exceptions.InternalError( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1115 "parsed_items must have the same lenght as items" |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1116 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1117 async with self.session() as session: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1118 async with session.begin(): |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1119 for idx, item in enumerate(items): |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1120 parsed = parsed_items[idx] if parsed_items else None |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1121 stmt = insert(PubsubItem).values( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1122 node_id = node.id, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1123 name = item["id"], |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1124 data = item, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1125 parsed = parsed, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1126 ).on_conflict_do_update( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1127 index_elements=(PubsubItem.node_id, PubsubItem.name), |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1128 set_={ |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1129 PubsubItem.data: item, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1130 PubsubItem.parsed: parsed, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1131 PubsubItem.updated: now() |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1132 } |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1133 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1134 await session.execute(stmt) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1135 await session.commit() |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1136 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1137 @aio |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1138 async def deletePubsubItems( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1139 self, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1140 node: PubsubNode, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1141 items_names: Optional[List[str]] = None |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1142 ) -> None: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1143 """Delete items cached for a node |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1144 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1145 @param node: node from which items must be deleted |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1146 @param items_names: names of items to delete |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1147 if None, ALL items will be deleted |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1148 """ |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1149 stmt = delete(PubsubItem) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1150 if node is not None: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1151 if isinstance(node, list): |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1152 stmt = stmt.where(PubsubItem.node_id.in_([n.id for n in node])) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1153 else: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1154 stmt = stmt.filter_by(node_id=node.id) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1155 if items_names is not None: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1156 stmt = stmt.where(PubsubItem.name.in_(items_names)) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1157 async with self.session() as session: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1158 await session.execute(stmt) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1159 await session.commit() |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1160 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1161 @aio |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1162 async def purgePubsubItems( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1163 self, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1164 services: Optional[List[jid.JID]] = None, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1165 names: Optional[List[str]] = None, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1166 types: Optional[List[str]] = None, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1167 subtypes: Optional[List[str]] = None, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1168 profiles: Optional[List[str]] = None, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1169 created_before: Optional[datetime] = None, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1170 updated_before: Optional[datetime] = None, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1171 ) -> None: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1172 """Delete items cached for a node |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1173 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1174 @param node: node from which items must be deleted |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1175 @param items_names: names of items to delete |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1176 if None, ALL items will be deleted |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1177 """ |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1178 stmt = delete(PubsubItem) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1179 node_fields = { |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1180 "service": services, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1181 "name": names, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1182 "type_": types, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1183 "subtype": subtypes, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1184 } |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1185 if any(x is not None for x in node_fields.values()): |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1186 sub_q = select(PubsubNode.id) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1187 for col, values in node_fields.items(): |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1188 if values is None: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1189 continue |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1190 sub_q = sub_q.where(getattr(PubsubNode, col).in_(values)) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1191 stmt = ( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1192 stmt |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1193 .where(PubsubItem.node_id.in_(sub_q)) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1194 .execution_options(synchronize_session=False) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1195 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1196 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1197 if profiles is not None: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1198 stmt = stmt.where( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1199 PubsubItem.profile_id.in_([self.profiles[p] for p in profiles]) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1200 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1201 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1202 if created_before is not None: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1203 stmt = stmt.where(PubsubItem.created < created_before) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1204 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1205 if updated_before is not None: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1206 stmt = stmt.where(PubsubItem.updated < updated_before) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1207 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1208 async with self.session() as session: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1209 await session.execute(stmt) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1210 await session.commit() |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1211 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1212 @aio |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1213 async def getItems( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1214 self, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1215 node: PubsubNode, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1216 max_items: Optional[int] = None, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1217 before: Optional[str] = None, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1218 after: Optional[str] = None, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1219 from_index: Optional[int] = None, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1220 order_by: Optional[List[str]] = None, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1221 desc: bool = True, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1222 force_rsm: bool = False, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1223 ) -> Tuple[List[PubsubItem], dict]: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1224 """Get Pubsub Items from cache |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1225 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1226 @param node: retrieve items from this node (must be synchronised) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1227 @param max_items: maximum number of items to retrieve |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1228 @param before: get items which are before the item with this name in given order |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1229 empty string is not managed here, use desc order to reproduce RSM |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1230 behaviour. |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1231 @param after: get items which are after the item with this name in given order |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1232 @param from_index: get items with item index (as defined in RSM spec) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1233 starting from this number |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1234 @param order_by: sorting order of items (one of C.ORDER_BY_*) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1235 @param desc: direction or ordering |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1236 @param force_rsm: if True, force the use of RSM worklow. |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1237 RSM workflow is automatically used if any of before, after or |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1238 from_index is used, but if only RSM max_items is used, it won't be |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1239 used by default. This parameter let's use RSM workflow in this |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1240 case. Note that in addition to RSM metadata, the result will not be |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1241 the same (max_items without RSM will returns most recent items, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1242 i.e. last items in modification order, while max_items with RSM |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1243 will return the oldest ones (i.e. first items in modification |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1244 order). |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1245 to be used when max_items is used from RSM |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1246 """ |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1247 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1248 metadata = { |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1249 "service": node.service, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1250 "node": node.name, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1251 "uri": uri.buildXMPPUri( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1252 "pubsub", |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1253 path=node.service.full(), |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1254 node=node.name, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1255 ), |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1256 } |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1257 if max_items is None: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1258 max_items = 20 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1259 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1260 use_rsm = any((before, after, from_index is not None)) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1261 if force_rsm and not use_rsm: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1262 # |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1263 use_rsm = True |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1264 from_index = 0 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1265 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1266 stmt = ( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1267 select(PubsubItem) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1268 .filter_by(node_id=node.id) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1269 .limit(max_items) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1270 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1271 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1272 if not order_by: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1273 order_by = [C.ORDER_BY_MODIFICATION] |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1274 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1275 order = [] |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1276 for order_type in order_by: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1277 if order_type == C.ORDER_BY_MODIFICATION: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1278 if desc: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1279 order.extend((PubsubItem.updated.desc(), PubsubItem.id.desc())) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1280 else: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1281 order.extend((PubsubItem.updated.asc(), PubsubItem.id.asc())) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1282 elif order_type == C.ORDER_BY_CREATION: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1283 if desc: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1284 order.append(PubsubItem.id.desc()) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1285 else: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1286 order.append(PubsubItem.id.asc()) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1287 else: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1288 raise exceptions.InternalError(f"Unknown order type {order_type!r}") |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1289 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1290 stmt = stmt.order_by(*order) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1291 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1292 if use_rsm: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1293 # CTE to have result row numbers |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1294 row_num_q = select( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1295 PubsubItem.id, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1296 PubsubItem.name, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1297 # row_number starts from 1, but RSM index must start from 0 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1298 (func.row_number().over(order_by=order)-1).label("item_index") |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1299 ).filter_by(node_id=node.id) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1300 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1301 row_num_cte = row_num_q.cte() |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1302 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1303 if max_items > 0: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1304 # as we can't simply use PubsubItem.id when we order by modification, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1305 # we need to use row number |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1306 item_name = before or after |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1307 row_num_limit_q = ( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1308 select(row_num_cte.c.item_index) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1309 .where(row_num_cte.c.name==item_name) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1310 ).scalar_subquery() |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1311 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1312 stmt = ( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1313 select(row_num_cte.c.item_index, PubsubItem) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1314 .join(row_num_cte, PubsubItem.id == row_num_cte.c.id) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1315 .limit(max_items) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1316 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1317 if before: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1318 stmt = ( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1319 stmt |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1320 .where(row_num_cte.c.item_index<row_num_limit_q) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1321 .order_by(row_num_cte.c.item_index.desc()) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1322 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1323 elif after: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1324 stmt = ( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1325 stmt |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1326 .where(row_num_cte.c.item_index>row_num_limit_q) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1327 .order_by(row_num_cte.c.item_index.asc()) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1328 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1329 else: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1330 stmt = ( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1331 stmt |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1332 .where(row_num_cte.c.item_index>=from_index) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1333 .order_by(row_num_cte.c.item_index.asc()) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1334 ) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1335 # from_index is used |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1336 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1337 async with self.session() as session: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1338 if max_items == 0: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1339 items = result = [] |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1340 else: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1341 result = await session.execute(stmt) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1342 result = result.all() |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1343 if before: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1344 result.reverse() |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1345 items = [row[-1] for row in result] |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1346 rows_count = ( |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1347 await session.execute(row_num_q.with_only_columns(count())) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1348 ).scalar_one() |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1349 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1350 try: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1351 index = result[0][0] |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1352 except IndexError: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1353 index = None |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1354 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1355 try: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1356 first = result[0][1].name |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1357 except IndexError: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1358 first = None |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1359 last = None |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1360 else: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1361 last = result[-1][1].name |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1362 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1363 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1364 metadata["rsm"] = { |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1365 "index": index, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1366 "count": rows_count, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1367 "first": first, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1368 "last": last, |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1369 } |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1370 metadata["complete"] = index + len(result) == rows_count |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1371 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1372 return items, metadata |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1373 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1374 async with self.session() as session: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1375 result = await session.execute(stmt) |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1376 |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1377 result = result.scalars().all() |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1378 if desc: |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1379 result.reverse() |
7510648e8e3a
core (memory/sqla): methods to manipulate pubsub tables
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
1380 return result, metadata |
3664
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1381 |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1382 def _getSqlitePath( |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1383 self, |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1384 path: List[Union[str, int]] |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1385 ) -> str: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1386 """generate path suitable to query JSON element with SQLite""" |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1387 return f"${''.join(f'[{p}]' if isinstance(p, int) else f'.{p}' for p in path)}" |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1388 |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1389 @aio |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1390 async def searchPubsubItems( |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1391 self, |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1392 query: dict, |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1393 ) -> Tuple[List[PubsubItem]]: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1394 """Search for pubsub items in cache |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1395 |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1396 @param query: search terms. Keys can be: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1397 :fts (str): |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1398 Full-Text Search query. Currently SQLite FT5 engine is used, its query |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1399 syntax can be used, see `FTS5 Query documentation |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1400 <https://sqlite.org/fts5.html#full_text_query_syntax>`_ |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1401 :profiles (list[str]): |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1402 filter on nodes linked to those profiles |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1403 :nodes (list[str]): |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1404 filter on nodes with those names |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1405 :services (list[jid.JID]): |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1406 filter on nodes from those services |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1407 :types (list[str|None]): |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1408 filter on nodes with those types. None can be used to filter on nodes with |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1409 no type set |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1410 :subtypes (list[str|None]): |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1411 filter on nodes with those subtypes. None can be used to filter on nodes with |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1412 no subtype set |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1413 :parsed (list[dict]): |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1414 Filter on a parsed data field. The dict must contain 3 keys: ``path`` |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1415 which is a list of str or int giving the path to the field of interest |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1416 (str for a dict key, int for a list index), ``operator`` with indicate the |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1417 operator to use to check the condition, and ``value`` which depends of |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1418 field type and operator. |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1419 |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1420 See documentation for details on operators (it's currently explained at |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1421 ``doc/libervia-cli/pubsub_cache.rst`` in ``search`` command |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1422 documentation). |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1423 |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1424 :order-by (list[dict]): |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1425 Indicates how to order results. The dict can contain either a ``order`` |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1426 for a well-know order or a ``path`` for a parsed data field path |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1427 (``order`` and ``path`` can't be used at the same time), an an optional |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1428 ``direction`` which can be ``asc`` or ``desc``. See documentation for |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1429 details on well-known orders (it's currently explained at |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1430 ``doc/libervia-cli/pubsub_cache.rst`` in ``search`` command |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1431 documentation). |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1432 |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1433 :index (int): |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1434 starting index of items to return from the query result. It's translated |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1435 to SQL's OFFSET |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1436 |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1437 :limit (int): |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1438 maximum number of items to return. It's translated to SQL's LIMIT. |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1439 |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1440 @result: found items (the ``node`` attribute will be filled with suitable |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1441 PubsubNode) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1442 """ |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1443 # TODO: FTS and parsed data filters use SQLite specific syntax |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1444 # when other DB engines will be used, this will have to be adapted |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1445 stmt = select(PubsubItem) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1446 |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1447 # Full-Text Search |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1448 fts = query.get("fts") |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1449 if fts: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1450 fts_select = text( |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1451 "SELECT rowid, rank FROM pubsub_items_fts(:fts_query)" |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1452 ).bindparams(fts_query=fts).columns(rowid=Integer).subquery() |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1453 stmt = ( |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1454 stmt |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1455 .select_from(fts_select) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1456 .outerjoin(PubsubItem, fts_select.c.rowid == PubsubItem.id) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1457 ) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1458 |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1459 # node related filters |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1460 profiles = query.get("profiles") |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1461 if (profiles |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1462 or any(query.get(k) for k in ("nodes", "services", "types", "subtypes")) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1463 ): |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1464 stmt = stmt.join(PubsubNode).options(contains_eager(PubsubItem.node)) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1465 if profiles: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1466 try: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1467 stmt = stmt.where( |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1468 PubsubNode.profile_id.in_(self.profiles[p] for p in profiles) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1469 ) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1470 except KeyError as e: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1471 raise exceptions.ProfileUnknownError( |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1472 f"This profile doesn't exist: {e.args[0]!r}" |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1473 ) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1474 for key, attr in ( |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1475 ("nodes", "name"), |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1476 ("services", "service"), |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1477 ("types", "type_"), |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1478 ("subtypes", "subtype") |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1479 ): |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1480 value = query.get(key) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1481 if not value: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1482 continue |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1483 if key in ("types", "subtypes") and None in value: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1484 # NULL can't be used with SQL's IN, so we have to add a condition with |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1485 # IS NULL, and use a OR if there are other values to check |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1486 value.remove(None) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1487 condition = getattr(PubsubNode, attr).is_(None) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1488 if value: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1489 condition = or_( |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1490 getattr(PubsubNode, attr).in_(value), |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1491 condition |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1492 ) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1493 else: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1494 condition = getattr(PubsubNode, attr).in_(value) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1495 stmt = stmt.where(condition) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1496 else: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1497 stmt = stmt.options(selectinload(PubsubItem.node)) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1498 |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1499 # parsed data filters |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1500 parsed = query.get("parsed", []) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1501 for filter_ in parsed: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1502 try: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1503 path = filter_["path"] |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1504 operator = filter_["op"] |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1505 value = filter_["value"] |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1506 except KeyError as e: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1507 raise ValueError( |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1508 f'missing mandatory key {e.args[0]!r} in "parsed" filter' |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1509 ) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1510 try: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1511 op_attr = OP_MAP[operator] |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1512 except KeyError: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1513 raise ValueError(f"invalid operator: {operator!r}") |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1514 sqlite_path = self._getSqlitePath(path) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1515 if operator in ("overlap", "ioverlap", "disjoint", "idisjoint"): |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1516 col = literal_column("json_each.value") |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1517 if operator[0] == "i": |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1518 col = func.lower(col) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1519 value = [str(v).lower() for v in value] |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1520 condition = ( |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1521 select(1) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1522 .select_from(func.json_each(PubsubItem.parsed, sqlite_path)) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1523 .where(col.in_(value)) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1524 ).scalar_subquery() |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1525 if operator in ("disjoint", "idisjoint"): |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1526 condition = condition.is_(None) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1527 stmt = stmt.where(condition) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1528 elif operator == "between": |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1529 try: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1530 left, right = value |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1531 except (ValueError, TypeError): |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1532 raise ValueError(_( |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1533 "invalid value for \"between\" filter, you must use a 2 items " |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1534 "array: {value!r}" |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1535 ).format(value=value)) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1536 col = func.json_extract(PubsubItem.parsed, sqlite_path) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1537 stmt = stmt.where(col.between(left, right)) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1538 else: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1539 # we use func.json_extract instead of generic JSON way because SQLAlchemy |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1540 # add a JSON_QUOTE to the value, and we want SQL value |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1541 col = func.json_extract(PubsubItem.parsed, sqlite_path) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1542 stmt = stmt.where(getattr(col, op_attr)(value)) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1543 |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1544 # order |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1545 order_by = query.get("order-by") or [{"order": "creation"}] |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1546 |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1547 for order_data in order_by: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1548 order, path = order_data.get("order"), order_data.get("path") |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1549 if order and path: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1550 raise ValueError(_( |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1551 '"order" and "path" can\'t be used at the same time in ' |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1552 '"order-by" data' |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1553 )) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1554 if order: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1555 if order == "creation": |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1556 col = PubsubItem.id |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1557 elif order == "modification": |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1558 col = PubsubItem.updated |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1559 elif order == "item_id": |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1560 col = PubsubItem.name |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1561 elif order == "rank": |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1562 if not fts: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1563 raise ValueError( |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1564 "'rank' order can only be used with Full-Text Search (fts)" |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1565 ) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1566 col = literal_column("rank") |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1567 else: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1568 raise NotImplementedError(f"Unknown {order!r} order") |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1569 else: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1570 # we have a JSON path |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1571 # sqlite_path = self._getSqlitePath(path) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1572 col = PubsubItem.parsed[path] |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1573 direction = order_data.get("direction", "ASC").lower() |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1574 if not direction in ("asc", "desc"): |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1575 raise ValueError(f"Invalid order-by direction: {direction!r}") |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1576 stmt = stmt.order_by(getattr(col, direction)()) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1577 |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1578 # offset, limit |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1579 index = query.get("index") |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1580 if index: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1581 stmt = stmt.offset(index) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1582 limit = query.get("limit") |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1583 if limit: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1584 stmt = stmt.limit(limit) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1585 |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1586 async with self.session() as session: |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1587 result = await session.execute(stmt) |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1588 |
9ae6ec74face
memory (sqla): implement `searchPubsubItems`:
Goffi <goffi@goffi.org>
parents:
3638
diff
changeset
|
1589 return result.scalars().all() |