Skip to content

Commit 8a3e892

Browse files
committed
Add missing adapter for UUIDs
1 parent 817e359 commit 8a3e892

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

docs/contents/changelog.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
ChangeLog
22
=========
33

4-
Version 5.2.1 (2020-09-25)
4+
Version 5.2.2 (to be released)
55
------------------------------
6+
- Added a missing adapter method for UUIDs in the classic `pg` module.
7+
8+
Version 5.2.1 (2020-09-25)
9+
--------------------------
610
- This version officially supports the new Python 3.9 and PostgreSQL 13.
711
- The `copy_to()` and `copy_from()` methods in the pgdb module now also work
812
with table names containing schema qualifiers (#47).

pg.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,14 @@ def _adapt_hstore(self, v):
487487
return str(Hstore(v))
488488
raise TypeError('Hstore parameter %s has wrong type' % v)
489489

490+
def _adapt_uuid(self, v):
491+
"""Adapt a UUID parameter."""
492+
if not v:
493+
return None
494+
if isinstance(v, basestring):
495+
return v
496+
return str(v)
497+
490498
@classmethod
491499
def _adapt_text_array(cls, v):
492500
"""Adapt a text type array parameter."""

tests/test_classic_dbwrapper.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4400,6 +4400,21 @@ def testAdaptQueryTypedWithHstore(self):
44004400
params[0] = ','.join(sorted(params[0].split(',')))
44014401
self.assertEqual(params, ['one=>"it\'s fine\",two=>2'])
44024402

4403+
def testAdaptQueryTypedWithUuid(self):
4404+
format_query = self.adapter.format_query
4405+
value = '12345678-1234-5678-1234-567812345678'
4406+
sql, params = format_query("select %s", (value,), 'uuid')
4407+
self.assertEqual(sql, "select $1")
4408+
self.assertEqual(params, ['12345678-1234-5678-1234-567812345678'])
4409+
value = UUID('{12345678-1234-5678-1234-567812345678}')
4410+
sql, params = format_query("select %s", (value,), 'uuid')
4411+
self.assertEqual(sql, "select $1")
4412+
self.assertEqual(params, ['12345678-1234-5678-1234-567812345678'])
4413+
value = UUID('{12345678-1234-5678-1234-567812345678}')
4414+
sql, params = format_query("select %s", (value,))
4415+
self.assertEqual(sql, "select $1")
4416+
self.assertEqual(params, ['12345678-1234-5678-1234-567812345678'])
4417+
44034418
def testAdaptQueryTypedDict(self):
44044419
format_query = self.adapter.format_query
44054420
self.assertRaises(

0 commit comments

Comments
 (0)