But if I used bound parameters in a query - wouldn't it be a solution? I mean, if a value for a bindparam is not inlined in a query and rather carried along - there is no need to quote it for the driver, is there? Like done for any other literal comparison, for example. The driver will insert it as is from some object/field and won't parse the resulting query for params any further...
четверг, 19 ноября 2015 г., 19:43:39 UTC+5 пользователь Michael Bayer написал: > > > > On 11/19/2015 05:29 AM, Юрий Пайков wrote: > > I based my code on the Michaeil Bayer's answer to this Stack Overflow > > question > > <http://stackoverflow.com/questions/18858291/values-clause-in-sqlalchemy>. > > > I extended it a little so it takes into account NULLs and ARRAY for > > Postgresql. > > > > |classvalues(FromClause):named_with_column > > =Truedef__init__(self,columns,*args,**kw):self._column_args =columns > > self.list =args self.alias_name =self.name > > =kw.pop('alias_name',None)def_populate_column_collection(self):# > > self._columns.update((col.name, col) for col in self._column_args)forc > > inself._column_args:c._make_proxy(self,c.name)@compiles(values)defcompile_values(element,compiler,asfrom=False,**kw):columns > > > > > =element.columns v ="VALUES %s"%", ".join("(%s)"%", > > > ".join(((compiler.visit_array(elem)+'::'+str(column.type))ifisinstance(column.type,ARRAY)elsecompiler.render_literal_value(elem,column.type))ifelem > > > > isnotNoneelsecompiler.render_literal_value(elem,NULLTYPE)forelem,column > > inzip(tup,columns))fortup inelement.list > > )ifasfrom:ifelement.alias_name:v ="(%s) AS %s > > (%s)"%(v,element.alias_name,(", ".join(c.name forc > > inelement.columns)))else:v ="(%s)"%v returnv| > > > > Everything worked fine until it turned out I couldn't insert values with > > "%"-sign to this VALUES clause - they get inlined in a resulting > > statement and this seems to cause binding problems > > > > I guess if instead of |render_literal_value()| we used |bindparam()| we > > could avoid such an error. But Everything under |@compiles| should > > return plain text, am I right? > > > > > > How could I amend this to get a query which contains bind parfms along > > with it? > > if the issue is percent signs, this is a driver limitation and you need > to double them up as %%, so that they aren't confused as format/pyformat > indicators. psycopg2 uses format/pyformat style for bound parameters. > > > > > > > -- > > You received this message because you are subscribed to the Google > > Groups "sqlalchemy" group. > > To unsubscribe from this group and stop receiving emails from it, send > > an email to sqlalchemy+...@googlegroups.com <javascript:> > > <mailto:sqlalchemy+unsubscr...@googlegroups.com <javascript:>>. > > To post to this group, send email to sqlal...@googlegroups.com > <javascript:> > > <mailto:sqlal...@googlegroups.com <javascript:>>. > > Visit this group at http://groups.google.com/group/sqlalchemy. > > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.