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 lun, 15/09/2008 alle 22.40 +0200, Stephane Bortzmeyer ha
scritto:
> On Mon, Sep 15, 2008 at 10:25:12PM +0200,
>  Stephane Bortzmeyer <[email protected]> wrote 
>  a message of 45 lines which said:
> 
> > For my IP addresses, should I use adapters?
> 
> Something like:
> 
> import psycopg2
> from psycopg2.extensions import adapt, register_adapter, AsIs
> 
> class Inet(object):
>     def __init__(self, list):
>         # TODO: check they are IP addresses or we risk SQL injection
>         self.list = list
> 
> def adapt_inet(obj):
>     return AsIs("'{%s}'" % ",".join (obj.list))
> 
> register_adapter(Inet, adapt_inet)
> 
> conn = psycopg2.connect("dbname=essais")
> curs = conn.cursor()
> # CREATE TABLE mytable (addrs INET[]);
> curs.execute("INSERT INTO mytable (addrs) VALUES (%s)",
>              (Inet(["2001:db8:::1", "176.34.0.2",
> 	     "2001:dead:babe:1:a:b:cafe:1"]), ))
> 
> Am I on the right track?

The right way would be to provide an adapter for a scalar INET type
(mapped to an Inet class in Python) and let psycopg do the adaptation of
arrays using the basic, scalar adapter. I have some patches pending but
after reviewing them I'll have a shot at this one (unless someone send a
patch before).

federico

-- 
Federico Di Gregorio                         http://people.initd.org/fog
Debian GNU/Linux Developer                                [email protected]
INIT.D Developer                                           fog-NGVKUo/i/[email protected]
 Il panda ha l'apparato digerente di un carnivoro (e.g., di un orso).
  Il panda ha scelto di cibarsi esclusivamente di germogli di bambù.
  Quindi, il panda è l'unico animale vegano del pianeta. Il panda
  merita di estinguersi.                       -- Maria, Alice, Federico

_______________________________________________
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)

iEYEABECAAYFAkjPchwACgkQvcCgrgZGjesC+gCdHjzu9PWVXJ6zmcJiUYuUsa/A
7V0AmwY63SuVrpGYMEA6+FRxJ/WNxZMS
=/fWa
-----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.