mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-01-31 09:24:07 +03:00
Fixed adaptation of None in composite types (ticket #26).
Added an adapter for None: it is usually not invoked as adaptation to NULL is a fast path in mogrify, but can be invoked by composite types. Notice that composite types still have the option to fast-path None (e.g. list adapter does).
This commit is contained in:
parent
30921b58a8
commit
fdfa2de1a1
7
NEWS-2.3
7
NEWS-2.3
|
@ -1,3 +1,10 @@
|
||||||
|
What's new in psycopg 2.3.3
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
- Fixed adaptation of None in composite types (ticket #26). Bug report by
|
||||||
|
Karsten Hilbert.
|
||||||
|
|
||||||
|
|
||||||
What's new in psycopg 2.3.2
|
What's new in psycopg 2.3.2
|
||||||
---------------------------
|
---------------------------
|
||||||
|
|
||||||
|
|
|
@ -82,6 +82,7 @@ from _psycopg import __version__
|
||||||
|
|
||||||
import psycopg2.extensions as _ext
|
import psycopg2.extensions as _ext
|
||||||
_ext.register_adapter(tuple, _ext.SQL_IN)
|
_ext.register_adapter(tuple, _ext.SQL_IN)
|
||||||
|
_ext.register_adapter(type(None), _ext.NoneAdapter)
|
||||||
|
|
||||||
__all__ = filter(lambda k: not k.startswith('_'), locals().keys())
|
__all__ = filter(lambda k: not k.startswith('_'), locals().keys())
|
||||||
|
|
||||||
|
|
|
@ -128,4 +128,17 @@ class SQL_IN(object):
|
||||||
__str__ = getquoted
|
__str__ = getquoted
|
||||||
|
|
||||||
|
|
||||||
|
class NoneAdapter(object):
|
||||||
|
"""Adapt None to NULL.
|
||||||
|
|
||||||
|
This adapter is not used normally as a fast path in mogrify uses NULL,
|
||||||
|
but it makes easier to adapt composite types.
|
||||||
|
"""
|
||||||
|
def __init__(self, obj):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def getquoted(self):
|
||||||
|
return "NULL"
|
||||||
|
|
||||||
|
|
||||||
__all__ = filter(lambda k: not k.startswith('_'), locals().keys())
|
__all__ = filter(lambda k: not k.startswith('_'), locals().keys())
|
||||||
|
|
|
@ -364,6 +364,26 @@ class AdaptTypeTestCase(unittest.TestCase):
|
||||||
d = curs.fetchone()[0]
|
d = curs.fetchone()[0]
|
||||||
self.assertEqual("(42,)", d)
|
self.assertEqual("(42,)", d)
|
||||||
|
|
||||||
|
def test_none_fast_path(self):
|
||||||
|
# the None adapter is not actually invoked in regular adaptation
|
||||||
|
ext = psycopg2.extensions
|
||||||
|
|
||||||
|
class WonkyAdapter(object):
|
||||||
|
def __init__(self, obj): pass
|
||||||
|
def getquoted(self): return "NOPE!"
|
||||||
|
|
||||||
|
curs = self.conn.cursor()
|
||||||
|
|
||||||
|
orig_adapter = ext.adapters[type(None), ext.ISQLQuote]
|
||||||
|
try:
|
||||||
|
ext.register_adapter(type(None), WonkyAdapter)
|
||||||
|
self.assertEqual(ext.adapt(None).getquoted(), "NOPE!")
|
||||||
|
|
||||||
|
s = curs.mogrify("SELECT %s;", (None,))
|
||||||
|
self.assertEqual("SELECT NULL;", s)
|
||||||
|
|
||||||
|
finally:
|
||||||
|
ext.register_adapter(type(None), orig_adapter)
|
||||||
|
|
||||||
def test_suite():
|
def test_suite():
|
||||||
return unittest.TestLoader().loadTestsFromName(__name__)
|
return unittest.TestLoader().loadTestsFromName(__name__)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user