From e97d30dbfe58202ba16688cfc00a36e440021254 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 3 Sep 2005 20:53:28 +0000 Subject: [PATCH] --- lib/sqlalchemy/engine.py | 9 +++-- lib/sqlalchemy/mapper.py | 26 +++++-------- lib/sqlalchemy/sql.py | 18 ++++++--- test/tables.py | 81 ++++++++++++++++++++++------------------ 4 files changed, 73 insertions(+), 61 deletions(-) diff --git a/lib/sqlalchemy/engine.py b/lib/sqlalchemy/engine.py index afbfb6fc62..6d8d17fb53 100644 --- a/lib/sqlalchemy/engine.py +++ b/lib/sqlalchemy/engine.py @@ -151,10 +151,10 @@ class SQLEngine(schema.SchemaEngine): self.context.transaction = None self.context.tcount = None - def pre_exec(self, connection, cursor, statement, parameters, echo = None, **kwargs): + def pre_exec(self, connection, cursor, statement, parameters, many = False, echo = None, **kwargs): pass - def post_exec(self, connection, cursor, statement, parameters, echo = None, **kwargs): + def post_exec(self, connection, cursor, statement, parameters, many = False, echo = None, **kwargs): pass def execute(self, statement, parameters, connection = None, echo = None, **kwargs): @@ -172,7 +172,10 @@ class SQLEngine(schema.SchemaEngine): c = connection.cursor() self.pre_exec(connection, c, statement, parameters, echo = echo, **kwargs) - c.execute(statement, parameters) + if isinstance(parameters, list): + c.executemany(statement, parameters) + else: + c.execute(statement, parameters) self.post_exec(connection, c, statement, parameters, echo = echo, **kwargs) return c diff --git a/lib/sqlalchemy/mapper.py b/lib/sqlalchemy/mapper.py index d117553f30..46dfde3e84 100644 --- a/lib/sqlalchemy/mapper.py +++ b/lib/sqlalchemy/mapper.py @@ -639,25 +639,17 @@ class ForeignKeySetter(sql.ClauseVisitor): def visit_binary(self, binary): if binary.operator == '=': - # TODO: this code is silly - if binary.left.table == self.primarytable and binary.right.table == self.secondarytable: + # play a little rock/paper/scissors here + colmap = {binary.left.table : binary.left, binary.right.table : binary.right} + if colmap.has_key(self.primarytable) and colmap.has_key(self.secondarytable): if self.clearkeys: - self.childmapper._setattrbycolumn(self.child, binary.right, None) + self.childmapper._setattrbycolumn(self.child, colmap[self.secondarytable], None) else: - self.childmapper._setattrbycolumn(self.child, binary.right, self.parentmapper._getattrbycolumn(self.obj, binary.left)) - elif binary.right.table == self.primarytable and binary.left.table == self.secondarytable: - if self.clearkeys: - self.childmapper._setattrbycolumn(self.child, binary.left, None) - else: - self.childmapper._setattrbycolumn(self.child, binary.left, self.parentmapper._getattrbycolumn(self.obj, binary.right)) - elif binary.right.table == self.associationtable and binary.left.table == self.primarytable: - self.associationrow[binary.right.key] = self.parentmapper._getattrbycolumn(self.obj, binary.left) - elif binary.left.table == self.associationtable and binary.right.table == self.primarytable: - self.associationrow[binary.left.key] = self.parentmapper._getattrbycolumn(self.obj, binary.right) - elif binary.right.table == self.associationtable and binary.left.table == self.secondarytable: - self.associationrow[binary.right.key] = self.childmapper._getattrbycolumn(self.child, binary.left) - elif binary.left.table == self.associationtable and binary.right.table == self.secondarytable: - self.associationrow[binary.left.key] = self.childmapper._getattrbycolumn(self.child, binary.right) + self.childmapper._setattrbycolumn(self.child, colmap[self.secondarytable], self.parentmapper._getattrbycolumn(self.obj, colmap[self.primarytable])) + elif colmap.has_key(self.primarytable) and colmap.has_key(self.associationtable): + self.associationrow[colmap[self.associationtable].key] = self.parentmapper._getattrbycolumn(self.obj, colmap[self.primarytable]) + elif colmap.has_key(self.secondarytable) and colmap.has_key(self.associationtable): + self.associationrow[colmap[self.associationtable].key] = self.childmapper._getattrbycolumn(self.child, colmap[self.secondarytable]) class LazyIzer(sql.ClauseVisitor): """converts an expression which refers to a table column into an diff --git a/lib/sqlalchemy/sql.py b/lib/sqlalchemy/sql.py index f2617d2a2b..b80436cde8 100644 --- a/lib/sqlalchemy/sql.py +++ b/lib/sqlalchemy/sql.py @@ -175,9 +175,13 @@ class Compiled(ClauseVisitor): those given in the **params dictionary""" raise NotImplementedError() - def execute(self, **params): + def execute(self, *multiparams, **params): """executes this compiled object using the underlying SQLEngine""" - return self.engine.execute(str(self), self.get_params(**params), echo = getattr(self.statement, 'echo', False), compiled = self) + if len(multiparams): + params = [self.get_params(**m) for m in multiparams] + else: + params = self.get_params(**params) + return self.engine.execute(str(self), params, echo = getattr(self.statement, 'echo', False), compiled = self) class ClauseElement(object): """base class for elements of a programmatically constructed SQL expression. @@ -221,13 +225,17 @@ class ClauseElement(object): table columns should be used in the statement.""" return engine.compile(self, bindparams = bindparams) - def execute(self, **params): + def execute(self, *multiparams, **params): """compiles and executes this SQL expression using its underlying SQLEngine. the given **params are used as bind parameters when compiling and executing the expression. the DBAPI cursor object is returned.""" e = self.engine - c = self.compile(e, bindparams = params) - return c.execute() + if len(multiparams): + bindparams = multiparams[0] + else: + bindparams = params + c = self.compile(e, bindparams = bindparams) + return c.execute(*multiparams, **params) def result(self, **params): """the same as execute(), except a RowProxy object is returned instead of a DBAPI cursor.""" diff --git a/test/tables.py b/test/tables.py index bfa711784c..bddb7b499d 100644 --- a/test/tables.py +++ b/test/tables.py @@ -2,6 +2,7 @@ from sqlalchemy.sql import * from sqlalchemy.schema import * from sqlalchemy.mapper import * +import os DBTYPE = 'sqlite_memory' @@ -51,50 +52,58 @@ itemkeywords = Table('itemkeywords', db, ) users.build() -users.insert().execute(user_id = 7, user_name = 'jack') -users.insert().execute(user_id = 8, user_name = 'ed') -users.insert().execute(user_id = 9, user_name = 'fred') +users.insert().execute( + dict(user_id = 7, user_name = 'jack'), + dict(user_id = 8, user_name = 'ed'), + dict(user_id = 9, user_name = 'fred') +) addresses.build() -addresses.insert().execute(address_id = 1, user_id = 7, email_address = "jack@bean.com") -addresses.insert().execute(address_id = 2, user_id = 8, email_address = "ed@wood.com") -addresses.insert().execute(address_id = 3, user_id = 8, email_address = "ed@lala.com") -db.connection().commit() +addresses.insert().execute( + dict(address_id = 1, user_id = 7, email_address = "jack@bean.com"), + dict(address_id = 2, user_id = 8, email_address = "ed@wood.com"), + dict(address_id = 3, user_id = 8, email_address = "ed@lala.com") +) orders.build() -orders.insert().execute(order_id = 1, user_id = 7, description = 'order 1', isopen=0) -orders.insert().execute(order_id = 2, user_id = 9, description = 'order 2', isopen=0) -orders.insert().execute(order_id = 3, user_id = 7, description = 'order 3', isopen=1) -orders.insert().execute(order_id = 4, user_id = 9, description = 'order 4', isopen=1) -orders.insert().execute(order_id = 5, user_id = 7, description = 'order 5', isopen=0) -db.connection().commit() +orders.insert().execute( + dict(order_id = 1, user_id = 7, description = 'order 1', isopen=0), + dict(order_id = 2, user_id = 9, description = 'order 2', isopen=0), + dict(order_id = 3, user_id = 7, description = 'order 3', isopen=1), + dict(order_id = 4, user_id = 9, description = 'order 4', isopen=1), + dict(order_id = 5, user_id = 7, description = 'order 5', isopen=0) +) orderitems.build() -orderitems.insert().execute(item_id=1, order_id=2, item_name='item 1') -orderitems.insert().execute(item_id=3, order_id=3, item_name='item 3') -orderitems.insert().execute(item_id=2, order_id=2, item_name='item 2') -orderitems.insert().execute(item_id=5, order_id=3, item_name='item 5') -orderitems.insert().execute(item_id=4, order_id=3, item_name='item 4') -db.connection().commit() +orderitems.insert().execute( + dict(item_id=1, order_id=2, item_name='item 1'), + dict(item_id=3, order_id=3, item_name='item 3'), + dict(item_id=2, order_id=2, item_name='item 2'), + dict(item_id=5, order_id=3, item_name='item 5'), + dict(item_id=4, order_id=3, item_name='item 4') +) keywords.build() -keywords.insert().execute(keyword_id=1, name='blue') -keywords.insert().execute(keyword_id=2, name='red') -keywords.insert().execute(keyword_id=3, name='green') -keywords.insert().execute(keyword_id=4, name='big') -keywords.insert().execute(keyword_id=5, name='small') -keywords.insert().execute(keyword_id=6, name='round') -keywords.insert().execute(keyword_id=7, name='square') -db.connection().commit() +keywords.insert().execute( + dict(keyword_id=1, name='blue'), + dict(keyword_id=2, name='red'), + dict(keyword_id=3, name='green'), + dict(keyword_id=4, name='big'), + dict(keyword_id=5, name='small'), + dict(keyword_id=6, name='round'), + dict(keyword_id=7, name='square') +) itemkeywords.build() -itemkeywords.insert().execute(keyword_id=2, item_id=1) -itemkeywords.insert().execute(keyword_id=2, item_id=2) -itemkeywords.insert().execute(keyword_id=4, item_id=1) -itemkeywords.insert().execute(keyword_id=6, item_id=1) -itemkeywords.insert().execute(keyword_id=7, item_id=2) -itemkeywords.insert().execute(keyword_id=6, item_id=3) -itemkeywords.insert().execute(keyword_id=3, item_id=3) -itemkeywords.insert().execute(keyword_id=5, item_id=2) -itemkeywords.insert().execute(keyword_id=4, item_id=3) +itemkeywords.insert().execute( + dict(keyword_id=2, item_id=1), + dict(keyword_id=2, item_id=2), + dict(keyword_id=4, item_id=1), + dict(keyword_id=6, item_id=1), + dict(keyword_id=7, item_id=2), + dict(keyword_id=6, item_id=3), + dict(keyword_id=3, item_id=3), + dict(keyword_id=5, item_id=2), + dict(keyword_id=4, item_id=3) +) db.connection().commit() -- 2.47.2