From 79f88a278cfa9f5168937e95e2d43b143e90ec0b Mon Sep 17 00:00:00 2001 From: redxef Date: Tue, 6 Dec 2022 18:14:45 +0100 Subject: [PATCH] Implement Storage.move. --- radicale_sql/__init__.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/radicale_sql/__init__.py b/radicale_sql/__init__.py index 41e1bda..8ac9ba4 100644 --- a/radicale_sql/__init__.py +++ b/radicale_sql/__init__.py @@ -460,7 +460,34 @@ class Storage(BaseStorage): def move(self, item: "radicale_item.Item", to_collection: "BaseCollection", to_href: str) -> None: - pass + assert isinstance(item.collection, Collection) + assert isinstance(to_collection, Collection) + src_collection_id = item.collection._id + dst_collection_id = to_collection._id + item_table = self._meta.tables['item'] + + delete_stmt = sa.delete( + item_table, + ).where( + sa.and_( + item_table.c.collection_id == dst_collection_id, + item_table.c.name == to_href, + ) + ) + update_stmt = sa.update( + item_table, + ).values( + collection_id=dst_collection_id, + name=to_href, + ).where( + sa.and_( + item_table.c.collection_id == src_collection_id, + item_table.c.name == item.href, + ) + ) + with self._engine.begin() as connection: + connection.execute(delete_stmt) + connection.execute(update_stmt) def create_collection( self,