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 [email protected] > <mailto:[email protected]>. > To post to this group, send email to [email protected] > <mailto:[email protected]>. > 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
