Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typed namedtuple Description #981

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions aiomysql/_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import NamedTuple, Optional


class Description(NamedTuple):
#: the name of the column returned.
name: str
#: the type of the column.
type_code: int
#: the actual length of the column in bytes.
display_size: Optional[int]
#: the size in bytes of the column associated to this column on the server.
internal_size: int
#: total number of significant digits in columns of type NUMERIC.
#: None for other types.
precision: Optional[int]
#: count of decimal digits in the fractional part in columns of type NUMERIC.
#: None for other types.
scale: Optional[int]
#: always None as not easy to retrieve from the libpq.
null_ok: Optional[bool]
3 changes: 2 additions & 1 deletion aiomysql/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from pymysql.connections import OKPacketWrapper
from pymysql.connections import LoadLocalPacketWrapper

from ._type import Description
# from aiomysql.utils import _convert_to_str
from .cursors import Cursor
from .utils import _pack_int24, _lenenc_int, _ConnectionContextManager, _ContextManager
Expand Down Expand Up @@ -1317,7 +1318,7 @@ async def _get_descriptions(self):
field = await self.connection._read_packet(
FieldDescriptorPacket)
self.fields.append(field)
description.append(field.description())
description.append(Description(*field.description()))
field_type = field.type_code
if use_unicode:
if field_type == FIELD_TYPE.JSON:
Expand Down
18 changes: 3 additions & 15 deletions aiomysql/cursors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
Warning, Error, InterfaceError, DataError,
DatabaseError, OperationalError, IntegrityError, InternalError,
NotSupportedError, ProgrammingError)
import typing as t

from ._type import Description
from .log import logger
from .connection import FIELD_TYPE

Expand Down Expand Up @@ -56,23 +58,9 @@ def connection(self):
return self._connection

@property
def description(self):
def description(self) -> t.Optional[t.Sequence[Description]]:
"""This read-only attribute is a sequence of 7-item sequences.

Each of these sequences is a collections.namedtuple containing
information describing one result column:

0. name: the name of the column returned.
1. type_code: the type of the column.
2. display_size: the actual length of the column in bytes.
3. internal_size: the size in bytes of the column associated to
this column on the server.
4. precision: total number of significant digits in columns of
type NUMERIC. None for other types.
5. scale: count of decimal digits in the fractional part in
columns of type NUMERIC. None for other types.
6. null_ok: always None as not easy to retrieve from the libpq.

This attribute will be None for operations that do not
return rows or if the cursor has not had an operation invoked
via the execute() method yet.
Expand Down
Loading