Skip to content
Permalink
Browse files
feat(dbapi): remove string conversion for numeric fields (#317)
* feat: updated googleapis proto changes for request tags

* feat: added support for numberic for python decimal value

* feat: added support for converting decimal field to numeric field and removed it's conversion to string
  • Loading branch information
vi3k6i5 committed Apr 26, 2021
1 parent 070a171 commit 772aa3c2ffbdf3f863c09db176697b3ad70adbcf
Showing with 2 additions and 27 deletions.
  1. +1 −15 google/cloud/spanner_dbapi/parse_utils.py
  2. +1 −12 tests/unit/spanner_dbapi/test_parse_utils.py
@@ -509,25 +509,11 @@ def sql_pyformat_args_to_spanner(sql, params):
resolved_value = pyfmt % params
named_args[key] = resolved_value
else:
named_args[key] = cast_for_spanner(params[i])
named_args[key] = params[i]

return sanitize_literals_for_upload(sql), named_args


def cast_for_spanner(value):
"""Convert the param to its Cloud Spanner equivalent type.
:type value: Any
:param value: The value to convert to a Cloud Spanner type.
:rtype: Any
:returns: The value converted to a Cloud Spanner type.
"""
if isinstance(value, decimal.Decimal):
return str(value)
return value


def get_param_types(params):
"""Determine Cloud Spanner types for the given parameters.
@@ -307,7 +307,7 @@ def test_sql_pyformat_args_to_spanner(self):
),
(
"SELECT (an.p + @a0) AS np FROM an WHERE (an.p + @a1) = @a2",
{"a0": 1, "a1": 1.0, "a2": str(31)},
{"a0": 1, "a1": 1.0, "a2": decimal.Decimal("31")},
),
),
]
@@ -339,17 +339,6 @@ def test_sql_pyformat_args_to_spanner_invalid(self):
lambda: sql_pyformat_args_to_spanner(sql, params),
)

def test_cast_for_spanner(self):
import decimal

from google.cloud.spanner_dbapi.parse_utils import cast_for_spanner

dec = 3
value = decimal.Decimal(dec)
self.assertEqual(cast_for_spanner(value), str(dec))
self.assertEqual(cast_for_spanner(5), 5)
self.assertEqual(cast_for_spanner("string"), "string")

@unittest.skipIf(skip_condition, skip_message)
def test_get_param_types(self):
import datetime

0 comments on commit 772aa3c

Please sign in to comment.