]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
(no commit message)
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 3 Sep 2005 20:53:28 +0000 (20:53 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 3 Sep 2005 20:53:28 +0000 (20:53 +0000)
lib/sqlalchemy/engine.py
lib/sqlalchemy/mapper.py
lib/sqlalchemy/sql.py
test/tables.py

index afbfb6fc6208490aa3dc87b1f213e44120c2998b..6d8d17fb5333bcd81293f250c7a9c9a4cc0494ae 100644 (file)
@@ -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
 
index d117553f307d969bf24af2109d83db6f079edad3..46dfde3e84085b313c00a1e3d5484e19e669ead8 100644 (file)
@@ -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
index f2617d2a2b0bb8e2c9196bc98cd45579c75a3014..b80436cde8bfcc8a9a85ad31b6427aa607f375b5 100644 (file)
@@ -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."""
index bfa711784c0a5a417ba11eb8e6e95824f5ab3006..bddb7b499d8db79cf1dc6200c8b800436b5af05a 100644 (file)
@@ -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()