This commit is contained in:
Mike Bayer
2005-09-03 20:53:28 +00:00
parent 99cb7b0937
commit e97d30dbfe
4 changed files with 73 additions and 61 deletions
+6 -3
View 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
+9 -17
View 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
+13 -5
View 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."""
+45 -36
View 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()