Skip to content

Commit

Permalink
refactor: Cursor.description property (#606)
Browse files Browse the repository at this point in the history
Co-authored-by: larkee <31196561+larkee@users.noreply.github.com>
  • Loading branch information
Ilya Gurov and larkee committed Nov 1, 2021
1 parent 4751b85 commit 5e0c364
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
2 changes: 1 addition & 1 deletion google/cloud/spanner_dbapi/_helpers.py
Expand Up @@ -46,7 +46,7 @@
# does not send back the actual size, we have to lookup the respective size.
# Some fields' sizes are dependent upon the dynamic data hence aren't sent back
# by Cloud Spanner.
code_to_display_size = {
CODE_TO_DISPLAY_SIZE = {
param_types.BOOL.code: 1,
param_types.DATE.code: 4,
param_types.FLOAT64.code: 8,
Expand Down
35 changes: 16 additions & 19 deletions google/cloud/spanner_dbapi/cursor.py
Expand Up @@ -35,7 +35,7 @@

from google.cloud.spanner_dbapi import _helpers
from google.cloud.spanner_dbapi._helpers import ColumnInfo
from google.cloud.spanner_dbapi._helpers import code_to_display_size
from google.cloud.spanner_dbapi._helpers import CODE_TO_DISPLAY_SIZE

from google.cloud.spanner_dbapi import parse_utils
from google.cloud.spanner_dbapi.parse_utils import get_param_types
Expand Down Expand Up @@ -80,7 +80,9 @@ def is_closed(self):

@property
def description(self):
"""Read-only attribute containing a sequence of the following items:
"""
Read-only attribute containing the result columns description
of a form:
- ``name``
- ``type_code``
Expand All @@ -91,28 +93,23 @@ def description(self):
- ``null_ok``
:rtype: tuple
:returns: A tuple of columns' information.
:returns: The result columns' description.
"""
if not self._result_set:
return None

if not getattr(self._result_set, "metadata", None):
return None
return

row_type = self._result_set.metadata.row_type
columns = []

for field in row_type.fields:
column_info = ColumnInfo(
name=field.name,
type_code=field.type_.code,
# Size of the SQL type of the column.
display_size=code_to_display_size.get(field.type_.code),
# Client perceived size of the column.
internal_size=field._pb.ByteSize(),
for field in self._result_set.metadata.row_type.fields:
columns.append(
ColumnInfo(
name=field.name,
type_code=field.type_.code,
# Size of the SQL type of the column.
display_size=CODE_TO_DISPLAY_SIZE.get(field.type_.code),
# Client perceived size of the column.
internal_size=field._pb.ByteSize(),
)
)
columns.append(column_info)

return tuple(columns)

@property
Expand Down

0 comments on commit 5e0c364

Please sign in to comment.