from sqlalchemy.sql import *
import sqlalchemy.util as util
import tables
-import string, sys
+import string, sys, time
"""a more advanced example of basic_tree.py. illustrates MapperExtension objects which
add application-specific functionality to a Mapper object."""
if instance.root is instance:
mapper.primarytable.update(TreeNode.c.id==instance.id, values=dict(root_node_id=instance.id)).execute()
instance.root_id = instance.id
- def append_result(self, mapper, row, imap, result, instance, populate_existing=False):
+ def append_result(self, mapper, row, imap, result, instance, isnew, populate_existing=False):
"""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
if instance.parent_id is None:
result.append(instance)
else:
- parentnode = imap[mapper.identity_key(instance.parent_id)]
- parentnode.children.append(instance, _mapper_nohistory=True)
+ if isnew or populate_existing:
+ parentnode = imap[mapper.identity_key(instance.parent_id)]
+ parentnode.children.append(instance, _mapper_nohistory=True)
return False
class TreeData(object):
#engine = sqlalchemy.engine.create_engine('sqlite', ':memory:', {}, echo = True)
engine = sqlalchemy.engine.create_engine('postgres', {'database':'test', 'host':'127.0.0.1', 'user':'scott', 'password':'tiger'}, echo=True)
+
"""create the treenodes table. This is ia basic adjacency list model table.
One additional column, "root_node_id", references a "root node" row and is used
in the 'byroot_tree' example."""
import sqlalchemy.ansisql as ansisql
import sqlalchemy.types as sqltypes
from sqlalchemy.ansisql import *
+try:
+ import psycopg2 as psycopg
+except:
+ import psycopg
class PGNumeric(sqltypes.Numeric):
def get_col_spec(self):
def __init__(self, opts, module = None, **params):
if module is None:
self.module = __import__('psycopg2')
+ #self.module = psycopg
else:
self.module = module
self.opts = opts or {}
if objectstore.uow().has_key(identitykey):
instance = objectstore.uow()._get(identitykey)
+ isnew = False
if populate_existing:
isnew = not imap.has_key(identitykey)
if isnew:
for prop in self.props.values():
prop.execute(instance, row, identitykey, imap, isnew)
- if self.extension.append_result(self, row, imap, result, instance, populate_existing=populate_existing):
+ if self.extension.append_result(self, row, imap, result, instance, isnew, populate_existing=populate_existing):
if result is not None:
result.append_nohistory(instance)
for prop in self.props.values():
prop.execute(instance, row, identitykey, imap, isnew)
- if self.extension.append_result(self, row, imap, result, instance, populate_existing=populate_existing):
+ if self.extension.append_result(self, row, imap, result, instance, isnew, populate_existing=populate_existing):
if result is not None:
result.append_nohistory(instance)
class MapperExtension(object):
def create_instance(self, mapper, row, imap, class_):
return None
- def append_result(self, mapper, row, imap, result, instance, populate_existing=False):
+ def append_result(self, mapper, row, imap, result, instance, isnew, populate_existing=False):
return True
def after_insert(self, mapper, instance):
pass
"""executes this UOWTask. saves objects to be saved, processes all dependencies
that have been registered, and deletes objects to be deleted. """
if self.circular is not None:
- print "CIRCULAR !"
self.circular.execute(trans)
- print "CIRCULAR DONE !"
return
- print "task " + str(self) + " tosave: " + repr(self.tosave_objects())
+# print "task " + str(self) + " tosave: " + repr(self.tosave_objects())
self.mapper.save_obj(self.tosave_objects(), trans)
for dep in self.save_dependencies():
(processor, targettask, isdelete) = dep
processor.process_dependencies(targettask, [elem.obj for elem in targettask.tosave_elements()], trans, delete = False)
- print "processed dependencies on " + repr([elem.obj for elem in targettask.tosave_elements()])
+ # print "processed dependencies on " + repr([elem.obj for elem in targettask.tosave_elements()])
for element in self.tosave_elements():
if element.childtask is not None:
- print "execute elem childtask " + str(element.childtask)
+# print "execute elem childtask " + str(element.childtask)
element.childtask.execute(trans)
for dep in self.delete_dependencies():
(processor, targettask, isdelete) = dep
if self.circular is not None:
s += " Circular Representation:"
s += self.circular.dump(indent + " ")
+ s += "\n----------------------"
return s
saveobj = self.tosave_elements()
if len(saveobj) > 0:
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
__ALL__ = ['OrderedProperties', 'OrderedDict']
-import thread, weakref, UserList
+import thread, weakref, UserList,string
class OrderedProperties(object):
"""an object that maintains the order in which attributes are set upon it.
childnode.parent.children.append(parentnode)
parentnode.children.append(childnode)
childnode.parent = parentnode
- #print str(head)
# go through the total list of items. for those
# that had no dependency tuples, and therefore are not
n = DependencySorter.Node(item)
head.children.append(n)
n.parent = head
+ #print str(head)
return head
\ No newline at end of file