Re: INET type and arrays: too much casting?

Federico Di Gregorio <fog-NGVKUo/i/[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Organization initd.org
Message-ID <[email protected]>
Il giorno mar, 16/09/2008 alle 16.42 +0200, Stephane Bortzmeyer ha
scritto:
> On Tue, Sep 16, 2008 at 10:45:16AM +0200,
>  Federico Di Gregorio <fog-NGVKUo/i/[email protected]> wrote 
>  a message of 86 lines which said:
> 
> > The right way would be to provide an adapter for a scalar INET type
> > (mapped to an Inet class in Python)
> 
> Here is what I did and which seems to work:
> 
> from psycopg2.extensions import adapt, register_adapter, AsIs
> 
> class Inet(object):
>     def __init__(self, addr):
>         """ Never call it with unchecked data, there is no protection against injection """
>         # TODO: check it is an IP address
>         self.addr = addr
> 
> def adapt_inet(address):
>     return AsIs("'%s'::inet" % address.addr)

Actually you don't need to register an adapter but just to provide the
right methods in the Inet class. This is what I did (in extras.py) and
pushed to the public repository.

federico

-- 
Federico Di Gregorio                         http://people.initd.org/fog
Debian GNU/Linux Developer                                [email protected]
INIT.D Developer                                           fog-NGVKUo/i/[email protected]
          The reverse side also has a reverse side.  -- Japanese proverb

_______________________________________________
Psycopg mailing list
Psycopg-IAPFreCvJWPBWskQ1e/[email protected]
http://lists.initd.org/mailman/listinfo/psycopg
signature.asc (application/pgp-signature, 197 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkjZfCIACgkQvcCgrgZGjeuYAACeOSghNTdcncwvlAm+7tMTh/JL
wxUAoIAw0/6sYI7JXK5lBcY/6gmq+Ke6
=lvPQ
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.