I based my code on the Michaeil Bayer's answer to this Stack Overflow question . I extended it a little so it takes into account NULLs and ARRAY for Postgresql.
class values(FromClause):
named_with_column = True
def __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)
for c in self._column_args:
c._make_proxy(self, c.name)
@compiles(values)
def compile_values(element, compiler, asfrom=False, **kw):
columns = element.columns
v = "VALUES %s" % ", ".join(
"(%s)" % ", ".join(
((compiler.visit_array(elem)+'::'+str(column.type)) if isinstance(column.type, ARRAY) else
compiler.render_literal_value(elem, column.type))
if elem is not None else compiler.render_literal_value(elem, NULLTYPE)
for elem, column in zip(tup, columns))
for tup in element.list
)
if asfrom:
if element.alias_name:
v = "(%s) AS %s (%s)" % (v, element.alias_name, (", ".join(c.name for c in element.columns)))
else:
v = "(%s)" % v
return vEverything 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?
> <mailto:sqlalchemy+unsub...@googlegroups.com>.
well what is the ARRAY data you're passing into it? can you provide a
complete example?
On 11/19/2015 02:04 PM, Юрий Пайков wrote:
> The problem is - I can't wrap my head round using it... Could you
> direct me to same compiled clause which uses bindparams?
> Or perhaps maybe to some documentation on SQLA internals, because it is
> quite difficult to track the whole query compilation process for me
> четверг, 19 ноября 2015 г., 21:14:59 UTC+5 пользователь Michael Bayer
> написал:
>
>
>
> you can try a bindparam(), sure. the original question, everything
> under @compiles should return plain text, yes, those functions all need
> to return text that is appended to the SQL statement.
>
> --
> 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
> <mailto:sqlalchemy+unsub...@googlegroups.com>.