Skip to content

asyncmy

Native execution adapter for asyncmy (mysql+asyncmy:// URLs).

AsyncmyAdapter

Bases: DriverAdapter

Runs bound statements on asyncmy cursors, which take %s-style SQL and a tuple of arguments::

Text Only
1
await cursor.execute("SELECT users.id FROM users WHERE users.id = %s", (5,))

Arguments are always passed as a tuple, even when empty. asyncmy only applies % formatting when arguments are given, and SQLAlchemy compiles a literal % as %%, so LIKE 'a%%' would otherwise reach MySQL with the %% intact.

Source code in ormar/databases/native/adapters/asyncmy.py
Python
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
class AsyncmyAdapter(DriverAdapter):
    """
    Runs bound statements on asyncmy cursors, which take ``%s``-style SQL and
    a tuple of arguments::

        await cursor.execute("SELECT users.id FROM users WHERE users.id = %s", (5,))

    Arguments are always passed as a tuple, even when empty. asyncmy only
    applies ``%`` formatting when arguments are given, and SQLAlchemy compiles
    a literal ``%`` as ``%%``, so ``LIKE 'a%%'`` would otherwise reach MySQL
    with the ``%%`` intact.
    """

    def __init__(self, dialect: Dialect) -> None:
        """
        :param dialect: dialect of the connected database
        :type dialect: Dialect
        """
        super().__init__(dialect=dialect)
        dbapi = dialect.dbapi
        # asyncmy raises AttributeError when its transport is gone mid-call;
        # SQLAlchemy's adapter turns it into a disconnect InternalError.
        self.driver_errors = (dbapi.Error, AttributeError)  # type: ignore[union-attr]
        self.unbuffered_cursor = dbapi.asyncmy.cursors.SSCursor  # type: ignore

    async def fetch_all(
        self, raw: Any, bound: BoundStatement, as_tuples: bool = False
    ) -> List[Any]:
        """
        Run a statement on a cursor and return all rows keyed by result label;
        the result column types come from ``cursor.description``.

        :param raw: pooled raw connection
        :type raw: Any
        :param bound: statement to execute
        :type bound: BoundStatement
        :param as_tuples: return tuples of values in select order instead
        :type as_tuples: bool
        :return: rows
        :rtype: List[Any]
        """
        async with execute_mutex(raw=raw), raw.driver_connection.cursor() as cursor:
            await cursor.execute(query=bound.sql, args=tuple(bound.args))
            self.resolve_processors(
                bound=bound, coltypes=[desc[1] for desc in cursor.description]
            )
            rows = await cursor.fetchall()
        convert = row_to_tuple if as_tuples else row_to_dict
        return [convert(plan=bound.plan, values=row) for row in rows]

    async def execute(self, raw: Any, bound: BoundStatement) -> Any:
        """
        Run a write statement. MySQL has no ``RETURNING``, so a generated primary
        key comes from ``cursor.lastrowid`` and the row count from
        ``cursor.rowcount``.

        :param raw: pooled raw connection
        :type raw: Any
        :param bound: statement to execute
        :type bound: BoundStatement
        :return: inserted primary key for INSERT, row count otherwise
        :rtype: Any
        """
        async with execute_mutex(raw=raw), raw.driver_connection.cursor() as cursor:
            await cursor.execute(query=bound.sql, args=tuple(bound.args))
            if bound.plan.is_insert:
                return inserted_primary_key(
                    bound=bound, returned=None, lastrowid=cursor.lastrowid
                )
            return cursor.rowcount

    async def execute_many(self, raw: Any, batch: BoundBatch) -> None:
        """
        Run a statement once per row with the cursor's ``executemany``.

        :param raw: pooled raw connection
        :type raw: Any
        :param batch: statement and arguments per row
        :type batch: BoundBatch
        """
        async with execute_mutex(raw=raw), raw.driver_connection.cursor() as cursor:
            await cursor.executemany(
                query=batch.sql, args=[tuple(a) for a in batch.args_list]
            )

    async def stream(
        self, raw: Any, bound: BoundStatement, batch_size: int
    ) -> AsyncGenerator[Dict[str, Any], None]:
        """
        Stream rows through an unbuffered cursor (``SSCursor``), which reads
        rows from the network as they are fetched instead of loading the whole
        result first. The cursor is always closed at the end, because MySQL
        cannot run another statement on the connection while it is open.

        The execution lock is held per fetch only (see the asyncpg adapter).

        :param raw: pooled raw connection
        :type raw: Any
        :param bound: statement to execute
        :type bound: BoundStatement
        :param batch_size: rows fetched per round trip
        :type batch_size: int
        :return: rows keyed by result label
        :rtype: AsyncGenerator[Dict[str, Any], None]
        """
        cursor = raw.driver_connection.cursor(cursor=self.unbuffered_cursor)
        try:
            async with execute_mutex(raw=raw):
                await cursor.execute(query=bound.sql, args=tuple(bound.args))
                self.resolve_processors(
                    bound=bound, coltypes=[desc[1] for desc in cursor.description]
                )
            while True:
                async with execute_mutex(raw=raw):
                    rows = await cursor.fetchmany(size=batch_size)
                if not rows:
                    return
                for row in rows:
                    yield row_to_dict(plan=bound.plan, values=row)
        finally:
            async with execute_mutex(raw=raw):
                await cursor.close()

    def translate_error(
        self, error: BaseException, dbapi_connection: Any
    ) -> BaseException:
        """
        Map an asyncmy exception the way SQLAlchemy's asyncmy wrapper does.
        asyncmy raises ``AttributeError`` when its connection is gone in the
        middle of a call; the wrapper turns it into ``InternalError`` so the
        dialect recognises a disconnect and drops the connection.

        :param error: exception raised by asyncmy
        :type error: BaseException
        :param dbapi_connection: SQLAlchemy's adapted asyncmy connection
        :type dbapi_connection: Any
        :return: DBAPI-level exception
        :rtype: BaseException
        """
        try:
            dbapi_connection._handle_exception(error=error)
        except Exception as translated:
            return translated
        return error  # pragma: no cover

__init__(dialect)

Parameters:

Name Type Description Default
dialect Dialect

dialect of the connected database

required
Source code in ormar/databases/native/adapters/asyncmy.py
Python
35
36
37
38
39
40
41
42
43
44
45
def __init__(self, dialect: Dialect) -> None:
    """
    :param dialect: dialect of the connected database
    :type dialect: Dialect
    """
    super().__init__(dialect=dialect)
    dbapi = dialect.dbapi
    # asyncmy raises AttributeError when its transport is gone mid-call;
    # SQLAlchemy's adapter turns it into a disconnect InternalError.
    self.driver_errors = (dbapi.Error, AttributeError)  # type: ignore[union-attr]
    self.unbuffered_cursor = dbapi.asyncmy.cursors.SSCursor  # type: ignore

execute(raw, bound) async

Run a write statement. MySQL has no RETURNING, so a generated primary key comes from cursor.lastrowid and the row count from cursor.rowcount.

Parameters:

Name Type Description Default
raw Any

pooled raw connection

required
bound BoundStatement

statement to execute

required

Returns:

Type Description
Any

inserted primary key for INSERT, row count otherwise

Source code in ormar/databases/native/adapters/asyncmy.py
Python
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
async def execute(self, raw: Any, bound: BoundStatement) -> Any:
    """
    Run a write statement. MySQL has no ``RETURNING``, so a generated primary
    key comes from ``cursor.lastrowid`` and the row count from
    ``cursor.rowcount``.

    :param raw: pooled raw connection
    :type raw: Any
    :param bound: statement to execute
    :type bound: BoundStatement
    :return: inserted primary key for INSERT, row count otherwise
    :rtype: Any
    """
    async with execute_mutex(raw=raw), raw.driver_connection.cursor() as cursor:
        await cursor.execute(query=bound.sql, args=tuple(bound.args))
        if bound.plan.is_insert:
            return inserted_primary_key(
                bound=bound, returned=None, lastrowid=cursor.lastrowid
            )
        return cursor.rowcount

execute_many(raw, batch) async

Run a statement once per row with the cursor's executemany.

Parameters:

Name Type Description Default
raw Any

pooled raw connection

required
batch BoundBatch

statement and arguments per row

required
Source code in ormar/databases/native/adapters/asyncmy.py
Python
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
async def execute_many(self, raw: Any, batch: BoundBatch) -> None:
    """
    Run a statement once per row with the cursor's ``executemany``.

    :param raw: pooled raw connection
    :type raw: Any
    :param batch: statement and arguments per row
    :type batch: BoundBatch
    """
    async with execute_mutex(raw=raw), raw.driver_connection.cursor() as cursor:
        await cursor.executemany(
            query=batch.sql, args=[tuple(a) for a in batch.args_list]
        )

fetch_all(raw, bound, as_tuples=False) async

Run a statement on a cursor and return all rows keyed by result label; the result column types come from cursor.description.

Parameters:

Name Type Description Default
raw Any

pooled raw connection

required
bound BoundStatement

statement to execute

required
as_tuples bool

return tuples of values in select order instead

False

Returns:

Type Description
List[Any]

rows

Source code in ormar/databases/native/adapters/asyncmy.py
Python
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
async def fetch_all(
    self, raw: Any, bound: BoundStatement, as_tuples: bool = False
) -> List[Any]:
    """
    Run a statement on a cursor and return all rows keyed by result label;
    the result column types come from ``cursor.description``.

    :param raw: pooled raw connection
    :type raw: Any
    :param bound: statement to execute
    :type bound: BoundStatement
    :param as_tuples: return tuples of values in select order instead
    :type as_tuples: bool
    :return: rows
    :rtype: List[Any]
    """
    async with execute_mutex(raw=raw), raw.driver_connection.cursor() as cursor:
        await cursor.execute(query=bound.sql, args=tuple(bound.args))
        self.resolve_processors(
            bound=bound, coltypes=[desc[1] for desc in cursor.description]
        )
        rows = await cursor.fetchall()
    convert = row_to_tuple if as_tuples else row_to_dict
    return [convert(plan=bound.plan, values=row) for row in rows]

stream(raw, bound, batch_size) async

Stream rows through an unbuffered cursor (SSCursor), which reads rows from the network as they are fetched instead of loading the whole result first. The cursor is always closed at the end, because MySQL cannot run another statement on the connection while it is open.

The execution lock is held per fetch only (see the asyncpg adapter).

Parameters:

Name Type Description Default
raw Any

pooled raw connection

required
bound BoundStatement

statement to execute

required
batch_size int

rows fetched per round trip

required

Returns:

Type Description
AsyncGenerator[Dict[str, Any], None]

rows keyed by result label

Source code in ormar/databases/native/adapters/asyncmy.py
Python
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
async def stream(
    self, raw: Any, bound: BoundStatement, batch_size: int
) -> AsyncGenerator[Dict[str, Any], None]:
    """
    Stream rows through an unbuffered cursor (``SSCursor``), which reads
    rows from the network as they are fetched instead of loading the whole
    result first. The cursor is always closed at the end, because MySQL
    cannot run another statement on the connection while it is open.

    The execution lock is held per fetch only (see the asyncpg adapter).

    :param raw: pooled raw connection
    :type raw: Any
    :param bound: statement to execute
    :type bound: BoundStatement
    :param batch_size: rows fetched per round trip
    :type batch_size: int
    :return: rows keyed by result label
    :rtype: AsyncGenerator[Dict[str, Any], None]
    """
    cursor = raw.driver_connection.cursor(cursor=self.unbuffered_cursor)
    try:
        async with execute_mutex(raw=raw):
            await cursor.execute(query=bound.sql, args=tuple(bound.args))
            self.resolve_processors(
                bound=bound, coltypes=[desc[1] for desc in cursor.description]
            )
        while True:
            async with execute_mutex(raw=raw):
                rows = await cursor.fetchmany(size=batch_size)
            if not rows:
                return
            for row in rows:
                yield row_to_dict(plan=bound.plan, values=row)
    finally:
        async with execute_mutex(raw=raw):
            await cursor.close()

translate_error(error, dbapi_connection)

Map an asyncmy exception the way SQLAlchemy's asyncmy wrapper does. asyncmy raises AttributeError when its connection is gone in the middle of a call; the wrapper turns it into InternalError so the dialect recognises a disconnect and drops the connection.

Parameters:

Name Type Description Default
error BaseException

exception raised by asyncmy

required
dbapi_connection Any

SQLAlchemy's adapted asyncmy connection

required

Returns:

Type Description
BaseException

DBAPI-level exception

Source code in ormar/databases/native/adapters/asyncmy.py
Python
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def translate_error(
    self, error: BaseException, dbapi_connection: Any
) -> BaseException:
    """
    Map an asyncmy exception the way SQLAlchemy's asyncmy wrapper does.
    asyncmy raises ``AttributeError`` when its connection is gone in the
    middle of a call; the wrapper turns it into ``InternalError`` so the
    dialect recognises a disconnect and drops the connection.

    :param error: exception raised by asyncmy
    :type error: BaseException
    :param dbapi_connection: SQLAlchemy's adapted asyncmy connection
    :type dbapi_connection: Any
    :return: DBAPI-level exception
    :rtype: BaseException
    """
    try:
        dbapi_connection._handle_exception(error=error)
    except Exception as translated:
        return translated
    return error  # pragma: no cover