~psycopg/psycopg/2.0.x

« back to all changes in this revision

Viewing changes to lib/extras.py

  • Committer: Federico Di Gregorio
  • Date: 2008-09-23 23:27:52 UTC
  • Revision ID: fog@initd.org-20080923232752-gacprtlmpvxxd2id
Added inet support

Show diffs side-by-side

added added

removed removed

Lines of Context:
306
306
        """Create the UUID type and an uuid.UUID adapter."""
307
307
        if not oid: oid = 2950
308
308
        _ext.UUID = _ext.new_type((oid, ), "UUID",
309
 
                                   lambda data, cursor: data and uuid.UUID(data) or None)
 
309
                lambda data, cursor: data and uuid.UUID(data) or None)
310
310
        _ext.register_type(_ext.UUID)
311
311
        _ext.register_adapter(uuid.UUID, UUID_adapter)
312
312
        return _ext.UUID
313
 
        
 
313
 
 
314
 
314
315
except ImportError, e:
315
316
    def register_uuid(oid=None):
316
317
        """Create the UUID type and an uuid.UUID adapter.
321
322
        raise e
322
323
 
323
324
 
 
325
# a type, dbtype and adapter for PostgreSQL inet type
 
326
 
 
327
class Inet(object):
 
328
    """Wrap a string to allow for correct SQL-quoting of inet values.
 
329
 
 
330
    Note that this adapter does NOT check the passed value to make
 
331
    sure it really is an inet-compatible address but DOES call adapt()
 
332
    on it to make sure it is impossible to execute an SQL-injection
 
333
    by passing an evil value to the initializer.
 
334
    """
 
335
    def __init__(self, addr):
 
336
        self.addr
 
337
    
 
338
    def prepare(self, conn):
 
339
        self._conn = conn
 
340
    
 
341
    def getquoted(self):
 
342
        obj = adapt(self.addr)
 
343
        if hasattr(obj, 'prepare'):
 
344
            obj.prepare(self._conn)
 
345
        return obj.getquoted()+"::inet"
 
346
 
 
347
    def __str__(self):
 
348
        return str(self.addr)
 
349
        
 
350
def register_inet(oid=None):
 
351
    """Create the INET type and an Inet adapter."""
 
352
    if not oid: oid = 869
 
353
    _ext.INET = _ext.new_type((oid, ), "INET",
 
354
            lambda data, cursor: data and Inet(data) or None)
 
355
    _ext.register_type(_ext.INET)
 
356
    return _ext.INET
 
357
 
 
358
 
324
359
__all__ = [ k for k in locals().keys() if not k.startswith('_') ]