summaryrefslogtreecommitdiff
path: root/examples/adjacencytree/byroot_tree.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/adjacencytree/byroot_tree.py')
-rw-r--r--examples/adjacencytree/byroot_tree.py61
1 files changed, 28 insertions, 33 deletions
diff --git a/examples/adjacencytree/byroot_tree.py b/examples/adjacencytree/byroot_tree.py
index 5ec055392..a61bde875 100644
--- a/examples/adjacencytree/byroot_tree.py
+++ b/examples/adjacencytree/byroot_tree.py
@@ -3,7 +3,8 @@ introduces a new selection method which selects an entire tree of nodes at once,
advantage of a custom MapperExtension to assemble incoming nodes into their correct structure."""
from sqlalchemy import *
-from sqlalchemy.util import OrderedDict
+from sqlalchemy.orm import *
+from sqlalchemy.orm.collections import attribute_mapped_collection
engine = create_engine('sqlite:///:memory:', echo=True)
@@ -28,82 +29,69 @@ treedata = Table(
)
-class NodeList(OrderedDict):
- """subclasses OrderedDict to allow usage as a list-based property."""
- def append(self, node):
- self[node.name] = node
- def __iter__(self):
- return iter(self.values())
-
-
class TreeNode(object):
"""a hierarchical Tree class, which adds the concept of a "root node". The root is
the topmost node in a tree, or in other words a node whose parent ID is NULL.
All child nodes that are decendents of a particular root, as well as a root node itself,
- reference this root node.
- this is useful as a way to identify all nodes in a tree as belonging to a single
- identifiable root. Any node can return its root node and therefore the "tree" that it
- belongs to, and entire trees can be selected from the database in one query, by
- identifying their common root ID."""
+ reference this root node. """
def __init__(self, name):
- """for data integrity, a TreeNode requires its name to be passed as a parameter
- to its constructor, so there is no chance of a TreeNode that doesnt have a name."""
self.name = name
- self.children = NodeList()
self.root = self
- self.parent = None
- self.id = None
- self.data =None
- self.parent_id = None
- self.root_id=None
+
def _set_root(self, root):
self.root = root
- for c in self.children:
+ for c in self.children.values():
c._set_root(root)
+
def append(self, node):
if isinstance(node, str):
node = TreeNode(node)
- node.parent = self
node._set_root(self.root)
self.children.append(node)
+
def __repr__(self):
return self._getstring(0, False)
+
def __str__(self):
return self._getstring(0, False)
+
def _getstring(self, level, expand = False):
s = (' ' * level) + "%s (%s,%s,%s, %d): %s" % (self.name, self.id,self.parent_id,self.root_id, id(self), repr(self.data)) + '\n'
if expand:
s += ''.join([n._getstring(level+1, True) for n in self.children.values()])
return s
+
def print_nodes(self):
return self._getstring(0, True)
class TreeLoader(MapperExtension):
- """an extension that will plug-in additional functionality to the Mapper."""
+
def after_insert(self, mapper, connection, instance):
"""runs after the insert of a new TreeNode row. The primary key of the row is not determined
until the insert is complete, since most DB's use autoincrementing columns. If this node is
the root node, we will take the new primary key and update it as the value of the node's
"root ID" as well, since its root node is itself."""
+
if instance.root is instance:
connection.execute(mapper.mapped_table.update(TreeNode.c.id==instance.id, values=dict(root_node_id=instance.id)))
instance.root_id = instance.id
- def append_result(self, mapper, selectcontext, row, instance, identitykey, result, isnew):
+ def append_result(self, mapper, selectcontext, row, instance, result, **flags):
"""runs as results from a SELECT statement are processed, and newly created or already-existing
instances that correspond to each row are appended to result lists. This method will only
append root nodes to the result list, and will attach child nodes to their appropriate parent
node as they arrive from the select results. This allows a SELECT statement which returns
both root and child nodes in one query to return a list of "roots"."""
+
+ isnew = flags.get('isnew', False)
+
if instance.parent_id is None:
result.append(instance)
else:
- if isnew or context.populate_existing:
+ if isnew or selectcontext.populate_existing:
parentnode = selectcontext.identity_map[mapper.identity_key(instance.parent_id)]
- parentnode.children.append_without_event(instance)
- # fire off lazy loader before the instance is part of the session
- instance.children
+ parentnode.children.append(instance)
return False
class TreeData(object):
@@ -127,12 +115,19 @@ mapper(TreeNode, trees, properties=dict(
name=trees.c.node_name,
parent_id=trees.c.parent_node_id,
root_id=trees.c.root_node_id,
- root=relation(TreeNode, primaryjoin=trees.c.root_node_id==trees.c.node_id, remote_side=trees.c.node_id, lazy=None, uselist=False),
- children=relation(TreeNode, primaryjoin=trees.c.parent_node_id==trees.c.node_id, lazy=None, uselist=True, cascade="delete,save-update", collection_class=NodeList),
- data=relation(mapper(TreeData, treedata, properties=dict(id=treedata.c.data_id)), cascade="delete,delete-orphan,save-update", lazy=False)
+ root=relation(TreeNode, primaryjoin=trees.c.root_node_id==trees.c.node_id, remote_side=trees.c.node_id, lazy=None),
+ children=relation(TreeNode,
+ primaryjoin=trees.c.parent_node_id==trees.c.node_id,
+ lazy=None,
+ cascade="all",
+ collection_class=attribute_mapped_collection('name'),
+ backref=backref('parent', primaryjoin=trees.c.parent_node_id==trees.c.node_id, remote_side=trees.c.node_id)
+ ),
+ data=relation(TreeData, cascade="all, delete-orphan", lazy=False)
), extension = TreeLoader())
+mapper(TreeData, treedata, properties={'id':treedata.c.data_id})
session = create_session()