mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-11-29 20:23:45 +03:00
Added inet support
This commit is contained in:
parent
56f6001d6d
commit
4c8e80038e
|
@ -1,3 +1,7 @@
|
||||||
|
2008-09-24 Federico Di Gregorio <fog@initd.org>
|
||||||
|
|
||||||
|
* lib/extras.py: added inet support and related tests.
|
||||||
|
|
||||||
2008-09-23 Federico Di Gregorio <fog@initd.org>
|
2008-09-23 Federico Di Gregorio <fog@initd.org>
|
||||||
|
|
||||||
* Applied patch from Brian Sutherland that fixes NULL
|
* Applied patch from Brian Sutherland that fixes NULL
|
||||||
|
|
|
@ -311,6 +311,7 @@ try:
|
||||||
_ext.register_adapter(uuid.UUID, UUID_adapter)
|
_ext.register_adapter(uuid.UUID, UUID_adapter)
|
||||||
return _ext.UUID
|
return _ext.UUID
|
||||||
|
|
||||||
|
|
||||||
except ImportError, e:
|
except ImportError, e:
|
||||||
def register_uuid(oid=None):
|
def register_uuid(oid=None):
|
||||||
"""Create the UUID type and an uuid.UUID adapter.
|
"""Create the UUID type and an uuid.UUID adapter.
|
||||||
|
@ -321,4 +322,38 @@ except ImportError, e:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
|
||||||
|
# a type, dbtype and adapter for PostgreSQL inet type
|
||||||
|
|
||||||
|
class Inet(object):
|
||||||
|
"""Wrap a string to allow for correct SQL-quoting of inet values.
|
||||||
|
|
||||||
|
Note that this adapter does NOT check the passed value to make
|
||||||
|
sure it really is an inet-compatible address but DOES call adapt()
|
||||||
|
on it to make sure it is impossible to execute an SQL-injection
|
||||||
|
by passing an evil value to the initializer.
|
||||||
|
"""
|
||||||
|
def __init__(self, addr):
|
||||||
|
self.addr
|
||||||
|
|
||||||
|
def prepare(self, conn):
|
||||||
|
self._conn = conn
|
||||||
|
|
||||||
|
def getquoted(self):
|
||||||
|
obj = adapt(self.addr)
|
||||||
|
if hasattr(obj, 'prepare'):
|
||||||
|
obj.prepare(self._conn)
|
||||||
|
return obj.getquoted()+"::inet"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return str(self.addr)
|
||||||
|
|
||||||
|
def register_inet(oid=None):
|
||||||
|
"""Create the INET type and an Inet adapter."""
|
||||||
|
if not oid: oid = 869
|
||||||
|
_ext.INET = _ext.new_type((oid, ), "INET",
|
||||||
|
lambda data, cursor: data and Inet(data) or None)
|
||||||
|
_ext.register_type(_ext.INET)
|
||||||
|
return _ext.INET
|
||||||
|
|
||||||
|
|
||||||
__all__ = [ k for k in locals().keys() if not k.startswith('_') ]
|
__all__ = [ k for k in locals().keys() if not k.startswith('_') ]
|
||||||
|
|
|
@ -49,6 +49,15 @@ class TypesBasicTests(unittest.TestCase):
|
||||||
s = self.execute("SELECT NULL::uuid AS foo")
|
s = self.execute("SELECT NULL::uuid AS foo")
|
||||||
self.failUnless(s is None)
|
self.failUnless(s is None)
|
||||||
|
|
||||||
|
def testINET(self):
|
||||||
|
psycopg2.extras.register_inet()
|
||||||
|
i = "192.168.1.0/24";
|
||||||
|
s = self.execute("SELECT %s AS foo", (i,))
|
||||||
|
self.failUnless(i == s)
|
||||||
|
# must survive NULL cast to inet
|
||||||
|
s = self.execute("SELECT NULL::inet AS foo")
|
||||||
|
self.failUnless(s is None)
|
||||||
|
|
||||||
def test_suite():
|
def test_suite():
|
||||||
return unittest.TestLoader().loadTestsFromName(__name__)
|
return unittest.TestLoader().loadTestsFromName(__name__)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user