mirror / sqlalchemy (http://svn.sqlalchemy.org/sqlalchemy/trunk/)
Mirror of the SQLAlchemy trunk.
Clone this repository (size: 20.0 MB): HTTPS / SSH
$ hg clone http://bitbucket.org/mirror/sqlalchemy/
| commit 4790: | d120bddca72d |
| parent 4789: | 307f938315af |
| branch: | default |
- the "connection" argument from engine.transaction() and
engine.run_callable() is removed - Connection itself
now has those methods. All four methods accept
*args and **kwargs which are passed to the given callable,
as well as the operating connection.
Changed (Δ1.3 KB):
raw changeset »
CHANGES (6 lines added, 1 lines removed)
lib/sqlalchemy/engine/base.py (40 lines added, 31 lines removed)
lib/sqlalchemy/schema.py (1 lines added, 3 lines removed)
test/engine/test_transaction.py (20 lines added, 2 lines removed)
| … | … | @@ -425,7 +425,12 @@ 0.6.0b1 |
425 |
425 |
result.inserted_primary_key |
426 |
426 |
* dialect.get_default_schema_name(connection) is now |
427 |
427 |
public via dialect.default_schema_name. |
428 |
||
428 |
* the "connection" argument from engine.transaction() and |
|
429 |
engine.run_callable() is removed - Connection itself |
|
430 |
now has those methods. All four methods accept |
|
431 |
*args and **kwargs which are passed to the given callable, |
|
432 |
as well as the operating connection. |
|
433 |
||
429 |
434 |
- schema |
430 |
435 |
- the `__contains__()` method of `MetaData` now accepts |
431 |
436 |
strings or `Table` objects as arguments. If given |
Up to file-list lib/sqlalchemy/engine/base.py:
| … | … | @@ -1220,8 +1220,28 @@ class Connection(Connectable): |
1220 |
1220 |
def default_schema_name(self): |
1221 |
1221 |
return self.engine.dialect.get_default_schema_name(self) |
1222 |
1222 |
|
1223 |
def run_callable(self, callable_): |
|
1224 |
return callable_(self) |
|
1223 |
def transaction(self, callable_, *args, **kwargs): |
|
1224 |
"""Execute the given function within a transaction boundary. |
|
1225 |
||
1226 |
This is a shortcut for explicitly calling `begin()` and `commit()` |
|
1227 |
and optionally `rollback()` when exceptions are raised. The |
|
1228 |
given `*args` and `**kwargs` will be passed to the function. |
|
1229 |
||
1230 |
See also transaction() on engine. |
|
1231 |
||
1232 |
""" |
|
1233 |
||
1234 |
trans = self.begin() |
|
1235 |
try: |
|
1236 |
ret = self.run_callable(callable_, *args, **kwargs) |
|
1237 |
trans.commit() |
|
1238 |
return ret |
|
1239 |
except: |
|
1240 |
trans.rollback() |
|
1241 |
raise |
|
1242 |
||
1243 |
def run_callable(self, callable_, *args, **kwargs): |
|
1244 |
return callable_(self, *args, **kwargs) |
|
1225 |
1245 |
|
1226 |
1246 |
|
1227 |
1247 |
class Transaction(object): |
| … | … | @@ -1406,42 +1426,31 @@ class Engine(Connectable): |
1406 |
1426 |
if connection is None: |
1407 |
1427 |
conn.close() |
1408 |
1428 |
|
1409 |
def transaction(self, callable_, |
|
1429 |
def transaction(self, callable_, *args, **kwargs): |
|
1410 |
1430 |
"""Execute the given function within a transaction boundary. |
1411 |
1431 |
|
1412 |
1432 |
This is a shortcut for explicitly calling `begin()` and `commit()` |
1413 |
1433 |
and optionally `rollback()` when exceptions are raised. The |
1414 |
given `*args` and `**kwargs` will be passed to the function, as |
|
1415 |
well as the Connection used in the transaction. |
|
1434 |
given `*args` and `**kwargs` will be passed to the function. |
|
1435 |
||
1436 |
The connection used is that of contextual_connect(). |
|
1437 |
||
1438 |
See also the similar method on Connection itself. |
|
1439 |
||
1416 |
1440 |
""" |
1441 |
||
1442 |
conn = self.contextual_connect() |
|
1443 |
try: |
|
1444 |
return conn.transaction(callable_, *args, **kwargs) |
|
1445 |
finally: |
|
1446 |
conn.close() |
|
1417 |
1447 |
|
1418 |
if connection is None: |
|
1419 |
conn = self.contextual_connect() |
|
1420 |
else: |
|
1421 |
conn = connection |
|
1448 |
def run_callable(self, callable_, *args, **kwargs): |
|
1449 |
conn = self.contextual_connect() |
|
1422 |
1450 |
try: |
1423 |
trans = conn.begin() |
|
1424 |
try: |
|
1425 |
ret = callable_(conn, *args, **kwargs) |
|
1426 |
trans.commit() |
|
1427 |
return ret |
|
1428 |
except: |
|
1429 |
trans.rollback() |
|
1430 |
raise |
|
1451 |
return conn.run_callable(callable_, *args, **kwargs) |
|
1431 |
1452 |
finally: |
1432 |
if connection is None: |
|
1433 |
conn.close() |
|
1434 |
||
1435 |
def run_callable(self, callable_, connection=None, *args, **kwargs): |
|
1436 |
if connection is None: |
|
1437 |
conn = self.contextual_connect() |
|
1438 |
else: |
|
1439 |
conn = connection |
|
1440 |
try: |
|
1441 |
return callable_(conn, *args, **kwargs) |
|
1442 |
finally: |
|
1443 |
if connection is None: |
|
1444 |
|
|
1453 |
conn.close() |
|
1445 |
1454 |
|
1446 |
1455 |
def execute(self, statement, *multiparams, **params): |
1447 |
1456 |
connection = self.contextual_connect(close_with_result=True) |
| … | … | @@ -1506,7 +1515,7 @@ class Engine(Connectable): |
1506 |
1515 |
conn.close() |
1507 |
1516 |
|
1508 |
1517 |
def has_table(self, table_name, schema=None): |
1509 |
return self.run_callable( |
|
1518 |
return self.run_callable(self.dialect.has_table, table_name, schema) |
|
1510 |
1519 |
|
1511 |
1520 |
def raw_connection(self): |
1512 |
1521 |
"""Return a DB-API connection.""" |
Up to file-list lib/sqlalchemy/schema.py:
| … | … | @@ -397,9 +397,7 @@ class Table(SchemaItem, expression.Table |
397 |
397 |
if bind is None: |
398 |
398 |
bind = _bind_or_error(self) |
399 |
399 |
|
400 |
def do(conn): |
|
401 |
return conn.dialect.has_table(conn, self.name, schema=self.schema) |
|
402 |
return bind.run_callable( |
|
400 |
return bind.run_callable(bind.dialect.has_table, self.name, schema=self.schema) |
|
403 |
401 |
|
404 |
402 |
def create(self, bind=None, checkfirst=False): |
405 |
403 |
"""Issue a ``CREATE`` statement for this table. |
Up to file-list test/engine/test_transaction.py:
1 |
1 |
from sqlalchemy.test.testing import eq_, assert_raises, assert_raises_message |
2 |
2 |
import sys, time, threading |
3 |
from sqlalchemy import create_engine, MetaData, INT, VARCHAR, Sequence, |
|
3 |
from sqlalchemy import create_engine, MetaData, INT, VARCHAR, Sequence, \ |
|
4 |
select, Integer, String, func, text, exc |
|
4 |
5 |
from sqlalchemy.test.schema import Table |
5 |
6 |
from sqlalchemy.test.schema import Column |
6 |
7 |
from sqlalchemy.test import TestBase, testing |
| … | … | @@ -73,7 +74,24 @@ class TransactionTest(TestBase): |
73 |
74 |
result = connection.execute("select * from query_users") |
74 |
75 |
assert len(result.fetchall()) == 0 |
75 |
76 |
connection.close() |
76 |
||
77 |
||
78 |
def test_transaction_container(self): |
|
79 |
||
80 |
def go(conn, table, data): |
|
81 |
for d in data: |
|
82 |
conn.execute(table.insert(), d) |
|
83 |
||
84 |
testing.db.transaction(go, users, [dict(user_id=1, user_name='user1')]) |
|
85 |
eq_(testing.db.execute(users.select()).fetchall(), [(1, 'user1')]) |
|
86 |
||
87 |
assert_raises(exc.DBAPIError, |
|
88 |
testing.db.transaction, go, users, [ |
|
89 |
{'user_id':2, 'user_name':'user2'}, |
|
90 |
{'user_id':1, 'user_name':'user3'}, |
|
91 |
] |
|
92 |
) |
|
93 |
eq_(testing.db.execute(users.select()).fetchall(), [(1, 'user1')]) |
|
94 |
||
77 |
95 |
def test_nested_rollback(self): |
78 |
96 |
connection = testing.db.connect() |
79 |
97 |
