From 722f97a826944073089998c42c48371a2514b9d6 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 23 Oct 2005 03:46:20 +0000 Subject: [PATCH] --- examples/adjacencytree/byroot_tree.py | 9 +++++---- examples/adjacencytree/tables.py | 1 + lib/sqlalchemy/databases/postgres.py | 5 +++++ lib/sqlalchemy/mapper.py | 7 ++++--- lib/sqlalchemy/objectstore.py | 9 ++++----- lib/sqlalchemy/util.py | 4 ++-- 6 files changed, 21 insertions(+), 14 deletions(-) diff --git a/examples/adjacencytree/byroot_tree.py b/examples/adjacencytree/byroot_tree.py index d352db4c71..ee1dd4a495 100644 --- a/examples/adjacencytree/byroot_tree.py +++ b/examples/adjacencytree/byroot_tree.py @@ -3,7 +3,7 @@ from sqlalchemy.schema import * 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.""" @@ -79,7 +79,7 @@ class TreeLoader(MapperExtension): 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 @@ -88,8 +88,9 @@ class TreeLoader(MapperExtension): 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): diff --git a/examples/adjacencytree/tables.py b/examples/adjacencytree/tables.py index 3c144afe60..e82c8ee598 100644 --- a/examples/adjacencytree/tables.py +++ b/examples/adjacencytree/tables.py @@ -4,6 +4,7 @@ import sqlalchemy.engine #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.""" diff --git a/lib/sqlalchemy/databases/postgres.py b/lib/sqlalchemy/databases/postgres.py index 74c2b53666..1d0349a07b 100644 --- a/lib/sqlalchemy/databases/postgres.py +++ b/lib/sqlalchemy/databases/postgres.py @@ -23,6 +23,10 @@ import sqlalchemy.schema as schema 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): @@ -67,6 +71,7 @@ class PGSQLEngine(ansisql.ANSISQLEngine): 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 {} diff --git a/lib/sqlalchemy/mapper.py b/lib/sqlalchemy/mapper.py index 787cfd27e6..5f65f02942 100644 --- a/lib/sqlalchemy/mapper.py +++ b/lib/sqlalchemy/mapper.py @@ -518,6 +518,7 @@ class Mapper(object): if objectstore.uow().has_key(identitykey): instance = objectstore.uow()._get(identitykey) + isnew = False if populate_existing: isnew = not imap.has_key(identitykey) if isnew: @@ -525,7 +526,7 @@ class Mapper(object): 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) @@ -561,7 +562,7 @@ class Mapper(object): 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) @@ -1128,7 +1129,7 @@ class BinaryVisitor(sql.ClauseVisitor): 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 diff --git a/lib/sqlalchemy/objectstore.py b/lib/sqlalchemy/objectstore.py index 8f7509f5e5..4fe35272b8 100644 --- a/lib/sqlalchemy/objectstore.py +++ b/lib/sqlalchemy/objectstore.py @@ -431,20 +431,18 @@ class UOWTask(object): """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 @@ -553,6 +551,7 @@ class UOWTask(object): 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: diff --git a/lib/sqlalchemy/util.py b/lib/sqlalchemy/util.py index 1425d231bb..da5e7c7e82 100644 --- a/lib/sqlalchemy/util.py +++ b/lib/sqlalchemy/util.py @@ -16,7 +16,7 @@ # 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. @@ -399,7 +399,6 @@ class DependencySorter(object): 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 @@ -417,5 +416,6 @@ class DependencySorter(object): n = DependencySorter.Node(item) head.children.append(n) n.parent = head + #print str(head) return head \ No newline at end of file -- 2.47.2