Re: About mogrify

Oswaldo Hernández <[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
Oswaldo Hernández escribió:
> Federico Di Gregorio escribió:
>> Il giorno lun, 22/09/2008 alle 12.54 +0200, Oswaldo Hernández ha
> ...
>> Why do you need to call mogrify here?
>>
>  ...
> The best way that i found to avoid these is using mogrify in the user 
> sql and pass the result as parameter in the execute:
> 
>  >>> sqluser = "select referencia, descripcion from v_productos where 
> referencia=%s"
>  >>> argsuser = ('333',)
>  >>> sqlcheck = "select nombre, tipo_base from describe_campos(%s)"
>  >>> sqluser = sqluser.encode(con.encoding)
>  >>> sqluser = con.cursor().mogrify(sqluser, argsuser)
>  >>> c.execute(sqlcheck, (sqluser,))
>  >>> c.fetchall()
> [('Referencia', 'varchar'), ('Descripci\xc3\xb3n', 'varchar')]
> 
> 

Hi Federico,

I have found one solution without using mogrify() directly :)

 >>> sqluser = "select referencia, descripcion, 'otro'||' '||'campo' as otro from v_productos where 
referencia=%s or referencia in %s or descripcion ~* %s"
 >>> argsuser = ('333', ('111','222'), 'PRODUCTO 1')
 >>> sqlcheck = "select campo, tipo_base from describe_campos($$%s$$)" % sqluser
 >>> c.execute(sqlcheck, argsuser)
 >>> c.fetchall()
[('referencia', 'varchar'), ('descripcion', 'varchar'), ('otro', 'text')]

I make a python replace enclosing usersql into sqlcheck using *dollar quoting*. Then do the execute 
with the user params.

The mogrify debug result is:

 >>> con.cursor().mogrify(sqlcheck, argsuser)
"select campo, tipo_base from describe_campos($$select referencia, descripcion, 'otro'||' '||'campo' 
as otro from v_productos where referencia=E'333' or referencia in (E'111', E'222') or descripcion ~* 
E'PRODUCTO 1'$$)"

Regards.

-- 
*****************************************
Oswaldo Hernández
oswaldo (@) soft-com (.) es
*****************************************
PD:
Antes de imprimir este mensaje, asegúrese de que es necesario.
El medio ambiente está en nuestra mano.
_______________________________________________
Psycopg mailing list
[email protected]
http://lists.initd.org/mailman/listinfo/psycopg
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.