1. angri
  2. sqlamp

Commits

angri  committed adee794

factored common code out from _move_subtree_by_parent() and _move_subtree_by_sibling()

  • Participants
  • Parent commits bd5f6b9
  • Branches default

Comments (0)

Files changed (1)

File sqlamp/__init__.py

View file
     def _move_subtree_by_parent(self, top_or_bottom, session,
                                 node_id, new_parent_id):
         opts = self._mp_opts
-        [[old_path, old_depth, old_tree_id]] = session.execute(
-            sqlalchemy.select([opts.path_field, opts.depth_field,
-                               opts.tree_id_field], opts.pk_field == node_id)
-        )
-        [[parents_path, parents_depth, new_tree_id]] = session.execute(
-            sqlalchemy.select([opts.path_field, opts.depth_field,
-                               opts.tree_id_field],
-                               opts.pk_field == new_parent_id)
-        )
-        if old_tree_id == new_tree_id and parents_path.startswith(old_path):
-            raise MovingToDescendantError()
+        old_path, old_depth, old_tree_id, \
+            parents_parent_id, parents_path, parents_depth, new_tree_id \
+            = self._prepare_to_move_subtree(session, node_id, new_parent_id)
 
         new_depth = parents_depth + 1
 
     def _move_subtree_by_sibling(self, before_or_after, session,
                                  node_id, anchor_id):
         opts = self._mp_opts
-        [[old_path, old_depth, old_tree_id]] = session.execute(
-            sqlalchemy.select([opts.path_field, opts.depth_field,
-                               opts.tree_id_field], opts.pk_field == node_id)
-        )
-        [[new_parent_id, anchors_path, new_depth, new_tree_id]] \
-            = session.execute(
-            sqlalchemy.select([opts.parent_id_field, opts.path_field,
-                               opts.depth_field, opts.tree_id_field],
-                               opts.pk_field == anchor_id)
-        )
-        if old_tree_id == new_tree_id and anchors_path.startswith(old_path):
-            raise MovingToDescendantError()
+        old_path, old_depth, old_tree_id, \
+            new_parent_id, anchors_path, new_depth, new_tree_id \
+            = self._prepare_to_move_subtree(session, node_id, anchor_id)
 
         if before_or_after == 'after':
             anchors_path = inc_path(anchors_path, opts.steplen)
         self._reparent(session, node_id, new_parent_id, new_tree_id, new_path,
                        new_depth, old_tree_id, old_path, old_depth)
 
+    def _prepare_to_move_subtree(self, session, node_id, anchor_id):
+        opts = self._mp_opts
+        columns = [opts.parent_id_field, opts.path_field,
+                   opts.depth_field, opts.tree_id_field]
+        node_select = sqlalchemy.select(columns, opts.pk_field == node_id)
+        anchor_select = sqlalchemy.select(columns, opts.pk_field == anchor_id)
+        [[old_parent_id, old_path, old_depth, old_tree_id],
+         [anchor_parent_id, anchor_path, anchor_depth, anchor_tree_id]] \
+                 = session.execute(node_select.union_all(anchor_select))
+        if old_tree_id == anchor_tree_id and anchor_path.startswith(old_path):
+            raise MovingToDescendantError()
+        return old_path, old_depth, old_tree_id, \
+               anchor_parent_id, anchor_path, anchor_depth, anchor_tree_id
+
     def _reparent(self, session, node_id, new_parent_id, new_tree_id, new_path,
                   new_depth, old_tree_id, old_path, old_depth):
         opts = self._mp_opts