Skip to content

Commit 66d8657

Browse files
elBroomterrycain
authored andcommitted
Custom SA Cursor (#374)
1 parent d097275 commit 66d8657

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

aiomysql/sa/engine.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from .connection import SAConnection
77
from .exc import InvalidRequestError, ArgumentError
88
from ..utils import _PoolContextManager, _PoolAcquireContextManager
9-
from ..cursors import Cursor
9+
from ..cursors import (
10+
Cursor, DeserializationCursor, DictCursor, SSCursor, SSDictCursor)
1011

1112

1213
try:
@@ -26,16 +27,23 @@ def create_engine(minsize=1, maxsize=10, loop=None,
2627
2728
Returns Engine instance with embedded connection pool.
2829
29-
The pool has *minsize* opened connections to PostgreSQL server.
30+
The pool has *minsize* opened connections to MySQL server.
3031
"""
32+
deprecated_cursor_classes = [
33+
DeserializationCursor, DictCursor, SSCursor, SSDictCursor,
34+
]
35+
36+
cursorclass = kwargs.get('cursorclass', Cursor)
37+
if not issubclass(cursorclass, Cursor) or any(
38+
issubclass(cursorclass, cursor_class)
39+
for cursor_class in deprecated_cursor_classes
40+
):
41+
raise ArgumentError('SQLAlchemy engine does not support '
42+
'this cursor class')
43+
3144
coro = _create_engine(minsize=minsize, maxsize=maxsize, loop=loop,
3245
dialect=dialect, pool_recycle=pool_recycle,
3346
compiled_cache=compiled_cache, **kwargs)
34-
compatible_cursor_classes = [Cursor]
35-
# Without provided kwarg, default is default cursor from Connection class
36-
if kwargs.get('cursorclass', Cursor) not in compatible_cursor_classes:
37-
raise ArgumentError('SQLAlchemy engine does not support '
38-
'this cursor class')
3947
return _EngineContextManager(coro)
4048

4149

tests/test_async_with.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import aiomysql
44
import pytest
55

6-
from aiomysql import sa, create_pool, DictCursor
6+
from aiomysql import sa, create_pool, DictCursor, Cursor
77
from sqlalchemy import MetaData, Table, Column, Integer, String
88

99

@@ -276,3 +276,16 @@ async def test_incompatible_cursor_fails(loop, mysql_params):
276276

277277
msg = 'SQLAlchemy engine does not support this cursor class'
278278
assert str(ctx.value) == msg
279+
280+
281+
@pytest.mark.run_loop
282+
async def test_compatible_cursor_correct(loop, mysql_params):
283+
class SubCursor(Cursor):
284+
pass
285+
286+
mysql_params['cursorclass'] = SubCursor
287+
async with sa.create_engine(loop=loop, **mysql_params) as engine:
288+
async with engine.acquire() as conn:
289+
# check not raise sa.ArgumentError exception
290+
pass
291+
assert conn.closed

0 commit comments

Comments
 (0)