Skip to content

Commit

Permalink
Add missing adapter for UUIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Oct 2, 2020
1 parent 817e359 commit 8a3e892
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion docs/contents/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
ChangeLog
=========

Version 5.2.1 (2020-09-25)
Version 5.2.2 (to be released)
------------------------------
- Added a missing adapter method for UUIDs in the classic `pg` module.

Version 5.2.1 (2020-09-25)
--------------------------
- This version officially supports the new Python 3.9 and PostgreSQL 13.
- The `copy_to()` and `copy_from()` methods in the pgdb module now also work
with table names containing schema qualifiers (#47).
Expand Down
8 changes: 8 additions & 0 deletions pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,14 @@ def _adapt_hstore(self, v):
return str(Hstore(v))
raise TypeError('Hstore parameter %s has wrong type' % v)

def _adapt_uuid(self, v):
"""Adapt a UUID parameter."""
if not v:
return None
if isinstance(v, basestring):
return v
return str(v)

@classmethod
def _adapt_text_array(cls, v):
"""Adapt a text type array parameter."""
Expand Down
15 changes: 15 additions & 0 deletions tests/test_classic_dbwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4400,6 +4400,21 @@ def testAdaptQueryTypedWithHstore(self):
params[0] = ','.join(sorted(params[0].split(',')))
self.assertEqual(params, ['one=>"it\'s fine\",two=>2'])

def testAdaptQueryTypedWithUuid(self):
format_query = self.adapter.format_query
value = '12345678-1234-5678-1234-567812345678'
sql, params = format_query("select %s", (value,), 'uuid')
self.assertEqual(sql, "select $1")
self.assertEqual(params, ['12345678-1234-5678-1234-567812345678'])
value = UUID('{12345678-1234-5678-1234-567812345678}')
sql, params = format_query("select %s", (value,), 'uuid')
self.assertEqual(sql, "select $1")
self.assertEqual(params, ['12345678-1234-5678-1234-567812345678'])
value = UUID('{12345678-1234-5678-1234-567812345678}')
sql, params = format_query("select %s", (value,))
self.assertEqual(sql, "select $1")
self.assertEqual(params, ['12345678-1234-5678-1234-567812345678'])

def testAdaptQueryTypedDict(self):
format_query = self.adapter.format_query
self.assertRaises(
Expand Down

0 comments on commit 8a3e892

Please sign in to comment.