Skip to content

Commit be0c9a3

Browse files
authored
Add default tuple adapter (#372)
1 parent 9bff1dd commit be0c9a3

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

vertica_python/tests/unit_tests/test_sql_literal.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from __future__ import print_function, division, absolute_import
1616

17+
from collections import namedtuple
1718
from decimal import Decimal
1819
from uuid import UUID
1920
import datetime
@@ -48,6 +49,14 @@ def test_default_adapters(self):
4849
# String
4950
self.assertEqual(cursor.object_to_sql_literal(u"string'1"), "'string''1'")
5051
self.assertEqual(cursor.object_to_sql_literal(b"string'1"), "'string''1'")
52+
# Tuple and namedtuple
53+
self.assertEqual(cursor.object_to_sql_literal(
54+
(123, u"string'1", None)), "(123,'string''1',NULL)")
55+
self.assertEqual(cursor.object_to_sql_literal(
56+
((1, u"a"), (2, u"b"), (3, u"c"))), "((1,'a'),(2,'b'),(3,'c'))")
57+
Point = namedtuple('Point', ['x', 'y', 'z'])
58+
p = Point(x=11, y=22, z=33)
59+
self.assertEqual(cursor.object_to_sql_literal(p), "(11,22,33)")
5160

5261
def test_register_adapters(self):
5362
class Point(object):

vertica_python/vertica/cursor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,11 @@ def object_to_string(self, py_obj, is_copy_data):
519519
return self.format_quote(as_text(py_obj), is_copy_data)
520520
elif isinstance(py_obj, (integer_types, float, Decimal)):
521521
return str(py_obj)
522+
elif isinstance(py_obj, tuple): # tuple and namedtuple
523+
elements = [None] * len(py_obj)
524+
for i in range(len(py_obj)):
525+
elements[i] = self.object_to_string(py_obj[i], is_copy_data)
526+
return "(" + ",".join(elements) + ")"
522527
elif isinstance(py_obj, (datetime.datetime, datetime.date, datetime.time, UUID)):
523528
return self.format_quote(as_text(str(py_obj)), is_copy_data)
524529
else:

0 commit comments

Comments
 (0)